blob: 9cc5e603c5c76b1553808032c5316f6aa4893f29 [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
James E. Blairb0fcae42012-07-17 11:12:10 -070032import zuul.scheduler
James E. Blairad28e912013-11-27 10:43:22 -080033import zuul.rpcclient
Paul Belanger9bba4902016-11-02 16:07:33 -040034import zuul.model
James E. Blairb0fcae42012-07-17 11:12:10 -070035
Maru Newby3fe5f852015-01-13 04:22:14 +000036from tests.base import (
Maru Newby3fe5f852015-01-13 04:22:14 +000037 ZuulTestCase,
38 repack_repo,
James E. Blair06cc3922017-04-19 10:08:10 -070039 simple_layout,
Maru Newby3fe5f852015-01-13 04:22:14 +000040)
James E. Blairb0fcae42012-07-17 11:12:10 -070041
James E. Blairb0fcae42012-07-17 11:12:10 -070042
Clark Boylanb640e052014-04-03 16:41:46 -070043class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070044 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010045
Paul Belanger174a8272017-03-14 13:20:10 -040046 def test_jobs_executed(self):
47 "Test that jobs are executed and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070048
James E. Blairb0fcae42012-07-17 11:12:10 -070049 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070050 A.addApproval('code-review', 2)
51 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070052 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040053 self.assertEqual(self.getJobFromHistory('project-merge').result,
54 'SUCCESS')
55 self.assertEqual(self.getJobFromHistory('project-test1').result,
56 'SUCCESS')
57 self.assertEqual(self.getJobFromHistory('project-test2').result,
58 'SUCCESS')
59 self.assertEqual(A.data['status'], 'MERGED')
60 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070061 self.assertEqual(self.getJobFromHistory('project-test1').node,
62 'image1')
Adam Gandelmaneace4692017-02-07 15:18:36 -080063 self.assertIsNone(self.getJobFromHistory('project-test2').node)
James E. Blairb0fcae42012-07-17 11:12:10 -070064
James E. Blair552b54f2016-07-22 13:55:32 -070065 # TODOv3(jeblair): we may want to report stats by tenant (also?).
James E. Blair66eeebf2013-07-27 17:44:32 -070066 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
67 self.assertReportedStat('zuul.pipeline.gate.current_changes',
68 value='1|g')
69 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
70 kind='ms')
71 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
72 value='1|c')
73 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
74 self.assertReportedStat('zuul.pipeline.gate.total_changes',
75 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070076 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070077 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -070078 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070079 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070080
James E. Blair5821bd92015-09-16 08:48:15 -070081 for build in self.builds:
82 self.assertEqual(build.parameters['ZUUL_VOTING'], '1')
83
James E. Blair3cb10702013-08-24 08:56:03 -070084 def test_initial_pipeline_gauges(self):
85 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -070086 self.assertReportedStat('zuul.pipeline.gate.current_changes',
87 value='0|g')
88 self.assertReportedStat('zuul.pipeline.check.current_changes',
89 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -070090
James E. Blair34776ee2016-08-25 13:53:54 -070091 def test_job_branch(self):
92 "Test the correct variant of a job runs on a branch"
93 self.create_branch('org/project', 'stable')
94 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
95 A.addApproval('code-review', 2)
96 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
97 self.waitUntilSettled()
98 self.assertEqual(self.getJobFromHistory('project-test1').result,
99 'SUCCESS')
100 self.assertEqual(self.getJobFromHistory('project-test2').result,
101 'SUCCESS')
102 self.assertEqual(A.data['status'], 'MERGED')
103 self.assertEqual(A.reported, 2,
104 "A should report start and success")
105 self.assertIn('gate', A.messages[1],
106 "A should transit gate")
107 self.assertEqual(self.getJobFromHistory('project-test1').node,
108 'image2')
109
James E. Blairb0fcae42012-07-17 11:12:10 -0700110 def test_parallel_changes(self):
111 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700112
Paul Belanger174a8272017-03-14 13:20:10 -0400113 self.executor_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700114 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
115 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
116 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700117 A.addApproval('code-review', 2)
118 B.addApproval('code-review', 2)
119 C.addApproval('code-review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700120
James E. Blair8b5408c2016-08-08 15:37:46 -0700121 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
122 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
123 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700124
125 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400126 self.assertEqual(len(self.builds), 1)
127 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700128 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700129
Paul Belanger174a8272017-03-14 13:20:10 -0400130 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700131 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400132 self.assertEqual(len(self.builds), 3)
133 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700134 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400135 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700136 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400137 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700138 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700139
Paul Belanger174a8272017-03-14 13:20:10 -0400140 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700141 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400142 self.assertEqual(len(self.builds), 5)
143 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700144 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400145 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700146 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700147
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400148 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700149 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400150 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700151 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700152
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400153 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700154 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700155
Paul Belanger174a8272017-03-14 13:20:10 -0400156 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700157 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400158 self.assertEqual(len(self.builds), 6)
159 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700160 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400161 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700162 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700163
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400164 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700165 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400166 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700167 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700168
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400169 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700170 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400171 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700172 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700173
Paul Belanger174a8272017-03-14 13:20:10 -0400174 self.executor_server.hold_jobs_in_build = False
175 self.executor_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700176 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400177 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700178
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400179 self.assertEqual(len(self.history), 9)
180 self.assertEqual(A.data['status'], 'MERGED')
181 self.assertEqual(B.data['status'], 'MERGED')
182 self.assertEqual(C.data['status'], 'MERGED')
183 self.assertEqual(A.reported, 2)
184 self.assertEqual(B.reported, 2)
185 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700186
187 def test_failed_changes(self):
188 "Test that a change behind a failed change is retested"
Paul Belanger174a8272017-03-14 13:20:10 -0400189 self.executor_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700190
James E. Blairb02a3bb2012-07-30 17:49:55 -0700191 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
192 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -0700193 A.addApproval('code-review', 2)
194 B.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700195
Paul Belanger174a8272017-03-14 13:20:10 -0400196 self.executor_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700197
James E. Blair8b5408c2016-08-08 15:37:46 -0700198 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
199 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700200 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700201 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400202
Paul Belanger174a8272017-03-14 13:20:10 -0400203 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400204 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700205 # A/project-merge is complete
206 self.assertBuilds([
207 dict(name='project-test1', changes='1,1'),
208 dict(name='project-test2', changes='1,1'),
209 dict(name='project-merge', changes='1,1 2,1'),
210 ])
James E. Blaire2819012013-06-28 17:17:26 -0400211
Paul Belanger174a8272017-03-14 13:20:10 -0400212 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400213 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700214 # A/project-merge is complete
215 # B/project-merge is complete
216 self.assertBuilds([
217 dict(name='project-test1', changes='1,1'),
218 dict(name='project-test2', changes='1,1'),
219 dict(name='project-test1', changes='1,1 2,1'),
220 dict(name='project-test2', changes='1,1 2,1'),
221 ])
222
223 # Release project-test1 for A which will fail. This will
Paul Belanger174a8272017-03-14 13:20:10 -0400224 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700225 self.builds[0].release()
226 self.waitUntilSettled()
227
228 self.orderedRelease()
229 self.assertHistory([
230 dict(name='project-merge', result='SUCCESS', changes='1,1'),
231 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
232 dict(name='project-test1', result='FAILURE', changes='1,1'),
233 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
234 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
235 dict(name='project-test2', result='SUCCESS', changes='1,1'),
236 dict(name='project-merge', result='SUCCESS', changes='2,1'),
237 dict(name='project-test1', result='SUCCESS', changes='2,1'),
238 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700239 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700240
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400241 self.assertEqual(A.data['status'], 'NEW')
242 self.assertEqual(B.data['status'], 'MERGED')
243 self.assertEqual(A.reported, 2)
244 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700245
246 def test_independent_queues(self):
247 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700248
Paul Belanger174a8272017-03-14 13:20:10 -0400249 self.executor_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900250 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700251 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
252 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700253 A.addApproval('code-review', 2)
254 B.addApproval('code-review', 2)
255 C.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700256
James E. Blair8b5408c2016-08-08 15:37:46 -0700257 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800258 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700259
James E. Blairf59f3cf2017-02-19 14:50:26 -0800260 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
261 self.waitUntilSettled()
262
263 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700264 self.waitUntilSettled()
265
266 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700267 self.assertBuilds([
268 dict(name='project-merge', changes='1,1'),
269 dict(name='project-merge', changes='2,1'),
270 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700271
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700272 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700273 self.builds[0].release()
274 self.waitUntilSettled()
275 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700276 self.waitUntilSettled()
277 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400278 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700279 self.waitUntilSettled()
280
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700281 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700282 self.assertBuilds([
283 dict(name='project-test1', changes='1,1'),
284 dict(name='project-test2', changes='1,1'),
285 dict(name='project-test1', changes='2,1'),
286 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500287 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700288 dict(name='project-test1', changes='2,1 3,1'),
289 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800290 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700291 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700292
James E. Blair5f0d49e2016-08-19 09:36:50 -0700293 self.orderedRelease()
294 self.assertHistory([
295 dict(name='project-merge', result='SUCCESS', changes='1,1'),
296 dict(name='project-merge', result='SUCCESS', changes='2,1'),
297 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
298 dict(name='project-test1', result='SUCCESS', changes='1,1'),
299 dict(name='project-test2', result='SUCCESS', changes='1,1'),
300 dict(name='project-test1', result='SUCCESS', changes='2,1'),
301 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500302 dict(
303 name='project1-project2-integration',
304 result='SUCCESS',
305 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700306 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
307 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800308 dict(name='project1-project2-integration',
309 result='SUCCESS',
310 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700311 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700312
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400313 self.assertEqual(A.data['status'], 'MERGED')
314 self.assertEqual(B.data['status'], 'MERGED')
315 self.assertEqual(C.data['status'], 'MERGED')
316 self.assertEqual(A.reported, 2)
317 self.assertEqual(B.reported, 2)
318 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700319
320 def test_failed_change_at_head(self):
321 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700322
Paul Belanger174a8272017-03-14 13:20:10 -0400323 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700324 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
325 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
326 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700327 A.addApproval('code-review', 2)
328 B.addApproval('code-review', 2)
329 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700330
Paul Belanger174a8272017-03-14 13:20:10 -0400331 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700332
James E. Blair8b5408c2016-08-08 15:37:46 -0700333 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
334 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
335 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700336
337 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700338
James E. Blairb536ecc2016-08-31 10:11:42 -0700339 self.assertBuilds([
340 dict(name='project-merge', changes='1,1'),
341 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700342
Paul Belanger174a8272017-03-14 13:20:10 -0400343 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700344 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400345 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700346 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400347 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700348 self.waitUntilSettled()
349
James E. Blairb536ecc2016-08-31 10:11:42 -0700350 self.assertBuilds([
351 dict(name='project-test1', changes='1,1'),
352 dict(name='project-test2', changes='1,1'),
353 dict(name='project-test1', changes='1,1 2,1'),
354 dict(name='project-test2', changes='1,1 2,1'),
355 dict(name='project-test1', changes='1,1 2,1 3,1'),
356 dict(name='project-test2', changes='1,1 2,1 3,1'),
357 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700358
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400359 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700360 self.waitUntilSettled()
361
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400362 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700363 self.assertBuilds([
364 dict(name='project-test2', changes='1,1'),
365 dict(name='project-merge', changes='2,1'),
366 ])
367 # Unordered history comparison because the aborts can finish
368 # in any order.
369 self.assertHistory([
370 dict(name='project-merge', result='SUCCESS',
371 changes='1,1'),
372 dict(name='project-merge', result='SUCCESS',
373 changes='1,1 2,1'),
374 dict(name='project-merge', result='SUCCESS',
375 changes='1,1 2,1 3,1'),
376 dict(name='project-test1', result='FAILURE',
377 changes='1,1'),
378 dict(name='project-test1', result='ABORTED',
379 changes='1,1 2,1'),
380 dict(name='project-test2', result='ABORTED',
381 changes='1,1 2,1'),
382 dict(name='project-test1', result='ABORTED',
383 changes='1,1 2,1 3,1'),
384 dict(name='project-test2', result='ABORTED',
385 changes='1,1 2,1 3,1'),
386 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700387
Paul Belanger174a8272017-03-14 13:20:10 -0400388 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700389 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400390 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700391 self.waitUntilSettled()
392 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700393
James E. Blairb536ecc2016-08-31 10:11:42 -0700394 self.assertBuilds([])
395 self.assertHistory([
396 dict(name='project-merge', result='SUCCESS',
397 changes='1,1'),
398 dict(name='project-merge', result='SUCCESS',
399 changes='1,1 2,1'),
400 dict(name='project-merge', result='SUCCESS',
401 changes='1,1 2,1 3,1'),
402 dict(name='project-test1', result='FAILURE',
403 changes='1,1'),
404 dict(name='project-test1', result='ABORTED',
405 changes='1,1 2,1'),
406 dict(name='project-test2', result='ABORTED',
407 changes='1,1 2,1'),
408 dict(name='project-test1', result='ABORTED',
409 changes='1,1 2,1 3,1'),
410 dict(name='project-test2', result='ABORTED',
411 changes='1,1 2,1 3,1'),
412 dict(name='project-merge', result='SUCCESS',
413 changes='2,1'),
414 dict(name='project-merge', result='SUCCESS',
415 changes='2,1 3,1'),
416 dict(name='project-test2', result='SUCCESS',
417 changes='1,1'),
418 dict(name='project-test1', result='SUCCESS',
419 changes='2,1'),
420 dict(name='project-test2', result='SUCCESS',
421 changes='2,1'),
422 dict(name='project-test1', result='SUCCESS',
423 changes='2,1 3,1'),
424 dict(name='project-test2', result='SUCCESS',
425 changes='2,1 3,1'),
426 ], ordered=False)
427
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400428 self.assertEqual(A.data['status'], 'NEW')
429 self.assertEqual(B.data['status'], 'MERGED')
430 self.assertEqual(C.data['status'], 'MERGED')
431 self.assertEqual(A.reported, 2)
432 self.assertEqual(B.reported, 2)
433 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700434
James E. Blair0aac4872013-08-23 14:02:38 -0700435 def test_failed_change_in_middle(self):
436 "Test a failed change in the middle of the queue"
437
Paul Belanger174a8272017-03-14 13:20:10 -0400438 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700439 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
440 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
441 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700442 A.addApproval('code-review', 2)
443 B.addApproval('code-review', 2)
444 C.addApproval('code-review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700445
Paul Belanger174a8272017-03-14 13:20:10 -0400446 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700447
James E. Blair8b5408c2016-08-08 15:37:46 -0700448 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
449 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
450 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700451
452 self.waitUntilSettled()
453
Paul Belanger174a8272017-03-14 13:20:10 -0400454 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700455 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400456 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700457 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400458 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700459 self.waitUntilSettled()
460
461 self.assertEqual(len(self.builds), 6)
462 self.assertEqual(self.builds[0].name, 'project-test1')
463 self.assertEqual(self.builds[1].name, 'project-test2')
464 self.assertEqual(self.builds[2].name, 'project-test1')
465 self.assertEqual(self.builds[3].name, 'project-test2')
466 self.assertEqual(self.builds[4].name, 'project-test1')
467 self.assertEqual(self.builds[5].name, 'project-test2')
468
469 self.release(self.builds[2])
470 self.waitUntilSettled()
471
James E. Blair972e3c72013-08-29 12:04:55 -0700472 # project-test1 and project-test2 for A
473 # project-test2 for B
474 # project-merge for C (without B)
475 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700476 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
477
Paul Belanger174a8272017-03-14 13:20:10 -0400478 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700479 self.waitUntilSettled()
480
481 # project-test1 and project-test2 for A
482 # project-test2 for B
483 # project-test1 and project-test2 for C
484 self.assertEqual(len(self.builds), 5)
485
Paul Belanger2e2a0492016-10-30 18:33:37 -0400486 tenant = self.sched.abide.tenants.get('tenant-one')
487 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700488 builds = items[0].current_build_set.getBuilds()
489 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
490 self.assertEqual(self.countJobResults(builds, None), 2)
491 builds = items[1].current_build_set.getBuilds()
492 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
493 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
494 self.assertEqual(self.countJobResults(builds, None), 1)
495 builds = items[2].current_build_set.getBuilds()
496 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700497 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700498
Paul Belanger174a8272017-03-14 13:20:10 -0400499 self.executor_server.hold_jobs_in_build = False
500 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700501 self.waitUntilSettled()
502
503 self.assertEqual(len(self.builds), 0)
504 self.assertEqual(len(self.history), 12)
505 self.assertEqual(A.data['status'], 'MERGED')
506 self.assertEqual(B.data['status'], 'NEW')
507 self.assertEqual(C.data['status'], 'MERGED')
508 self.assertEqual(A.reported, 2)
509 self.assertEqual(B.reported, 2)
510 self.assertEqual(C.reported, 2)
511
James E. Blaird466dc42012-07-31 10:42:56 -0700512 def test_failed_change_at_head_with_queue(self):
513 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700514
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700515 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700516 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
517 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
518 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700519 A.addApproval('code-review', 2)
520 B.addApproval('code-review', 2)
521 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700522
Paul Belanger174a8272017-03-14 13:20:10 -0400523 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700524
James E. Blair8b5408c2016-08-08 15:37:46 -0700525 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
526 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
527 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700528
529 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700530 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400531 self.assertEqual(len(self.builds), 0)
532 self.assertEqual(len(queue), 1)
Clint Byrumf322fe22017-05-10 20:53:12 -0700533 self.assertEqual(queue[0].name, b'executor:execute')
534 job_args = json.loads(queue[0].arguments.decode('utf8'))
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800535 self.assertEqual(job_args['job'], 'project-merge')
536 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700537
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700538 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700539 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700540 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700541 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700542 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700543 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700544 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700545
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400546 self.assertEqual(len(self.builds), 0)
547 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800548
549 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700550 json.loads(queue[0].arguments.decode('utf8'))['job'],
551 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800552 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700553 json.loads(queue[1].arguments.decode('utf8'))['job'],
554 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800555 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700556 json.loads(queue[2].arguments.decode('utf8'))['job'],
557 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800558 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700559 json.loads(queue[3].arguments.decode('utf8'))['job'],
560 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800561 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700562 json.loads(queue[4].arguments.decode('utf8'))['job'],
563 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800564 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700565 json.loads(queue[5].arguments.decode('utf8'))['job'],
566 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700567
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700568 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700569 self.waitUntilSettled()
570
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400571 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700572 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400573 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
574 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700575
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700576 self.gearman_server.hold_jobs_in_queue = False
577 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700578 self.waitUntilSettled()
579
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400580 self.assertEqual(len(self.builds), 0)
581 self.assertEqual(len(self.history), 11)
582 self.assertEqual(A.data['status'], 'NEW')
583 self.assertEqual(B.data['status'], 'MERGED')
584 self.assertEqual(C.data['status'], 'MERGED')
585 self.assertEqual(A.reported, 2)
586 self.assertEqual(B.reported, 2)
587 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700588
James E. Blairec056492016-07-22 09:45:56 -0700589 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700590 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400591 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700592 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -0700593 A.addApproval('code-review', 2)
594 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700595 self.waitUntilSettled()
596 time.sleep(2)
597
598 data = json.loads(self.sched.formatStatusJSON())
599 found_job = None
600 for pipeline in data['pipelines']:
601 if pipeline['name'] != 'gate':
602 continue
603 for queue in pipeline['change_queues']:
604 for head in queue['heads']:
605 for item in head:
606 for job in item['jobs']:
607 if job['name'] == 'project-merge':
608 found_job = job
609 break
610
611 self.assertIsNotNone(found_job)
612 if iteration == 1:
613 self.assertIsNotNone(found_job['estimated_time'])
614 self.assertIsNone(found_job['remaining_time'])
615 else:
616 self.assertIsNotNone(found_job['estimated_time'])
617 self.assertTrue(found_job['estimated_time'] >= 2)
618 self.assertIsNotNone(found_job['remaining_time'])
619
Paul Belanger174a8272017-03-14 13:20:10 -0400620 self.executor_server.hold_jobs_in_build = False
621 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700622 self.waitUntilSettled()
623
James E. Blairec056492016-07-22 09:45:56 -0700624 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700625 def test_time_database(self):
626 "Test the time database"
627
628 self._test_time_database(1)
629 self._test_time_database(2)
630
James E. Blairfef71632013-09-23 11:15:47 -0700631 def test_two_failed_changes_at_head(self):
632 "Test that changes are reparented correctly if 2 fail at head"
633
Paul Belanger174a8272017-03-14 13:20:10 -0400634 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700635 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
636 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
637 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700638 A.addApproval('code-review', 2)
639 B.addApproval('code-review', 2)
640 C.addApproval('code-review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700641
Paul Belanger174a8272017-03-14 13:20:10 -0400642 self.executor_server.failJob('project-test1', A)
643 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700644
James E. Blair8b5408c2016-08-08 15:37:46 -0700645 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
646 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
647 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700648 self.waitUntilSettled()
649
Paul Belanger174a8272017-03-14 13:20:10 -0400650 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700651 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400652 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700653 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400654 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700655 self.waitUntilSettled()
656
657 self.assertEqual(len(self.builds), 6)
658 self.assertEqual(self.builds[0].name, 'project-test1')
659 self.assertEqual(self.builds[1].name, 'project-test2')
660 self.assertEqual(self.builds[2].name, 'project-test1')
661 self.assertEqual(self.builds[3].name, 'project-test2')
662 self.assertEqual(self.builds[4].name, 'project-test1')
663 self.assertEqual(self.builds[5].name, 'project-test2')
664
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400665 self.assertTrue(self.builds[0].hasChanges(A))
666 self.assertTrue(self.builds[2].hasChanges(A))
667 self.assertTrue(self.builds[2].hasChanges(B))
668 self.assertTrue(self.builds[4].hasChanges(A))
669 self.assertTrue(self.builds[4].hasChanges(B))
670 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700671
672 # Fail change B first
673 self.release(self.builds[2])
674 self.waitUntilSettled()
675
676 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400677 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700678 self.waitUntilSettled()
679
680 self.assertEqual(len(self.builds), 5)
681 self.assertEqual(self.builds[0].name, 'project-test1')
682 self.assertEqual(self.builds[1].name, 'project-test2')
683 self.assertEqual(self.builds[2].name, 'project-test2')
684 self.assertEqual(self.builds[3].name, 'project-test1')
685 self.assertEqual(self.builds[4].name, 'project-test2')
686
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400687 self.assertTrue(self.builds[1].hasChanges(A))
688 self.assertTrue(self.builds[2].hasChanges(A))
689 self.assertTrue(self.builds[2].hasChanges(B))
690 self.assertTrue(self.builds[4].hasChanges(A))
691 self.assertFalse(self.builds[4].hasChanges(B))
692 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700693
694 # Finish running all passing jobs for change A
695 self.release(self.builds[1])
696 self.waitUntilSettled()
697 # Fail and report change A
698 self.release(self.builds[0])
699 self.waitUntilSettled()
700
701 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400702 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700703 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400704 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700705 self.waitUntilSettled()
706
707 self.assertEqual(len(self.builds), 4)
708 self.assertEqual(self.builds[0].name, 'project-test1') # B
709 self.assertEqual(self.builds[1].name, 'project-test2') # B
710 self.assertEqual(self.builds[2].name, 'project-test1') # C
711 self.assertEqual(self.builds[3].name, 'project-test2') # C
712
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400713 self.assertFalse(self.builds[1].hasChanges(A))
714 self.assertTrue(self.builds[1].hasChanges(B))
715 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700716
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400717 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700718 # After A failed and B and C restarted, B should be back in
719 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400720 self.assertTrue(self.builds[2].hasChanges(B))
721 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700722
Paul Belanger174a8272017-03-14 13:20:10 -0400723 self.executor_server.hold_jobs_in_build = False
724 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700725 self.waitUntilSettled()
726
727 self.assertEqual(len(self.builds), 0)
728 self.assertEqual(len(self.history), 21)
729 self.assertEqual(A.data['status'], 'NEW')
730 self.assertEqual(B.data['status'], 'NEW')
731 self.assertEqual(C.data['status'], 'MERGED')
732 self.assertEqual(A.reported, 2)
733 self.assertEqual(B.reported, 2)
734 self.assertEqual(C.reported, 2)
735
James E. Blair8c803f82012-07-31 16:25:42 -0700736 def test_patch_order(self):
737 "Test that dependent patches are tested in the right order"
738 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
739 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
740 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700741 A.addApproval('code-review', 2)
742 B.addApproval('code-review', 2)
743 C.addApproval('code-review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700744
745 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
746 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
747 M2.setMerged()
748 M1.setMerged()
749
750 # C -> B -> A -> M1 -> M2
751 # M2 is here to make sure it is never queried. If it is, it
752 # means zuul is walking down the entire history of merged
753 # changes.
754
755 C.setDependsOn(B, 1)
756 B.setDependsOn(A, 1)
757 A.setDependsOn(M1, 1)
758 M1.setDependsOn(M2, 1)
759
James E. Blair8b5408c2016-08-08 15:37:46 -0700760 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700761
762 self.waitUntilSettled()
763
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400764 self.assertEqual(A.data['status'], 'NEW')
765 self.assertEqual(B.data['status'], 'NEW')
766 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700767
James E. Blair8b5408c2016-08-08 15:37:46 -0700768 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
769 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700770
771 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400772 self.assertEqual(M2.queried, 0)
773 self.assertEqual(A.data['status'], 'MERGED')
774 self.assertEqual(B.data['status'], 'MERGED')
775 self.assertEqual(C.data['status'], 'MERGED')
776 self.assertEqual(A.reported, 2)
777 self.assertEqual(B.reported, 2)
778 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700779
James E. Blair063672f2015-01-29 13:09:12 -0800780 def test_needed_changes_enqueue(self):
781 "Test that a needed change is enqueued ahead"
782 # A Given a git tree like this, if we enqueue
783 # / \ change C, we should walk up and down the tree
784 # B G and enqueue changes in the order ABCDEFG.
785 # /|\ This is also the order that you would get if
786 # *C E F you enqueued changes in the order ABCDEFG, so
787 # / the ordering is stable across re-enqueue events.
788 # D
789
790 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
791 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
792 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
793 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
794 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
795 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
796 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
797 B.setDependsOn(A, 1)
798 C.setDependsOn(B, 1)
799 D.setDependsOn(C, 1)
800 E.setDependsOn(B, 1)
801 F.setDependsOn(B, 1)
802 G.setDependsOn(A, 1)
803
James E. Blair8b5408c2016-08-08 15:37:46 -0700804 A.addApproval('code-review', 2)
805 B.addApproval('code-review', 2)
806 C.addApproval('code-review', 2)
807 D.addApproval('code-review', 2)
808 E.addApproval('code-review', 2)
809 F.addApproval('code-review', 2)
810 G.addApproval('code-review', 2)
811 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800812
813 self.waitUntilSettled()
814
815 self.assertEqual(A.data['status'], 'NEW')
816 self.assertEqual(B.data['status'], 'NEW')
817 self.assertEqual(C.data['status'], 'NEW')
818 self.assertEqual(D.data['status'], 'NEW')
819 self.assertEqual(E.data['status'], 'NEW')
820 self.assertEqual(F.data['status'], 'NEW')
821 self.assertEqual(G.data['status'], 'NEW')
822
823 # We're about to add approvals to changes without adding the
824 # triggering events to Zuul, so that we can be sure that it is
825 # enqueing the changes based on dependencies, not because of
826 # triggering events. Since it will have the changes cached
827 # already (without approvals), we need to clear the cache
828 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400829 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100830 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800831
Paul Belanger174a8272017-03-14 13:20:10 -0400832 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -0700833 A.addApproval('approved', 1)
834 B.addApproval('approved', 1)
835 D.addApproval('approved', 1)
836 E.addApproval('approved', 1)
837 F.addApproval('approved', 1)
838 G.addApproval('approved', 1)
839 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800840
841 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400842 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800843 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400844 self.executor_server.hold_jobs_in_build = False
845 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800846 self.waitUntilSettled()
847
848 self.assertEqual(A.data['status'], 'MERGED')
849 self.assertEqual(B.data['status'], 'MERGED')
850 self.assertEqual(C.data['status'], 'MERGED')
851 self.assertEqual(D.data['status'], 'MERGED')
852 self.assertEqual(E.data['status'], 'MERGED')
853 self.assertEqual(F.data['status'], 'MERGED')
854 self.assertEqual(G.data['status'], 'MERGED')
855 self.assertEqual(A.reported, 2)
856 self.assertEqual(B.reported, 2)
857 self.assertEqual(C.reported, 2)
858 self.assertEqual(D.reported, 2)
859 self.assertEqual(E.reported, 2)
860 self.assertEqual(F.reported, 2)
861 self.assertEqual(G.reported, 2)
862 self.assertEqual(self.history[6].changes,
863 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
864
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100865 def test_source_cache(self):
866 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400867 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700868
869 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
870 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
871 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
James E. Blair8b5408c2016-08-08 15:37:46 -0700872 A.addApproval('code-review', 2)
873 B.addApproval('code-review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700874
875 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
876 M1.setMerged()
877
878 B.setDependsOn(A, 1)
879 A.setDependsOn(M1, 1)
880
James E. Blair8b5408c2016-08-08 15:37:46 -0700881 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700882 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
883
884 self.waitUntilSettled()
885
886 for build in self.builds:
887 if build.parameters['ZUUL_PIPELINE'] == 'check':
888 build.release()
889 self.waitUntilSettled()
890 for build in self.builds:
891 if build.parameters['ZUUL_PIPELINE'] == 'check':
892 build.release()
893 self.waitUntilSettled()
894
James E. Blair8b5408c2016-08-08 15:37:46 -0700895 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700896 self.waitUntilSettled()
897
Joshua Hesketh352264b2015-08-11 23:42:08 +1000898 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700899 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000900 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700901
Paul Belanger174a8272017-03-14 13:20:10 -0400902 self.executor_server.hold_jobs_in_build = False
903 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700904 self.waitUntilSettled()
905
906 self.assertEqual(A.data['status'], 'MERGED')
907 self.assertEqual(B.data['status'], 'MERGED')
908 self.assertEqual(A.queried, 2) # Initial and isMerged
909 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
910
James E. Blair8c803f82012-07-31 16:25:42 -0700911 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700912 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700913 # TODO: move to test_gerrit (this is a unit test!)
914 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400915 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -0700916 (trusted, project) = tenant.getProject('org/project')
917 source = project.source
Paul Belanger9bba4902016-11-02 16:07:33 -0400918
919 # TODO(pabelanger): As we add more source / trigger APIs we should make
920 # it easier for users to create events for testing.
921 event = zuul.model.TriggerEvent()
922 event.trigger_name = 'gerrit'
923 event.change_number = '1'
924 event.patch_number = '2'
925
926 a = source.getChange(event)
927 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700928 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700929
James E. Blair8b5408c2016-08-08 15:37:46 -0700930 A.addApproval('code-review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400931 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700932 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700933
James E. Blair8b5408c2016-08-08 15:37:46 -0700934 A.addApproval('approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400935 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700936 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700937
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700938 def test_project_merge_conflict(self):
939 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700940
941 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700942 A = self.fake_gerrit.addFakeChange('org/project',
943 'master', 'A',
944 files={'conflict': 'foo'})
945 B = self.fake_gerrit.addFakeChange('org/project',
946 'master', 'B',
947 files={'conflict': 'bar'})
948 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -0700949 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700950 A.addApproval('code-review', 2)
951 B.addApproval('code-review', 2)
952 C.addApproval('code-review', 2)
953 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
954 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
955 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700956 self.waitUntilSettled()
957
James E. Blair6736beb2013-07-11 15:18:15 -0700958 self.assertEqual(A.reported, 1)
James E. Blair6736beb2013-07-11 15:18:15 -0700959 self.assertEqual(C.reported, 1)
960
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700961 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700962 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700963 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700964 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700965 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700966 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700967
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700968 self.gearman_server.hold_jobs_in_queue = False
969 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700970 self.waitUntilSettled()
971
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400972 self.assertEqual(A.data['status'], 'MERGED')
973 self.assertEqual(B.data['status'], 'NEW')
974 self.assertEqual(C.data['status'], 'MERGED')
975 self.assertEqual(A.reported, 2)
Tobias Henkel9842bd72017-05-16 13:40:03 +0200976 self.assertIn('Merge Failed', B.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400977 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700978
979 self.assertHistory([
980 dict(name='project-merge', result='SUCCESS', changes='1,1'),
981 dict(name='project-test1', result='SUCCESS', changes='1,1'),
982 dict(name='project-test2', result='SUCCESS', changes='1,1'),
983 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
984 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
985 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
986 ], ordered=False)
987
988 def test_delayed_merge_conflict(self):
989 "Test that delayed check merge conflicts are handled properly"
990
991 # Hold jobs in the gearman queue so that we can test whether
James E. Blair1960d682017-04-28 15:44:14 -0700992 # the executor sucesfully merges a change based on an old
993 # repo state (frozen by the scheduler) which would otherwise
994 # conflict.
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700995 self.gearman_server.hold_jobs_in_queue = True
996 A = self.fake_gerrit.addFakeChange('org/project',
997 'master', 'A',
998 files={'conflict': 'foo'})
999 B = self.fake_gerrit.addFakeChange('org/project',
1000 'master', 'B',
1001 files={'conflict': 'bar'})
1002 C = self.fake_gerrit.addFakeChange('org/project',
1003 'master', 'C')
1004 C.setDependsOn(B, 1)
1005
1006 # A enters the gate queue; B and C enter the check queue
1007 A.addApproval('code-review', 2)
1008 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1009 self.waitUntilSettled()
1010 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
James E. Blair8d144dc2017-05-05 10:13:45 -07001011 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001012 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1013 self.waitUntilSettled()
1014
1015 self.assertEqual(A.reported, 1)
1016 self.assertEqual(B.reported, 0) # Check does not report start
1017 self.assertEqual(C.reported, 0) # Check does not report start
1018
1019 # A merges while B and C are queued in check
1020 # Release A project-merge
1021 queue = self.gearman_server.getQueue()
1022 self.release(queue[0])
1023 self.waitUntilSettled()
1024
1025 # Release A project-test*
1026 # gate has higher precedence, so A's test jobs are added in
1027 # front of the merge jobs for B and C
1028 queue = self.gearman_server.getQueue()
1029 self.release(queue[0])
1030 self.release(queue[1])
1031 self.waitUntilSettled()
1032
1033 self.assertEqual(A.data['status'], 'MERGED')
1034 self.assertEqual(B.data['status'], 'NEW')
1035 self.assertEqual(C.data['status'], 'NEW')
1036 self.assertEqual(A.reported, 2)
1037 self.assertEqual(B.reported, 0)
1038 self.assertEqual(C.reported, 0)
1039 self.assertHistory([
1040 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1041 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1042 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1043 ], ordered=False)
1044
1045 # B and C report merge conflicts
1046 # Release B project-merge
1047 queue = self.gearman_server.getQueue()
1048 self.release(queue[0])
1049 self.waitUntilSettled()
1050
1051 # Release C
1052 self.gearman_server.hold_jobs_in_queue = False
1053 self.gearman_server.release()
1054 self.waitUntilSettled()
1055
1056 self.assertEqual(A.data['status'], 'MERGED')
1057 self.assertEqual(B.data['status'], 'NEW')
1058 self.assertEqual(C.data['status'], 'NEW')
1059 self.assertEqual(A.reported, 2)
1060 self.assertEqual(B.reported, 1)
1061 self.assertEqual(C.reported, 1)
1062
1063 self.assertHistory([
1064 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1065 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1066 dict(name='project-test2', result='SUCCESS', changes='1,1'),
James E. Blair1960d682017-04-28 15:44:14 -07001067 dict(name='project-merge', result='SUCCESS', changes='2,1'),
1068 dict(name='project-test1', result='SUCCESS', changes='2,1'),
1069 dict(name='project-test2', result='SUCCESS', changes='2,1'),
1070 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
1071 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
1072 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001073 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001074
James E. Blairdaabed22012-08-15 15:38:57 -07001075 def test_post(self):
1076 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001077
Zhongyue Luo5d556072012-09-21 02:00:47 +09001078 e = {
1079 "type": "ref-updated",
1080 "submitter": {
1081 "name": "User Name",
1082 },
1083 "refUpdate": {
1084 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1085 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1086 "refName": "master",
1087 "project": "org/project",
1088 }
1089 }
James E. Blairdaabed22012-08-15 15:38:57 -07001090 self.fake_gerrit.addEvent(e)
1091 self.waitUntilSettled()
1092
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001093 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001094 self.assertEqual(len(self.history), 1)
1095 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001096
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001097 def test_post_ignore_deletes(self):
1098 "Test that deleting refs does not trigger post jobs"
1099
1100 e = {
1101 "type": "ref-updated",
1102 "submitter": {
1103 "name": "User Name",
1104 },
1105 "refUpdate": {
1106 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1107 "newRev": "0000000000000000000000000000000000000000",
1108 "refName": "master",
1109 "project": "org/project",
1110 }
1111 }
1112 self.fake_gerrit.addEvent(e)
1113 self.waitUntilSettled()
1114
1115 job_names = [x.name for x in self.history]
1116 self.assertEqual(len(self.history), 0)
1117 self.assertNotIn('project-post', job_names)
1118
James E. Blaircc324192017-04-19 14:36:08 -07001119 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001120 def test_post_ignore_deletes_negative(self):
1121 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001122 e = {
1123 "type": "ref-updated",
1124 "submitter": {
1125 "name": "User Name",
1126 },
1127 "refUpdate": {
1128 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1129 "newRev": "0000000000000000000000000000000000000000",
1130 "refName": "master",
1131 "project": "org/project",
1132 }
1133 }
1134 self.fake_gerrit.addEvent(e)
1135 self.waitUntilSettled()
1136
1137 job_names = [x.name for x in self.history]
1138 self.assertEqual(len(self.history), 1)
1139 self.assertIn('project-post', job_names)
1140
James E. Blairec056492016-07-22 09:45:56 -07001141 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001142 def test_build_configuration_branch_interaction(self):
1143 "Test that switching between branches works"
1144 self.test_build_configuration()
1145 self.test_build_configuration_branch()
1146 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001147 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001148 repo = git.Repo(path)
1149 repo.heads.master.commit = repo.commit('init')
1150 self.test_build_configuration()
1151
James E. Blaircaec0c52012-08-22 14:52:22 -07001152 def test_dependent_changes_dequeue(self):
1153 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001154
James E. Blaircaec0c52012-08-22 14:52:22 -07001155 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1156 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1157 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001158 A.addApproval('code-review', 2)
1159 B.addApproval('code-review', 2)
1160 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001161
1162 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1163 M1.setMerged()
1164
1165 # C -> B -> A -> M1
1166
1167 C.setDependsOn(B, 1)
1168 B.setDependsOn(A, 1)
1169 A.setDependsOn(M1, 1)
1170
Paul Belanger174a8272017-03-14 13:20:10 -04001171 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001172
James E. Blair8b5408c2016-08-08 15:37:46 -07001173 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1174 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1175 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001176
1177 self.waitUntilSettled()
1178
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001179 self.assertEqual(A.data['status'], 'NEW')
1180 self.assertEqual(A.reported, 2)
1181 self.assertEqual(B.data['status'], 'NEW')
1182 self.assertEqual(B.reported, 2)
1183 self.assertEqual(C.data['status'], 'NEW')
James E. Blair0d3e83b2017-06-05 13:51:57 -07001184 self.assertIn('This change depends on a change that failed to merge.',
1185 C.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001186 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001187
James E. Blair972e3c72013-08-29 12:04:55 -07001188 def test_failing_dependent_changes(self):
1189 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001190 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001191 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1192 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1193 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1194 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1195 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001196 A.addApproval('code-review', 2)
1197 B.addApproval('code-review', 2)
1198 C.addApproval('code-review', 2)
1199 D.addApproval('code-review', 2)
1200 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001201
1202 # E, D -> C -> B, A
1203
1204 D.setDependsOn(C, 1)
1205 C.setDependsOn(B, 1)
1206
Paul Belanger174a8272017-03-14 13:20:10 -04001207 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001208
James E. Blair8b5408c2016-08-08 15:37:46 -07001209 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1210 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1211 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1212 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1213 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001214
1215 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001216 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001217 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001218 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001219 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001220 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001221 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001222 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001223 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001224 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001225 self.waitUntilSettled()
1226
Paul Belanger174a8272017-03-14 13:20:10 -04001227 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001228 for build in self.builds:
1229 if build.parameters['ZUUL_CHANGE'] != '1':
1230 build.release()
1231 self.waitUntilSettled()
1232
Paul Belanger174a8272017-03-14 13:20:10 -04001233 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001234 self.waitUntilSettled()
1235
1236 self.assertEqual(A.data['status'], 'MERGED')
1237 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001238 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001239 self.assertEqual(B.data['status'], 'NEW')
1240 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001241 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001242 self.assertEqual(C.data['status'], 'NEW')
1243 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001244 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001245 self.assertEqual(D.data['status'], 'NEW')
1246 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001247 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001248 self.assertEqual(E.data['status'], 'MERGED')
1249 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001250 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001251 self.assertEqual(len(self.history), 18)
1252
James E. Blairec590122012-08-22 15:19:31 -07001253 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001254 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001255 # If it's dequeued more than once, we should see extra
1256 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001257
Paul Belanger174a8272017-03-14 13:20:10 -04001258 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001259 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1260 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1261 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001262 A.addApproval('code-review', 2)
1263 B.addApproval('code-review', 2)
1264 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001265
Paul Belanger174a8272017-03-14 13:20:10 -04001266 self.executor_server.failJob('project-test1', A)
1267 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001268
James E. Blair8b5408c2016-08-08 15:37:46 -07001269 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1270 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1271 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001272
1273 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001274
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001275 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001276 self.assertEqual(self.builds[0].name, 'project-merge')
1277 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001278
Paul Belanger174a8272017-03-14 13:20:10 -04001279 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001280 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001281 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001282 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001283 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001284 self.waitUntilSettled()
1285
James E. Blair6df67132017-04-19 13:53:20 -07001286 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001287 self.assertEqual(self.builds[0].name, 'project-test1')
1288 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001289 self.assertEqual(self.builds[2].name, 'project-test1')
1290 self.assertEqual(self.builds[3].name, 'project-test2')
1291 self.assertEqual(self.builds[4].name, 'project-test1')
1292 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001293
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001294 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001295 self.waitUntilSettled()
1296
James E. Blair6df67132017-04-19 13:53:20 -07001297 self.assertEqual(len(self.builds), 2) # test2, merge for B
1298 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001299
Paul Belanger174a8272017-03-14 13:20:10 -04001300 self.executor_server.hold_jobs_in_build = False
1301 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001302 self.waitUntilSettled()
1303
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001304 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001305 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001306
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001307 self.assertEqual(A.data['status'], 'NEW')
1308 self.assertEqual(B.data['status'], 'MERGED')
1309 self.assertEqual(C.data['status'], 'MERGED')
1310 self.assertEqual(A.reported, 2)
1311 self.assertEqual(B.reported, 2)
1312 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001313
James E. Blaircc020532017-04-19 13:04:44 -07001314 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001315 def test_nonvoting_job(self):
1316 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001317
James E. Blair4ec821f2012-08-23 15:28:28 -07001318 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1319 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001320 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001321 self.executor_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001322 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001323
1324 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001325
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001326 self.assertEqual(A.data['status'], 'MERGED')
1327 self.assertEqual(A.reported, 2)
1328 self.assertEqual(
1329 self.getJobFromHistory('nonvoting-project-merge').result,
1330 'SUCCESS')
1331 self.assertEqual(
1332 self.getJobFromHistory('nonvoting-project-test1').result,
1333 'SUCCESS')
1334 self.assertEqual(
1335 self.getJobFromHistory('nonvoting-project-test2').result,
1336 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001337
James E. Blair5821bd92015-09-16 08:48:15 -07001338 for build in self.builds:
1339 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1340
James E. Blaire0487072012-08-29 17:38:31 -07001341 def test_check_queue_success(self):
1342 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001343
James E. Blaire0487072012-08-29 17:38:31 -07001344 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1345 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1346
1347 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001348
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001349 self.assertEqual(A.data['status'], 'NEW')
1350 self.assertEqual(A.reported, 1)
1351 self.assertEqual(self.getJobFromHistory('project-merge').result,
1352 'SUCCESS')
1353 self.assertEqual(self.getJobFromHistory('project-test1').result,
1354 'SUCCESS')
1355 self.assertEqual(self.getJobFromHistory('project-test2').result,
1356 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001357
1358 def test_check_queue_failure(self):
1359 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001360
James E. Blaire0487072012-08-29 17:38:31 -07001361 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001362 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001363 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1364
1365 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001366
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001367 self.assertEqual(A.data['status'], 'NEW')
1368 self.assertEqual(A.reported, 1)
1369 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001370 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001371 self.assertEqual(self.getJobFromHistory('project-test1').result,
1372 'SUCCESS')
1373 self.assertEqual(self.getJobFromHistory('project-test2').result,
1374 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001375
James E. Blair7cb84542017-04-19 13:35:05 -07001376 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001377 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001378 # This particular test does a large amount of merges and needs a little
1379 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001380 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001381 "test that dependent changes behind dequeued changes work"
1382 # This complicated test is a reproduction of a real life bug
1383 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001384
Paul Belanger174a8272017-03-14 13:20:10 -04001385 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001386 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1387 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1388 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1389 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1390 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1391 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1392 D.setDependsOn(C, 1)
1393 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001394 A.addApproval('code-review', 2)
1395 B.addApproval('code-review', 2)
1396 C.addApproval('code-review', 2)
1397 D.addApproval('code-review', 2)
1398 E.addApproval('code-review', 2)
1399 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001400
1401 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001402
1403 # Change object re-use in the gerrit trigger is hidden if
1404 # changes are added in quick succession; waiting makes it more
1405 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001406 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001407 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001408 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001409 self.waitUntilSettled()
1410
Paul Belanger174a8272017-03-14 13:20:10 -04001411 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001412 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001413 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001414 self.waitUntilSettled()
1415
James E. Blair8b5408c2016-08-08 15:37:46 -07001416 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001417 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001418 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001419 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001420 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001421 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001422 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001423 self.waitUntilSettled()
1424
Paul Belanger174a8272017-03-14 13:20:10 -04001425 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001426 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001427 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001428 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001429 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001430 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001431 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001432 self.waitUntilSettled()
1433
1434 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001435
1436 # Grab pointers to the jobs we want to release before
1437 # releasing any, because list indexes may change as
1438 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001439 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001440 a.release()
1441 b.release()
1442 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001443 self.waitUntilSettled()
1444
Paul Belanger174a8272017-03-14 13:20:10 -04001445 self.executor_server.hold_jobs_in_build = False
1446 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001447 self.waitUntilSettled()
1448
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001449 self.assertEqual(A.data['status'], 'NEW')
1450 self.assertEqual(B.data['status'], 'MERGED')
1451 self.assertEqual(C.data['status'], 'MERGED')
1452 self.assertEqual(D.data['status'], 'MERGED')
1453 self.assertEqual(E.data['status'], 'MERGED')
1454 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001455
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001456 self.assertEqual(A.reported, 2)
1457 self.assertEqual(B.reported, 2)
1458 self.assertEqual(C.reported, 2)
1459 self.assertEqual(D.reported, 2)
1460 self.assertEqual(E.reported, 2)
1461 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001462
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001463 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1464 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001465
1466 def test_merger_repack(self):
1467 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001468
James E. Blair05fed602012-09-07 12:45:24 -07001469 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001470 A.addApproval('code-review', 2)
1471 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001472 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001473 self.assertEqual(self.getJobFromHistory('project-merge').result,
1474 'SUCCESS')
1475 self.assertEqual(self.getJobFromHistory('project-test1').result,
1476 'SUCCESS')
1477 self.assertEqual(self.getJobFromHistory('project-test2').result,
1478 'SUCCESS')
1479 self.assertEqual(A.data['status'], 'MERGED')
1480 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001481 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001482 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001483
James E. Blair2a535672017-04-27 12:03:15 -07001484 path = os.path.join(self.merger_src_root, "review.example.com",
1485 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001486 if os.path.exists(path):
1487 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001488 path = os.path.join(self.executor_src_root, "review.example.com",
1489 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001490 if os.path.exists(path):
1491 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001492
1493 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001494 A.addApproval('code-review', 2)
1495 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001496 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001497 self.assertEqual(self.getJobFromHistory('project-merge').result,
1498 'SUCCESS')
1499 self.assertEqual(self.getJobFromHistory('project-test1').result,
1500 'SUCCESS')
1501 self.assertEqual(self.getJobFromHistory('project-test2').result,
1502 'SUCCESS')
1503 self.assertEqual(A.data['status'], 'MERGED')
1504 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001505
James E. Blair4886f282012-11-15 09:27:33 -08001506 def test_merger_repack_large_change(self):
1507 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001508 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001509 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001510 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001511 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001512 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001513 self.executor_server.merger._addProject('review.example.com',
1514 'org/project', url)
James E. Blair6df67132017-04-19 13:53:20 -07001515 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001516 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001517 # TODOv3(jeblair): add hostname to upstream root
1518 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001519 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001520 path = os.path.join(self.merger_src_root, 'review.example.com',
1521 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001522 if os.path.exists(path):
1523 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001524 path = os.path.join(self.executor_src_root, 'review.example.com',
1525 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001526 if os.path.exists(path):
1527 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001528
James E. Blair8b5408c2016-08-08 15:37:46 -07001529 A.addApproval('code-review', 2)
1530 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001531 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001532 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001533 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001534 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001535 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001536 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001537 'SUCCESS')
1538 self.assertEqual(A.data['status'], 'MERGED')
1539 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001540
James E. Blair2fa50962013-01-30 21:50:41 -08001541 def test_new_patchset_dequeues_old(self):
1542 "Test that a new patchset causes the old to be dequeued"
1543 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001544 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001545 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1546 M.setMerged()
1547
1548 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1549 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1550 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1551 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001552 A.addApproval('code-review', 2)
1553 B.addApproval('code-review', 2)
1554 C.addApproval('code-review', 2)
1555 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001556
1557 C.setDependsOn(B, 1)
1558 B.setDependsOn(A, 1)
1559 A.setDependsOn(M, 1)
1560
James E. Blair8b5408c2016-08-08 15:37:46 -07001561 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1562 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1563 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1564 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001565 self.waitUntilSettled()
1566
1567 B.addPatchset()
1568 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1569 self.waitUntilSettled()
1570
Paul Belanger174a8272017-03-14 13:20:10 -04001571 self.executor_server.hold_jobs_in_build = False
1572 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001573 self.waitUntilSettled()
1574
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001575 self.assertEqual(A.data['status'], 'MERGED')
1576 self.assertEqual(A.reported, 2)
1577 self.assertEqual(B.data['status'], 'NEW')
1578 self.assertEqual(B.reported, 2)
1579 self.assertEqual(C.data['status'], 'NEW')
1580 self.assertEqual(C.reported, 2)
1581 self.assertEqual(D.data['status'], 'MERGED')
1582 self.assertEqual(D.reported, 2)
1583 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001584
James E. Blairba437362015-02-07 11:41:52 -08001585 def test_new_patchset_check(self):
1586 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001587
Paul Belanger174a8272017-03-14 13:20:10 -04001588 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001589
1590 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001591 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001592 tenant = self.sched.abide.tenants.get('tenant-one')
1593 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001594
1595 # Add two git-dependent changes
1596 B.setDependsOn(A, 1)
1597 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1598 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001599 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1600 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001601
1602 # A live item, and a non-live/live pair
1603 items = check_pipeline.getAllItems()
1604 self.assertEqual(len(items), 3)
1605
1606 self.assertEqual(items[0].change.number, '1')
1607 self.assertEqual(items[0].change.patchset, '1')
1608 self.assertFalse(items[0].live)
1609
1610 self.assertEqual(items[1].change.number, '2')
1611 self.assertEqual(items[1].change.patchset, '1')
1612 self.assertTrue(items[1].live)
1613
1614 self.assertEqual(items[2].change.number, '1')
1615 self.assertEqual(items[2].change.patchset, '1')
1616 self.assertTrue(items[2].live)
1617
1618 # Add a new patchset to A
1619 A.addPatchset()
1620 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1621 self.waitUntilSettled()
1622
1623 # The live copy of A,1 should be gone, but the non-live and B
1624 # should continue, and we should have a new A,2
1625 items = check_pipeline.getAllItems()
1626 self.assertEqual(len(items), 3)
1627
1628 self.assertEqual(items[0].change.number, '1')
1629 self.assertEqual(items[0].change.patchset, '1')
1630 self.assertFalse(items[0].live)
1631
1632 self.assertEqual(items[1].change.number, '2')
1633 self.assertEqual(items[1].change.patchset, '1')
1634 self.assertTrue(items[1].live)
1635
1636 self.assertEqual(items[2].change.number, '1')
1637 self.assertEqual(items[2].change.patchset, '2')
1638 self.assertTrue(items[2].live)
1639
1640 # Add a new patchset to B
1641 B.addPatchset()
1642 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1643 self.waitUntilSettled()
1644
1645 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1646 # but we should have a new B,2 (still based on A,1)
1647 items = check_pipeline.getAllItems()
1648 self.assertEqual(len(items), 3)
1649
1650 self.assertEqual(items[0].change.number, '1')
1651 self.assertEqual(items[0].change.patchset, '2')
1652 self.assertTrue(items[0].live)
1653
1654 self.assertEqual(items[1].change.number, '1')
1655 self.assertEqual(items[1].change.patchset, '1')
1656 self.assertFalse(items[1].live)
1657
1658 self.assertEqual(items[2].change.number, '2')
1659 self.assertEqual(items[2].change.patchset, '2')
1660 self.assertTrue(items[2].live)
1661
1662 self.builds[0].release()
1663 self.waitUntilSettled()
1664 self.builds[0].release()
1665 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001666 self.executor_server.hold_jobs_in_build = False
1667 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001668 self.waitUntilSettled()
1669
1670 self.assertEqual(A.reported, 1)
1671 self.assertEqual(B.reported, 1)
1672 self.assertEqual(self.history[0].result, 'ABORTED')
1673 self.assertEqual(self.history[0].changes, '1,1')
1674 self.assertEqual(self.history[1].result, 'ABORTED')
1675 self.assertEqual(self.history[1].changes, '1,1 2,1')
1676 self.assertEqual(self.history[2].result, 'SUCCESS')
1677 self.assertEqual(self.history[2].changes, '1,2')
1678 self.assertEqual(self.history[3].result, 'SUCCESS')
1679 self.assertEqual(self.history[3].changes, '1,1 2,2')
1680
1681 def test_abandoned_gate(self):
1682 "Test that an abandoned change is dequeued from gate"
1683
Paul Belanger174a8272017-03-14 13:20:10 -04001684 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001685
1686 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001687 A.addApproval('code-review', 2)
1688 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001689 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001690 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1691 self.assertEqual(self.builds[0].name, 'project-merge')
1692
1693 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1694 self.waitUntilSettled()
1695
Paul Belanger174a8272017-03-14 13:20:10 -04001696 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001697 self.waitUntilSettled()
1698
Arie5b4048c2016-10-05 16:12:06 +03001699 self.assertBuilds([])
1700 self.assertHistory([
1701 dict(name='project-merge', result='ABORTED', changes='1,1')],
1702 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001703 self.assertEqual(A.reported, 1,
1704 "Abandoned gate change should report only start")
1705
1706 def test_abandoned_check(self):
1707 "Test that an abandoned change is dequeued from check"
1708
Paul Belanger174a8272017-03-14 13:20:10 -04001709 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001710
1711 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1712 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001713 tenant = self.sched.abide.tenants.get('tenant-one')
1714 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001715
1716 # Add two git-dependent changes
1717 B.setDependsOn(A, 1)
1718 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1719 self.waitUntilSettled()
1720 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1721 self.waitUntilSettled()
1722 # A live item, and a non-live/live pair
1723 items = check_pipeline.getAllItems()
1724 self.assertEqual(len(items), 3)
1725
1726 self.assertEqual(items[0].change.number, '1')
1727 self.assertFalse(items[0].live)
1728
1729 self.assertEqual(items[1].change.number, '2')
1730 self.assertTrue(items[1].live)
1731
1732 self.assertEqual(items[2].change.number, '1')
1733 self.assertTrue(items[2].live)
1734
1735 # Abandon A
1736 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1737 self.waitUntilSettled()
1738
1739 # The live copy of A should be gone, but the non-live and B
1740 # should continue
1741 items = check_pipeline.getAllItems()
1742 self.assertEqual(len(items), 2)
1743
1744 self.assertEqual(items[0].change.number, '1')
1745 self.assertFalse(items[0].live)
1746
1747 self.assertEqual(items[1].change.number, '2')
1748 self.assertTrue(items[1].live)
1749
Paul Belanger174a8272017-03-14 13:20:10 -04001750 self.executor_server.hold_jobs_in_build = False
1751 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001752 self.waitUntilSettled()
1753
1754 self.assertEqual(len(self.history), 4)
1755 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001756 'Build should have been aborted')
1757 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001758 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001759
Steve Varnau7b78b312015-04-03 14:49:46 -07001760 def test_abandoned_not_timer(self):
1761 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001762 # This test can not use simple_layout because it must start
1763 # with a configuration which does not include a
1764 # timer-triggered job so that we have an opportunity to set
1765 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001766 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001767 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001768 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001769 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001770 # The pipeline triggers every second, so we should have seen
1771 # several by now.
1772 time.sleep(5)
1773 self.waitUntilSettled()
1774 # Stop queuing timer triggered jobs so that the assertions
1775 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001776 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001777 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001778 self.sched.reconfigure(self.config)
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001779
Steve Varnau7b78b312015-04-03 14:49:46 -07001780 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1781
1782 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1783 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1784 self.waitUntilSettled()
1785 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1786
1787 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1788 self.waitUntilSettled()
1789
1790 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1791
Paul Belanger174a8272017-03-14 13:20:10 -04001792 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001793 self.waitUntilSettled()
1794
Arx Cruzb1b010d2013-10-28 19:49:59 -02001795 def test_zuul_url_return(self):
1796 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001797 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Paul Belanger174a8272017-03-14 13:20:10 -04001798 self.executor_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001799
1800 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001801 A.addApproval('code-review', 2)
1802 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001803 self.waitUntilSettled()
1804
1805 self.assertEqual(len(self.builds), 1)
1806 for build in self.builds:
1807 self.assertTrue('ZUUL_URL' in build.parameters)
1808
Paul Belanger174a8272017-03-14 13:20:10 -04001809 self.executor_server.hold_jobs_in_build = False
1810 self.executor_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001811 self.waitUntilSettled()
1812
James E. Blair2fa50962013-01-30 21:50:41 -08001813 def test_new_patchset_dequeues_old_on_head(self):
1814 "Test that a new patchset causes the old to be dequeued (at head)"
1815 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001816 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001817 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1818 M.setMerged()
1819 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1820 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1821 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1822 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001823 A.addApproval('code-review', 2)
1824 B.addApproval('code-review', 2)
1825 C.addApproval('code-review', 2)
1826 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001827
1828 C.setDependsOn(B, 1)
1829 B.setDependsOn(A, 1)
1830 A.setDependsOn(M, 1)
1831
James E. Blair8b5408c2016-08-08 15:37:46 -07001832 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1833 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1834 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1835 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001836 self.waitUntilSettled()
1837
1838 A.addPatchset()
1839 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1840 self.waitUntilSettled()
1841
Paul Belanger174a8272017-03-14 13:20:10 -04001842 self.executor_server.hold_jobs_in_build = False
1843 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001844 self.waitUntilSettled()
1845
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001846 self.assertEqual(A.data['status'], 'NEW')
1847 self.assertEqual(A.reported, 2)
1848 self.assertEqual(B.data['status'], 'NEW')
1849 self.assertEqual(B.reported, 2)
1850 self.assertEqual(C.data['status'], 'NEW')
1851 self.assertEqual(C.reported, 2)
1852 self.assertEqual(D.data['status'], 'MERGED')
1853 self.assertEqual(D.reported, 2)
1854 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001855
1856 def test_new_patchset_dequeues_old_without_dependents(self):
1857 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001858 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001859 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1860 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1861 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001862 A.addApproval('code-review', 2)
1863 B.addApproval('code-review', 2)
1864 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001865
James E. Blair8b5408c2016-08-08 15:37:46 -07001866 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1867 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1868 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001869 self.waitUntilSettled()
1870
1871 B.addPatchset()
1872 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1873 self.waitUntilSettled()
1874
Paul Belanger174a8272017-03-14 13:20:10 -04001875 self.executor_server.hold_jobs_in_build = False
1876 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001877 self.waitUntilSettled()
1878
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001879 self.assertEqual(A.data['status'], 'MERGED')
1880 self.assertEqual(A.reported, 2)
1881 self.assertEqual(B.data['status'], 'NEW')
1882 self.assertEqual(B.reported, 2)
1883 self.assertEqual(C.data['status'], 'MERGED')
1884 self.assertEqual(C.reported, 2)
1885 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001886
1887 def test_new_patchset_dequeues_old_independent_queue(self):
1888 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001889 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001890 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1891 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1892 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1893 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1894 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1895 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1896 self.waitUntilSettled()
1897
1898 B.addPatchset()
1899 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1900 self.waitUntilSettled()
1901
Paul Belanger174a8272017-03-14 13:20:10 -04001902 self.executor_server.hold_jobs_in_build = False
1903 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001904 self.waitUntilSettled()
1905
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001906 self.assertEqual(A.data['status'], 'NEW')
1907 self.assertEqual(A.reported, 1)
1908 self.assertEqual(B.data['status'], 'NEW')
1909 self.assertEqual(B.reported, 1)
1910 self.assertEqual(C.data['status'], 'NEW')
1911 self.assertEqual(C.reported, 1)
1912 self.assertEqual(len(self.history), 10)
1913 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001914
James E. Blaircc020532017-04-19 13:04:44 -07001915 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07001916 def test_noop_job(self):
1917 "Test that the internal noop job works"
1918 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001919 A.addApproval('code-review', 2)
1920 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001921 self.waitUntilSettled()
1922
1923 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1924 self.assertTrue(self.sched._areAllBuildsComplete())
1925 self.assertEqual(len(self.history), 0)
1926 self.assertEqual(A.data['status'], 'MERGED')
1927 self.assertEqual(A.reported, 2)
1928
James E. Blair06cc3922017-04-19 10:08:10 -07001929 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001930 def test_no_job_project(self):
1931 "Test that reports with no jobs don't get sent"
1932 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1933 'master', 'A')
1934 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1935 self.waitUntilSettled()
1936
1937 # Change wasn't reported to
1938 self.assertEqual(A.reported, False)
1939
1940 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001941 tenant = self.sched.abide.tenants.get('tenant-one')
1942 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001943 items = check_pipeline.getAllItems()
1944 self.assertEqual(len(items), 0)
1945
1946 self.assertEqual(len(self.history), 0)
1947
James E. Blair7d0dedc2013-02-21 17:26:09 -08001948 def test_zuul_refs(self):
1949 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04001950 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001951 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1952 M1.setMerged()
1953 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1954 M2.setMerged()
1955
1956 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1957 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1958 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1959 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001960 A.addApproval('code-review', 2)
1961 B.addApproval('code-review', 2)
1962 C.addApproval('code-review', 2)
1963 D.addApproval('code-review', 2)
1964 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1965 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1966 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1967 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001968
1969 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001970 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001971 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001972 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001973 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001974 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001975 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001976 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001977 self.waitUntilSettled()
1978
James E. Blair7d0dedc2013-02-21 17:26:09 -08001979 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08001980 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001981 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001982 if x.parameters['ZUUL_CHANGE'] == '3':
1983 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001984 a_build = x
1985 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001986 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001987 b_build = x
1988 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001989 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001990 c_build = x
1991 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001992 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001993 d_build = x
1994 if a_build and b_build and c_build and d_build:
1995 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08001996
1997 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001998 self.assertIsNotNone(a_zref)
1999 self.assertIsNotNone(b_zref)
2000 self.assertIsNotNone(c_zref)
2001 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002002
2003 # And they should all be different
2004 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002005 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002006
Clint Byrum3343e3e2016-11-15 16:05:03 -08002007 # should have a, not b, and should not be in project2
2008 self.assertTrue(a_build.hasChanges(A))
2009 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002010
Clint Byrum3343e3e2016-11-15 16:05:03 -08002011 # should have a and b, and should not be in project2
2012 self.assertTrue(b_build.hasChanges(A, B))
2013 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002014
Clint Byrum3343e3e2016-11-15 16:05:03 -08002015 # should have a and b in 1, c in 2
2016 self.assertTrue(c_build.hasChanges(A, B, C))
2017 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002018
Clint Byrum3343e3e2016-11-15 16:05:03 -08002019 # should have a and b in 1, c and d in 2
2020 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002021
Paul Belanger174a8272017-03-14 13:20:10 -04002022 self.executor_server.hold_jobs_in_build = False
2023 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002024 self.waitUntilSettled()
2025
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002026 self.assertEqual(A.data['status'], 'MERGED')
2027 self.assertEqual(A.reported, 2)
2028 self.assertEqual(B.data['status'], 'MERGED')
2029 self.assertEqual(B.reported, 2)
2030 self.assertEqual(C.data['status'], 'MERGED')
2031 self.assertEqual(C.reported, 2)
2032 self.assertEqual(D.data['status'], 'MERGED')
2033 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002034
James E. Blair4a28a882013-08-23 15:17:33 -07002035 def test_rerun_on_error(self):
2036 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002037 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002038 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002039 A.addApproval('code-review', 2)
2040 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002041 self.waitUntilSettled()
2042
James E. Blair412fba82017-01-26 15:00:50 -08002043 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002044 self.executor_server.hold_jobs_in_build = False
2045 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002046 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002047 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002048 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2049
James E. Blair412e5582013-04-22 15:50:12 -07002050 def test_statsd(self):
2051 "Test each of the statsd methods used in the scheduler"
2052 import extras
2053 statsd = extras.try_import('statsd.statsd')
2054 statsd.incr('test-incr')
2055 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002056 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002057 self.assertReportedStat('test-incr', '1|c')
2058 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002059 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002060
James E. Blairdad52252014-02-07 16:59:17 -08002061 def test_stuck_job_cleanup(self):
2062 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002063
2064 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002065 self.gearman_server.hold_jobs_in_queue = True
2066 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002067 A.addApproval('code-review', 2)
2068 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002069 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002070 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002071 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2072
James E. Blair31cb3e82017-04-20 13:08:56 -07002073 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002074 self.sched.reconfigure(self.config)
2075 self.waitUntilSettled()
2076
James E. Blair18c64442014-03-18 10:14:45 -07002077 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002078 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002079 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002080 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2081 self.assertTrue(self.sched._areAllBuildsComplete())
2082
2083 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002084 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002085 self.assertEqual(self.history[0].result, 'SUCCESS')
2086
James E. Blair879dafb2015-07-17 14:04:49 -07002087 def test_file_head(self):
2088 # This is a regression test for an observed bug. A change
2089 # with a file named "HEAD" in the root directory of the repo
2090 # was processed by a merger. It then was unable to reset the
2091 # repo because of:
2092 # GitCommandError: 'git reset --hard HEAD' returned
2093 # with exit code 128
2094 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2095 # and filename
2096 # Use '--' to separate filenames from revisions'
2097
2098 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002099 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002100 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2101
2102 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2103 self.waitUntilSettled()
2104
2105 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2106 self.waitUntilSettled()
2107
2108 self.assertIn('Build succeeded', A.messages[0])
2109 self.assertIn('Build succeeded', B.messages[0])
2110
James E. Blair70c71582013-03-06 08:50:50 -08002111 def test_file_jobs(self):
2112 "Test that file jobs run only when appropriate"
2113 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002114 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002115 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002116 A.addApproval('code-review', 2)
2117 B.addApproval('code-review', 2)
2118 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2119 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002120 self.waitUntilSettled()
2121
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002122 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002123 if x.name == 'project-testfile']
2124
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002125 self.assertEqual(len(testfile_jobs), 1)
2126 self.assertEqual(testfile_jobs[0].changes, '1,2')
2127 self.assertEqual(A.data['status'], 'MERGED')
2128 self.assertEqual(A.reported, 2)
2129 self.assertEqual(B.data['status'], 'MERGED')
2130 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002131
Clint Byrum3ee39f32016-11-17 23:45:07 -08002132 def _test_irrelevant_files_jobs(self, should_skip):
2133 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002134 if should_skip:
2135 files = {'ignoreme': 'ignored\n'}
2136 else:
2137 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002138
2139 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002140 'master',
2141 'test irrelevant-files',
2142 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002143 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2144 self.waitUntilSettled()
2145
2146 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002147 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002148
2149 if should_skip:
2150 self.assertEqual([], tested_change_ids)
2151 else:
2152 self.assertIn(change.data['number'], tested_change_ids)
2153
James E. Blaircc324192017-04-19 14:36:08 -07002154 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002155 def test_irrelevant_files_match_skips_job(self):
2156 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002157
James E. Blaircc324192017-04-19 14:36:08 -07002158 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002159 def test_irrelevant_files_no_match_runs_job(self):
2160 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002161
James E. Blaircc324192017-04-19 14:36:08 -07002162 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002163 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002164 files = {'ignoreme': 'ignored\n'}
2165
2166 change = self.fake_gerrit.addFakeChange('org/project',
2167 'master',
2168 'test irrelevant-files',
2169 files=files)
2170 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2171 self.waitUntilSettled()
2172
2173 run_jobs = set([build.name for build in self.history])
2174
2175 self.assertEqual(set(['project-test-nomatch-starts-empty',
2176 'project-test-nomatch-starts-full']), run_jobs)
2177
James E. Blairc8a1e052014-02-25 09:29:26 -08002178 def test_queue_names(self):
2179 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002180 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002181 (trusted, project1) = tenant.getProject('org/project1')
2182 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002183 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2184 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2185 self.assertEqual(q1.name, 'integrated')
2186 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002187
James E. Blair64ed6f22013-07-10 14:07:23 -07002188 def test_queue_precedence(self):
2189 "Test that queue precedence works"
2190
2191 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002192 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002193 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2194 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002195 A.addApproval('code-review', 2)
2196 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002197
2198 self.waitUntilSettled()
2199 self.gearman_server.hold_jobs_in_queue = False
2200 self.gearman_server.release()
2201 self.waitUntilSettled()
2202
James E. Blair8de58bd2013-07-18 16:23:33 -07002203 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002204 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002205 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002206 self.waitUntilSettled()
2207
James E. Blair64ed6f22013-07-10 14:07:23 -07002208 self.log.debug(self.history)
2209 self.assertEqual(self.history[0].pipeline, 'gate')
2210 self.assertEqual(self.history[1].pipeline, 'check')
2211 self.assertEqual(self.history[2].pipeline, 'gate')
2212 self.assertEqual(self.history[3].pipeline, 'gate')
2213 self.assertEqual(self.history[4].pipeline, 'check')
2214 self.assertEqual(self.history[5].pipeline, 'check')
2215
Clark Boylana5edbe42014-06-03 16:39:10 -07002216 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002217 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002218 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002219 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002220 A.addApproval('code-review', 2)
2221 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002222 self.waitUntilSettled()
2223
Paul Belanger174a8272017-03-14 13:20:10 -04002224 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002225 self.waitUntilSettled()
2226
James E. Blair1843a552013-07-03 14:19:52 -07002227 port = self.webapp.server.socket.getsockname()[1]
2228
Adam Gandelman77a12c72017-02-03 17:43:43 -08002229 req = urllib.request.Request(
2230 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002231 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002232 headers = f.info()
2233 self.assertIn('Content-Length', headers)
2234 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002235 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2236 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002237 self.assertIn('Access-Control-Allow-Origin', headers)
2238 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002239 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002240 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002241 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002242
Paul Belanger174a8272017-03-14 13:20:10 -04002243 self.executor_server.hold_jobs_in_build = False
2244 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002245 self.waitUntilSettled()
2246
2247 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002248 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002249 for p in data['pipelines']:
2250 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002251 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002252 self.assertEqual(q['window'], 20)
2253 else:
2254 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002255 for head in q['heads']:
2256 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002257 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002258 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002259 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002260 status_jobs.append(job)
2261 self.assertEqual('project-merge', status_jobs[0]['name'])
2262 self.assertEqual('https://server/job/project-merge/0/',
2263 status_jobs[0]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002264 self.assertEqual('https://server/job/project-merge/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002265 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002266 self.assertEqual('project-test1', status_jobs[1]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002267 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002268 status_jobs[1]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002269 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002270 status_jobs[1]['report_url'])
2271
2272 self.assertEqual('project-test2', status_jobs[2]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002273 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002274 status_jobs[2]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002275 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002276 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002277
James E. Blaircdccd972013-07-01 12:10:22 -07002278 def test_live_reconfiguration(self):
2279 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002280 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002281 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002282 A.addApproval('code-review', 2)
2283 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002284 self.waitUntilSettled()
2285
2286 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002287 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002288
Paul Belanger174a8272017-03-14 13:20:10 -04002289 self.executor_server.hold_jobs_in_build = False
2290 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002291 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002292 self.assertEqual(self.getJobFromHistory('project-merge').result,
2293 'SUCCESS')
2294 self.assertEqual(self.getJobFromHistory('project-test1').result,
2295 'SUCCESS')
2296 self.assertEqual(self.getJobFromHistory('project-test2').result,
2297 'SUCCESS')
2298 self.assertEqual(A.data['status'], 'MERGED')
2299 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002300
James E. Blair6bc782d2015-07-17 16:20:21 -07002301 def test_live_reconfiguration_merge_conflict(self):
2302 # A real-world bug: a change in a gate queue has a merge
2303 # conflict and a job is added to its project while it's
2304 # sitting in the queue. The job gets added to the change and
2305 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002306 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002307
2308 # This change is fine. It's here to stop the queue long
2309 # enough for the next change to be subject to the
2310 # reconfiguration, as well as to provide a conflict for the
2311 # next change. This change will succeed and merge.
2312 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002313 A.addPatchset({'conflict': 'A'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002314 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002315
2316 # This change will be in merge conflict. During the
2317 # reconfiguration, we will add a job. We want to make sure
2318 # that doesn't cause it to get stuck.
2319 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002320 B.addPatchset({'conflict': 'B'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002321 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002322
James E. Blair8b5408c2016-08-08 15:37:46 -07002323 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2324 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002325
2326 self.waitUntilSettled()
2327
2328 # No jobs have run yet
2329 self.assertEqual(A.data['status'], 'NEW')
2330 self.assertEqual(A.reported, 1)
2331 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002332 self.assertEqual(len(self.history), 0)
2333
2334 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002335 self.commitConfigUpdate('common-config',
2336 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002337 self.sched.reconfigure(self.config)
2338 self.waitUntilSettled()
2339
Paul Belanger174a8272017-03-14 13:20:10 -04002340 self.executor_server.hold_jobs_in_build = False
2341 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002342 self.waitUntilSettled()
2343
2344 self.assertEqual(A.data['status'], 'MERGED')
2345 self.assertEqual(A.reported, 2)
2346 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002347 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002348 self.assertEqual(self.getJobFromHistory('project-merge').result,
2349 'SUCCESS')
2350 self.assertEqual(self.getJobFromHistory('project-test1').result,
2351 'SUCCESS')
2352 self.assertEqual(self.getJobFromHistory('project-test2').result,
2353 'SUCCESS')
2354 self.assertEqual(self.getJobFromHistory('project-test3').result,
2355 'SUCCESS')
2356 self.assertEqual(len(self.history), 4)
2357
James E. Blair400e8fd2015-07-30 17:44:45 -07002358 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002359 # An extrapolation of test_live_reconfiguration_merge_conflict
2360 # that tests a job added to a job tree with a failed root does
2361 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002362 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002363
2364 # This change is fine. It's here to stop the queue long
2365 # enough for the next change to be subject to the
2366 # reconfiguration. This change will succeed and merge.
2367 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002368 A.addPatchset({'conflict': 'A'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002369 A.addApproval('code-review', 2)
2370 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002371 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002372 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002373 self.waitUntilSettled()
2374
2375 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002376 self.executor_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002377 B.addApproval('code-review', 2)
2378 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002379 self.waitUntilSettled()
2380
Paul Belanger174a8272017-03-14 13:20:10 -04002381 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002382 self.waitUntilSettled()
2383
2384 # Both -merge jobs have run, but no others.
2385 self.assertEqual(A.data['status'], 'NEW')
2386 self.assertEqual(A.reported, 1)
2387 self.assertEqual(B.data['status'], 'NEW')
2388 self.assertEqual(B.reported, 1)
2389 self.assertEqual(self.history[0].result, 'SUCCESS')
2390 self.assertEqual(self.history[0].name, 'project-merge')
2391 self.assertEqual(self.history[1].result, 'FAILURE')
2392 self.assertEqual(self.history[1].name, 'project-merge')
2393 self.assertEqual(len(self.history), 2)
2394
2395 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002396 self.commitConfigUpdate('common-config',
2397 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002398 self.sched.reconfigure(self.config)
2399 self.waitUntilSettled()
2400
Paul Belanger174a8272017-03-14 13:20:10 -04002401 self.executor_server.hold_jobs_in_build = False
2402 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002403 self.waitUntilSettled()
2404
2405 self.assertEqual(A.data['status'], 'MERGED')
2406 self.assertEqual(A.reported, 2)
2407 self.assertEqual(B.data['status'], 'NEW')
2408 self.assertEqual(B.reported, 2)
2409 self.assertEqual(self.history[0].result, 'SUCCESS')
2410 self.assertEqual(self.history[0].name, 'project-merge')
2411 self.assertEqual(self.history[1].result, 'FAILURE')
2412 self.assertEqual(self.history[1].name, 'project-merge')
2413 self.assertEqual(self.history[2].result, 'SUCCESS')
2414 self.assertEqual(self.history[3].result, 'SUCCESS')
2415 self.assertEqual(self.history[4].result, 'SUCCESS')
2416 self.assertEqual(len(self.history), 5)
2417
James E. Blair400e8fd2015-07-30 17:44:45 -07002418 def test_live_reconfiguration_failed_job(self):
2419 # Test that a change with a removed failing job does not
2420 # disrupt reconfiguration. If a change has a failed job and
2421 # that job is removed during a reconfiguration, we observed a
2422 # bug where the code to re-set build statuses would run on
2423 # that build and raise an exception because the job no longer
2424 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002425 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002426
2427 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2428
2429 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002430 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002431
2432 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2433 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002434 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002435 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002436 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002437 self.waitUntilSettled()
2438
2439 self.assertEqual(A.data['status'], 'NEW')
2440 self.assertEqual(A.reported, 0)
2441
2442 self.assertEqual(self.getJobFromHistory('project-merge').result,
2443 'SUCCESS')
2444 self.assertEqual(self.getJobFromHistory('project-test1').result,
2445 'FAILURE')
2446 self.assertEqual(len(self.history), 2)
2447
2448 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002449 self.commitConfigUpdate('common-config',
2450 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002451 self.sched.reconfigure(self.config)
2452 self.waitUntilSettled()
2453
Paul Belanger174a8272017-03-14 13:20:10 -04002454 self.executor_server.hold_jobs_in_build = False
2455 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002456 self.waitUntilSettled()
2457
2458 self.assertEqual(self.getJobFromHistory('project-test2').result,
2459 'SUCCESS')
2460 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2461 'SUCCESS')
2462 self.assertEqual(len(self.history), 4)
2463
2464 self.assertEqual(A.data['status'], 'NEW')
2465 self.assertEqual(A.reported, 1)
2466 self.assertIn('Build succeeded', A.messages[0])
2467 # Ensure the removed job was not included in the report.
2468 self.assertNotIn('project-test1', A.messages[0])
2469
James E. Blairfe707d12015-08-05 15:18:15 -07002470 def test_live_reconfiguration_shared_queue(self):
2471 # Test that a change with a failing job which was removed from
2472 # this project but otherwise still exists in the system does
2473 # not disrupt reconfiguration.
2474
Paul Belanger174a8272017-03-14 13:20:10 -04002475 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002476
2477 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2478
Paul Belanger174a8272017-03-14 13:20:10 -04002479 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002480
2481 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2482 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002483 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002484 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002485 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002486 self.waitUntilSettled()
2487
2488 self.assertEqual(A.data['status'], 'NEW')
2489 self.assertEqual(A.reported, 0)
2490
Clint Byrum69663d42017-05-01 14:51:04 -07002491 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002492 'SUCCESS')
2493 self.assertEqual(self.getJobFromHistory(
2494 'project1-project2-integration').result, 'FAILURE')
2495 self.assertEqual(len(self.history), 2)
2496
2497 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002498 self.commitConfigUpdate(
2499 'common-config',
2500 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002501 self.sched.reconfigure(self.config)
2502 self.waitUntilSettled()
2503
Paul Belanger174a8272017-03-14 13:20:10 -04002504 self.executor_server.hold_jobs_in_build = False
2505 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002506 self.waitUntilSettled()
2507
Clint Byrum69663d42017-05-01 14:51:04 -07002508 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002509 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002510 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002511 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002512 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002513 'SUCCESS')
2514 self.assertEqual(self.getJobFromHistory(
2515 'project1-project2-integration').result, 'FAILURE')
2516 self.assertEqual(len(self.history), 4)
2517
2518 self.assertEqual(A.data['status'], 'NEW')
2519 self.assertEqual(A.reported, 1)
2520 self.assertIn('Build succeeded', A.messages[0])
2521 # Ensure the removed job was not included in the report.
2522 self.assertNotIn('project1-project2-integration', A.messages[0])
2523
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002524 def test_double_live_reconfiguration_shared_queue(self):
2525 # This was a real-world regression. A change is added to
2526 # gate; a reconfigure happens, a second change which depends
2527 # on the first is added, and a second reconfiguration happens.
2528 # Ensure that both changes merge.
2529
2530 # A failure may indicate incorrect caching or cleaning up of
2531 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002532 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002533
2534 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2535 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2536 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002537 A.addApproval('code-review', 2)
2538 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002539
2540 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002541 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002542 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002543 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002544 self.waitUntilSettled()
2545
2546 # Reconfigure (with only one change in the pipeline).
2547 self.sched.reconfigure(self.config)
2548 self.waitUntilSettled()
2549
2550 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002551 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002552 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002553 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002554 self.waitUntilSettled()
2555
2556 # Reconfigure (with both in the pipeline).
2557 self.sched.reconfigure(self.config)
2558 self.waitUntilSettled()
2559
Paul Belanger174a8272017-03-14 13:20:10 -04002560 self.executor_server.hold_jobs_in_build = False
2561 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002562 self.waitUntilSettled()
2563
2564 self.assertEqual(len(self.history), 8)
2565
2566 self.assertEqual(A.data['status'], 'MERGED')
2567 self.assertEqual(A.reported, 2)
2568 self.assertEqual(B.data['status'], 'MERGED')
2569 self.assertEqual(B.reported, 2)
2570
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002571 def test_live_reconfiguration_del_project(self):
2572 # Test project deletion from layout
2573 # while changes are enqueued
2574
Paul Belanger174a8272017-03-14 13:20:10 -04002575 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002576 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2577 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2578 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2579
2580 # A Depends-On: B
2581 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2582 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002583 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002584
2585 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2586 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2587 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002588 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002589 self.waitUntilSettled()
2590 self.assertEqual(len(self.builds), 5)
2591
2592 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002593 self.commitConfigUpdate(
2594 'common-config',
2595 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002596 self.sched.reconfigure(self.config)
2597 self.waitUntilSettled()
2598
2599 # Builds for C aborted, builds for A succeed,
2600 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002601 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002602 self.assertEqual(job_c.changes, '3,1')
2603 self.assertEqual(job_c.result, 'ABORTED')
2604
Paul Belanger174a8272017-03-14 13:20:10 -04002605 self.executor_server.hold_jobs_in_build = False
2606 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002607 self.waitUntilSettled()
2608
Clint Byrum0149ada2017-04-04 12:22:01 -07002609 self.assertEqual(
2610 self.getJobFromHistory('project-test1', 'org/project').changes,
2611 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002612
2613 self.assertEqual(A.data['status'], 'NEW')
2614 self.assertEqual(B.data['status'], 'NEW')
2615 self.assertEqual(C.data['status'], 'NEW')
2616 self.assertEqual(A.reported, 1)
2617 self.assertEqual(B.reported, 0)
2618 self.assertEqual(C.reported, 0)
2619
Clint Byrum0149ada2017-04-04 12:22:01 -07002620 tenant = self.sched.abide.tenants.get('tenant-one')
2621 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002622 self.assertIn('Build succeeded', A.messages[0])
2623
James E. Blairec056492016-07-22 09:45:56 -07002624 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002625 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002626 self.updateConfigLayout(
2627 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002628 self.sched.reconfigure(self.config)
2629
2630 self.init_repo("org/new-project")
2631 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2632
James E. Blair8b5408c2016-08-08 15:37:46 -07002633 A.addApproval('code-review', 2)
2634 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002635 self.waitUntilSettled()
2636 self.assertEqual(self.getJobFromHistory('project-merge').result,
2637 'SUCCESS')
2638 self.assertEqual(self.getJobFromHistory('project-test1').result,
2639 'SUCCESS')
2640 self.assertEqual(self.getJobFromHistory('project-test2').result,
2641 'SUCCESS')
2642 self.assertEqual(A.data['status'], 'MERGED')
2643 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002644
James E. Blair9ea70072017-04-19 16:05:30 -07002645 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002646 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002647 self.init_repo("org/delete-project")
2648 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2649
James E. Blair8b5408c2016-08-08 15:37:46 -07002650 A.addApproval('code-review', 2)
2651 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002652 self.waitUntilSettled()
2653 self.assertEqual(self.getJobFromHistory('project-merge').result,
2654 'SUCCESS')
2655 self.assertEqual(self.getJobFromHistory('project-test1').result,
2656 'SUCCESS')
2657 self.assertEqual(self.getJobFromHistory('project-test2').result,
2658 'SUCCESS')
2659 self.assertEqual(A.data['status'], 'MERGED')
2660 self.assertEqual(A.reported, 2)
2661
2662 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002663 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002664 if os.path.exists(os.path.join(self.merger_src_root, p)):
2665 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002666 if os.path.exists(os.path.join(self.executor_src_root, p)):
2667 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002668
2669 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2670
James E. Blair8b5408c2016-08-08 15:37:46 -07002671 B.addApproval('code-review', 2)
2672 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002673 self.waitUntilSettled()
2674 self.assertEqual(self.getJobFromHistory('project-merge').result,
2675 'SUCCESS')
2676 self.assertEqual(self.getJobFromHistory('project-test1').result,
2677 'SUCCESS')
2678 self.assertEqual(self.getJobFromHistory('project-test2').result,
2679 'SUCCESS')
2680 self.assertEqual(B.data['status'], 'MERGED')
2681 self.assertEqual(B.reported, 2)
2682
James E. Blaire68d43f2017-04-19 14:03:30 -07002683 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002684 def test_tags(self):
2685 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002686 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2687 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2688 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2689 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2690 self.waitUntilSettled()
2691
James E. Blaire68d43f2017-04-19 14:03:30 -07002692 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002693
2694 results = {self.getJobFromHistory('merge',
2695 project='org/project1').uuid: 'extratag merge',
2696 self.getJobFromHistory('merge',
2697 project='org/project2').uuid: 'merge'}
James E. Blair456f2fb2016-02-09 09:29:33 -08002698
2699 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002700 self.assertEqual(results.get(build.uuid, ''),
2701 build.parameters['vars']['zuul'].get('tags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002702
James E. Blair63bb0ef2013-07-29 17:14:51 -07002703 def test_timer(self):
2704 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002705 # This test can not use simple_layout because it must start
2706 # with a configuration which does not include a
2707 # timer-triggered job so that we have an opportunity to set
2708 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002709 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002710 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002711 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002712
Clark Boylan3ee090a2014-04-03 20:55:09 -07002713 # The pipeline triggers every second, so we should have seen
2714 # several by now.
2715 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002716 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002717
2718 self.assertEqual(len(self.builds), 2)
2719
James E. Blair63bb0ef2013-07-29 17:14:51 -07002720 port = self.webapp.server.socket.getsockname()[1]
2721
Clint Byrum5e729e92017-02-08 06:34:57 -08002722 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002723 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002724 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002725 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002726
Paul Belanger174a8272017-03-14 13:20:10 -04002727 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002728 # Stop queuing timer triggered jobs so that the assertions
2729 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002730 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002731 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04002732 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002733 self.waitUntilSettled()
2734
2735 self.assertEqual(self.getJobFromHistory(
2736 'project-bitrot-stable-old').result, 'SUCCESS')
2737 self.assertEqual(self.getJobFromHistory(
2738 'project-bitrot-stable-older').result, 'SUCCESS')
2739
2740 data = json.loads(data)
2741 status_jobs = set()
2742 for p in data['pipelines']:
2743 for q in p['change_queues']:
2744 for head in q['heads']:
2745 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002746 for job in change['jobs']:
2747 status_jobs.add(job['name'])
2748 self.assertIn('project-bitrot-stable-old', status_jobs)
2749 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002750
James E. Blair4f6033c2014-03-27 15:49:09 -07002751 def test_idle(self):
2752 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002753 # This test can not use simple_layout because it must start
2754 # with a configuration which does not include a
2755 # timer-triggered job so that we have an opportunity to set
2756 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002757 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002758
Clark Boylan3ee090a2014-04-03 20:55:09 -07002759 for x in range(1, 3):
2760 # Test that timer triggers periodic jobs even across
2761 # layout config reloads.
2762 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002763 self.commitConfigUpdate('common-config',
2764 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002765 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002766 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002767
Clark Boylan3ee090a2014-04-03 20:55:09 -07002768 # The pipeline triggers every second, so we should have seen
2769 # several by now.
2770 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002771
2772 # Stop queuing timer triggered jobs so that the assertions
2773 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002774 self.commitConfigUpdate('common-config',
2775 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002776 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002777 self.waitUntilSettled()
Clint Byrum58264dc2017-02-07 21:21:22 -08002778 self.assertEqual(len(self.builds), 2,
2779 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002780 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002781 self.waitUntilSettled()
2782 self.assertEqual(len(self.builds), 0)
2783 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002784
James E. Blaircc324192017-04-19 14:36:08 -07002785 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002786 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002787 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2788 self.waitUntilSettled()
2789
2790 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2791 self.waitUntilSettled()
2792
James E. Blairff80a2f2013-12-27 13:24:06 -08002793 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002794
2795 # A.messages only holds what FakeGerrit places in it. Thus we
2796 # work on the knowledge of what the first message should be as
2797 # it is only configured to go to SMTP.
2798
2799 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002800 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002801 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002802 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002803 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002804 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002805
2806 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002807 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002808 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002809 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002810 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002811 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002812
James E. Blaire5910202013-12-27 09:50:31 -08002813 def test_timer_smtp(self):
2814 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002815 # This test can not use simple_layout because it must start
2816 # with a configuration which does not include a
2817 # timer-triggered job so that we have an opportunity to set
2818 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002819 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002820 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002821 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002822
Clark Boylan3ee090a2014-04-03 20:55:09 -07002823 # The pipeline triggers every second, so we should have seen
2824 # several by now.
2825 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002826 self.waitUntilSettled()
2827
Clark Boylan3ee090a2014-04-03 20:55:09 -07002828 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04002829 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002830 self.waitUntilSettled()
2831 self.assertEqual(len(self.history), 2)
2832
James E. Blaire5910202013-12-27 09:50:31 -08002833 self.assertEqual(self.getJobFromHistory(
2834 'project-bitrot-stable-old').result, 'SUCCESS')
2835 self.assertEqual(self.getJobFromHistory(
2836 'project-bitrot-stable-older').result, 'SUCCESS')
2837
James E. Blairff80a2f2013-12-27 13:24:06 -08002838 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002839
2840 # A.messages only holds what FakeGerrit places in it. Thus we
2841 # work on the knowledge of what the first message should be as
2842 # it is only configured to go to SMTP.
2843
2844 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002845 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002846 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002847 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002848 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002849 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002850
Clark Boylan3ee090a2014-04-03 20:55:09 -07002851 # Stop queuing timer triggered jobs and let any that may have
2852 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002853 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002854 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07002855 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002856 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002857 self.waitUntilSettled()
2858
Joshua Hesketh25695cb2017-03-06 12:50:04 +11002859 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00002860 def test_timer_sshkey(self):
2861 "Test that a periodic job can setup SSH key authentication"
2862 self.worker.hold_jobs_in_build = True
2863 self.config.set('zuul', 'layout_config',
2864 'tests/fixtures/layout-timer.yaml')
2865 self.sched.reconfigure(self.config)
2866 self.registerJobs()
2867
2868 # The pipeline triggers every second, so we should have seen
2869 # several by now.
2870 time.sleep(5)
2871 self.waitUntilSettled()
2872
2873 self.assertEqual(len(self.builds), 2)
2874
2875 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
2876 self.assertTrue(os.path.isfile(ssh_wrapper))
2877 with open(ssh_wrapper) as f:
2878 ssh_wrapper_content = f.read()
2879 self.assertIn("fake_id_rsa", ssh_wrapper_content)
2880 # In the unit tests Merger runs in the same process,
2881 # so we see its' environment variables
2882 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
2883
2884 self.worker.release('.*')
2885 self.waitUntilSettled()
2886 self.assertEqual(len(self.history), 2)
2887
2888 self.assertEqual(self.getJobFromHistory(
2889 'project-bitrot-stable-old').result, 'SUCCESS')
2890 self.assertEqual(self.getJobFromHistory(
2891 'project-bitrot-stable-older').result, 'SUCCESS')
2892
2893 # Stop queuing timer triggered jobs and let any that may have
2894 # queued through so that end of test assertions pass.
2895 self.config.set('zuul', 'layout_config',
2896 'tests/fixtures/layout-no-timer.yaml')
2897 self.sched.reconfigure(self.config)
2898 self.registerJobs()
2899 self.waitUntilSettled()
2900 self.worker.release('.*')
2901 self.waitUntilSettled()
2902
James E. Blair91e34592015-07-31 16:45:59 -07002903 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08002904 "Test that the RPC client can enqueue a change"
2905 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002906 A.addApproval('code-review', 2)
2907 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08002908
2909 client = zuul.rpcclient.RPCClient('127.0.0.1',
2910 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002911 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04002912 r = client.enqueue(tenant='tenant-one',
2913 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002914 project='org/project',
2915 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002916 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002917 self.waitUntilSettled()
2918 self.assertEqual(self.getJobFromHistory('project-merge').result,
2919 'SUCCESS')
2920 self.assertEqual(self.getJobFromHistory('project-test1').result,
2921 'SUCCESS')
2922 self.assertEqual(self.getJobFromHistory('project-test2').result,
2923 'SUCCESS')
2924 self.assertEqual(A.data['status'], 'MERGED')
2925 self.assertEqual(A.reported, 2)
2926 self.assertEqual(r, True)
2927
James E. Blair91e34592015-07-31 16:45:59 -07002928 def test_client_enqueue_ref(self):
2929 "Test that the RPC client can enqueue a ref"
2930
2931 client = zuul.rpcclient.RPCClient('127.0.0.1',
2932 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002933 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07002934 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04002935 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07002936 pipeline='post',
2937 project='org/project',
2938 trigger='gerrit',
2939 ref='master',
2940 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
2941 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
2942 self.waitUntilSettled()
2943 job_names = [x.name for x in self.history]
2944 self.assertEqual(len(self.history), 1)
2945 self.assertIn('project-post', job_names)
2946 self.assertEqual(r, True)
2947
James E. Blairad28e912013-11-27 10:43:22 -08002948 def test_client_enqueue_negative(self):
2949 "Test that the RPC client returns errors"
2950 client = zuul.rpcclient.RPCClient('127.0.0.1',
2951 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002952 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08002953 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04002954 "Invalid tenant"):
2955 r = client.enqueue(tenant='tenant-foo',
2956 pipeline='gate',
2957 project='org/project',
2958 trigger='gerrit',
2959 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04002960 self.assertEqual(r, False)
2961
2962 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08002963 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002964 r = client.enqueue(tenant='tenant-one',
2965 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002966 project='project-does-not-exist',
2967 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002968 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002969 self.assertEqual(r, False)
2970
2971 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2972 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002973 r = client.enqueue(tenant='tenant-one',
2974 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08002975 project='org/project',
2976 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002977 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002978 self.assertEqual(r, False)
2979
2980 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2981 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002982 r = client.enqueue(tenant='tenant-one',
2983 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002984 project='org/project',
2985 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08002986 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002987 self.assertEqual(r, False)
2988
2989 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2990 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002991 r = client.enqueue(tenant='tenant-one',
2992 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002993 project='org/project',
2994 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002995 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002996 self.assertEqual(r, False)
2997
2998 self.waitUntilSettled()
2999 self.assertEqual(len(self.history), 0)
3000 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003001
3002 def test_client_promote(self):
3003 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003004 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003005 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3006 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3007 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003008 A.addApproval('code-review', 2)
3009 B.addApproval('code-review', 2)
3010 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003011
James E. Blair8b5408c2016-08-08 15:37:46 -07003012 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3013 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3014 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003015
3016 self.waitUntilSettled()
3017
Paul Belangerbaca3132016-11-04 12:49:54 -04003018 tenant = self.sched.abide.tenants.get('tenant-one')
3019 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003020 enqueue_times = {}
3021 for item in items:
3022 enqueue_times[str(item.change)] = item.enqueue_time
3023
James E. Blair36658cf2013-12-06 17:53:48 -08003024 client = zuul.rpcclient.RPCClient('127.0.0.1',
3025 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003026 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003027 r = client.promote(tenant='tenant-one',
3028 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003029 change_ids=['2,1', '3,1'])
3030
Sean Daguef39b9ca2014-01-10 21:34:35 -05003031 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003032 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003033 for item in items:
3034 self.assertEqual(
3035 enqueue_times[str(item.change)], item.enqueue_time)
3036
James E. Blair78acec92014-02-06 07:11:32 -08003037 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003038 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003039 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003040 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003041 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003042 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003043 self.waitUntilSettled()
3044
3045 self.assertEqual(len(self.builds), 6)
3046 self.assertEqual(self.builds[0].name, 'project-test1')
3047 self.assertEqual(self.builds[1].name, 'project-test2')
3048 self.assertEqual(self.builds[2].name, 'project-test1')
3049 self.assertEqual(self.builds[3].name, 'project-test2')
3050 self.assertEqual(self.builds[4].name, 'project-test1')
3051 self.assertEqual(self.builds[5].name, 'project-test2')
3052
Paul Belangerbaca3132016-11-04 12:49:54 -04003053 self.assertTrue(self.builds[0].hasChanges(B))
3054 self.assertFalse(self.builds[0].hasChanges(A))
3055 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003056
Paul Belangerbaca3132016-11-04 12:49:54 -04003057 self.assertTrue(self.builds[2].hasChanges(B))
3058 self.assertTrue(self.builds[2].hasChanges(C))
3059 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003060
Paul Belangerbaca3132016-11-04 12:49:54 -04003061 self.assertTrue(self.builds[4].hasChanges(B))
3062 self.assertTrue(self.builds[4].hasChanges(C))
3063 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003064
Paul Belanger174a8272017-03-14 13:20:10 -04003065 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003066 self.waitUntilSettled()
3067
3068 self.assertEqual(A.data['status'], 'MERGED')
3069 self.assertEqual(A.reported, 2)
3070 self.assertEqual(B.data['status'], 'MERGED')
3071 self.assertEqual(B.reported, 2)
3072 self.assertEqual(C.data['status'], 'MERGED')
3073 self.assertEqual(C.reported, 2)
3074
James E. Blair36658cf2013-12-06 17:53:48 -08003075 self.assertEqual(r, True)
3076
3077 def test_client_promote_dependent(self):
3078 "Test that the RPC client can promote a dependent change"
3079 # C (depends on B) -> B -> A ; then promote C to get:
3080 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003081 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003082 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3083 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3084 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3085
3086 C.setDependsOn(B, 1)
3087
James E. Blair8b5408c2016-08-08 15:37:46 -07003088 A.addApproval('code-review', 2)
3089 B.addApproval('code-review', 2)
3090 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003091
James E. Blair8b5408c2016-08-08 15:37:46 -07003092 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3093 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3094 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003095
3096 self.waitUntilSettled()
3097
3098 client = zuul.rpcclient.RPCClient('127.0.0.1',
3099 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003100 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003101 r = client.promote(tenant='tenant-one',
3102 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003103 change_ids=['3,1'])
3104
James E. Blair78acec92014-02-06 07:11:32 -08003105 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003106 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003107 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003108 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003109 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003110 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003111 self.waitUntilSettled()
3112
3113 self.assertEqual(len(self.builds), 6)
3114 self.assertEqual(self.builds[0].name, 'project-test1')
3115 self.assertEqual(self.builds[1].name, 'project-test2')
3116 self.assertEqual(self.builds[2].name, 'project-test1')
3117 self.assertEqual(self.builds[3].name, 'project-test2')
3118 self.assertEqual(self.builds[4].name, 'project-test1')
3119 self.assertEqual(self.builds[5].name, 'project-test2')
3120
Paul Belangerbaca3132016-11-04 12:49:54 -04003121 self.assertTrue(self.builds[0].hasChanges(B))
3122 self.assertFalse(self.builds[0].hasChanges(A))
3123 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003124
Paul Belangerbaca3132016-11-04 12:49:54 -04003125 self.assertTrue(self.builds[2].hasChanges(B))
3126 self.assertTrue(self.builds[2].hasChanges(C))
3127 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003128
Paul Belangerbaca3132016-11-04 12:49:54 -04003129 self.assertTrue(self.builds[4].hasChanges(B))
3130 self.assertTrue(self.builds[4].hasChanges(C))
3131 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003132
Paul Belanger174a8272017-03-14 13:20:10 -04003133 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003134 self.waitUntilSettled()
3135
3136 self.assertEqual(A.data['status'], 'MERGED')
3137 self.assertEqual(A.reported, 2)
3138 self.assertEqual(B.data['status'], 'MERGED')
3139 self.assertEqual(B.reported, 2)
3140 self.assertEqual(C.data['status'], 'MERGED')
3141 self.assertEqual(C.reported, 2)
3142
James E. Blair36658cf2013-12-06 17:53:48 -08003143 self.assertEqual(r, True)
3144
3145 def test_client_promote_negative(self):
3146 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003147 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003148 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003149 A.addApproval('code-review', 2)
3150 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003151 self.waitUntilSettled()
3152
3153 client = zuul.rpcclient.RPCClient('127.0.0.1',
3154 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003155 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003156
3157 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003158 r = client.promote(tenant='tenant-one',
3159 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003160 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003161 self.assertEqual(r, False)
3162
3163 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003164 r = client.promote(tenant='tenant-one',
3165 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003166 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003167 self.assertEqual(r, False)
3168
Paul Belanger174a8272017-03-14 13:20:10 -04003169 self.executor_server.hold_jobs_in_build = False
3170 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003171 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003172
James E. Blaircc324192017-04-19 14:36:08 -07003173 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003174 def test_queue_rate_limiting(self):
3175 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003176 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003177 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3178 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3179 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3180
3181 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003182 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003183
James E. Blair8b5408c2016-08-08 15:37:46 -07003184 A.addApproval('code-review', 2)
3185 B.addApproval('code-review', 2)
3186 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003187
James E. Blair8b5408c2016-08-08 15:37:46 -07003188 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3189 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3190 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003191 self.waitUntilSettled()
3192
3193 # Only A and B will have their merge jobs queued because
3194 # window is 2.
3195 self.assertEqual(len(self.builds), 2)
3196 self.assertEqual(self.builds[0].name, 'project-merge')
3197 self.assertEqual(self.builds[1].name, 'project-merge')
3198
James E. Blair732ee862017-04-21 07:49:26 -07003199 # Release the merge jobs one at a time.
3200 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003201 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003202 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003203 self.waitUntilSettled()
3204
3205 # Only A and B will have their test jobs queued because
3206 # window is 2.
3207 self.assertEqual(len(self.builds), 4)
3208 self.assertEqual(self.builds[0].name, 'project-test1')
3209 self.assertEqual(self.builds[1].name, 'project-test2')
3210 self.assertEqual(self.builds[2].name, 'project-test1')
3211 self.assertEqual(self.builds[3].name, 'project-test2')
3212
Paul Belanger174a8272017-03-14 13:20:10 -04003213 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003214 self.waitUntilSettled()
3215
James E. Blaircc324192017-04-19 14:36:08 -07003216 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003217 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003218 # A failed so window is reduced by 1 to 1.
3219 self.assertEqual(queue.window, 1)
3220 self.assertEqual(queue.window_floor, 1)
3221 self.assertEqual(A.data['status'], 'NEW')
3222
3223 # Gate is reset and only B's merge job is queued because
3224 # window shrunk to 1.
3225 self.assertEqual(len(self.builds), 1)
3226 self.assertEqual(self.builds[0].name, 'project-merge')
3227
Paul Belanger174a8272017-03-14 13:20:10 -04003228 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003229 self.waitUntilSettled()
3230
3231 # Only B's test jobs are queued because window is still 1.
3232 self.assertEqual(len(self.builds), 2)
3233 self.assertEqual(self.builds[0].name, 'project-test1')
3234 self.assertEqual(self.builds[1].name, 'project-test2')
3235
Paul Belanger174a8272017-03-14 13:20:10 -04003236 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003237 self.waitUntilSettled()
3238
3239 # B was successfully merged so window is increased to 2.
3240 self.assertEqual(queue.window, 2)
3241 self.assertEqual(queue.window_floor, 1)
3242 self.assertEqual(B.data['status'], 'MERGED')
3243
3244 # Only C is left and its merge job is queued.
3245 self.assertEqual(len(self.builds), 1)
3246 self.assertEqual(self.builds[0].name, 'project-merge')
3247
Paul Belanger174a8272017-03-14 13:20:10 -04003248 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003249 self.waitUntilSettled()
3250
3251 # After successful merge job the test jobs for C are queued.
3252 self.assertEqual(len(self.builds), 2)
3253 self.assertEqual(self.builds[0].name, 'project-test1')
3254 self.assertEqual(self.builds[1].name, 'project-test2')
3255
Paul Belanger174a8272017-03-14 13:20:10 -04003256 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003257 self.waitUntilSettled()
3258
3259 # C successfully merged so window is bumped to 3.
3260 self.assertEqual(queue.window, 3)
3261 self.assertEqual(queue.window_floor, 1)
3262 self.assertEqual(C.data['status'], 'MERGED')
3263
James E. Blaircc324192017-04-19 14:36:08 -07003264 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003265 def test_queue_rate_limiting_dependent(self):
3266 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003267 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003268 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3269 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3270 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3271
3272 B.setDependsOn(A, 1)
3273
Paul Belanger174a8272017-03-14 13:20:10 -04003274 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003275
James E. Blair8b5408c2016-08-08 15:37:46 -07003276 A.addApproval('code-review', 2)
3277 B.addApproval('code-review', 2)
3278 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003279
James E. Blair8b5408c2016-08-08 15:37:46 -07003280 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3281 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3282 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003283 self.waitUntilSettled()
3284
3285 # Only A and B will have their merge jobs queued because
3286 # window is 2.
3287 self.assertEqual(len(self.builds), 2)
3288 self.assertEqual(self.builds[0].name, 'project-merge')
3289 self.assertEqual(self.builds[1].name, 'project-merge')
3290
James E. Blairf4a5f022017-04-18 14:01:10 -07003291 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003292
3293 # Only A and B will have their test jobs queued because
3294 # window is 2.
3295 self.assertEqual(len(self.builds), 4)
3296 self.assertEqual(self.builds[0].name, 'project-test1')
3297 self.assertEqual(self.builds[1].name, 'project-test2')
3298 self.assertEqual(self.builds[2].name, 'project-test1')
3299 self.assertEqual(self.builds[3].name, 'project-test2')
3300
Paul Belanger174a8272017-03-14 13:20:10 -04003301 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003302 self.waitUntilSettled()
3303
James E. Blaircc324192017-04-19 14:36:08 -07003304 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003305 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003306 # A failed so window is reduced by 1 to 1.
3307 self.assertEqual(queue.window, 1)
3308 self.assertEqual(queue.window_floor, 1)
3309 self.assertEqual(A.data['status'], 'NEW')
3310 self.assertEqual(B.data['status'], 'NEW')
3311
3312 # Gate is reset and only C's merge job is queued because
3313 # window shrunk to 1 and A and B were dequeued.
3314 self.assertEqual(len(self.builds), 1)
3315 self.assertEqual(self.builds[0].name, 'project-merge')
3316
James E. Blairf4a5f022017-04-18 14:01:10 -07003317 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003318
3319 # Only C's test jobs are queued because window is still 1.
3320 self.assertEqual(len(self.builds), 2)
3321 self.assertEqual(self.builds[0].name, 'project-test1')
3322 self.assertEqual(self.builds[1].name, 'project-test2')
3323
Paul Belanger174a8272017-03-14 13:20:10 -04003324 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003325 self.waitUntilSettled()
3326
3327 # C was successfully merged so window is increased to 2.
3328 self.assertEqual(queue.window, 2)
3329 self.assertEqual(queue.window_floor, 1)
3330 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003331
James E. Blairec056492016-07-22 09:45:56 -07003332 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003333 def test_worker_update_metadata(self):
3334 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003335 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003336
3337 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003338 A.addApproval('code-review', 2)
3339 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003340 self.waitUntilSettled()
3341
Paul Belanger174a8272017-03-14 13:20:10 -04003342 self.assertEqual(len(self.executor.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003343
3344 self.log.debug('Current builds:')
Paul Belanger174a8272017-03-14 13:20:10 -04003345 self.log.debug(self.executor.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003346
3347 start = time.time()
3348 while True:
3349 if time.time() - start > 10:
3350 raise Exception("Timeout waiting for gearman server to report "
3351 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003352 build = list(self.executor.builds.values())[0]
Joshua Heskethba8776a2014-01-12 14:35:40 +08003353 if build.worker.name == "My Worker":
3354 break
3355 else:
3356 time.sleep(0)
3357
3358 self.log.debug(build)
3359 self.assertEqual("My Worker", build.worker.name)
3360 self.assertEqual("localhost", build.worker.hostname)
3361 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3362 self.assertEqual("zuul.example.org", build.worker.fqdn)
3363 self.assertEqual("FakeBuilder", build.worker.program)
3364 self.assertEqual("v1.1", build.worker.version)
3365 self.assertEqual({'something': 'else'}, build.worker.extra)
3366
Paul Belanger174a8272017-03-14 13:20:10 -04003367 self.executor_server.hold_jobs_in_build = False
3368 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003369 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003370
James E. Blaircc324192017-04-19 14:36:08 -07003371 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003372 def test_footer_message(self):
3373 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003374 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003375 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003376 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003377 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003378 self.waitUntilSettled()
3379
3380 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003381 B.addApproval('code-review', 2)
3382 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003383 self.waitUntilSettled()
3384
3385 self.assertEqual(2, len(self.smtp_messages))
3386
Adam Gandelman82258fd2017-02-07 16:51:58 -08003387 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003388Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003389http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003390
Adam Gandelman82258fd2017-02-07 16:51:58 -08003391 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003392For CI problems and help debugging, contact ci@example.org"""
3393
Adam Gandelman82258fd2017-02-07 16:51:58 -08003394 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3395 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3396 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3397 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003398
Tobias Henkel9842bd72017-05-16 13:40:03 +02003399 @simple_layout('layouts/unmanaged-project.yaml')
3400 def test_unmanaged_project_start_message(self):
3401 "Test start reporting is not done for unmanaged projects."
3402 self.init_repo("org/project", tag='init')
3403 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3404 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3405 self.waitUntilSettled()
3406
3407 self.assertEqual(0, len(A.messages))
3408
James E. Blairec056492016-07-22 09:45:56 -07003409 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003410 def test_merge_failure_reporters(self):
3411 """Check that the config is set up correctly"""
3412
James E. Blairf84026c2015-12-08 16:11:46 -08003413 self.updateConfigLayout(
3414 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003415 self.sched.reconfigure(self.config)
3416 self.registerJobs()
3417
3418 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003419 "Merge Failed.\n\nThis change or one of its cross-repo "
3420 "dependencies was unable to be automatically merged with the "
3421 "current state of its repository. Please rebase the change and "
3422 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003423 self.sched.layout.pipelines['check'].merge_failure_message)
3424 self.assertEqual(
3425 "The merge failed! For more information...",
3426 self.sched.layout.pipelines['gate'].merge_failure_message)
3427
3428 self.assertEqual(
3429 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3430 self.assertEqual(
3431 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3432
3433 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003434 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3435 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003436
3437 self.assertTrue(
3438 (
3439 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003440 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003441 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003442 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003443 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003444 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003445 ) or (
3446 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003447 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003448 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003449 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003450 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003451 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003452 )
3453 )
3454
James E. Blairec056492016-07-22 09:45:56 -07003455 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003456 def test_merge_failure_reports(self):
3457 """Check that when a change fails to merge the correct message is sent
3458 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003459 self.updateConfigLayout(
3460 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003461 self.sched.reconfigure(self.config)
3462 self.registerJobs()
3463
3464 # Check a test failure isn't reported to SMTP
3465 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003466 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003467 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003468 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003469 self.waitUntilSettled()
3470
3471 self.assertEqual(3, len(self.history)) # 3 jobs
3472 self.assertEqual(0, len(self.smtp_messages))
3473
3474 # Check a merge failure is reported to SMTP
3475 # B should be merged, but C will conflict with B
3476 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3477 B.addPatchset(['conflict'])
3478 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3479 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003480 B.addApproval('code-review', 2)
3481 C.addApproval('code-review', 2)
3482 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3483 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003484 self.waitUntilSettled()
3485
3486 self.assertEqual(6, len(self.history)) # A and B jobs
3487 self.assertEqual(1, len(self.smtp_messages))
3488 self.assertEqual('The merge failed! For more information...',
3489 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003490
James E. Blairec056492016-07-22 09:45:56 -07003491 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003492 def test_default_merge_failure_reports(self):
3493 """Check that the default merge failure reports are correct."""
3494
3495 # A should report success, B should report merge failure.
3496 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3497 A.addPatchset(['conflict'])
3498 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3499 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003500 A.addApproval('code-review', 2)
3501 B.addApproval('code-review', 2)
3502 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3503 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003504 self.waitUntilSettled()
3505
3506 self.assertEqual(3, len(self.history)) # A jobs
3507 self.assertEqual(A.reported, 2)
3508 self.assertEqual(B.reported, 2)
3509 self.assertEqual(A.data['status'], 'MERGED')
3510 self.assertEqual(B.data['status'], 'NEW')
3511 self.assertIn('Build succeeded', A.messages[1])
3512 self.assertIn('Merge Failed', B.messages[1])
3513 self.assertIn('automatically merged', B.messages[1])
3514 self.assertNotIn('logs.example.com', B.messages[1])
3515 self.assertNotIn('SKIPPED', B.messages[1])
3516
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003517 def test_client_get_running_jobs(self):
3518 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003519 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003520 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003521 A.addApproval('code-review', 2)
3522 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003523 self.waitUntilSettled()
3524
3525 client = zuul.rpcclient.RPCClient('127.0.0.1',
3526 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003527 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003528
3529 # Wait for gearman server to send the initial workData back to zuul
3530 start = time.time()
3531 while True:
3532 if time.time() - start > 10:
3533 raise Exception("Timeout waiting for gearman server to report "
3534 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003535 build = list(self.executor_client.builds.values())[0]
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003536 if build.worker.name == "My Worker":
3537 break
3538 else:
3539 time.sleep(0)
3540
3541 running_items = client.get_running_jobs()
3542
3543 self.assertEqual(1, len(running_items))
3544 running_item = running_items[0]
3545 self.assertEqual([], running_item['failing_reasons'])
3546 self.assertEqual([], running_item['items_behind'])
3547 self.assertEqual('https://hostname/1', running_item['url'])
3548 self.assertEqual(None, running_item['item_ahead'])
3549 self.assertEqual('org/project', running_item['project'])
3550 self.assertEqual(None, running_item['remaining_time'])
3551 self.assertEqual(True, running_item['active'])
3552 self.assertEqual('1,1', running_item['id'])
3553
3554 self.assertEqual(3, len(running_item['jobs']))
3555 for job in running_item['jobs']:
3556 if job['name'] == 'project-merge':
3557 self.assertEqual('project-merge', job['name'])
3558 self.assertEqual('gate', job['pipeline'])
3559 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003560 self.assertEqual('https://server/job/project-merge/0/',
3561 job['url'])
3562 self.assertEqual(7, len(job['worker']))
3563 self.assertEqual(False, job['canceled'])
3564 self.assertEqual(True, job['voting'])
3565 self.assertEqual(None, job['result'])
3566 self.assertEqual('gate', job['pipeline'])
3567 break
3568
Paul Belanger174a8272017-03-14 13:20:10 -04003569 self.executor_server.hold_jobs_in_build = False
3570 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003571 self.waitUntilSettled()
3572
3573 running_items = client.get_running_jobs()
3574 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003575
James E. Blairb70e55a2017-04-19 12:57:02 -07003576 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003577 def test_nonvoting_pipeline(self):
3578 "Test that a nonvoting pipeline (experimental) can still report"
3579
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003580 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3581 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003582 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3583 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003584
3585 self.assertEqual(self.getJobFromHistory('project-merge').result,
3586 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003587 self.assertEqual(
3588 self.getJobFromHistory('experimental-project-test').result,
3589 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003590 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003591
3592 def test_crd_gate(self):
3593 "Test cross-repo dependencies"
3594 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3595 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003596 A.addApproval('code-review', 2)
3597 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003598
3599 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3600 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3601 AM2.setMerged()
3602 AM1.setMerged()
3603
3604 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3605 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3606 BM2.setMerged()
3607 BM1.setMerged()
3608
3609 # A -> AM1 -> AM2
3610 # B -> BM1 -> BM2
3611 # A Depends-On: B
3612 # M2 is here to make sure it is never queried. If it is, it
3613 # means zuul is walking down the entire history of merged
3614 # changes.
3615
3616 B.setDependsOn(BM1, 1)
3617 BM1.setDependsOn(BM2, 1)
3618
3619 A.setDependsOn(AM1, 1)
3620 AM1.setDependsOn(AM2, 1)
3621
3622 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3623 A.subject, B.data['id'])
3624
James E. Blair8b5408c2016-08-08 15:37:46 -07003625 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003626 self.waitUntilSettled()
3627
3628 self.assertEqual(A.data['status'], 'NEW')
3629 self.assertEqual(B.data['status'], 'NEW')
3630
Paul Belanger5dccbe72016-11-14 11:17:37 -05003631 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003632 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003633
Paul Belanger174a8272017-03-14 13:20:10 -04003634 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003635 B.addApproval('approved', 1)
3636 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003637 self.waitUntilSettled()
3638
Paul Belanger174a8272017-03-14 13:20:10 -04003639 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003640 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003641 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003642 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003643 self.executor_server.hold_jobs_in_build = False
3644 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003645 self.waitUntilSettled()
3646
3647 self.assertEqual(AM2.queried, 0)
3648 self.assertEqual(BM2.queried, 0)
3649 self.assertEqual(A.data['status'], 'MERGED')
3650 self.assertEqual(B.data['status'], 'MERGED')
3651 self.assertEqual(A.reported, 2)
3652 self.assertEqual(B.reported, 2)
3653
Paul Belanger5dccbe72016-11-14 11:17:37 -05003654 changes = self.getJobFromHistory(
3655 'project-merge', 'org/project1').changes
3656 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003657
3658 def test_crd_branch(self):
3659 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003660
3661 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003662 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3663 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3664 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3665 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003666 A.addApproval('code-review', 2)
3667 B.addApproval('code-review', 2)
3668 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003669
3670 # A Depends-On: B+C
3671 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3672 A.subject, B.data['id'])
3673
Paul Belanger174a8272017-03-14 13:20:10 -04003674 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003675 B.addApproval('approved', 1)
3676 C.addApproval('approved', 1)
3677 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003678 self.waitUntilSettled()
3679
Paul Belanger174a8272017-03-14 13:20:10 -04003680 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003681 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003682 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003683 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003684 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003685 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003686 self.executor_server.hold_jobs_in_build = False
3687 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003688 self.waitUntilSettled()
3689
3690 self.assertEqual(A.data['status'], 'MERGED')
3691 self.assertEqual(B.data['status'], 'MERGED')
3692 self.assertEqual(C.data['status'], 'MERGED')
3693 self.assertEqual(A.reported, 2)
3694 self.assertEqual(B.reported, 2)
3695 self.assertEqual(C.reported, 2)
3696
Paul Belanger6379db12016-11-14 13:57:54 -05003697 changes = self.getJobFromHistory(
3698 'project-merge', 'org/project1').changes
3699 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003700
3701 def test_crd_multiline(self):
3702 "Test multiple depends-on lines in commit"
3703 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3704 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3705 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003706 A.addApproval('code-review', 2)
3707 B.addApproval('code-review', 2)
3708 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003709
3710 # A Depends-On: B+C
3711 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3712 A.subject, B.data['id'], C.data['id'])
3713
Paul Belanger174a8272017-03-14 13:20:10 -04003714 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003715 B.addApproval('approved', 1)
3716 C.addApproval('approved', 1)
3717 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003718 self.waitUntilSettled()
3719
Paul Belanger174a8272017-03-14 13:20:10 -04003720 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003721 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003722 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003723 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003724 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003725 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003726 self.executor_server.hold_jobs_in_build = False
3727 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003728 self.waitUntilSettled()
3729
3730 self.assertEqual(A.data['status'], 'MERGED')
3731 self.assertEqual(B.data['status'], 'MERGED')
3732 self.assertEqual(C.data['status'], 'MERGED')
3733 self.assertEqual(A.reported, 2)
3734 self.assertEqual(B.reported, 2)
3735 self.assertEqual(C.reported, 2)
3736
Paul Belanger5dccbe72016-11-14 11:17:37 -05003737 changes = self.getJobFromHistory(
3738 'project-merge', 'org/project1').changes
3739 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003740
3741 def test_crd_unshared_gate(self):
3742 "Test cross-repo dependencies in unshared gate queues"
3743 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3744 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003745 A.addApproval('code-review', 2)
3746 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003747
3748 # A Depends-On: B
3749 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3750 A.subject, B.data['id'])
3751
3752 # A and B do not share a queue, make sure that A is unable to
3753 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003754 B.addApproval('approved', 1)
3755 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003756 self.waitUntilSettled()
3757
3758 self.assertEqual(A.data['status'], 'NEW')
3759 self.assertEqual(B.data['status'], 'NEW')
3760 self.assertEqual(A.reported, 0)
3761 self.assertEqual(B.reported, 0)
3762 self.assertEqual(len(self.history), 0)
3763
3764 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003765 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003766 self.waitUntilSettled()
3767
3768 self.assertEqual(B.data['status'], 'MERGED')
3769 self.assertEqual(B.reported, 2)
3770
3771 # Now that B is merged, A should be able to be enqueued and
3772 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003773 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003774 self.waitUntilSettled()
3775
3776 self.assertEqual(A.data['status'], 'MERGED')
3777 self.assertEqual(A.reported, 2)
3778
James E. Blair96698e22015-04-02 07:48:21 -07003779 def test_crd_gate_reverse(self):
3780 "Test reverse cross-repo dependencies"
3781 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3782 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003783 A.addApproval('code-review', 2)
3784 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003785
3786 # A Depends-On: B
3787
3788 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3789 A.subject, B.data['id'])
3790
James E. Blair8b5408c2016-08-08 15:37:46 -07003791 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003792 self.waitUntilSettled()
3793
3794 self.assertEqual(A.data['status'], 'NEW')
3795 self.assertEqual(B.data['status'], 'NEW')
3796
Paul Belanger174a8272017-03-14 13:20:10 -04003797 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003798 A.addApproval('approved', 1)
3799 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003800 self.waitUntilSettled()
3801
Paul Belanger174a8272017-03-14 13:20:10 -04003802 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003803 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003804 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003805 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003806 self.executor_server.hold_jobs_in_build = False
3807 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003808 self.waitUntilSettled()
3809
3810 self.assertEqual(A.data['status'], 'MERGED')
3811 self.assertEqual(B.data['status'], 'MERGED')
3812 self.assertEqual(A.reported, 2)
3813 self.assertEqual(B.reported, 2)
3814
Paul Belanger5dccbe72016-11-14 11:17:37 -05003815 changes = self.getJobFromHistory(
3816 'project-merge', 'org/project1').changes
3817 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003818
James E. Blair5ee24252014-12-30 10:12:29 -08003819 def test_crd_cycle(self):
3820 "Test cross-repo dependency cycles"
3821 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3822 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003823 A.addApproval('code-review', 2)
3824 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003825
3826 # A -> B -> A (via commit-depends)
3827
3828 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3829 A.subject, B.data['id'])
3830 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3831 B.subject, A.data['id'])
3832
James E. Blair8b5408c2016-08-08 15:37:46 -07003833 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003834 self.waitUntilSettled()
3835
3836 self.assertEqual(A.reported, 0)
3837 self.assertEqual(B.reported, 0)
3838 self.assertEqual(A.data['status'], 'NEW')
3839 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003840
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003841 def test_crd_gate_unknown(self):
3842 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07003843 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003844 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3845 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003846 A.addApproval('code-review', 2)
3847 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003848
3849 # A Depends-On: B
3850 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3851 A.subject, B.data['id'])
3852
James E. Blair8b5408c2016-08-08 15:37:46 -07003853 B.addApproval('approved', 1)
3854 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003855 self.waitUntilSettled()
3856
3857 # Unknown projects cannot share a queue with any other
3858 # since they don't have common jobs with any other (they have no jobs).
3859 # Changes which depend on unknown project changes
3860 # should not be processed in dependent pipeline
3861 self.assertEqual(A.data['status'], 'NEW')
3862 self.assertEqual(B.data['status'], 'NEW')
3863 self.assertEqual(A.reported, 0)
3864 self.assertEqual(B.reported, 0)
3865 self.assertEqual(len(self.history), 0)
3866
3867 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07003868 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003869 B.setMerged()
3870 self.waitUntilSettled()
3871 self.assertEqual(len(self.history), 0)
3872
3873 # Now that B is merged, A should be able to be enqueued and
3874 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003875 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003876 self.waitUntilSettled()
3877
3878 self.assertEqual(A.data['status'], 'MERGED')
3879 self.assertEqual(A.reported, 2)
3880 self.assertEqual(B.data['status'], 'MERGED')
3881 self.assertEqual(B.reported, 0)
3882
James E. Blairbfb8e042014-12-30 17:01:44 -08003883 def test_crd_check(self):
3884 "Test cross-repo dependencies in independent pipelines"
3885
Paul Belanger174a8272017-03-14 13:20:10 -04003886 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08003887 self.gearman_server.hold_jobs_in_queue = True
3888 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3889 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3890
3891 # A Depends-On: B
3892 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3893 A.subject, B.data['id'])
3894
3895 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3896 self.waitUntilSettled()
3897
James E. Blairbfb8e042014-12-30 17:01:44 -08003898 self.gearman_server.hold_jobs_in_queue = False
3899 self.gearman_server.release()
3900 self.waitUntilSettled()
3901
Paul Belanger174a8272017-03-14 13:20:10 -04003902 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05003903 self.waitUntilSettled()
3904
James E. Blair2a535672017-04-27 12:03:15 -07003905 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08003906
Paul Belanger174a8272017-03-14 13:20:10 -04003907 self.executor_server.hold_jobs_in_build = False
3908 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05003909 self.waitUntilSettled()
3910
James E. Blairbfb8e042014-12-30 17:01:44 -08003911 self.assertEqual(A.data['status'], 'NEW')
3912 self.assertEqual(B.data['status'], 'NEW')
3913 self.assertEqual(A.reported, 1)
3914 self.assertEqual(B.reported, 0)
3915
3916 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05003917 tenant = self.sched.abide.tenants.get('tenant-one')
3918 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08003919
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003920 def test_crd_check_git_depends(self):
3921 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07003922 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003923 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3924 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3925
3926 # Add two git-dependent changes and make sure they both report
3927 # success.
3928 B.setDependsOn(A, 1)
3929 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3930 self.waitUntilSettled()
3931 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3932 self.waitUntilSettled()
3933
James E. Blairb8c16472015-05-05 14:55:26 -07003934 self.orderedRelease()
3935 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003936 self.waitUntilSettled()
3937
3938 self.assertEqual(A.data['status'], 'NEW')
3939 self.assertEqual(B.data['status'], 'NEW')
3940 self.assertEqual(A.reported, 1)
3941 self.assertEqual(B.reported, 1)
3942
3943 self.assertEqual(self.history[0].changes, '1,1')
3944 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04003945 tenant = self.sched.abide.tenants.get('tenant-one')
3946 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003947
3948 self.assertIn('Build succeeded', A.messages[0])
3949 self.assertIn('Build succeeded', B.messages[0])
3950
3951 def test_crd_check_duplicate(self):
3952 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04003953 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003954 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3955 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04003956 tenant = self.sched.abide.tenants.get('tenant-one')
3957 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003958
3959 # Add two git-dependent changes...
3960 B.setDependsOn(A, 1)
3961 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3962 self.waitUntilSettled()
3963 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3964
3965 # ...make sure the live one is not duplicated...
3966 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3967 self.waitUntilSettled()
3968 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3969
3970 # ...but the non-live one is able to be.
3971 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3972 self.waitUntilSettled()
3973 self.assertEqual(len(check_pipeline.getAllItems()), 3)
3974
Clark Boylandd849822015-03-02 12:38:14 -08003975 # Release jobs in order to avoid races with change A jobs
3976 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07003977 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04003978 self.executor_server.hold_jobs_in_build = False
3979 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003980 self.waitUntilSettled()
3981
3982 self.assertEqual(A.data['status'], 'NEW')
3983 self.assertEqual(B.data['status'], 'NEW')
3984 self.assertEqual(A.reported, 1)
3985 self.assertEqual(B.reported, 1)
3986
3987 self.assertEqual(self.history[0].changes, '1,1 2,1')
3988 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04003989 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003990
3991 self.assertIn('Build succeeded', A.messages[0])
3992 self.assertIn('Build succeeded', B.messages[0])
3993
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003994 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08003995 "Test cross-repo dependencies re-enqueued in independent pipelines"
3996
3997 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003998 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
3999 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004000
4001 # A Depends-On: B
4002 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4003 A.subject, B.data['id'])
4004
4005 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4006 self.waitUntilSettled()
4007
4008 self.sched.reconfigure(self.config)
4009
4010 # Make sure the items still share a change queue, and the
4011 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004012 tenant = self.sched.abide.tenants.get('tenant-one')
4013 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4014 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004015 first_item = queue.queue[0]
4016 for item in queue.queue:
4017 self.assertEqual(item.queue, first_item.queue)
4018 self.assertFalse(first_item.live)
4019 self.assertTrue(queue.queue[1].live)
4020
4021 self.gearman_server.hold_jobs_in_queue = False
4022 self.gearman_server.release()
4023 self.waitUntilSettled()
4024
4025 self.assertEqual(A.data['status'], 'NEW')
4026 self.assertEqual(B.data['status'], 'NEW')
4027 self.assertEqual(A.reported, 1)
4028 self.assertEqual(B.reported, 0)
4029
4030 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004031 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004032
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004033 def test_crd_check_reconfiguration(self):
4034 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4035
4036 def test_crd_undefined_project(self):
4037 """Test that undefined projects in dependencies are handled for
4038 independent pipelines"""
4039 # It's a hack for fake gerrit,
4040 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004041 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004042 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4043
Jesse Keatingea68ab42017-04-19 15:03:30 -07004044 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004045 def test_crd_check_ignore_dependencies(self):
4046 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004047
4048 self.gearman_server.hold_jobs_in_queue = True
4049 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4050 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4051 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4052
4053 # A Depends-On: B
4054 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4055 A.subject, B.data['id'])
4056 # C git-depends on B
4057 C.setDependsOn(B, 1)
4058 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4059 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4060 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4061 self.waitUntilSettled()
4062
4063 # Make sure none of the items share a change queue, and all
4064 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004065 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004066 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004067 self.assertEqual(len(check_pipeline.queues), 3)
4068 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4069 for item in check_pipeline.getAllItems():
4070 self.assertTrue(item.live)
4071
4072 self.gearman_server.hold_jobs_in_queue = False
4073 self.gearman_server.release()
4074 self.waitUntilSettled()
4075
4076 self.assertEqual(A.data['status'], 'NEW')
4077 self.assertEqual(B.data['status'], 'NEW')
4078 self.assertEqual(C.data['status'], 'NEW')
4079 self.assertEqual(A.reported, 1)
4080 self.assertEqual(B.reported, 1)
4081 self.assertEqual(C.reported, 1)
4082
4083 # Each job should have tested exactly one change
4084 for job in self.history:
4085 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004086
James E. Blair7cb84542017-04-19 13:35:05 -07004087 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004088 def test_crd_check_transitive(self):
4089 "Test transitive cross-repo dependencies"
4090 # Specifically, if A -> B -> C, and C gets a new patchset and
4091 # A gets a new patchset, ensure the test of A,2 includes B,1
4092 # and C,2 (not C,1 which would indicate stale data in the
4093 # cache for B).
4094 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4095 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4096 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4097
4098 # A Depends-On: B
4099 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4100 A.subject, B.data['id'])
4101
4102 # B Depends-On: C
4103 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4104 B.subject, C.data['id'])
4105
4106 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4107 self.waitUntilSettled()
4108 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4109
4110 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4111 self.waitUntilSettled()
4112 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4113
4114 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4115 self.waitUntilSettled()
4116 self.assertEqual(self.history[-1].changes, '3,1')
4117
4118 C.addPatchset()
4119 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4120 self.waitUntilSettled()
4121 self.assertEqual(self.history[-1].changes, '3,2')
4122
4123 A.addPatchset()
4124 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4125 self.waitUntilSettled()
4126 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004127
James E. Blaira8b90b32016-08-24 15:18:50 -07004128 def test_crd_check_unknown(self):
4129 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004130 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004131 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4132 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4133 # A Depends-On: B
4134 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4135 A.subject, B.data['id'])
4136
4137 # Make sure zuul has seen an event on B.
4138 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4139 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4140 self.waitUntilSettled()
4141
4142 self.assertEqual(A.data['status'], 'NEW')
4143 self.assertEqual(A.reported, 1)
4144 self.assertEqual(B.data['status'], 'NEW')
4145 self.assertEqual(B.reported, 0)
4146
James E. Blair92464a22016-04-05 10:21:26 -07004147 def test_crd_cycle_join(self):
4148 "Test an updated change creates a cycle"
4149 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4150
4151 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4152 self.waitUntilSettled()
4153
4154 # Create B->A
4155 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4156 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4157 B.subject, A.data['id'])
4158 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4159 self.waitUntilSettled()
4160
4161 # Update A to add A->B (a cycle).
4162 A.addPatchset()
4163 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4164 A.subject, B.data['id'])
4165 # Normally we would submit the patchset-created event for
4166 # processing here, however, we have no way of noting whether
4167 # the dependency cycle detection correctly raised an
4168 # exception, so instead, we reach into the source driver and
4169 # call the method that would ultimately be called by the event
4170 # processing.
4171
Paul Belanger0e155e22016-11-14 14:12:23 -05004172 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07004173 (trusted, project) = tenant.getProject('org/project')
4174 source = project.source
Paul Belanger0e155e22016-11-14 14:12:23 -05004175
4176 # TODO(pabelanger): As we add more source / trigger APIs we should make
4177 # it easier for users to create events for testing.
4178 event = zuul.model.TriggerEvent()
4179 event.trigger_name = 'gerrit'
4180 event.change_number = '1'
4181 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004182 with testtools.ExpectedException(
4183 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004184 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004185 self.log.debug("Got expected dependency cycle exception")
4186
4187 # Now if we update B to remove the depends-on, everything
4188 # should be okay. B; A->B
4189
4190 B.addPatchset()
4191 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004192
4193 source.getChange(event, True)
4194 event.change_number = '2'
4195 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004196
Jesse Keatingcc67b122017-04-19 14:54:57 -07004197 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004198 def test_disable_at(self):
4199 "Test a pipeline will only report to the disabled trigger when failing"
4200
Jesse Keatingcc67b122017-04-19 14:54:57 -07004201 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004202 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004203 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004204 0, tenant.layout.pipelines['check']._consecutive_failures)
4205 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004206
4207 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4208 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4209 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4210 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4211 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4212 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4213 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4214 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4215 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4216 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4217 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4218
Paul Belanger174a8272017-03-14 13:20:10 -04004219 self.executor_server.failJob('project-test1', A)
4220 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004221 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004222 self.executor_server.failJob('project-test1', D)
4223 self.executor_server.failJob('project-test1', E)
4224 self.executor_server.failJob('project-test1', F)
4225 self.executor_server.failJob('project-test1', G)
4226 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004227 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004228 self.executor_server.failJob('project-test1', J)
4229 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004230
4231 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4232 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4233 self.waitUntilSettled()
4234
4235 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004236 2, tenant.layout.pipelines['check']._consecutive_failures)
4237 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004238
4239 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4240 self.waitUntilSettled()
4241
4242 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004243 0, tenant.layout.pipelines['check']._consecutive_failures)
4244 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004245
4246 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4247 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4248 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4249 self.waitUntilSettled()
4250
4251 # We should be disabled now
4252 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004253 3, tenant.layout.pipelines['check']._consecutive_failures)
4254 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004255
4256 # We need to wait between each of these patches to make sure the
4257 # smtp messages come back in an expected order
4258 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4259 self.waitUntilSettled()
4260 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4261 self.waitUntilSettled()
4262 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4263 self.waitUntilSettled()
4264
4265 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4266 # leaving a message on each change
4267 self.assertEqual(1, len(A.messages))
4268 self.assertIn('Build failed.', A.messages[0])
4269 self.assertEqual(1, len(B.messages))
4270 self.assertIn('Build failed.', B.messages[0])
4271 self.assertEqual(1, len(C.messages))
4272 self.assertIn('Build succeeded.', C.messages[0])
4273 self.assertEqual(1, len(D.messages))
4274 self.assertIn('Build failed.', D.messages[0])
4275 self.assertEqual(1, len(E.messages))
4276 self.assertIn('Build failed.', E.messages[0])
4277 self.assertEqual(1, len(F.messages))
4278 self.assertIn('Build failed.', F.messages[0])
4279
4280 # The last 3 (GHI) would have only reported via smtp.
4281 self.assertEqual(3, len(self.smtp_messages))
4282 self.assertEqual(0, len(G.messages))
4283 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004284 self.assertIn(
4285 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004286 self.assertEqual(0, len(H.messages))
4287 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004288 self.assertIn(
4289 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004290 self.assertEqual(0, len(I.messages))
4291 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004292 self.assertIn(
4293 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004294
4295 # Now reload the configuration (simulate a HUP) to check the pipeline
4296 # comes out of disabled
4297 self.sched.reconfigure(self.config)
4298
Jesse Keatingcc67b122017-04-19 14:54:57 -07004299 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004300
4301 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004302 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004303 0, tenant.layout.pipelines['check']._consecutive_failures)
4304 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004305
4306 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4307 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4308 self.waitUntilSettled()
4309
4310 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004311 2, tenant.layout.pipelines['check']._consecutive_failures)
4312 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004313
4314 # J and K went back to gerrit
4315 self.assertEqual(1, len(J.messages))
4316 self.assertIn('Build failed.', J.messages[0])
4317 self.assertEqual(1, len(K.messages))
4318 self.assertIn('Build failed.', K.messages[0])
4319 # No more messages reported via smtp
4320 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004321
James E. Blaircc020532017-04-19 13:04:44 -07004322 @simple_layout('layouts/one-job-project.yaml')
4323 def test_one_job_project(self):
4324 "Test that queueing works with one job"
4325 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4326 'master', 'A')
4327 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4328 'master', 'B')
4329 A.addApproval('code-review', 2)
4330 B.addApproval('code-review', 2)
4331 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4332 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4333 self.waitUntilSettled()
4334
4335 self.assertEqual(A.data['status'], 'MERGED')
4336 self.assertEqual(A.reported, 2)
4337 self.assertEqual(B.data['status'], 'MERGED')
4338 self.assertEqual(B.reported, 2)
4339
Paul Belanger71d98172016-11-08 10:56:31 -05004340 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004341 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004342
Paul Belanger174a8272017-03-14 13:20:10 -04004343 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004344 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4345 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4346 self.waitUntilSettled()
4347
Paul Belanger174a8272017-03-14 13:20:10 -04004348 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004349 self.waitUntilSettled()
4350
4351 self.assertEqual(len(self.builds), 2)
4352 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004353 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004354 self.waitUntilSettled()
4355
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004356 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004357 self.assertEqual(len(self.builds), 1,
4358 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004359 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004360 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004361 self.waitUntilSettled()
4362
Paul Belanger174a8272017-03-14 13:20:10 -04004363 self.executor_server.hold_jobs_in_build = False
4364 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004365 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004366 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004367 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4368 self.assertEqual(A.reported, 1)
4369 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004370
James E. Blair15be0e12017-01-03 13:45:20 -08004371 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004372 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004373
4374 self.fake_nodepool.paused = True
4375 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4376 A.addApproval('code-review', 2)
4377 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4378 self.waitUntilSettled()
4379
4380 self.zk.client.stop()
4381 self.zk.client.start()
4382 self.fake_nodepool.paused = False
4383 self.waitUntilSettled()
4384
4385 self.assertEqual(A.data['status'], 'MERGED')
4386 self.assertEqual(A.reported, 2)
4387
James E. Blair6ab79e02017-01-06 10:10:17 -08004388 def test_nodepool_failure(self):
4389 "Test that jobs are reported after a nodepool failure"
4390
4391 self.fake_nodepool.paused = True
4392 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4393 A.addApproval('code-review', 2)
4394 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4395 self.waitUntilSettled()
4396
4397 req = self.fake_nodepool.getNodeRequests()[0]
4398 self.fake_nodepool.addFailRequest(req)
4399
4400 self.fake_nodepool.paused = False
4401 self.waitUntilSettled()
4402
4403 self.assertEqual(A.data['status'], 'NEW')
4404 self.assertEqual(A.reported, 2)
4405 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4406 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4407 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4408
Arieb6f068c2016-10-09 13:11:06 +03004409
James E. Blaira002b032017-04-18 10:35:48 -07004410class TestExecutor(ZuulTestCase):
4411 tenant_config_file = 'config/single-tenant/main.yaml'
4412
4413 def assertFinalState(self):
4414 # In this test, we expect to shut down in a non-final state,
4415 # so skip these checks.
4416 pass
4417
4418 def assertCleanShutdown(self):
4419 self.log.debug("Assert clean shutdown")
4420
4421 # After shutdown, make sure no jobs are running
4422 self.assertEqual({}, self.executor_server.job_workers)
4423
4424 # Make sure that git.Repo objects have been garbage collected.
4425 repos = []
4426 gc.collect()
4427 for obj in gc.get_objects():
4428 if isinstance(obj, git.Repo):
4429 self.log.debug("Leaked git repo object: %s" % repr(obj))
4430 repos.append(obj)
4431 self.assertEqual(len(repos), 0)
4432
4433 def test_executor_shutdown(self):
4434 "Test that the executor can shut down with jobs running"
4435
4436 self.executor_server.hold_jobs_in_build = True
4437 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4438 A.addApproval('code-review', 2)
4439 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4440 self.waitUntilSettled()
4441
4442
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004443class TestDependencyGraph(ZuulTestCase):
4444 tenant_config_file = 'config/dependency-graph/main.yaml'
4445
4446 def test_dependeny_graph_dispatch_jobs_once(self):
4447 "Test a job in a dependency graph is queued only once"
4448 # Job dependencies, starting with A
4449 # A
4450 # / \
4451 # B C
4452 # / \ / \
4453 # D F E
4454 # |
4455 # G
4456
4457 self.executor_server.hold_jobs_in_build = True
4458 change = self.fake_gerrit.addFakeChange(
4459 'org/project', 'master', 'change')
4460 change.addApproval('code-review', 2)
4461 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4462
4463 self.waitUntilSettled()
4464 self.assertEqual([b.name for b in self.builds], ['A'])
4465
4466 self.executor_server.release('A')
4467 self.waitUntilSettled()
4468 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4469
4470 self.executor_server.release('B')
4471 self.waitUntilSettled()
4472 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4473
4474 self.executor_server.release('D')
4475 self.waitUntilSettled()
4476 self.assertEqual([b.name for b in self.builds], ['C'])
4477
4478 self.executor_server.release('C')
4479 self.waitUntilSettled()
4480 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4481
4482 self.executor_server.release('F')
4483 self.waitUntilSettled()
4484 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4485
4486 self.executor_server.release('G')
4487 self.waitUntilSettled()
4488 self.assertEqual([b.name for b in self.builds], ['E'])
4489
4490 self.executor_server.release('E')
4491 self.waitUntilSettled()
4492 self.assertEqual(len(self.builds), 0)
4493
4494 self.executor_server.hold_jobs_in_build = False
4495 self.executor_server.release()
4496 self.waitUntilSettled()
4497
4498 self.assertEqual(len(self.builds), 0)
4499 self.assertEqual(len(self.history), 7)
4500
4501 self.assertEqual(change.data['status'], 'MERGED')
4502 self.assertEqual(change.reported, 2)
4503
4504 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4505 "Test that a job waits till all dependencies are successful"
4506 # Job dependencies, starting with A
4507 # A
4508 # / \
4509 # B C*
4510 # / \ / \
4511 # D F E
4512 # |
4513 # G
4514
4515 self.executor_server.hold_jobs_in_build = True
4516 change = self.fake_gerrit.addFakeChange(
4517 'org/project', 'master', 'change')
4518 change.addApproval('code-review', 2)
4519
4520 self.executor_server.failJob('C', change)
4521
4522 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4523
4524 self.waitUntilSettled()
4525 self.assertEqual([b.name for b in self.builds], ['A'])
4526
4527 self.executor_server.release('A')
4528 self.waitUntilSettled()
4529 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4530
4531 self.executor_server.release('B')
4532 self.waitUntilSettled()
4533 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4534
4535 self.executor_server.release('D')
4536 self.waitUntilSettled()
4537 self.assertEqual([b.name for b in self.builds], ['C'])
4538
4539 self.executor_server.release('C')
4540 self.waitUntilSettled()
4541 self.assertEqual(len(self.builds), 0)
4542
4543 self.executor_server.hold_jobs_in_build = False
4544 self.executor_server.release()
4545 self.waitUntilSettled()
4546
4547 self.assertEqual(len(self.builds), 0)
4548 self.assertEqual(len(self.history), 4)
4549
4550 self.assertEqual(change.data['status'], 'NEW')
4551 self.assertEqual(change.reported, 2)
4552
4553
Arieb6f068c2016-10-09 13:11:06 +03004554class TestDuplicatePipeline(ZuulTestCase):
4555 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4556
4557 def test_duplicate_pipelines(self):
4558 "Test that a change matching multiple pipelines works"
4559
4560 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4561 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4562 self.waitUntilSettled()
4563
4564 self.assertHistory([
4565 dict(name='project-test1', result='SUCCESS', changes='1,1',
4566 pipeline='dup1'),
4567 dict(name='project-test1', result='SUCCESS', changes='1,1',
4568 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004569 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004570
4571 self.assertEqual(len(A.messages), 2)
4572
Paul Belangera46a3742016-11-09 14:23:03 -05004573 if 'dup1' in A.messages[0]:
4574 self.assertIn('dup1', A.messages[0])
4575 self.assertNotIn('dup2', A.messages[0])
4576 self.assertIn('project-test1', A.messages[0])
4577 self.assertIn('dup2', A.messages[1])
4578 self.assertNotIn('dup1', A.messages[1])
4579 self.assertIn('project-test1', A.messages[1])
4580 else:
4581 self.assertIn('dup1', A.messages[1])
4582 self.assertNotIn('dup2', A.messages[1])
4583 self.assertIn('project-test1', A.messages[1])
4584 self.assertIn('dup2', A.messages[0])
4585 self.assertNotIn('dup1', A.messages[0])
4586 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004587
4588
Paul Belanger86085b32016-11-03 12:48:57 -04004589class TestSchedulerTemplatedProject(ZuulTestCase):
4590 tenant_config_file = 'config/templated-project/main.yaml'
4591
Paul Belanger174a8272017-03-14 13:20:10 -04004592 def test_job_from_templates_executed(self):
4593 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004594
4595 A = self.fake_gerrit.addFakeChange(
4596 'org/templated-project', 'master', 'A')
4597 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4598 self.waitUntilSettled()
4599
4600 self.assertEqual(self.getJobFromHistory('project-test1').result,
4601 'SUCCESS')
4602 self.assertEqual(self.getJobFromHistory('project-test2').result,
4603 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004604
4605 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004606 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004607
4608 A = self.fake_gerrit.addFakeChange(
4609 'org/layered-project', 'master', 'A')
4610 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4611 self.waitUntilSettled()
4612
4613 self.assertEqual(self.getJobFromHistory('project-test1').result,
4614 'SUCCESS')
4615 self.assertEqual(self.getJobFromHistory('project-test2').result,
4616 'SUCCESS')
4617 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4618 ).result, 'SUCCESS')
4619 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4620 ).result, 'SUCCESS')
4621 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4622 ).result, 'SUCCESS')
4623 self.assertEqual(self.getJobFromHistory('project-test6').result,
4624 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004625
4626
4627class TestSchedulerSuccessURL(ZuulTestCase):
4628 tenant_config_file = 'config/success-url/main.yaml'
4629
4630 def test_success_url(self):
4631 "Ensure bad build params are ignored"
4632 self.sched.reconfigure(self.config)
4633 self.init_repo('org/docs')
4634
4635 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4636 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4637 self.waitUntilSettled()
4638
4639 # Both builds ran: docs-draft-test + docs-draft-test2
4640 self.assertEqual(len(self.history), 2)
4641
4642 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004643 for build in self.history:
4644 if build.name == 'docs-draft-test':
4645 uuid = build.uuid[:7]
4646 break
Adam Gandelman94a60062016-11-21 16:43:14 -08004647
4648 # Two msgs: 'Starting...' + results
4649 self.assertEqual(len(self.smtp_messages), 2)
4650 body = self.smtp_messages[1]['body'].splitlines()
4651 self.assertEqual('Build succeeded.', body[0])
4652
4653 self.assertIn(
4654 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4655 'docs-draft-test/%s/publish-docs/' % uuid,
4656 body[2])
4657
Paul Belanger174a8272017-03-14 13:20:10 -04004658 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004659 self.assertIn(
4660 '- docs-draft-test2 https://server/job',
4661 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004662
4663
Adam Gandelman4da00f62016-12-09 15:47:33 -08004664class TestSchedulerMerges(ZuulTestCase):
4665 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004666
4667 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004668 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004669 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004670 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004671 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4672 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4673 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4674 A.addApproval('code-review', 2)
4675 B.addApproval('code-review', 2)
4676 C.addApproval('code-review', 2)
4677 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4678 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4679 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4680 self.waitUntilSettled()
4681
4682 build = self.builds[-1]
4683 ref = self.getParameter(build, 'ZUUL_REF')
4684
James E. Blair2a535672017-04-27 12:03:15 -07004685 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4686 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004687 repo = git.Repo(path)
4688 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4689 repo_messages.reverse()
4690
Paul Belanger174a8272017-03-14 13:20:10 -04004691 self.executor_server.hold_jobs_in_build = False
4692 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004693 self.waitUntilSettled()
4694
4695 return repo_messages
4696
4697 def _test_merge(self, mode):
4698 us_path = os.path.join(
4699 self.upstream_root, 'org/project-%s' % mode)
4700 expected_messages = [
4701 'initial commit',
4702 'add content from fixture',
4703 # the intermediate commits order is nondeterministic
4704 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4705 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4706 ]
4707 result = self._test_project_merge_mode(mode)
4708 self.assertEqual(result[:2], expected_messages[:2])
4709 self.assertEqual(result[-2:], expected_messages[-2:])
4710
4711 def test_project_merge_mode_merge(self):
4712 self._test_merge('merge')
4713
4714 def test_project_merge_mode_merge_resolve(self):
4715 self._test_merge('merge-resolve')
4716
4717 def test_project_merge_mode_cherrypick(self):
4718 expected_messages = [
4719 'initial commit',
4720 'add content from fixture',
4721 'A-1',
4722 'B-1',
4723 'C-1']
4724 result = self._test_project_merge_mode('cherry-pick')
4725 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004726
4727 def test_merge_branch(self):
4728 "Test that the right commits are on alternate branches"
4729 self.create_branch('org/project-merge-branches', 'mp')
4730
Paul Belanger174a8272017-03-14 13:20:10 -04004731 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004732 A = self.fake_gerrit.addFakeChange(
4733 'org/project-merge-branches', 'mp', 'A')
4734 B = self.fake_gerrit.addFakeChange(
4735 'org/project-merge-branches', 'mp', 'B')
4736 C = self.fake_gerrit.addFakeChange(
4737 'org/project-merge-branches', 'mp', 'C')
4738 A.addApproval('code-review', 2)
4739 B.addApproval('code-review', 2)
4740 C.addApproval('code-review', 2)
4741 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4742 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4743 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4744 self.waitUntilSettled()
4745
Paul Belanger174a8272017-03-14 13:20:10 -04004746 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004747 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004748 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004749 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004750 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004751 self.waitUntilSettled()
4752
4753 build = self.builds[-1]
4754 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
4755 ref = self.getParameter(build, 'ZUUL_REF')
James E. Blair2a535672017-04-27 12:03:15 -07004756 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4757 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004758 repo = git.Repo(path)
4759
4760 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4761 repo_messages.reverse()
4762 correct_messages = [
4763 'initial commit',
4764 'add content from fixture',
4765 'mp commit',
4766 'A-1', 'B-1', 'C-1']
4767 self.assertEqual(repo_messages, correct_messages)
4768
Paul Belanger174a8272017-03-14 13:20:10 -04004769 self.executor_server.hold_jobs_in_build = False
4770 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004771 self.waitUntilSettled()
4772
4773 def test_merge_multi_branch(self):
4774 "Test that dependent changes on multiple branches are merged"
4775 self.create_branch('org/project-merge-branches', 'mp')
4776
Paul Belanger174a8272017-03-14 13:20:10 -04004777 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004778 A = self.fake_gerrit.addFakeChange(
4779 'org/project-merge-branches', 'master', 'A')
4780 B = self.fake_gerrit.addFakeChange(
4781 'org/project-merge-branches', 'mp', 'B')
4782 C = self.fake_gerrit.addFakeChange(
4783 'org/project-merge-branches', 'master', 'C')
4784 A.addApproval('code-review', 2)
4785 B.addApproval('code-review', 2)
4786 C.addApproval('code-review', 2)
4787 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4788 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4789 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4790 self.waitUntilSettled()
4791
4792 job_A = None
4793 for job in self.builds:
4794 if 'project-merge' in job.name:
4795 job_A = job
4796 ref_A = self.getParameter(job_A, 'ZUUL_REF')
4797 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
4798 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
4799 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
4800
James E. Blair2a535672017-04-27 12:03:15 -07004801 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
4802 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004803 repo = git.Repo(path)
4804 repo_messages = [c.message.strip()
4805 for c in repo.iter_commits(ref_A)]
4806 repo_messages.reverse()
4807 correct_messages = [
4808 'initial commit', 'add content from fixture', 'A-1']
4809 self.assertEqual(repo_messages, correct_messages)
4810
Paul Belanger174a8272017-03-14 13:20:10 -04004811 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004812 self.waitUntilSettled()
4813
4814 job_B = None
4815 for job in self.builds:
4816 if 'project-merge' in job.name:
4817 job_B = job
4818 ref_B = self.getParameter(job_B, 'ZUUL_REF')
4819 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
4820 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
4821 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
4822
James E. Blair2a535672017-04-27 12:03:15 -07004823 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
4824 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004825 repo = git.Repo(path)
4826 repo_messages = [c.message.strip()
4827 for c in repo.iter_commits(ref_B)]
4828 repo_messages.reverse()
4829 correct_messages = [
4830 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4831 self.assertEqual(repo_messages, correct_messages)
4832
Paul Belanger174a8272017-03-14 13:20:10 -04004833 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004834 self.waitUntilSettled()
4835
4836 job_C = None
4837 for job in self.builds:
4838 if 'project-merge' in job.name:
4839 job_C = job
4840 ref_C = self.getParameter(job_C, 'ZUUL_REF')
4841 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
4842 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
4843 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair2a535672017-04-27 12:03:15 -07004844 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
4845 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004846 repo = git.Repo(path)
4847 repo_messages = [c.message.strip()
4848 for c in repo.iter_commits(ref_C)]
4849
4850 repo_messages.reverse()
4851 correct_messages = [
4852 'initial commit', 'add content from fixture',
4853 'A-1', 'C-1']
4854 # Ensure the right commits are in the history for this ref
4855 self.assertEqual(repo_messages, correct_messages)
4856
Paul Belanger174a8272017-03-14 13:20:10 -04004857 self.executor_server.hold_jobs_in_build = False
4858 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004859 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01004860
4861
James E. Blair9ea0d0b2017-04-20 09:27:15 -07004862class TestSemaphore(ZuulTestCase):
4863 tenant_config_file = 'config/semaphore/main.yaml'
4864
4865 def test_semaphore_one(self):
4866 "Test semaphores with max=1 (mutex)"
4867 tenant = self.sched.abide.tenants.get('tenant-one')
4868
4869 self.executor_server.hold_jobs_in_build = True
4870
4871 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4872 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4873 self.assertFalse('test-semaphore' in
4874 tenant.semaphore_handler.semaphores)
4875
4876 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4877 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4878 self.waitUntilSettled()
4879
4880 self.assertEqual(len(self.builds), 3)
4881 self.assertEqual(self.builds[0].name, 'project-test1')
4882 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
4883 self.assertEqual(self.builds[2].name, 'project-test1')
4884
4885 self.executor_server.release('semaphore-one-test1')
4886 self.waitUntilSettled()
4887
4888 self.assertEqual(len(self.builds), 3)
4889 self.assertEqual(self.builds[0].name, 'project-test1')
4890 self.assertEqual(self.builds[1].name, 'project-test1')
4891 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4892 self.assertTrue('test-semaphore' in
4893 tenant.semaphore_handler.semaphores)
4894
4895 self.executor_server.release('semaphore-one-test2')
4896 self.waitUntilSettled()
4897
4898 self.assertEqual(len(self.builds), 3)
4899 self.assertEqual(self.builds[0].name, 'project-test1')
4900 self.assertEqual(self.builds[1].name, 'project-test1')
4901 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
4902 self.assertTrue('test-semaphore' in
4903 tenant.semaphore_handler.semaphores)
4904
4905 self.executor_server.release('semaphore-one-test1')
4906 self.waitUntilSettled()
4907
4908 self.assertEqual(len(self.builds), 3)
4909 self.assertEqual(self.builds[0].name, 'project-test1')
4910 self.assertEqual(self.builds[1].name, 'project-test1')
4911 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4912 self.assertTrue('test-semaphore' in
4913 tenant.semaphore_handler.semaphores)
4914
4915 self.executor_server.release('semaphore-one-test2')
4916 self.waitUntilSettled()
4917
4918 self.assertEqual(len(self.builds), 2)
4919 self.assertEqual(self.builds[0].name, 'project-test1')
4920 self.assertEqual(self.builds[1].name, 'project-test1')
4921 self.assertFalse('test-semaphore' in
4922 tenant.semaphore_handler.semaphores)
4923
4924 self.executor_server.hold_jobs_in_build = False
4925 self.executor_server.release()
4926
4927 self.waitUntilSettled()
4928 self.assertEqual(len(self.builds), 0)
4929
4930 self.assertEqual(A.reported, 1)
4931 self.assertEqual(B.reported, 1)
4932 self.assertFalse('test-semaphore' in
4933 tenant.semaphore_handler.semaphores)
4934
4935 def test_semaphore_two(self):
4936 "Test semaphores with max>1"
4937 tenant = self.sched.abide.tenants.get('tenant-one')
4938
4939 self.executor_server.hold_jobs_in_build = True
4940 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4941 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4942 self.assertFalse('test-semaphore-two' in
4943 tenant.semaphore_handler.semaphores)
4944
4945 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4946 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4947 self.waitUntilSettled()
4948
4949 self.assertEqual(len(self.builds), 4)
4950 self.assertEqual(self.builds[0].name, 'project-test1')
4951 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
4952 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
4953 self.assertEqual(self.builds[3].name, 'project-test1')
4954 self.assertTrue('test-semaphore-two' in
4955 tenant.semaphore_handler.semaphores)
4956 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4957 'test-semaphore-two', [])), 2)
4958
4959 self.executor_server.release('semaphore-two-test1')
4960 self.waitUntilSettled()
4961
4962 self.assertEqual(len(self.builds), 4)
4963 self.assertEqual(self.builds[0].name, 'project-test1')
4964 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
4965 self.assertEqual(self.builds[2].name, 'project-test1')
4966 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
4967 self.assertTrue('test-semaphore-two' in
4968 tenant.semaphore_handler.semaphores)
4969 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4970 'test-semaphore-two', [])), 2)
4971
4972 self.executor_server.release('semaphore-two-test2')
4973 self.waitUntilSettled()
4974
4975 self.assertEqual(len(self.builds), 4)
4976 self.assertEqual(self.builds[0].name, 'project-test1')
4977 self.assertEqual(self.builds[1].name, 'project-test1')
4978 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
4979 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
4980 self.assertTrue('test-semaphore-two' in
4981 tenant.semaphore_handler.semaphores)
4982 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4983 'test-semaphore-two', [])), 2)
4984
4985 self.executor_server.release('semaphore-two-test1')
4986 self.waitUntilSettled()
4987
4988 self.assertEqual(len(self.builds), 3)
4989 self.assertEqual(self.builds[0].name, 'project-test1')
4990 self.assertEqual(self.builds[1].name, 'project-test1')
4991 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
4992 self.assertTrue('test-semaphore-two' in
4993 tenant.semaphore_handler.semaphores)
4994 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4995 'test-semaphore-two', [])), 1)
4996
4997 self.executor_server.release('semaphore-two-test2')
4998 self.waitUntilSettled()
4999
5000 self.assertEqual(len(self.builds), 2)
5001 self.assertEqual(self.builds[0].name, 'project-test1')
5002 self.assertEqual(self.builds[1].name, 'project-test1')
5003 self.assertFalse('test-semaphore-two' in
5004 tenant.semaphore_handler.semaphores)
5005
5006 self.executor_server.hold_jobs_in_build = False
5007 self.executor_server.release()
5008
5009 self.waitUntilSettled()
5010 self.assertEqual(len(self.builds), 0)
5011
5012 self.assertEqual(A.reported, 1)
5013 self.assertEqual(B.reported, 1)
5014
5015 def test_semaphore_abandon(self):
5016 "Test abandon with job semaphores"
5017 self.executor_server.hold_jobs_in_build = True
5018 tenant = self.sched.abide.tenants.get('tenant-one')
5019 check_pipeline = tenant.layout.pipelines['check']
5020
5021 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5022 self.assertFalse('test-semaphore' in
5023 tenant.semaphore_handler.semaphores)
5024
5025 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5026 self.waitUntilSettled()
5027
5028 self.assertTrue('test-semaphore' in
5029 tenant.semaphore_handler.semaphores)
5030
5031 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5032 self.waitUntilSettled()
5033
5034 # The check pipeline should be empty
5035 items = check_pipeline.getAllItems()
5036 self.assertEqual(len(items), 0)
5037
5038 # The semaphore should be released
5039 self.assertFalse('test-semaphore' in
5040 tenant.semaphore_handler.semaphores)
5041
5042 self.executor_server.hold_jobs_in_build = False
5043 self.executor_server.release()
5044 self.waitUntilSettled()
5045
5046 def test_semaphore_reconfigure(self):
5047 "Test reconfigure with job semaphores"
5048 self.executor_server.hold_jobs_in_build = True
5049 tenant = self.sched.abide.tenants.get('tenant-one')
5050 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5051 self.assertFalse('test-semaphore' in
5052 tenant.semaphore_handler.semaphores)
5053
5054 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5055 self.waitUntilSettled()
5056
5057 self.assertTrue('test-semaphore' in
5058 tenant.semaphore_handler.semaphores)
5059
5060 # reconfigure without layout change
5061 self.sched.reconfigure(self.config)
5062 self.waitUntilSettled()
5063 tenant = self.sched.abide.tenants.get('tenant-one')
5064
5065 # semaphore still must be held
5066 self.assertTrue('test-semaphore' in
5067 tenant.semaphore_handler.semaphores)
5068
5069 self.commitConfigUpdate(
5070 'common-config',
5071 'config/semaphore/zuul-reconfiguration.yaml')
5072 self.sched.reconfigure(self.config)
5073 self.waitUntilSettled()
5074 tenant = self.sched.abide.tenants.get('tenant-one')
5075
5076 self.executor_server.release('project-test1')
5077 self.waitUntilSettled()
5078
5079 # There should be no builds anymore
5080 self.assertEqual(len(self.builds), 0)
5081
5082 # The semaphore should be released
5083 self.assertFalse('test-semaphore' in
5084 tenant.semaphore_handler.semaphores)
5085
5086
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005087class TestSemaphoreMultiTenant(ZuulTestCase):
5088 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5089
5090 def test_semaphore_tenant_isolation(self):
5091 "Test semaphores in multiple tenants"
5092
5093 self.waitUntilSettled()
5094 tenant_one = self.sched.abide.tenants.get('tenant-one')
5095 tenant_two = self.sched.abide.tenants.get('tenant-two')
5096
5097 self.executor_server.hold_jobs_in_build = True
5098 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5099 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5100 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5101 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5102 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5103 self.assertFalse('test-semaphore' in
5104 tenant_one.semaphore_handler.semaphores)
5105 self.assertFalse('test-semaphore' in
5106 tenant_two.semaphore_handler.semaphores)
5107
5108 # add patches to project1 of tenant-one
5109 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5110 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5111 self.waitUntilSettled()
5112
5113 # one build of project1-test1 must run
5114 # semaphore of tenant-one must be acquired once
5115 # semaphore of tenant-two must not be acquired
5116 self.assertEqual(len(self.builds), 1)
5117 self.assertEqual(self.builds[0].name, 'project1-test1')
5118 self.assertTrue('test-semaphore' in
5119 tenant_one.semaphore_handler.semaphores)
5120 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5121 'test-semaphore', [])), 1)
5122 self.assertFalse('test-semaphore' in
5123 tenant_two.semaphore_handler.semaphores)
5124
5125 # add patches to project2 of tenant-two
5126 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5127 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5128 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5129 self.waitUntilSettled()
5130
5131 # one build of project1-test1 must run
5132 # two builds of project2-test1 must run
5133 # semaphore of tenant-one must be acquired once
5134 # semaphore of tenant-two must be acquired twice
5135 self.assertEqual(len(self.builds), 3)
5136 self.assertEqual(self.builds[0].name, 'project1-test1')
5137 self.assertEqual(self.builds[1].name, 'project2-test1')
5138 self.assertEqual(self.builds[2].name, 'project2-test1')
5139 self.assertTrue('test-semaphore' in
5140 tenant_one.semaphore_handler.semaphores)
5141 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5142 'test-semaphore', [])), 1)
5143 self.assertTrue('test-semaphore' in
5144 tenant_two.semaphore_handler.semaphores)
5145 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5146 'test-semaphore', [])), 2)
5147
5148 self.executor_server.release('project1-test1')
5149 self.waitUntilSettled()
5150
5151 # one build of project1-test1 must run
5152 # two builds of project2-test1 must run
5153 # semaphore of tenant-one must be acquired once
5154 # semaphore of tenant-two must be acquired twice
5155 self.assertEqual(len(self.builds), 3)
5156 self.assertEqual(self.builds[0].name, 'project2-test1')
5157 self.assertEqual(self.builds[1].name, 'project2-test1')
5158 self.assertEqual(self.builds[2].name, 'project1-test1')
5159 self.assertTrue('test-semaphore' in
5160 tenant_one.semaphore_handler.semaphores)
5161 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5162 'test-semaphore', [])), 1)
5163 self.assertTrue('test-semaphore' in
5164 tenant_two.semaphore_handler.semaphores)
5165 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5166 'test-semaphore', [])), 2)
5167
5168 self.executor_server.release('project2-test1')
5169 self.waitUntilSettled()
5170
5171 # one build of project1-test1 must run
5172 # one build of project2-test1 must run
5173 # semaphore of tenant-one must be acquired once
5174 # semaphore of tenant-two must be acquired once
5175 self.assertEqual(len(self.builds), 2)
5176 self.assertTrue('test-semaphore' in
5177 tenant_one.semaphore_handler.semaphores)
5178 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5179 'test-semaphore', [])), 1)
5180 self.assertTrue('test-semaphore' in
5181 tenant_two.semaphore_handler.semaphores)
5182 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5183 'test-semaphore', [])), 1)
5184
5185 self.executor_server.hold_jobs_in_build = False
5186 self.executor_server.release()
5187
5188 self.waitUntilSettled()
5189
5190 # no build must run
5191 # semaphore of tenant-one must not be acquired
5192 # semaphore of tenant-two must not be acquired
5193 self.assertEqual(len(self.builds), 0)
5194 self.assertFalse('test-semaphore' in
5195 tenant_one.semaphore_handler.semaphores)
5196 self.assertFalse('test-semaphore' in
5197 tenant_two.semaphore_handler.semaphores)
5198
5199 self.assertEqual(A.reported, 1)
5200 self.assertEqual(B.reported, 1)
5201
5202
5203class TestSemaphoreInRepo(ZuulTestCase):
5204 tenant_config_file = 'config/in-repo/main.yaml'
5205
5206 def test_semaphore_in_repo(self):
5207 "Test semaphores in repo config"
5208
5209 # This tests dynamic semaphore handling in project repos. The semaphore
5210 # max value should not be evaluated dynamically but must be updated
5211 # after the change lands.
5212
5213 self.waitUntilSettled()
5214 tenant = self.sched.abide.tenants.get('tenant-one')
5215
5216 in_repo_conf = textwrap.dedent(
5217 """
5218 - job:
5219 name: project-test2
5220 semaphore: test-semaphore
5221
5222 - project:
5223 name: org/project
5224 tenant-one-gate:
5225 jobs:
5226 - project-test2
5227
5228 # the max value in dynamic layout must be ignored
5229 - semaphore:
5230 name: test-semaphore
5231 max: 2
5232 """)
5233
5234 in_repo_playbook = textwrap.dedent(
5235 """
5236 - hosts: all
5237 tasks: []
5238 """)
5239
5240 file_dict = {'.zuul.yaml': in_repo_conf,
5241 'playbooks/project-test2.yaml': in_repo_playbook}
5242 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5243 files=file_dict)
5244 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5245 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5246 B.setDependsOn(A, 1)
5247 C.setDependsOn(A, 1)
5248
5249 self.executor_server.hold_jobs_in_build = True
5250
5251 A.addApproval('code-review', 2)
5252 B.addApproval('code-review', 2)
5253 C.addApproval('code-review', 2)
5254 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
5255 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
5256 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
5257 self.waitUntilSettled()
5258
5259 # check that the layout in a queue item still has max value of 1
5260 # for test-semaphore
5261 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5262 queue = None
5263 for queue_candidate in pipeline.queues:
5264 if queue_candidate.name == 'org/project':
5265 queue = queue_candidate
5266 break
5267 queue_item = queue.queue[0]
5268 item_dynamic_layout = queue_item.current_build_set.layout
5269 dynamic_test_semaphore = \
5270 item_dynamic_layout.semaphores.get('test-semaphore')
5271 self.assertEqual(dynamic_test_semaphore.max, 1)
5272
5273 # one build must be in queue, one semaphores acquired
5274 self.assertEqual(len(self.builds), 1)
5275 self.assertEqual(self.builds[0].name, 'project-test2')
5276 self.assertTrue('test-semaphore' in
5277 tenant.semaphore_handler.semaphores)
5278 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5279 'test-semaphore', [])), 1)
5280
5281 self.executor_server.release('project-test2')
5282 self.waitUntilSettled()
5283
5284 # change A must be merged
5285 self.assertEqual(A.data['status'], 'MERGED')
5286 self.assertEqual(A.reported, 2)
5287
5288 # send change-merged event as the gerrit mock doesn't send it
5289 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5290 self.waitUntilSettled()
5291
5292 # now that change A was merged, the new semaphore max must be effective
5293 tenant = self.sched.abide.tenants.get('tenant-one')
5294 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5295
5296 # two builds must be in queue, two semaphores acquired
5297 self.assertEqual(len(self.builds), 2)
5298 self.assertEqual(self.builds[0].name, 'project-test2')
5299 self.assertEqual(self.builds[1].name, 'project-test2')
5300 self.assertTrue('test-semaphore' in
5301 tenant.semaphore_handler.semaphores)
5302 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5303 'test-semaphore', [])), 2)
5304
5305 self.executor_server.release('project-test2')
5306 self.waitUntilSettled()
5307
5308 self.assertEqual(len(self.builds), 0)
5309 self.assertFalse('test-semaphore' in
5310 tenant.semaphore_handler.semaphores)
5311
5312 self.executor_server.hold_jobs_in_build = False
5313 self.executor_server.release()
5314
5315 self.waitUntilSettled()
5316 self.assertEqual(len(self.builds), 0)
5317
5318 self.assertEqual(A.reported, 2)
5319 self.assertEqual(B.reported, 2)
5320 self.assertEqual(C.reported, 2)