blob: aaf7132d15489d93fa512bc5b0f2d0c165c6a11c [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
James E. Blairec056492016-07-22 09:45:56 -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)
959 self.assertEqual(B.reported, 1)
960 self.assertEqual(C.reported, 1)
961
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700962 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700963 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700964 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700965 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700966 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700967 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700968
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700969 self.gearman_server.hold_jobs_in_queue = False
970 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700971 self.waitUntilSettled()
972
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400973 self.assertEqual(A.data['status'], 'MERGED')
974 self.assertEqual(B.data['status'], 'NEW')
975 self.assertEqual(C.data['status'], 'MERGED')
976 self.assertEqual(A.reported, 2)
977 self.assertEqual(B.reported, 2)
978 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700979
980 self.assertHistory([
981 dict(name='project-merge', result='SUCCESS', changes='1,1'),
982 dict(name='project-test1', result='SUCCESS', changes='1,1'),
983 dict(name='project-test2', result='SUCCESS', changes='1,1'),
984 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
985 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
986 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
987 ], ordered=False)
988
989 def test_delayed_merge_conflict(self):
990 "Test that delayed check merge conflicts are handled properly"
991
992 # Hold jobs in the gearman queue so that we can test whether
993 # the executor returns a merge failure after the scheduler has
994 # successfully merged.
995 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))
1011 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1012 self.waitUntilSettled()
1013
1014 self.assertEqual(A.reported, 1)
1015 self.assertEqual(B.reported, 0) # Check does not report start
1016 self.assertEqual(C.reported, 0) # Check does not report start
1017
1018 # A merges while B and C are queued in check
1019 # Release A project-merge
1020 queue = self.gearman_server.getQueue()
1021 self.release(queue[0])
1022 self.waitUntilSettled()
1023
1024 # Release A project-test*
1025 # gate has higher precedence, so A's test jobs are added in
1026 # front of the merge jobs for B and C
1027 queue = self.gearman_server.getQueue()
1028 self.release(queue[0])
1029 self.release(queue[1])
1030 self.waitUntilSettled()
1031
1032 self.assertEqual(A.data['status'], 'MERGED')
1033 self.assertEqual(B.data['status'], 'NEW')
1034 self.assertEqual(C.data['status'], 'NEW')
1035 self.assertEqual(A.reported, 2)
1036 self.assertEqual(B.reported, 0)
1037 self.assertEqual(C.reported, 0)
1038 self.assertHistory([
1039 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1040 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1041 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1042 ], ordered=False)
1043
1044 # B and C report merge conflicts
1045 # Release B project-merge
1046 queue = self.gearman_server.getQueue()
1047 self.release(queue[0])
1048 self.waitUntilSettled()
1049
1050 # Release C
1051 self.gearman_server.hold_jobs_in_queue = False
1052 self.gearman_server.release()
1053 self.waitUntilSettled()
1054
1055 self.assertEqual(A.data['status'], 'MERGED')
1056 self.assertEqual(B.data['status'], 'NEW')
1057 self.assertEqual(C.data['status'], 'NEW')
1058 self.assertEqual(A.reported, 2)
1059 self.assertEqual(B.reported, 1)
1060 self.assertEqual(C.reported, 1)
1061
1062 self.assertHistory([
1063 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1064 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1065 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1066 dict(name='project-merge', result='MERGER_FAILURE', changes='2,1'),
1067 dict(name='project-merge', result='MERGER_FAILURE',
1068 changes='2,1 3,1'),
1069 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001070
James E. Blairdaabed22012-08-15 15:38:57 -07001071 def test_post(self):
1072 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001073
Zhongyue Luo5d556072012-09-21 02:00:47 +09001074 e = {
1075 "type": "ref-updated",
1076 "submitter": {
1077 "name": "User Name",
1078 },
1079 "refUpdate": {
1080 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1081 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1082 "refName": "master",
1083 "project": "org/project",
1084 }
1085 }
James E. Blairdaabed22012-08-15 15:38:57 -07001086 self.fake_gerrit.addEvent(e)
1087 self.waitUntilSettled()
1088
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001089 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001090 self.assertEqual(len(self.history), 1)
1091 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001092
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001093 def test_post_ignore_deletes(self):
1094 "Test that deleting refs does not trigger post jobs"
1095
1096 e = {
1097 "type": "ref-updated",
1098 "submitter": {
1099 "name": "User Name",
1100 },
1101 "refUpdate": {
1102 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1103 "newRev": "0000000000000000000000000000000000000000",
1104 "refName": "master",
1105 "project": "org/project",
1106 }
1107 }
1108 self.fake_gerrit.addEvent(e)
1109 self.waitUntilSettled()
1110
1111 job_names = [x.name for x in self.history]
1112 self.assertEqual(len(self.history), 0)
1113 self.assertNotIn('project-post', job_names)
1114
James E. Blaircc324192017-04-19 14:36:08 -07001115 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001116 def test_post_ignore_deletes_negative(self):
1117 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001118 e = {
1119 "type": "ref-updated",
1120 "submitter": {
1121 "name": "User Name",
1122 },
1123 "refUpdate": {
1124 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1125 "newRev": "0000000000000000000000000000000000000000",
1126 "refName": "master",
1127 "project": "org/project",
1128 }
1129 }
1130 self.fake_gerrit.addEvent(e)
1131 self.waitUntilSettled()
1132
1133 job_names = [x.name for x in self.history]
1134 self.assertEqual(len(self.history), 1)
1135 self.assertIn('project-post', job_names)
1136
James E. Blairec056492016-07-22 09:45:56 -07001137 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001138 def test_build_configuration_branch_interaction(self):
1139 "Test that switching between branches works"
1140 self.test_build_configuration()
1141 self.test_build_configuration_branch()
1142 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001143 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001144 repo = git.Repo(path)
1145 repo.heads.master.commit = repo.commit('init')
1146 self.test_build_configuration()
1147
James E. Blaircaec0c52012-08-22 14:52:22 -07001148 def test_dependent_changes_dequeue(self):
1149 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001150
James E. Blaircaec0c52012-08-22 14:52:22 -07001151 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1152 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1153 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001154 A.addApproval('code-review', 2)
1155 B.addApproval('code-review', 2)
1156 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001157
1158 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1159 M1.setMerged()
1160
1161 # C -> B -> A -> M1
1162
1163 C.setDependsOn(B, 1)
1164 B.setDependsOn(A, 1)
1165 A.setDependsOn(M1, 1)
1166
Paul Belanger174a8272017-03-14 13:20:10 -04001167 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001168
James E. Blair8b5408c2016-08-08 15:37:46 -07001169 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1170 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1171 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001172
1173 self.waitUntilSettled()
1174
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001175 self.assertEqual(A.data['status'], 'NEW')
1176 self.assertEqual(A.reported, 2)
1177 self.assertEqual(B.data['status'], 'NEW')
1178 self.assertEqual(B.reported, 2)
1179 self.assertEqual(C.data['status'], 'NEW')
1180 self.assertEqual(C.reported, 2)
1181 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001182
James E. Blair972e3c72013-08-29 12:04:55 -07001183 def test_failing_dependent_changes(self):
1184 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001185 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001186 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1187 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1188 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1189 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1190 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001191 A.addApproval('code-review', 2)
1192 B.addApproval('code-review', 2)
1193 C.addApproval('code-review', 2)
1194 D.addApproval('code-review', 2)
1195 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001196
1197 # E, D -> C -> B, A
1198
1199 D.setDependsOn(C, 1)
1200 C.setDependsOn(B, 1)
1201
Paul Belanger174a8272017-03-14 13:20:10 -04001202 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001203
James E. Blair8b5408c2016-08-08 15:37:46 -07001204 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1205 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1206 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1207 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1208 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001209
1210 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001211 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001212 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001213 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001214 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001215 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001216 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001217 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001218 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001219 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001220 self.waitUntilSettled()
1221
Paul Belanger174a8272017-03-14 13:20:10 -04001222 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001223 for build in self.builds:
1224 if build.parameters['ZUUL_CHANGE'] != '1':
1225 build.release()
1226 self.waitUntilSettled()
1227
Paul Belanger174a8272017-03-14 13:20:10 -04001228 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001229 self.waitUntilSettled()
1230
1231 self.assertEqual(A.data['status'], 'MERGED')
1232 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001233 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001234 self.assertEqual(B.data['status'], 'NEW')
1235 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001236 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001237 self.assertEqual(C.data['status'], 'NEW')
1238 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001239 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001240 self.assertEqual(D.data['status'], 'NEW')
1241 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001242 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001243 self.assertEqual(E.data['status'], 'MERGED')
1244 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001245 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001246 self.assertEqual(len(self.history), 18)
1247
James E. Blairec590122012-08-22 15:19:31 -07001248 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001249 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001250 # If it's dequeued more than once, we should see extra
1251 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001252
Paul Belanger174a8272017-03-14 13:20:10 -04001253 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001254 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1255 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1256 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001257 A.addApproval('code-review', 2)
1258 B.addApproval('code-review', 2)
1259 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001260
Paul Belanger174a8272017-03-14 13:20:10 -04001261 self.executor_server.failJob('project-test1', A)
1262 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001263
James E. Blair8b5408c2016-08-08 15:37:46 -07001264 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1265 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1266 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001267
1268 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001269
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001270 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001271 self.assertEqual(self.builds[0].name, 'project-merge')
1272 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001273
Paul Belanger174a8272017-03-14 13:20:10 -04001274 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001275 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001276 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001277 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001278 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001279 self.waitUntilSettled()
1280
James E. Blair6df67132017-04-19 13:53:20 -07001281 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001282 self.assertEqual(self.builds[0].name, 'project-test1')
1283 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001284 self.assertEqual(self.builds[2].name, 'project-test1')
1285 self.assertEqual(self.builds[3].name, 'project-test2')
1286 self.assertEqual(self.builds[4].name, 'project-test1')
1287 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001288
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001289 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001290 self.waitUntilSettled()
1291
James E. Blair6df67132017-04-19 13:53:20 -07001292 self.assertEqual(len(self.builds), 2) # test2, merge for B
1293 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001294
Paul Belanger174a8272017-03-14 13:20:10 -04001295 self.executor_server.hold_jobs_in_build = False
1296 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001297 self.waitUntilSettled()
1298
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001299 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001300 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001301
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001302 self.assertEqual(A.data['status'], 'NEW')
1303 self.assertEqual(B.data['status'], 'MERGED')
1304 self.assertEqual(C.data['status'], 'MERGED')
1305 self.assertEqual(A.reported, 2)
1306 self.assertEqual(B.reported, 2)
1307 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001308
James E. Blaircc020532017-04-19 13:04:44 -07001309 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001310 def test_nonvoting_job(self):
1311 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001312
James E. Blair4ec821f2012-08-23 15:28:28 -07001313 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1314 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001315 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001316 self.executor_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001317 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001318
1319 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001320
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001321 self.assertEqual(A.data['status'], 'MERGED')
1322 self.assertEqual(A.reported, 2)
1323 self.assertEqual(
1324 self.getJobFromHistory('nonvoting-project-merge').result,
1325 'SUCCESS')
1326 self.assertEqual(
1327 self.getJobFromHistory('nonvoting-project-test1').result,
1328 'SUCCESS')
1329 self.assertEqual(
1330 self.getJobFromHistory('nonvoting-project-test2').result,
1331 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001332
James E. Blair5821bd92015-09-16 08:48:15 -07001333 for build in self.builds:
1334 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1335
James E. Blaire0487072012-08-29 17:38:31 -07001336 def test_check_queue_success(self):
1337 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001338
James E. Blaire0487072012-08-29 17:38:31 -07001339 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1340 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1341
1342 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001343
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001344 self.assertEqual(A.data['status'], 'NEW')
1345 self.assertEqual(A.reported, 1)
1346 self.assertEqual(self.getJobFromHistory('project-merge').result,
1347 'SUCCESS')
1348 self.assertEqual(self.getJobFromHistory('project-test1').result,
1349 'SUCCESS')
1350 self.assertEqual(self.getJobFromHistory('project-test2').result,
1351 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001352
1353 def test_check_queue_failure(self):
1354 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001355
James E. Blaire0487072012-08-29 17:38:31 -07001356 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001357 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001358 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1359
1360 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001361
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001362 self.assertEqual(A.data['status'], 'NEW')
1363 self.assertEqual(A.reported, 1)
1364 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001365 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001366 self.assertEqual(self.getJobFromHistory('project-test1').result,
1367 'SUCCESS')
1368 self.assertEqual(self.getJobFromHistory('project-test2').result,
1369 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001370
James E. Blair7cb84542017-04-19 13:35:05 -07001371 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001372 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001373 # This particular test does a large amount of merges and needs a little
1374 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001375 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001376 "test that dependent changes behind dequeued changes work"
1377 # This complicated test is a reproduction of a real life bug
1378 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001379
Paul Belanger174a8272017-03-14 13:20:10 -04001380 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001381 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1382 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1383 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1384 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1385 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1386 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1387 D.setDependsOn(C, 1)
1388 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001389 A.addApproval('code-review', 2)
1390 B.addApproval('code-review', 2)
1391 C.addApproval('code-review', 2)
1392 D.addApproval('code-review', 2)
1393 E.addApproval('code-review', 2)
1394 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001395
1396 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001397
1398 # Change object re-use in the gerrit trigger is hidden if
1399 # changes are added in quick succession; waiting makes it more
1400 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001401 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001402 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001403 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001404 self.waitUntilSettled()
1405
Paul Belanger174a8272017-03-14 13:20:10 -04001406 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001407 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001408 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001409 self.waitUntilSettled()
1410
James E. Blair8b5408c2016-08-08 15:37:46 -07001411 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001412 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001413 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001414 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001415 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001416 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001417 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001418 self.waitUntilSettled()
1419
Paul Belanger174a8272017-03-14 13:20:10 -04001420 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001421 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001422 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001423 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001424 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001425 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001426 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001427 self.waitUntilSettled()
1428
1429 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001430
1431 # Grab pointers to the jobs we want to release before
1432 # releasing any, because list indexes may change as
1433 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001434 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001435 a.release()
1436 b.release()
1437 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001438 self.waitUntilSettled()
1439
Paul Belanger174a8272017-03-14 13:20:10 -04001440 self.executor_server.hold_jobs_in_build = False
1441 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001442 self.waitUntilSettled()
1443
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001444 self.assertEqual(A.data['status'], 'NEW')
1445 self.assertEqual(B.data['status'], 'MERGED')
1446 self.assertEqual(C.data['status'], 'MERGED')
1447 self.assertEqual(D.data['status'], 'MERGED')
1448 self.assertEqual(E.data['status'], 'MERGED')
1449 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001450
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001451 self.assertEqual(A.reported, 2)
1452 self.assertEqual(B.reported, 2)
1453 self.assertEqual(C.reported, 2)
1454 self.assertEqual(D.reported, 2)
1455 self.assertEqual(E.reported, 2)
1456 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001457
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001458 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1459 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001460
1461 def test_merger_repack(self):
1462 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001463
James E. Blair05fed602012-09-07 12:45:24 -07001464 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001465 A.addApproval('code-review', 2)
1466 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001467 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001468 self.assertEqual(self.getJobFromHistory('project-merge').result,
1469 'SUCCESS')
1470 self.assertEqual(self.getJobFromHistory('project-test1').result,
1471 'SUCCESS')
1472 self.assertEqual(self.getJobFromHistory('project-test2').result,
1473 'SUCCESS')
1474 self.assertEqual(A.data['status'], 'MERGED')
1475 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001476 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001477 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001478
James E. Blair2a535672017-04-27 12:03:15 -07001479 path = os.path.join(self.merger_src_root, "review.example.com",
1480 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001481 if os.path.exists(path):
1482 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001483 path = os.path.join(self.executor_src_root, "review.example.com",
1484 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001485 if os.path.exists(path):
1486 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001487
1488 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001489 A.addApproval('code-review', 2)
1490 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001491 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001492 self.assertEqual(self.getJobFromHistory('project-merge').result,
1493 'SUCCESS')
1494 self.assertEqual(self.getJobFromHistory('project-test1').result,
1495 'SUCCESS')
1496 self.assertEqual(self.getJobFromHistory('project-test2').result,
1497 'SUCCESS')
1498 self.assertEqual(A.data['status'], 'MERGED')
1499 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001500
James E. Blair4886f282012-11-15 09:27:33 -08001501 def test_merger_repack_large_change(self):
1502 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001503 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001504 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001505 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001506 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001507 url = self.fake_gerrit.getGitUrl(project)
James E. Blair2a535672017-04-27 12:03:15 -07001508 self.merge_server.merger._addProject('review.example.com',
1509 'org/project', url)
James E. Blair6df67132017-04-19 13:53:20 -07001510 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001511 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001512 # TODOv3(jeblair): add hostname to upstream root
1513 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001514 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001515 path = os.path.join(self.merger_src_root, 'review.example.com',
1516 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001517 if os.path.exists(path):
1518 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001519 path = os.path.join(self.executor_src_root, 'review.example.com',
1520 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001521 if os.path.exists(path):
1522 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001523
James E. Blair8b5408c2016-08-08 15:37:46 -07001524 A.addApproval('code-review', 2)
1525 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001526 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001527 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001528 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001529 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001530 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001531 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001532 'SUCCESS')
1533 self.assertEqual(A.data['status'], 'MERGED')
1534 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001535
James E. Blair2fa50962013-01-30 21:50:41 -08001536 def test_new_patchset_dequeues_old(self):
1537 "Test that a new patchset causes the old to be dequeued"
1538 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001539 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001540 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1541 M.setMerged()
1542
1543 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1544 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1545 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1546 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001547 A.addApproval('code-review', 2)
1548 B.addApproval('code-review', 2)
1549 C.addApproval('code-review', 2)
1550 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001551
1552 C.setDependsOn(B, 1)
1553 B.setDependsOn(A, 1)
1554 A.setDependsOn(M, 1)
1555
James E. Blair8b5408c2016-08-08 15:37:46 -07001556 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1557 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1558 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1559 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001560 self.waitUntilSettled()
1561
1562 B.addPatchset()
1563 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1564 self.waitUntilSettled()
1565
Paul Belanger174a8272017-03-14 13:20:10 -04001566 self.executor_server.hold_jobs_in_build = False
1567 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001568 self.waitUntilSettled()
1569
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001570 self.assertEqual(A.data['status'], 'MERGED')
1571 self.assertEqual(A.reported, 2)
1572 self.assertEqual(B.data['status'], 'NEW')
1573 self.assertEqual(B.reported, 2)
1574 self.assertEqual(C.data['status'], 'NEW')
1575 self.assertEqual(C.reported, 2)
1576 self.assertEqual(D.data['status'], 'MERGED')
1577 self.assertEqual(D.reported, 2)
1578 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001579
James E. Blairba437362015-02-07 11:41:52 -08001580 def test_new_patchset_check(self):
1581 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001582
Paul Belanger174a8272017-03-14 13:20:10 -04001583 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001584
1585 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001586 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001587 tenant = self.sched.abide.tenants.get('tenant-one')
1588 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001589
1590 # Add two git-dependent changes
1591 B.setDependsOn(A, 1)
1592 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1593 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001594 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1595 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001596
1597 # A live item, and a non-live/live pair
1598 items = check_pipeline.getAllItems()
1599 self.assertEqual(len(items), 3)
1600
1601 self.assertEqual(items[0].change.number, '1')
1602 self.assertEqual(items[0].change.patchset, '1')
1603 self.assertFalse(items[0].live)
1604
1605 self.assertEqual(items[1].change.number, '2')
1606 self.assertEqual(items[1].change.patchset, '1')
1607 self.assertTrue(items[1].live)
1608
1609 self.assertEqual(items[2].change.number, '1')
1610 self.assertEqual(items[2].change.patchset, '1')
1611 self.assertTrue(items[2].live)
1612
1613 # Add a new patchset to A
1614 A.addPatchset()
1615 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1616 self.waitUntilSettled()
1617
1618 # The live copy of A,1 should be gone, but the non-live and B
1619 # should continue, and we should have a new A,2
1620 items = check_pipeline.getAllItems()
1621 self.assertEqual(len(items), 3)
1622
1623 self.assertEqual(items[0].change.number, '1')
1624 self.assertEqual(items[0].change.patchset, '1')
1625 self.assertFalse(items[0].live)
1626
1627 self.assertEqual(items[1].change.number, '2')
1628 self.assertEqual(items[1].change.patchset, '1')
1629 self.assertTrue(items[1].live)
1630
1631 self.assertEqual(items[2].change.number, '1')
1632 self.assertEqual(items[2].change.patchset, '2')
1633 self.assertTrue(items[2].live)
1634
1635 # Add a new patchset to B
1636 B.addPatchset()
1637 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1638 self.waitUntilSettled()
1639
1640 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1641 # but we should have a new B,2 (still based on A,1)
1642 items = check_pipeline.getAllItems()
1643 self.assertEqual(len(items), 3)
1644
1645 self.assertEqual(items[0].change.number, '1')
1646 self.assertEqual(items[0].change.patchset, '2')
1647 self.assertTrue(items[0].live)
1648
1649 self.assertEqual(items[1].change.number, '1')
1650 self.assertEqual(items[1].change.patchset, '1')
1651 self.assertFalse(items[1].live)
1652
1653 self.assertEqual(items[2].change.number, '2')
1654 self.assertEqual(items[2].change.patchset, '2')
1655 self.assertTrue(items[2].live)
1656
1657 self.builds[0].release()
1658 self.waitUntilSettled()
1659 self.builds[0].release()
1660 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001661 self.executor_server.hold_jobs_in_build = False
1662 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001663 self.waitUntilSettled()
1664
1665 self.assertEqual(A.reported, 1)
1666 self.assertEqual(B.reported, 1)
1667 self.assertEqual(self.history[0].result, 'ABORTED')
1668 self.assertEqual(self.history[0].changes, '1,1')
1669 self.assertEqual(self.history[1].result, 'ABORTED')
1670 self.assertEqual(self.history[1].changes, '1,1 2,1')
1671 self.assertEqual(self.history[2].result, 'SUCCESS')
1672 self.assertEqual(self.history[2].changes, '1,2')
1673 self.assertEqual(self.history[3].result, 'SUCCESS')
1674 self.assertEqual(self.history[3].changes, '1,1 2,2')
1675
1676 def test_abandoned_gate(self):
1677 "Test that an abandoned change is dequeued from gate"
1678
Paul Belanger174a8272017-03-14 13:20:10 -04001679 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001680
1681 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001682 A.addApproval('code-review', 2)
1683 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001684 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001685 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1686 self.assertEqual(self.builds[0].name, 'project-merge')
1687
1688 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1689 self.waitUntilSettled()
1690
Paul Belanger174a8272017-03-14 13:20:10 -04001691 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001692 self.waitUntilSettled()
1693
Arie5b4048c2016-10-05 16:12:06 +03001694 self.assertBuilds([])
1695 self.assertHistory([
1696 dict(name='project-merge', result='ABORTED', changes='1,1')],
1697 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001698 self.assertEqual(A.reported, 1,
1699 "Abandoned gate change should report only start")
1700
1701 def test_abandoned_check(self):
1702 "Test that an abandoned change is dequeued from check"
1703
Paul Belanger174a8272017-03-14 13:20:10 -04001704 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001705
1706 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1707 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001708 tenant = self.sched.abide.tenants.get('tenant-one')
1709 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001710
1711 # Add two git-dependent changes
1712 B.setDependsOn(A, 1)
1713 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1714 self.waitUntilSettled()
1715 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1716 self.waitUntilSettled()
1717 # A live item, and a non-live/live pair
1718 items = check_pipeline.getAllItems()
1719 self.assertEqual(len(items), 3)
1720
1721 self.assertEqual(items[0].change.number, '1')
1722 self.assertFalse(items[0].live)
1723
1724 self.assertEqual(items[1].change.number, '2')
1725 self.assertTrue(items[1].live)
1726
1727 self.assertEqual(items[2].change.number, '1')
1728 self.assertTrue(items[2].live)
1729
1730 # Abandon A
1731 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1732 self.waitUntilSettled()
1733
1734 # The live copy of A should be gone, but the non-live and B
1735 # should continue
1736 items = check_pipeline.getAllItems()
1737 self.assertEqual(len(items), 2)
1738
1739 self.assertEqual(items[0].change.number, '1')
1740 self.assertFalse(items[0].live)
1741
1742 self.assertEqual(items[1].change.number, '2')
1743 self.assertTrue(items[1].live)
1744
Paul Belanger174a8272017-03-14 13:20:10 -04001745 self.executor_server.hold_jobs_in_build = False
1746 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001747 self.waitUntilSettled()
1748
1749 self.assertEqual(len(self.history), 4)
1750 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001751 'Build should have been aborted')
1752 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001753 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001754
Steve Varnau7b78b312015-04-03 14:49:46 -07001755 def test_abandoned_not_timer(self):
1756 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001757 # This test can not use simple_layout because it must start
1758 # with a configuration which does not include a
1759 # timer-triggered job so that we have an opportunity to set
1760 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001761 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001762 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001763 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001764 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001765 # The pipeline triggers every second, so we should have seen
1766 # several by now.
1767 time.sleep(5)
1768 self.waitUntilSettled()
1769 # Stop queuing timer triggered jobs so that the assertions
1770 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001771 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001772 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001773 self.sched.reconfigure(self.config)
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001774
Steve Varnau7b78b312015-04-03 14:49:46 -07001775 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1776
1777 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1778 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1779 self.waitUntilSettled()
1780 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1781
1782 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1783 self.waitUntilSettled()
1784
1785 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1786
Paul Belanger174a8272017-03-14 13:20:10 -04001787 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001788 self.waitUntilSettled()
1789
Arx Cruzb1b010d2013-10-28 19:49:59 -02001790 def test_zuul_url_return(self):
1791 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001792 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Paul Belanger174a8272017-03-14 13:20:10 -04001793 self.executor_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001794
1795 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001796 A.addApproval('code-review', 2)
1797 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001798 self.waitUntilSettled()
1799
1800 self.assertEqual(len(self.builds), 1)
1801 for build in self.builds:
1802 self.assertTrue('ZUUL_URL' in build.parameters)
1803
Paul Belanger174a8272017-03-14 13:20:10 -04001804 self.executor_server.hold_jobs_in_build = False
1805 self.executor_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001806 self.waitUntilSettled()
1807
James E. Blair2fa50962013-01-30 21:50:41 -08001808 def test_new_patchset_dequeues_old_on_head(self):
1809 "Test that a new patchset causes the old to be dequeued (at head)"
1810 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001811 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001812 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1813 M.setMerged()
1814 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1815 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1816 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1817 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001818 A.addApproval('code-review', 2)
1819 B.addApproval('code-review', 2)
1820 C.addApproval('code-review', 2)
1821 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001822
1823 C.setDependsOn(B, 1)
1824 B.setDependsOn(A, 1)
1825 A.setDependsOn(M, 1)
1826
James E. Blair8b5408c2016-08-08 15:37:46 -07001827 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1828 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1829 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1830 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001831 self.waitUntilSettled()
1832
1833 A.addPatchset()
1834 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1835 self.waitUntilSettled()
1836
Paul Belanger174a8272017-03-14 13:20:10 -04001837 self.executor_server.hold_jobs_in_build = False
1838 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001839 self.waitUntilSettled()
1840
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001841 self.assertEqual(A.data['status'], 'NEW')
1842 self.assertEqual(A.reported, 2)
1843 self.assertEqual(B.data['status'], 'NEW')
1844 self.assertEqual(B.reported, 2)
1845 self.assertEqual(C.data['status'], 'NEW')
1846 self.assertEqual(C.reported, 2)
1847 self.assertEqual(D.data['status'], 'MERGED')
1848 self.assertEqual(D.reported, 2)
1849 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001850
1851 def test_new_patchset_dequeues_old_without_dependents(self):
1852 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001853 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001854 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1855 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1856 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001857 A.addApproval('code-review', 2)
1858 B.addApproval('code-review', 2)
1859 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001860
James E. Blair8b5408c2016-08-08 15:37:46 -07001861 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1862 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1863 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001864 self.waitUntilSettled()
1865
1866 B.addPatchset()
1867 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1868 self.waitUntilSettled()
1869
Paul Belanger174a8272017-03-14 13:20:10 -04001870 self.executor_server.hold_jobs_in_build = False
1871 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001872 self.waitUntilSettled()
1873
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001874 self.assertEqual(A.data['status'], 'MERGED')
1875 self.assertEqual(A.reported, 2)
1876 self.assertEqual(B.data['status'], 'NEW')
1877 self.assertEqual(B.reported, 2)
1878 self.assertEqual(C.data['status'], 'MERGED')
1879 self.assertEqual(C.reported, 2)
1880 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001881
1882 def test_new_patchset_dequeues_old_independent_queue(self):
1883 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001884 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001885 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1886 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1887 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1888 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1889 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1890 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1891 self.waitUntilSettled()
1892
1893 B.addPatchset()
1894 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1895 self.waitUntilSettled()
1896
Paul Belanger174a8272017-03-14 13:20:10 -04001897 self.executor_server.hold_jobs_in_build = False
1898 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001899 self.waitUntilSettled()
1900
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001901 self.assertEqual(A.data['status'], 'NEW')
1902 self.assertEqual(A.reported, 1)
1903 self.assertEqual(B.data['status'], 'NEW')
1904 self.assertEqual(B.reported, 1)
1905 self.assertEqual(C.data['status'], 'NEW')
1906 self.assertEqual(C.reported, 1)
1907 self.assertEqual(len(self.history), 10)
1908 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001909
James E. Blaircc020532017-04-19 13:04:44 -07001910 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07001911 def test_noop_job(self):
1912 "Test that the internal noop job works"
1913 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001914 A.addApproval('code-review', 2)
1915 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001916 self.waitUntilSettled()
1917
1918 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1919 self.assertTrue(self.sched._areAllBuildsComplete())
1920 self.assertEqual(len(self.history), 0)
1921 self.assertEqual(A.data['status'], 'MERGED')
1922 self.assertEqual(A.reported, 2)
1923
James E. Blair06cc3922017-04-19 10:08:10 -07001924 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001925 def test_no_job_project(self):
1926 "Test that reports with no jobs don't get sent"
1927 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1928 'master', 'A')
1929 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1930 self.waitUntilSettled()
1931
1932 # Change wasn't reported to
1933 self.assertEqual(A.reported, False)
1934
1935 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001936 tenant = self.sched.abide.tenants.get('tenant-one')
1937 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001938 items = check_pipeline.getAllItems()
1939 self.assertEqual(len(items), 0)
1940
1941 self.assertEqual(len(self.history), 0)
1942
James E. Blair7d0dedc2013-02-21 17:26:09 -08001943 def test_zuul_refs(self):
1944 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04001945 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001946 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1947 M1.setMerged()
1948 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1949 M2.setMerged()
1950
1951 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1952 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1953 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1954 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001955 A.addApproval('code-review', 2)
1956 B.addApproval('code-review', 2)
1957 C.addApproval('code-review', 2)
1958 D.addApproval('code-review', 2)
1959 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1960 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1961 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1962 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001963
1964 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001965 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001966 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001967 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001968 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001969 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001970 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001971 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001972 self.waitUntilSettled()
1973
James E. Blair7d0dedc2013-02-21 17:26:09 -08001974 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08001975 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001976 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001977 if x.parameters['ZUUL_CHANGE'] == '3':
1978 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001979 a_build = x
1980 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001981 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001982 b_build = x
1983 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001984 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001985 c_build = x
1986 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001987 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001988 d_build = x
1989 if a_build and b_build and c_build and d_build:
1990 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08001991
1992 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001993 self.assertIsNotNone(a_zref)
1994 self.assertIsNotNone(b_zref)
1995 self.assertIsNotNone(c_zref)
1996 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001997
1998 # And they should all be different
1999 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002000 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002001
Clint Byrum3343e3e2016-11-15 16:05:03 -08002002 # should have a, not b, and should not be in project2
2003 self.assertTrue(a_build.hasChanges(A))
2004 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002005
Clint Byrum3343e3e2016-11-15 16:05:03 -08002006 # should have a and b, and should not be in project2
2007 self.assertTrue(b_build.hasChanges(A, B))
2008 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002009
Clint Byrum3343e3e2016-11-15 16:05:03 -08002010 # should have a and b in 1, c in 2
2011 self.assertTrue(c_build.hasChanges(A, B, C))
2012 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002013
Clint Byrum3343e3e2016-11-15 16:05:03 -08002014 # should have a and b in 1, c and d in 2
2015 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002016
Paul Belanger174a8272017-03-14 13:20:10 -04002017 self.executor_server.hold_jobs_in_build = False
2018 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002019 self.waitUntilSettled()
2020
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002021 self.assertEqual(A.data['status'], 'MERGED')
2022 self.assertEqual(A.reported, 2)
2023 self.assertEqual(B.data['status'], 'MERGED')
2024 self.assertEqual(B.reported, 2)
2025 self.assertEqual(C.data['status'], 'MERGED')
2026 self.assertEqual(C.reported, 2)
2027 self.assertEqual(D.data['status'], 'MERGED')
2028 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002029
James E. Blair4a28a882013-08-23 15:17:33 -07002030 def test_rerun_on_error(self):
2031 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002032 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002033 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002034 A.addApproval('code-review', 2)
2035 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002036 self.waitUntilSettled()
2037
James E. Blair412fba82017-01-26 15:00:50 -08002038 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002039 self.executor_server.hold_jobs_in_build = False
2040 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002041 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002042 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002043 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2044
James E. Blair412e5582013-04-22 15:50:12 -07002045 def test_statsd(self):
2046 "Test each of the statsd methods used in the scheduler"
2047 import extras
2048 statsd = extras.try_import('statsd.statsd')
2049 statsd.incr('test-incr')
2050 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002051 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002052 self.assertReportedStat('test-incr', '1|c')
2053 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002054 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002055
James E. Blairdad52252014-02-07 16:59:17 -08002056 def test_stuck_job_cleanup(self):
2057 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002058
2059 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002060 self.gearman_server.hold_jobs_in_queue = True
2061 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002062 A.addApproval('code-review', 2)
2063 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002064 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002065 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002066 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2067
James E. Blair31cb3e82017-04-20 13:08:56 -07002068 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002069 self.sched.reconfigure(self.config)
2070 self.waitUntilSettled()
2071
James E. Blair18c64442014-03-18 10:14:45 -07002072 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002073 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002074 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002075 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2076 self.assertTrue(self.sched._areAllBuildsComplete())
2077
2078 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002079 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002080 self.assertEqual(self.history[0].result, 'SUCCESS')
2081
James E. Blair879dafb2015-07-17 14:04:49 -07002082 def test_file_head(self):
2083 # This is a regression test for an observed bug. A change
2084 # with a file named "HEAD" in the root directory of the repo
2085 # was processed by a merger. It then was unable to reset the
2086 # repo because of:
2087 # GitCommandError: 'git reset --hard HEAD' returned
2088 # with exit code 128
2089 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2090 # and filename
2091 # Use '--' to separate filenames from revisions'
2092
2093 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002094 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002095 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2096
2097 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2098 self.waitUntilSettled()
2099
2100 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2101 self.waitUntilSettled()
2102
2103 self.assertIn('Build succeeded', A.messages[0])
2104 self.assertIn('Build succeeded', B.messages[0])
2105
James E. Blair70c71582013-03-06 08:50:50 -08002106 def test_file_jobs(self):
2107 "Test that file jobs run only when appropriate"
2108 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002109 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002110 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002111 A.addApproval('code-review', 2)
2112 B.addApproval('code-review', 2)
2113 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2114 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002115 self.waitUntilSettled()
2116
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002117 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002118 if x.name == 'project-testfile']
2119
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002120 self.assertEqual(len(testfile_jobs), 1)
2121 self.assertEqual(testfile_jobs[0].changes, '1,2')
2122 self.assertEqual(A.data['status'], 'MERGED')
2123 self.assertEqual(A.reported, 2)
2124 self.assertEqual(B.data['status'], 'MERGED')
2125 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002126
Clint Byrum3ee39f32016-11-17 23:45:07 -08002127 def _test_irrelevant_files_jobs(self, should_skip):
2128 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002129 if should_skip:
2130 files = {'ignoreme': 'ignored\n'}
2131 else:
2132 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002133
2134 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002135 'master',
2136 'test irrelevant-files',
2137 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002138 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2139 self.waitUntilSettled()
2140
2141 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002142 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002143
2144 if should_skip:
2145 self.assertEqual([], tested_change_ids)
2146 else:
2147 self.assertIn(change.data['number'], tested_change_ids)
2148
James E. Blaircc324192017-04-19 14:36:08 -07002149 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002150 def test_irrelevant_files_match_skips_job(self):
2151 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002152
James E. Blaircc324192017-04-19 14:36:08 -07002153 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002154 def test_irrelevant_files_no_match_runs_job(self):
2155 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002156
James E. Blaircc324192017-04-19 14:36:08 -07002157 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002158 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002159 files = {'ignoreme': 'ignored\n'}
2160
2161 change = self.fake_gerrit.addFakeChange('org/project',
2162 'master',
2163 'test irrelevant-files',
2164 files=files)
2165 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2166 self.waitUntilSettled()
2167
2168 run_jobs = set([build.name for build in self.history])
2169
2170 self.assertEqual(set(['project-test-nomatch-starts-empty',
2171 'project-test-nomatch-starts-full']), run_jobs)
2172
James E. Blairc8a1e052014-02-25 09:29:26 -08002173 def test_queue_names(self):
2174 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002175 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002176 (trusted, project1) = tenant.getProject('org/project1')
2177 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002178 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2179 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2180 self.assertEqual(q1.name, 'integrated')
2181 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002182
James E. Blair64ed6f22013-07-10 14:07:23 -07002183 def test_queue_precedence(self):
2184 "Test that queue precedence works"
2185
2186 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002187 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002188 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2189 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002190 A.addApproval('code-review', 2)
2191 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002192
2193 self.waitUntilSettled()
2194 self.gearman_server.hold_jobs_in_queue = False
2195 self.gearman_server.release()
2196 self.waitUntilSettled()
2197
James E. Blair8de58bd2013-07-18 16:23:33 -07002198 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002199 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002200 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002201 self.waitUntilSettled()
2202
James E. Blair64ed6f22013-07-10 14:07:23 -07002203 self.log.debug(self.history)
2204 self.assertEqual(self.history[0].pipeline, 'gate')
2205 self.assertEqual(self.history[1].pipeline, 'check')
2206 self.assertEqual(self.history[2].pipeline, 'gate')
2207 self.assertEqual(self.history[3].pipeline, 'gate')
2208 self.assertEqual(self.history[4].pipeline, 'check')
2209 self.assertEqual(self.history[5].pipeline, 'check')
2210
Clark Boylana5edbe42014-06-03 16:39:10 -07002211 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002212 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002213 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002214 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002215 A.addApproval('code-review', 2)
2216 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002217 self.waitUntilSettled()
2218
Paul Belanger174a8272017-03-14 13:20:10 -04002219 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002220 self.waitUntilSettled()
2221
James E. Blair1843a552013-07-03 14:19:52 -07002222 port = self.webapp.server.socket.getsockname()[1]
2223
Adam Gandelman77a12c72017-02-03 17:43:43 -08002224 req = urllib.request.Request(
2225 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002226 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002227 headers = f.info()
2228 self.assertIn('Content-Length', headers)
2229 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002230 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2231 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002232 self.assertIn('Access-Control-Allow-Origin', headers)
2233 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002234 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002235 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002236 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002237
Paul Belanger174a8272017-03-14 13:20:10 -04002238 self.executor_server.hold_jobs_in_build = False
2239 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002240 self.waitUntilSettled()
2241
2242 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002243 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002244 for p in data['pipelines']:
2245 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002246 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002247 self.assertEqual(q['window'], 20)
2248 else:
2249 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002250 for head in q['heads']:
2251 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002252 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002253 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002254 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002255 status_jobs.append(job)
2256 self.assertEqual('project-merge', status_jobs[0]['name'])
2257 self.assertEqual('https://server/job/project-merge/0/',
2258 status_jobs[0]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002259 self.assertEqual('https://server/job/project-merge/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002260 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002261 self.assertEqual('project-test1', status_jobs[1]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002262 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002263 status_jobs[1]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002264 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002265 status_jobs[1]['report_url'])
2266
2267 self.assertEqual('project-test2', status_jobs[2]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002268 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002269 status_jobs[2]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002270 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002271 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002272
James E. Blaircdccd972013-07-01 12:10:22 -07002273 def test_live_reconfiguration(self):
2274 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002275 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002276 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002277 A.addApproval('code-review', 2)
2278 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002279 self.waitUntilSettled()
2280
2281 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002282 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002283
Paul Belanger174a8272017-03-14 13:20:10 -04002284 self.executor_server.hold_jobs_in_build = False
2285 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002286 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002287 self.assertEqual(self.getJobFromHistory('project-merge').result,
2288 'SUCCESS')
2289 self.assertEqual(self.getJobFromHistory('project-test1').result,
2290 'SUCCESS')
2291 self.assertEqual(self.getJobFromHistory('project-test2').result,
2292 'SUCCESS')
2293 self.assertEqual(A.data['status'], 'MERGED')
2294 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002295
James E. Blair6bc782d2015-07-17 16:20:21 -07002296 def test_live_reconfiguration_merge_conflict(self):
2297 # A real-world bug: a change in a gate queue has a merge
2298 # conflict and a job is added to its project while it's
2299 # sitting in the queue. The job gets added to the change and
2300 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002301 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002302
2303 # This change is fine. It's here to stop the queue long
2304 # enough for the next change to be subject to the
2305 # reconfiguration, as well as to provide a conflict for the
2306 # next change. This change will succeed and merge.
2307 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002308 A.addPatchset({'conflict': 'A'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002309 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002310
2311 # This change will be in merge conflict. During the
2312 # reconfiguration, we will add a job. We want to make sure
2313 # that doesn't cause it to get stuck.
2314 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002315 B.addPatchset({'conflict': 'B'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002316 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002317
James E. Blair8b5408c2016-08-08 15:37:46 -07002318 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2319 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002320
2321 self.waitUntilSettled()
2322
2323 # No jobs have run yet
2324 self.assertEqual(A.data['status'], 'NEW')
2325 self.assertEqual(A.reported, 1)
2326 self.assertEqual(B.data['status'], 'NEW')
2327 self.assertEqual(B.reported, 1)
2328 self.assertEqual(len(self.history), 0)
2329
2330 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002331 self.commitConfigUpdate('common-config',
2332 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002333 self.sched.reconfigure(self.config)
2334 self.waitUntilSettled()
2335
Paul Belanger174a8272017-03-14 13:20:10 -04002336 self.executor_server.hold_jobs_in_build = False
2337 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002338 self.waitUntilSettled()
2339
2340 self.assertEqual(A.data['status'], 'MERGED')
2341 self.assertEqual(A.reported, 2)
2342 self.assertEqual(B.data['status'], 'NEW')
2343 self.assertEqual(B.reported, 2)
2344 self.assertEqual(self.getJobFromHistory('project-merge').result,
2345 'SUCCESS')
2346 self.assertEqual(self.getJobFromHistory('project-test1').result,
2347 'SUCCESS')
2348 self.assertEqual(self.getJobFromHistory('project-test2').result,
2349 'SUCCESS')
2350 self.assertEqual(self.getJobFromHistory('project-test3').result,
2351 'SUCCESS')
2352 self.assertEqual(len(self.history), 4)
2353
James E. Blair400e8fd2015-07-30 17:44:45 -07002354 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002355 # An extrapolation of test_live_reconfiguration_merge_conflict
2356 # that tests a job added to a job tree with a failed root does
2357 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002358 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002359
2360 # This change is fine. It's here to stop the queue long
2361 # enough for the next change to be subject to the
2362 # reconfiguration. This change will succeed and merge.
2363 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002364 A.addPatchset({'conflict': 'A'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002365 A.addApproval('code-review', 2)
2366 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002367 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002368 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002369 self.waitUntilSettled()
2370
2371 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002372 self.executor_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002373 B.addApproval('code-review', 2)
2374 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002375 self.waitUntilSettled()
2376
Paul Belanger174a8272017-03-14 13:20:10 -04002377 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002378 self.waitUntilSettled()
2379
2380 # Both -merge jobs have run, but no others.
2381 self.assertEqual(A.data['status'], 'NEW')
2382 self.assertEqual(A.reported, 1)
2383 self.assertEqual(B.data['status'], 'NEW')
2384 self.assertEqual(B.reported, 1)
2385 self.assertEqual(self.history[0].result, 'SUCCESS')
2386 self.assertEqual(self.history[0].name, 'project-merge')
2387 self.assertEqual(self.history[1].result, 'FAILURE')
2388 self.assertEqual(self.history[1].name, 'project-merge')
2389 self.assertEqual(len(self.history), 2)
2390
2391 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002392 self.commitConfigUpdate('common-config',
2393 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002394 self.sched.reconfigure(self.config)
2395 self.waitUntilSettled()
2396
Paul Belanger174a8272017-03-14 13:20:10 -04002397 self.executor_server.hold_jobs_in_build = False
2398 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002399 self.waitUntilSettled()
2400
2401 self.assertEqual(A.data['status'], 'MERGED')
2402 self.assertEqual(A.reported, 2)
2403 self.assertEqual(B.data['status'], 'NEW')
2404 self.assertEqual(B.reported, 2)
2405 self.assertEqual(self.history[0].result, 'SUCCESS')
2406 self.assertEqual(self.history[0].name, 'project-merge')
2407 self.assertEqual(self.history[1].result, 'FAILURE')
2408 self.assertEqual(self.history[1].name, 'project-merge')
2409 self.assertEqual(self.history[2].result, 'SUCCESS')
2410 self.assertEqual(self.history[3].result, 'SUCCESS')
2411 self.assertEqual(self.history[4].result, 'SUCCESS')
2412 self.assertEqual(len(self.history), 5)
2413
James E. Blair400e8fd2015-07-30 17:44:45 -07002414 def test_live_reconfiguration_failed_job(self):
2415 # Test that a change with a removed failing job does not
2416 # disrupt reconfiguration. If a change has a failed job and
2417 # that job is removed during a reconfiguration, we observed a
2418 # bug where the code to re-set build statuses would run on
2419 # that build and raise an exception because the job no longer
2420 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002421 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002422
2423 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2424
2425 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002426 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002427
2428 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2429 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002430 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002431 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002432 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002433 self.waitUntilSettled()
2434
2435 self.assertEqual(A.data['status'], 'NEW')
2436 self.assertEqual(A.reported, 0)
2437
2438 self.assertEqual(self.getJobFromHistory('project-merge').result,
2439 'SUCCESS')
2440 self.assertEqual(self.getJobFromHistory('project-test1').result,
2441 'FAILURE')
2442 self.assertEqual(len(self.history), 2)
2443
2444 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002445 self.commitConfigUpdate('common-config',
2446 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002447 self.sched.reconfigure(self.config)
2448 self.waitUntilSettled()
2449
Paul Belanger174a8272017-03-14 13:20:10 -04002450 self.executor_server.hold_jobs_in_build = False
2451 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002452 self.waitUntilSettled()
2453
2454 self.assertEqual(self.getJobFromHistory('project-test2').result,
2455 'SUCCESS')
2456 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2457 'SUCCESS')
2458 self.assertEqual(len(self.history), 4)
2459
2460 self.assertEqual(A.data['status'], 'NEW')
2461 self.assertEqual(A.reported, 1)
2462 self.assertIn('Build succeeded', A.messages[0])
2463 # Ensure the removed job was not included in the report.
2464 self.assertNotIn('project-test1', A.messages[0])
2465
James E. Blairfe707d12015-08-05 15:18:15 -07002466 def test_live_reconfiguration_shared_queue(self):
2467 # Test that a change with a failing job which was removed from
2468 # this project but otherwise still exists in the system does
2469 # not disrupt reconfiguration.
2470
Paul Belanger174a8272017-03-14 13:20:10 -04002471 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002472
2473 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2474
Paul Belanger174a8272017-03-14 13:20:10 -04002475 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002476
2477 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2478 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002479 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002480 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002481 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002482 self.waitUntilSettled()
2483
2484 self.assertEqual(A.data['status'], 'NEW')
2485 self.assertEqual(A.reported, 0)
2486
Clint Byrum69663d42017-05-01 14:51:04 -07002487 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002488 'SUCCESS')
2489 self.assertEqual(self.getJobFromHistory(
2490 'project1-project2-integration').result, 'FAILURE')
2491 self.assertEqual(len(self.history), 2)
2492
2493 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002494 self.commitConfigUpdate(
2495 'common-config',
2496 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002497 self.sched.reconfigure(self.config)
2498 self.waitUntilSettled()
2499
Paul Belanger174a8272017-03-14 13:20:10 -04002500 self.executor_server.hold_jobs_in_build = False
2501 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002502 self.waitUntilSettled()
2503
Clint Byrum69663d42017-05-01 14:51:04 -07002504 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002505 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002506 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002507 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002508 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002509 'SUCCESS')
2510 self.assertEqual(self.getJobFromHistory(
2511 'project1-project2-integration').result, 'FAILURE')
2512 self.assertEqual(len(self.history), 4)
2513
2514 self.assertEqual(A.data['status'], 'NEW')
2515 self.assertEqual(A.reported, 1)
2516 self.assertIn('Build succeeded', A.messages[0])
2517 # Ensure the removed job was not included in the report.
2518 self.assertNotIn('project1-project2-integration', A.messages[0])
2519
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002520 def test_double_live_reconfiguration_shared_queue(self):
2521 # This was a real-world regression. A change is added to
2522 # gate; a reconfigure happens, a second change which depends
2523 # on the first is added, and a second reconfiguration happens.
2524 # Ensure that both changes merge.
2525
2526 # A failure may indicate incorrect caching or cleaning up of
2527 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002528 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002529
2530 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2531 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2532 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002533 A.addApproval('code-review', 2)
2534 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002535
2536 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002537 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002538 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002539 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002540 self.waitUntilSettled()
2541
2542 # Reconfigure (with only one change in the pipeline).
2543 self.sched.reconfigure(self.config)
2544 self.waitUntilSettled()
2545
2546 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002547 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002548 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002549 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002550 self.waitUntilSettled()
2551
2552 # Reconfigure (with both in the pipeline).
2553 self.sched.reconfigure(self.config)
2554 self.waitUntilSettled()
2555
Paul Belanger174a8272017-03-14 13:20:10 -04002556 self.executor_server.hold_jobs_in_build = False
2557 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002558 self.waitUntilSettled()
2559
2560 self.assertEqual(len(self.history), 8)
2561
2562 self.assertEqual(A.data['status'], 'MERGED')
2563 self.assertEqual(A.reported, 2)
2564 self.assertEqual(B.data['status'], 'MERGED')
2565 self.assertEqual(B.reported, 2)
2566
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002567 def test_live_reconfiguration_del_project(self):
2568 # Test project deletion from layout
2569 # while changes are enqueued
2570
Paul Belanger174a8272017-03-14 13:20:10 -04002571 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002572 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2573 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2574 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2575
2576 # A Depends-On: B
2577 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2578 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002579 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002580
2581 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2582 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2583 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002584 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002585 self.waitUntilSettled()
2586 self.assertEqual(len(self.builds), 5)
2587
2588 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002589 self.commitConfigUpdate(
2590 'common-config',
2591 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002592 self.sched.reconfigure(self.config)
2593 self.waitUntilSettled()
2594
2595 # Builds for C aborted, builds for A succeed,
2596 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002597 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002598 self.assertEqual(job_c.changes, '3,1')
2599 self.assertEqual(job_c.result, 'ABORTED')
2600
Paul Belanger174a8272017-03-14 13:20:10 -04002601 self.executor_server.hold_jobs_in_build = False
2602 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002603 self.waitUntilSettled()
2604
Clint Byrum0149ada2017-04-04 12:22:01 -07002605 self.assertEqual(
2606 self.getJobFromHistory('project-test1', 'org/project').changes,
2607 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002608
2609 self.assertEqual(A.data['status'], 'NEW')
2610 self.assertEqual(B.data['status'], 'NEW')
2611 self.assertEqual(C.data['status'], 'NEW')
2612 self.assertEqual(A.reported, 1)
2613 self.assertEqual(B.reported, 0)
2614 self.assertEqual(C.reported, 0)
2615
Clint Byrum0149ada2017-04-04 12:22:01 -07002616 tenant = self.sched.abide.tenants.get('tenant-one')
2617 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002618 self.assertIn('Build succeeded', A.messages[0])
2619
James E. Blairec056492016-07-22 09:45:56 -07002620 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002621 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002622 self.updateConfigLayout(
2623 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002624 self.sched.reconfigure(self.config)
2625
2626 self.init_repo("org/new-project")
2627 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2628
James E. Blair8b5408c2016-08-08 15:37:46 -07002629 A.addApproval('code-review', 2)
2630 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002631 self.waitUntilSettled()
2632 self.assertEqual(self.getJobFromHistory('project-merge').result,
2633 'SUCCESS')
2634 self.assertEqual(self.getJobFromHistory('project-test1').result,
2635 'SUCCESS')
2636 self.assertEqual(self.getJobFromHistory('project-test2').result,
2637 'SUCCESS')
2638 self.assertEqual(A.data['status'], 'MERGED')
2639 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002640
James E. Blair9ea70072017-04-19 16:05:30 -07002641 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002642 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002643 self.init_repo("org/delete-project")
2644 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2645
James E. Blair8b5408c2016-08-08 15:37:46 -07002646 A.addApproval('code-review', 2)
2647 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002648 self.waitUntilSettled()
2649 self.assertEqual(self.getJobFromHistory('project-merge').result,
2650 'SUCCESS')
2651 self.assertEqual(self.getJobFromHistory('project-test1').result,
2652 'SUCCESS')
2653 self.assertEqual(self.getJobFromHistory('project-test2').result,
2654 'SUCCESS')
2655 self.assertEqual(A.data['status'], 'MERGED')
2656 self.assertEqual(A.reported, 2)
2657
2658 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002659 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002660 if os.path.exists(os.path.join(self.merger_src_root, p)):
2661 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002662 if os.path.exists(os.path.join(self.executor_src_root, p)):
2663 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002664
2665 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2666
James E. Blair8b5408c2016-08-08 15:37:46 -07002667 B.addApproval('code-review', 2)
2668 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002669 self.waitUntilSettled()
2670 self.assertEqual(self.getJobFromHistory('project-merge').result,
2671 'SUCCESS')
2672 self.assertEqual(self.getJobFromHistory('project-test1').result,
2673 'SUCCESS')
2674 self.assertEqual(self.getJobFromHistory('project-test2').result,
2675 'SUCCESS')
2676 self.assertEqual(B.data['status'], 'MERGED')
2677 self.assertEqual(B.reported, 2)
2678
James E. Blaire68d43f2017-04-19 14:03:30 -07002679 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002680 def test_tags(self):
2681 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002682 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2683 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2684 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2685 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2686 self.waitUntilSettled()
2687
James E. Blaire68d43f2017-04-19 14:03:30 -07002688 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002689
2690 results = {self.getJobFromHistory('merge',
2691 project='org/project1').uuid: 'extratag merge',
2692 self.getJobFromHistory('merge',
2693 project='org/project2').uuid: 'merge'}
James E. Blair456f2fb2016-02-09 09:29:33 -08002694
2695 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002696 self.assertEqual(results.get(build.uuid, ''),
2697 build.parameters['vars']['zuul'].get('tags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002698
James E. Blair63bb0ef2013-07-29 17:14:51 -07002699 def test_timer(self):
2700 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002701 # This test can not use simple_layout because it must start
2702 # with a configuration which does not include a
2703 # timer-triggered job so that we have an opportunity to set
2704 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002705 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002706 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002707 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002708
Clark Boylan3ee090a2014-04-03 20:55:09 -07002709 # The pipeline triggers every second, so we should have seen
2710 # several by now.
2711 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002712 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002713
2714 self.assertEqual(len(self.builds), 2)
2715
James E. Blair63bb0ef2013-07-29 17:14:51 -07002716 port = self.webapp.server.socket.getsockname()[1]
2717
Clint Byrum5e729e92017-02-08 06:34:57 -08002718 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002719 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002720 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002721 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002722
Paul Belanger174a8272017-03-14 13:20:10 -04002723 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002724 # Stop queuing timer triggered jobs so that the assertions
2725 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002726 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002727 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04002728 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002729 self.waitUntilSettled()
2730
2731 self.assertEqual(self.getJobFromHistory(
2732 'project-bitrot-stable-old').result, 'SUCCESS')
2733 self.assertEqual(self.getJobFromHistory(
2734 'project-bitrot-stable-older').result, 'SUCCESS')
2735
2736 data = json.loads(data)
2737 status_jobs = set()
2738 for p in data['pipelines']:
2739 for q in p['change_queues']:
2740 for head in q['heads']:
2741 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002742 for job in change['jobs']:
2743 status_jobs.add(job['name'])
2744 self.assertIn('project-bitrot-stable-old', status_jobs)
2745 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002746
James E. Blair4f6033c2014-03-27 15:49:09 -07002747 def test_idle(self):
2748 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002749 # This test can not use simple_layout because it must start
2750 # with a configuration which does not include a
2751 # timer-triggered job so that we have an opportunity to set
2752 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002753 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002754
Clark Boylan3ee090a2014-04-03 20:55:09 -07002755 for x in range(1, 3):
2756 # Test that timer triggers periodic jobs even across
2757 # layout config reloads.
2758 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002759 self.commitConfigUpdate('common-config',
2760 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002761 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002762 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002763
Clark Boylan3ee090a2014-04-03 20:55:09 -07002764 # The pipeline triggers every second, so we should have seen
2765 # several by now.
2766 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002767
2768 # Stop queuing timer triggered jobs so that the assertions
2769 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002770 self.commitConfigUpdate('common-config',
2771 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002772 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002773 self.waitUntilSettled()
Clint Byrum58264dc2017-02-07 21:21:22 -08002774 self.assertEqual(len(self.builds), 2,
2775 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002776 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002777 self.waitUntilSettled()
2778 self.assertEqual(len(self.builds), 0)
2779 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002780
James E. Blaircc324192017-04-19 14:36:08 -07002781 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002782 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002783 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2784 self.waitUntilSettled()
2785
2786 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2787 self.waitUntilSettled()
2788
James E. Blairff80a2f2013-12-27 13:24:06 -08002789 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002790
2791 # A.messages only holds what FakeGerrit places in it. Thus we
2792 # work on the knowledge of what the first message should be as
2793 # it is only configured to go to SMTP.
2794
2795 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002796 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002797 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002798 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002799 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002800 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002801
2802 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002803 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002804 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002805 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002806 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002807 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002808
James E. Blaire5910202013-12-27 09:50:31 -08002809 def test_timer_smtp(self):
2810 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002811 # This test can not use simple_layout because it must start
2812 # with a configuration which does not include a
2813 # timer-triggered job so that we have an opportunity to set
2814 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002815 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002816 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002817 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002818
Clark Boylan3ee090a2014-04-03 20:55:09 -07002819 # The pipeline triggers every second, so we should have seen
2820 # several by now.
2821 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002822 self.waitUntilSettled()
2823
Clark Boylan3ee090a2014-04-03 20:55:09 -07002824 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04002825 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002826 self.waitUntilSettled()
2827 self.assertEqual(len(self.history), 2)
2828
James E. Blaire5910202013-12-27 09:50:31 -08002829 self.assertEqual(self.getJobFromHistory(
2830 'project-bitrot-stable-old').result, 'SUCCESS')
2831 self.assertEqual(self.getJobFromHistory(
2832 'project-bitrot-stable-older').result, 'SUCCESS')
2833
James E. Blairff80a2f2013-12-27 13:24:06 -08002834 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002835
2836 # A.messages only holds what FakeGerrit places in it. Thus we
2837 # work on the knowledge of what the first message should be as
2838 # it is only configured to go to SMTP.
2839
2840 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002841 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002842 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002843 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002844 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002845 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002846
Clark Boylan3ee090a2014-04-03 20:55:09 -07002847 # Stop queuing timer triggered jobs and let any that may have
2848 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002849 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002850 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07002851 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002852 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002853 self.waitUntilSettled()
2854
Joshua Hesketh25695cb2017-03-06 12:50:04 +11002855 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00002856 def test_timer_sshkey(self):
2857 "Test that a periodic job can setup SSH key authentication"
2858 self.worker.hold_jobs_in_build = True
2859 self.config.set('zuul', 'layout_config',
2860 'tests/fixtures/layout-timer.yaml')
2861 self.sched.reconfigure(self.config)
2862 self.registerJobs()
2863
2864 # The pipeline triggers every second, so we should have seen
2865 # several by now.
2866 time.sleep(5)
2867 self.waitUntilSettled()
2868
2869 self.assertEqual(len(self.builds), 2)
2870
2871 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
2872 self.assertTrue(os.path.isfile(ssh_wrapper))
2873 with open(ssh_wrapper) as f:
2874 ssh_wrapper_content = f.read()
2875 self.assertIn("fake_id_rsa", ssh_wrapper_content)
2876 # In the unit tests Merger runs in the same process,
2877 # so we see its' environment variables
2878 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
2879
2880 self.worker.release('.*')
2881 self.waitUntilSettled()
2882 self.assertEqual(len(self.history), 2)
2883
2884 self.assertEqual(self.getJobFromHistory(
2885 'project-bitrot-stable-old').result, 'SUCCESS')
2886 self.assertEqual(self.getJobFromHistory(
2887 'project-bitrot-stable-older').result, 'SUCCESS')
2888
2889 # Stop queuing timer triggered jobs and let any that may have
2890 # queued through so that end of test assertions pass.
2891 self.config.set('zuul', 'layout_config',
2892 'tests/fixtures/layout-no-timer.yaml')
2893 self.sched.reconfigure(self.config)
2894 self.registerJobs()
2895 self.waitUntilSettled()
2896 self.worker.release('.*')
2897 self.waitUntilSettled()
2898
James E. Blair91e34592015-07-31 16:45:59 -07002899 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08002900 "Test that the RPC client can enqueue a change"
2901 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002902 A.addApproval('code-review', 2)
2903 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08002904
2905 client = zuul.rpcclient.RPCClient('127.0.0.1',
2906 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002907 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04002908 r = client.enqueue(tenant='tenant-one',
2909 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002910 project='org/project',
2911 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002912 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002913 self.waitUntilSettled()
2914 self.assertEqual(self.getJobFromHistory('project-merge').result,
2915 'SUCCESS')
2916 self.assertEqual(self.getJobFromHistory('project-test1').result,
2917 'SUCCESS')
2918 self.assertEqual(self.getJobFromHistory('project-test2').result,
2919 'SUCCESS')
2920 self.assertEqual(A.data['status'], 'MERGED')
2921 self.assertEqual(A.reported, 2)
2922 self.assertEqual(r, True)
2923
James E. Blair91e34592015-07-31 16:45:59 -07002924 def test_client_enqueue_ref(self):
2925 "Test that the RPC client can enqueue a ref"
2926
2927 client = zuul.rpcclient.RPCClient('127.0.0.1',
2928 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002929 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07002930 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04002931 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07002932 pipeline='post',
2933 project='org/project',
2934 trigger='gerrit',
2935 ref='master',
2936 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
2937 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
2938 self.waitUntilSettled()
2939 job_names = [x.name for x in self.history]
2940 self.assertEqual(len(self.history), 1)
2941 self.assertIn('project-post', job_names)
2942 self.assertEqual(r, True)
2943
James E. Blairad28e912013-11-27 10:43:22 -08002944 def test_client_enqueue_negative(self):
2945 "Test that the RPC client returns errors"
2946 client = zuul.rpcclient.RPCClient('127.0.0.1',
2947 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002948 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08002949 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04002950 "Invalid tenant"):
2951 r = client.enqueue(tenant='tenant-foo',
2952 pipeline='gate',
2953 project='org/project',
2954 trigger='gerrit',
2955 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04002956 self.assertEqual(r, False)
2957
2958 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08002959 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002960 r = client.enqueue(tenant='tenant-one',
2961 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002962 project='project-does-not-exist',
2963 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002964 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002965 self.assertEqual(r, False)
2966
2967 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2968 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002969 r = client.enqueue(tenant='tenant-one',
2970 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08002971 project='org/project',
2972 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002973 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002974 self.assertEqual(r, False)
2975
2976 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2977 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002978 r = client.enqueue(tenant='tenant-one',
2979 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002980 project='org/project',
2981 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08002982 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002983 self.assertEqual(r, False)
2984
2985 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2986 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002987 r = client.enqueue(tenant='tenant-one',
2988 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002989 project='org/project',
2990 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002991 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002992 self.assertEqual(r, False)
2993
2994 self.waitUntilSettled()
2995 self.assertEqual(len(self.history), 0)
2996 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08002997
2998 def test_client_promote(self):
2999 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003000 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003001 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3002 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3003 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003004 A.addApproval('code-review', 2)
3005 B.addApproval('code-review', 2)
3006 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003007
James E. Blair8b5408c2016-08-08 15:37:46 -07003008 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3009 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3010 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003011
3012 self.waitUntilSettled()
3013
Paul Belangerbaca3132016-11-04 12:49:54 -04003014 tenant = self.sched.abide.tenants.get('tenant-one')
3015 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003016 enqueue_times = {}
3017 for item in items:
3018 enqueue_times[str(item.change)] = item.enqueue_time
3019
James E. Blair36658cf2013-12-06 17:53:48 -08003020 client = zuul.rpcclient.RPCClient('127.0.0.1',
3021 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003022 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003023 r = client.promote(tenant='tenant-one',
3024 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003025 change_ids=['2,1', '3,1'])
3026
Sean Daguef39b9ca2014-01-10 21:34:35 -05003027 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003028 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003029 for item in items:
3030 self.assertEqual(
3031 enqueue_times[str(item.change)], item.enqueue_time)
3032
James E. Blair78acec92014-02-06 07:11:32 -08003033 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003034 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003035 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003036 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -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()
3040
3041 self.assertEqual(len(self.builds), 6)
3042 self.assertEqual(self.builds[0].name, 'project-test1')
3043 self.assertEqual(self.builds[1].name, 'project-test2')
3044 self.assertEqual(self.builds[2].name, 'project-test1')
3045 self.assertEqual(self.builds[3].name, 'project-test2')
3046 self.assertEqual(self.builds[4].name, 'project-test1')
3047 self.assertEqual(self.builds[5].name, 'project-test2')
3048
Paul Belangerbaca3132016-11-04 12:49:54 -04003049 self.assertTrue(self.builds[0].hasChanges(B))
3050 self.assertFalse(self.builds[0].hasChanges(A))
3051 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003052
Paul Belangerbaca3132016-11-04 12:49:54 -04003053 self.assertTrue(self.builds[2].hasChanges(B))
3054 self.assertTrue(self.builds[2].hasChanges(C))
3055 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003056
Paul Belangerbaca3132016-11-04 12:49:54 -04003057 self.assertTrue(self.builds[4].hasChanges(B))
3058 self.assertTrue(self.builds[4].hasChanges(C))
3059 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003060
Paul Belanger174a8272017-03-14 13:20:10 -04003061 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003062 self.waitUntilSettled()
3063
3064 self.assertEqual(A.data['status'], 'MERGED')
3065 self.assertEqual(A.reported, 2)
3066 self.assertEqual(B.data['status'], 'MERGED')
3067 self.assertEqual(B.reported, 2)
3068 self.assertEqual(C.data['status'], 'MERGED')
3069 self.assertEqual(C.reported, 2)
3070
James E. Blair36658cf2013-12-06 17:53:48 -08003071 self.assertEqual(r, True)
3072
3073 def test_client_promote_dependent(self):
3074 "Test that the RPC client can promote a dependent change"
3075 # C (depends on B) -> B -> A ; then promote C to get:
3076 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003077 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003078 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3079 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3080 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3081
3082 C.setDependsOn(B, 1)
3083
James E. Blair8b5408c2016-08-08 15:37:46 -07003084 A.addApproval('code-review', 2)
3085 B.addApproval('code-review', 2)
3086 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003087
James E. Blair8b5408c2016-08-08 15:37:46 -07003088 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3089 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3090 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003091
3092 self.waitUntilSettled()
3093
3094 client = zuul.rpcclient.RPCClient('127.0.0.1',
3095 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003096 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003097 r = client.promote(tenant='tenant-one',
3098 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003099 change_ids=['3,1'])
3100
James E. Blair78acec92014-02-06 07:11:32 -08003101 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003102 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003103 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003104 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -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()
3108
3109 self.assertEqual(len(self.builds), 6)
3110 self.assertEqual(self.builds[0].name, 'project-test1')
3111 self.assertEqual(self.builds[1].name, 'project-test2')
3112 self.assertEqual(self.builds[2].name, 'project-test1')
3113 self.assertEqual(self.builds[3].name, 'project-test2')
3114 self.assertEqual(self.builds[4].name, 'project-test1')
3115 self.assertEqual(self.builds[5].name, 'project-test2')
3116
Paul Belangerbaca3132016-11-04 12:49:54 -04003117 self.assertTrue(self.builds[0].hasChanges(B))
3118 self.assertFalse(self.builds[0].hasChanges(A))
3119 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003120
Paul Belangerbaca3132016-11-04 12:49:54 -04003121 self.assertTrue(self.builds[2].hasChanges(B))
3122 self.assertTrue(self.builds[2].hasChanges(C))
3123 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003124
Paul Belangerbaca3132016-11-04 12:49:54 -04003125 self.assertTrue(self.builds[4].hasChanges(B))
3126 self.assertTrue(self.builds[4].hasChanges(C))
3127 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003128
Paul Belanger174a8272017-03-14 13:20:10 -04003129 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003130 self.waitUntilSettled()
3131
3132 self.assertEqual(A.data['status'], 'MERGED')
3133 self.assertEqual(A.reported, 2)
3134 self.assertEqual(B.data['status'], 'MERGED')
3135 self.assertEqual(B.reported, 2)
3136 self.assertEqual(C.data['status'], 'MERGED')
3137 self.assertEqual(C.reported, 2)
3138
James E. Blair36658cf2013-12-06 17:53:48 -08003139 self.assertEqual(r, True)
3140
3141 def test_client_promote_negative(self):
3142 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003143 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003144 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003145 A.addApproval('code-review', 2)
3146 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003147 self.waitUntilSettled()
3148
3149 client = zuul.rpcclient.RPCClient('127.0.0.1',
3150 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003151 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003152
3153 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003154 r = client.promote(tenant='tenant-one',
3155 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003156 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003157 self.assertEqual(r, False)
3158
3159 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003160 r = client.promote(tenant='tenant-one',
3161 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003162 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003163 self.assertEqual(r, False)
3164
Paul Belanger174a8272017-03-14 13:20:10 -04003165 self.executor_server.hold_jobs_in_build = False
3166 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003167 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003168
James E. Blaircc324192017-04-19 14:36:08 -07003169 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003170 def test_queue_rate_limiting(self):
3171 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003172 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003173 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3174 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3175 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3176
3177 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003178 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003179
James E. Blair8b5408c2016-08-08 15:37:46 -07003180 A.addApproval('code-review', 2)
3181 B.addApproval('code-review', 2)
3182 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003183
James E. Blair8b5408c2016-08-08 15:37:46 -07003184 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3185 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3186 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003187 self.waitUntilSettled()
3188
3189 # Only A and B will have their merge jobs queued because
3190 # window is 2.
3191 self.assertEqual(len(self.builds), 2)
3192 self.assertEqual(self.builds[0].name, 'project-merge')
3193 self.assertEqual(self.builds[1].name, 'project-merge')
3194
James E. Blair732ee862017-04-21 07:49:26 -07003195 # Release the merge jobs one at a time.
3196 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003197 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003198 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003199 self.waitUntilSettled()
3200
3201 # Only A and B will have their test jobs queued because
3202 # window is 2.
3203 self.assertEqual(len(self.builds), 4)
3204 self.assertEqual(self.builds[0].name, 'project-test1')
3205 self.assertEqual(self.builds[1].name, 'project-test2')
3206 self.assertEqual(self.builds[2].name, 'project-test1')
3207 self.assertEqual(self.builds[3].name, 'project-test2')
3208
Paul Belanger174a8272017-03-14 13:20:10 -04003209 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003210 self.waitUntilSettled()
3211
James E. Blaircc324192017-04-19 14:36:08 -07003212 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003213 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003214 # A failed so window is reduced by 1 to 1.
3215 self.assertEqual(queue.window, 1)
3216 self.assertEqual(queue.window_floor, 1)
3217 self.assertEqual(A.data['status'], 'NEW')
3218
3219 # Gate is reset and only B's merge job is queued because
3220 # window shrunk to 1.
3221 self.assertEqual(len(self.builds), 1)
3222 self.assertEqual(self.builds[0].name, 'project-merge')
3223
Paul Belanger174a8272017-03-14 13:20:10 -04003224 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003225 self.waitUntilSettled()
3226
3227 # Only B's test jobs are queued because window is still 1.
3228 self.assertEqual(len(self.builds), 2)
3229 self.assertEqual(self.builds[0].name, 'project-test1')
3230 self.assertEqual(self.builds[1].name, 'project-test2')
3231
Paul Belanger174a8272017-03-14 13:20:10 -04003232 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003233 self.waitUntilSettled()
3234
3235 # B was successfully merged so window is increased to 2.
3236 self.assertEqual(queue.window, 2)
3237 self.assertEqual(queue.window_floor, 1)
3238 self.assertEqual(B.data['status'], 'MERGED')
3239
3240 # Only C is left and its merge job is queued.
3241 self.assertEqual(len(self.builds), 1)
3242 self.assertEqual(self.builds[0].name, 'project-merge')
3243
Paul Belanger174a8272017-03-14 13:20:10 -04003244 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003245 self.waitUntilSettled()
3246
3247 # After successful merge job the test jobs for C are queued.
3248 self.assertEqual(len(self.builds), 2)
3249 self.assertEqual(self.builds[0].name, 'project-test1')
3250 self.assertEqual(self.builds[1].name, 'project-test2')
3251
Paul Belanger174a8272017-03-14 13:20:10 -04003252 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003253 self.waitUntilSettled()
3254
3255 # C successfully merged so window is bumped to 3.
3256 self.assertEqual(queue.window, 3)
3257 self.assertEqual(queue.window_floor, 1)
3258 self.assertEqual(C.data['status'], 'MERGED')
3259
James E. Blaircc324192017-04-19 14:36:08 -07003260 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003261 def test_queue_rate_limiting_dependent(self):
3262 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003263 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003264 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3265 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3266 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3267
3268 B.setDependsOn(A, 1)
3269
Paul Belanger174a8272017-03-14 13:20:10 -04003270 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003271
James E. Blair8b5408c2016-08-08 15:37:46 -07003272 A.addApproval('code-review', 2)
3273 B.addApproval('code-review', 2)
3274 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003275
James E. Blair8b5408c2016-08-08 15:37:46 -07003276 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3277 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3278 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003279 self.waitUntilSettled()
3280
3281 # Only A and B will have their merge jobs queued because
3282 # window is 2.
3283 self.assertEqual(len(self.builds), 2)
3284 self.assertEqual(self.builds[0].name, 'project-merge')
3285 self.assertEqual(self.builds[1].name, 'project-merge')
3286
James E. Blairf4a5f022017-04-18 14:01:10 -07003287 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003288
3289 # Only A and B will have their test jobs queued because
3290 # window is 2.
3291 self.assertEqual(len(self.builds), 4)
3292 self.assertEqual(self.builds[0].name, 'project-test1')
3293 self.assertEqual(self.builds[1].name, 'project-test2')
3294 self.assertEqual(self.builds[2].name, 'project-test1')
3295 self.assertEqual(self.builds[3].name, 'project-test2')
3296
Paul Belanger174a8272017-03-14 13:20:10 -04003297 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003298 self.waitUntilSettled()
3299
James E. Blaircc324192017-04-19 14:36:08 -07003300 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003301 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003302 # A failed so window is reduced by 1 to 1.
3303 self.assertEqual(queue.window, 1)
3304 self.assertEqual(queue.window_floor, 1)
3305 self.assertEqual(A.data['status'], 'NEW')
3306 self.assertEqual(B.data['status'], 'NEW')
3307
3308 # Gate is reset and only C's merge job is queued because
3309 # window shrunk to 1 and A and B were dequeued.
3310 self.assertEqual(len(self.builds), 1)
3311 self.assertEqual(self.builds[0].name, 'project-merge')
3312
James E. Blairf4a5f022017-04-18 14:01:10 -07003313 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003314
3315 # Only C's test jobs are queued because window is still 1.
3316 self.assertEqual(len(self.builds), 2)
3317 self.assertEqual(self.builds[0].name, 'project-test1')
3318 self.assertEqual(self.builds[1].name, 'project-test2')
3319
Paul Belanger174a8272017-03-14 13:20:10 -04003320 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003321 self.waitUntilSettled()
3322
3323 # C was successfully merged so window is increased to 2.
3324 self.assertEqual(queue.window, 2)
3325 self.assertEqual(queue.window_floor, 1)
3326 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003327
James E. Blairec056492016-07-22 09:45:56 -07003328 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003329 def test_worker_update_metadata(self):
3330 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003331 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003332
3333 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003334 A.addApproval('code-review', 2)
3335 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003336 self.waitUntilSettled()
3337
Paul Belanger174a8272017-03-14 13:20:10 -04003338 self.assertEqual(len(self.executor.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003339
3340 self.log.debug('Current builds:')
Paul Belanger174a8272017-03-14 13:20:10 -04003341 self.log.debug(self.executor.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003342
3343 start = time.time()
3344 while True:
3345 if time.time() - start > 10:
3346 raise Exception("Timeout waiting for gearman server to report "
3347 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003348 build = list(self.executor.builds.values())[0]
Joshua Heskethba8776a2014-01-12 14:35:40 +08003349 if build.worker.name == "My Worker":
3350 break
3351 else:
3352 time.sleep(0)
3353
3354 self.log.debug(build)
3355 self.assertEqual("My Worker", build.worker.name)
3356 self.assertEqual("localhost", build.worker.hostname)
3357 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3358 self.assertEqual("zuul.example.org", build.worker.fqdn)
3359 self.assertEqual("FakeBuilder", build.worker.program)
3360 self.assertEqual("v1.1", build.worker.version)
3361 self.assertEqual({'something': 'else'}, build.worker.extra)
3362
Paul Belanger174a8272017-03-14 13:20:10 -04003363 self.executor_server.hold_jobs_in_build = False
3364 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003365 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003366
James E. Blaircc324192017-04-19 14:36:08 -07003367 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003368 def test_footer_message(self):
3369 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003370 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003371 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003372 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003373 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003374 self.waitUntilSettled()
3375
3376 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003377 B.addApproval('code-review', 2)
3378 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003379 self.waitUntilSettled()
3380
3381 self.assertEqual(2, len(self.smtp_messages))
3382
Adam Gandelman82258fd2017-02-07 16:51:58 -08003383 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003384Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003385http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003386
Adam Gandelman82258fd2017-02-07 16:51:58 -08003387 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003388For CI problems and help debugging, contact ci@example.org"""
3389
Adam Gandelman82258fd2017-02-07 16:51:58 -08003390 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3391 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3392 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3393 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003394
James E. Blairec056492016-07-22 09:45:56 -07003395 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003396 def test_merge_failure_reporters(self):
3397 """Check that the config is set up correctly"""
3398
James E. Blairf84026c2015-12-08 16:11:46 -08003399 self.updateConfigLayout(
3400 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003401 self.sched.reconfigure(self.config)
3402 self.registerJobs()
3403
3404 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003405 "Merge Failed.\n\nThis change or one of its cross-repo "
3406 "dependencies was unable to be automatically merged with the "
3407 "current state of its repository. Please rebase the change and "
3408 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003409 self.sched.layout.pipelines['check'].merge_failure_message)
3410 self.assertEqual(
3411 "The merge failed! For more information...",
3412 self.sched.layout.pipelines['gate'].merge_failure_message)
3413
3414 self.assertEqual(
3415 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3416 self.assertEqual(
3417 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3418
3419 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003420 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3421 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003422
3423 self.assertTrue(
3424 (
3425 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003426 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003427 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003428 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003429 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003430 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003431 ) or (
3432 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003433 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003434 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003435 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003436 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003437 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003438 )
3439 )
3440
James E. Blairec056492016-07-22 09:45:56 -07003441 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003442 def test_merge_failure_reports(self):
3443 """Check that when a change fails to merge the correct message is sent
3444 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003445 self.updateConfigLayout(
3446 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003447 self.sched.reconfigure(self.config)
3448 self.registerJobs()
3449
3450 # Check a test failure isn't reported to SMTP
3451 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003452 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003453 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003454 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003455 self.waitUntilSettled()
3456
3457 self.assertEqual(3, len(self.history)) # 3 jobs
3458 self.assertEqual(0, len(self.smtp_messages))
3459
3460 # Check a merge failure is reported to SMTP
3461 # B should be merged, but C will conflict with B
3462 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3463 B.addPatchset(['conflict'])
3464 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3465 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003466 B.addApproval('code-review', 2)
3467 C.addApproval('code-review', 2)
3468 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3469 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003470 self.waitUntilSettled()
3471
3472 self.assertEqual(6, len(self.history)) # A and B jobs
3473 self.assertEqual(1, len(self.smtp_messages))
3474 self.assertEqual('The merge failed! For more information...',
3475 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003476
James E. Blairec056492016-07-22 09:45:56 -07003477 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003478 def test_default_merge_failure_reports(self):
3479 """Check that the default merge failure reports are correct."""
3480
3481 # A should report success, B should report merge failure.
3482 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3483 A.addPatchset(['conflict'])
3484 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3485 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003486 A.addApproval('code-review', 2)
3487 B.addApproval('code-review', 2)
3488 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3489 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003490 self.waitUntilSettled()
3491
3492 self.assertEqual(3, len(self.history)) # A jobs
3493 self.assertEqual(A.reported, 2)
3494 self.assertEqual(B.reported, 2)
3495 self.assertEqual(A.data['status'], 'MERGED')
3496 self.assertEqual(B.data['status'], 'NEW')
3497 self.assertIn('Build succeeded', A.messages[1])
3498 self.assertIn('Merge Failed', B.messages[1])
3499 self.assertIn('automatically merged', B.messages[1])
3500 self.assertNotIn('logs.example.com', B.messages[1])
3501 self.assertNotIn('SKIPPED', B.messages[1])
3502
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003503 def test_client_get_running_jobs(self):
3504 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003505 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003506 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003507 A.addApproval('code-review', 2)
3508 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003509 self.waitUntilSettled()
3510
3511 client = zuul.rpcclient.RPCClient('127.0.0.1',
3512 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003513 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003514
3515 # Wait for gearman server to send the initial workData back to zuul
3516 start = time.time()
3517 while True:
3518 if time.time() - start > 10:
3519 raise Exception("Timeout waiting for gearman server to report "
3520 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003521 build = list(self.executor_client.builds.values())[0]
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003522 if build.worker.name == "My Worker":
3523 break
3524 else:
3525 time.sleep(0)
3526
3527 running_items = client.get_running_jobs()
3528
3529 self.assertEqual(1, len(running_items))
3530 running_item = running_items[0]
3531 self.assertEqual([], running_item['failing_reasons'])
3532 self.assertEqual([], running_item['items_behind'])
3533 self.assertEqual('https://hostname/1', running_item['url'])
3534 self.assertEqual(None, running_item['item_ahead'])
3535 self.assertEqual('org/project', running_item['project'])
3536 self.assertEqual(None, running_item['remaining_time'])
3537 self.assertEqual(True, running_item['active'])
3538 self.assertEqual('1,1', running_item['id'])
3539
3540 self.assertEqual(3, len(running_item['jobs']))
3541 for job in running_item['jobs']:
3542 if job['name'] == 'project-merge':
3543 self.assertEqual('project-merge', job['name'])
3544 self.assertEqual('gate', job['pipeline'])
3545 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003546 self.assertEqual('https://server/job/project-merge/0/',
3547 job['url'])
3548 self.assertEqual(7, len(job['worker']))
3549 self.assertEqual(False, job['canceled'])
3550 self.assertEqual(True, job['voting'])
3551 self.assertEqual(None, job['result'])
3552 self.assertEqual('gate', job['pipeline'])
3553 break
3554
Paul Belanger174a8272017-03-14 13:20:10 -04003555 self.executor_server.hold_jobs_in_build = False
3556 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003557 self.waitUntilSettled()
3558
3559 running_items = client.get_running_jobs()
3560 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003561
James E. Blairb70e55a2017-04-19 12:57:02 -07003562 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003563 def test_nonvoting_pipeline(self):
3564 "Test that a nonvoting pipeline (experimental) can still report"
3565
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003566 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3567 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003568 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3569 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003570
3571 self.assertEqual(self.getJobFromHistory('project-merge').result,
3572 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003573 self.assertEqual(
3574 self.getJobFromHistory('experimental-project-test').result,
3575 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003576 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003577
3578 def test_crd_gate(self):
3579 "Test cross-repo dependencies"
3580 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3581 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003582 A.addApproval('code-review', 2)
3583 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003584
3585 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3586 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3587 AM2.setMerged()
3588 AM1.setMerged()
3589
3590 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3591 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3592 BM2.setMerged()
3593 BM1.setMerged()
3594
3595 # A -> AM1 -> AM2
3596 # B -> BM1 -> BM2
3597 # A Depends-On: B
3598 # M2 is here to make sure it is never queried. If it is, it
3599 # means zuul is walking down the entire history of merged
3600 # changes.
3601
3602 B.setDependsOn(BM1, 1)
3603 BM1.setDependsOn(BM2, 1)
3604
3605 A.setDependsOn(AM1, 1)
3606 AM1.setDependsOn(AM2, 1)
3607
3608 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3609 A.subject, B.data['id'])
3610
James E. Blair8b5408c2016-08-08 15:37:46 -07003611 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003612 self.waitUntilSettled()
3613
3614 self.assertEqual(A.data['status'], 'NEW')
3615 self.assertEqual(B.data['status'], 'NEW')
3616
Paul Belanger5dccbe72016-11-14 11:17:37 -05003617 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003618 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003619
Paul Belanger174a8272017-03-14 13:20:10 -04003620 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003621 B.addApproval('approved', 1)
3622 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003623 self.waitUntilSettled()
3624
Paul Belanger174a8272017-03-14 13:20:10 -04003625 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003626 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003627 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003628 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003629 self.executor_server.hold_jobs_in_build = False
3630 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003631 self.waitUntilSettled()
3632
3633 self.assertEqual(AM2.queried, 0)
3634 self.assertEqual(BM2.queried, 0)
3635 self.assertEqual(A.data['status'], 'MERGED')
3636 self.assertEqual(B.data['status'], 'MERGED')
3637 self.assertEqual(A.reported, 2)
3638 self.assertEqual(B.reported, 2)
3639
Paul Belanger5dccbe72016-11-14 11:17:37 -05003640 changes = self.getJobFromHistory(
3641 'project-merge', 'org/project1').changes
3642 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003643
3644 def test_crd_branch(self):
3645 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003646
3647 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003648 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3649 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3650 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3651 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003652 A.addApproval('code-review', 2)
3653 B.addApproval('code-review', 2)
3654 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003655
3656 # A Depends-On: B+C
3657 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3658 A.subject, B.data['id'])
3659
Paul Belanger174a8272017-03-14 13:20:10 -04003660 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003661 B.addApproval('approved', 1)
3662 C.addApproval('approved', 1)
3663 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003664 self.waitUntilSettled()
3665
Paul Belanger174a8272017-03-14 13:20:10 -04003666 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003667 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003668 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003669 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003670 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003671 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003672 self.executor_server.hold_jobs_in_build = False
3673 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003674 self.waitUntilSettled()
3675
3676 self.assertEqual(A.data['status'], 'MERGED')
3677 self.assertEqual(B.data['status'], 'MERGED')
3678 self.assertEqual(C.data['status'], 'MERGED')
3679 self.assertEqual(A.reported, 2)
3680 self.assertEqual(B.reported, 2)
3681 self.assertEqual(C.reported, 2)
3682
Paul Belanger6379db12016-11-14 13:57:54 -05003683 changes = self.getJobFromHistory(
3684 'project-merge', 'org/project1').changes
3685 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003686
3687 def test_crd_multiline(self):
3688 "Test multiple depends-on lines in commit"
3689 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3690 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3691 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003692 A.addApproval('code-review', 2)
3693 B.addApproval('code-review', 2)
3694 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003695
3696 # A Depends-On: B+C
3697 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3698 A.subject, B.data['id'], C.data['id'])
3699
Paul Belanger174a8272017-03-14 13:20:10 -04003700 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003701 B.addApproval('approved', 1)
3702 C.addApproval('approved', 1)
3703 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003704 self.waitUntilSettled()
3705
Paul Belanger174a8272017-03-14 13:20:10 -04003706 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003707 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003708 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003709 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003710 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003711 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003712 self.executor_server.hold_jobs_in_build = False
3713 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003714 self.waitUntilSettled()
3715
3716 self.assertEqual(A.data['status'], 'MERGED')
3717 self.assertEqual(B.data['status'], 'MERGED')
3718 self.assertEqual(C.data['status'], 'MERGED')
3719 self.assertEqual(A.reported, 2)
3720 self.assertEqual(B.reported, 2)
3721 self.assertEqual(C.reported, 2)
3722
Paul Belanger5dccbe72016-11-14 11:17:37 -05003723 changes = self.getJobFromHistory(
3724 'project-merge', 'org/project1').changes
3725 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003726
3727 def test_crd_unshared_gate(self):
3728 "Test cross-repo dependencies in unshared gate queues"
3729 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3730 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003731 A.addApproval('code-review', 2)
3732 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003733
3734 # A Depends-On: B
3735 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3736 A.subject, B.data['id'])
3737
3738 # A and B do not share a queue, make sure that A is unable to
3739 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003740 B.addApproval('approved', 1)
3741 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003742 self.waitUntilSettled()
3743
3744 self.assertEqual(A.data['status'], 'NEW')
3745 self.assertEqual(B.data['status'], 'NEW')
3746 self.assertEqual(A.reported, 0)
3747 self.assertEqual(B.reported, 0)
3748 self.assertEqual(len(self.history), 0)
3749
3750 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003751 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003752 self.waitUntilSettled()
3753
3754 self.assertEqual(B.data['status'], 'MERGED')
3755 self.assertEqual(B.reported, 2)
3756
3757 # Now that B is merged, A should be able to be enqueued and
3758 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003759 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003760 self.waitUntilSettled()
3761
3762 self.assertEqual(A.data['status'], 'MERGED')
3763 self.assertEqual(A.reported, 2)
3764
James E. Blair96698e22015-04-02 07:48:21 -07003765 def test_crd_gate_reverse(self):
3766 "Test reverse cross-repo dependencies"
3767 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3768 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003769 A.addApproval('code-review', 2)
3770 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003771
3772 # A Depends-On: B
3773
3774 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3775 A.subject, B.data['id'])
3776
James E. Blair8b5408c2016-08-08 15:37:46 -07003777 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003778 self.waitUntilSettled()
3779
3780 self.assertEqual(A.data['status'], 'NEW')
3781 self.assertEqual(B.data['status'], 'NEW')
3782
Paul Belanger174a8272017-03-14 13:20:10 -04003783 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003784 A.addApproval('approved', 1)
3785 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003786 self.waitUntilSettled()
3787
Paul Belanger174a8272017-03-14 13:20:10 -04003788 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003789 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003790 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003791 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003792 self.executor_server.hold_jobs_in_build = False
3793 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003794 self.waitUntilSettled()
3795
3796 self.assertEqual(A.data['status'], 'MERGED')
3797 self.assertEqual(B.data['status'], 'MERGED')
3798 self.assertEqual(A.reported, 2)
3799 self.assertEqual(B.reported, 2)
3800
Paul Belanger5dccbe72016-11-14 11:17:37 -05003801 changes = self.getJobFromHistory(
3802 'project-merge', 'org/project1').changes
3803 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003804
James E. Blair5ee24252014-12-30 10:12:29 -08003805 def test_crd_cycle(self):
3806 "Test cross-repo dependency cycles"
3807 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3808 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003809 A.addApproval('code-review', 2)
3810 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003811
3812 # A -> B -> A (via commit-depends)
3813
3814 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3815 A.subject, B.data['id'])
3816 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3817 B.subject, A.data['id'])
3818
James E. Blair8b5408c2016-08-08 15:37:46 -07003819 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003820 self.waitUntilSettled()
3821
3822 self.assertEqual(A.reported, 0)
3823 self.assertEqual(B.reported, 0)
3824 self.assertEqual(A.data['status'], 'NEW')
3825 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003826
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003827 def test_crd_gate_unknown(self):
3828 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07003829 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003830 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3831 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003832 A.addApproval('code-review', 2)
3833 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003834
3835 # A Depends-On: B
3836 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3837 A.subject, B.data['id'])
3838
James E. Blair8b5408c2016-08-08 15:37:46 -07003839 B.addApproval('approved', 1)
3840 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003841 self.waitUntilSettled()
3842
3843 # Unknown projects cannot share a queue with any other
3844 # since they don't have common jobs with any other (they have no jobs).
3845 # Changes which depend on unknown project changes
3846 # should not be processed in dependent pipeline
3847 self.assertEqual(A.data['status'], 'NEW')
3848 self.assertEqual(B.data['status'], 'NEW')
3849 self.assertEqual(A.reported, 0)
3850 self.assertEqual(B.reported, 0)
3851 self.assertEqual(len(self.history), 0)
3852
3853 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07003854 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003855 B.setMerged()
3856 self.waitUntilSettled()
3857 self.assertEqual(len(self.history), 0)
3858
3859 # Now that B is merged, A should be able to be enqueued and
3860 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003861 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003862 self.waitUntilSettled()
3863
3864 self.assertEqual(A.data['status'], 'MERGED')
3865 self.assertEqual(A.reported, 2)
3866 self.assertEqual(B.data['status'], 'MERGED')
3867 self.assertEqual(B.reported, 0)
3868
James E. Blairbfb8e042014-12-30 17:01:44 -08003869 def test_crd_check(self):
3870 "Test cross-repo dependencies in independent pipelines"
3871
Paul Belanger174a8272017-03-14 13:20:10 -04003872 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08003873 self.gearman_server.hold_jobs_in_queue = True
3874 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3875 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3876
3877 # A Depends-On: B
3878 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3879 A.subject, B.data['id'])
3880
3881 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3882 self.waitUntilSettled()
3883
James E. Blairbfb8e042014-12-30 17:01:44 -08003884 self.gearman_server.hold_jobs_in_queue = False
3885 self.gearman_server.release()
3886 self.waitUntilSettled()
3887
Paul Belanger174a8272017-03-14 13:20:10 -04003888 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05003889 self.waitUntilSettled()
3890
James E. Blair2a535672017-04-27 12:03:15 -07003891 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08003892
Paul Belanger174a8272017-03-14 13:20:10 -04003893 self.executor_server.hold_jobs_in_build = False
3894 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05003895 self.waitUntilSettled()
3896
James E. Blairbfb8e042014-12-30 17:01:44 -08003897 self.assertEqual(A.data['status'], 'NEW')
3898 self.assertEqual(B.data['status'], 'NEW')
3899 self.assertEqual(A.reported, 1)
3900 self.assertEqual(B.reported, 0)
3901
3902 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05003903 tenant = self.sched.abide.tenants.get('tenant-one')
3904 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08003905
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003906 def test_crd_check_git_depends(self):
3907 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07003908 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003909 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3910 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3911
3912 # Add two git-dependent changes and make sure they both report
3913 # success.
3914 B.setDependsOn(A, 1)
3915 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3916 self.waitUntilSettled()
3917 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3918 self.waitUntilSettled()
3919
James E. Blairb8c16472015-05-05 14:55:26 -07003920 self.orderedRelease()
3921 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003922 self.waitUntilSettled()
3923
3924 self.assertEqual(A.data['status'], 'NEW')
3925 self.assertEqual(B.data['status'], 'NEW')
3926 self.assertEqual(A.reported, 1)
3927 self.assertEqual(B.reported, 1)
3928
3929 self.assertEqual(self.history[0].changes, '1,1')
3930 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04003931 tenant = self.sched.abide.tenants.get('tenant-one')
3932 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003933
3934 self.assertIn('Build succeeded', A.messages[0])
3935 self.assertIn('Build succeeded', B.messages[0])
3936
3937 def test_crd_check_duplicate(self):
3938 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04003939 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003940 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3941 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04003942 tenant = self.sched.abide.tenants.get('tenant-one')
3943 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003944
3945 # Add two git-dependent changes...
3946 B.setDependsOn(A, 1)
3947 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3948 self.waitUntilSettled()
3949 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3950
3951 # ...make sure the live one is not duplicated...
3952 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3953 self.waitUntilSettled()
3954 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3955
3956 # ...but the non-live one is able to be.
3957 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3958 self.waitUntilSettled()
3959 self.assertEqual(len(check_pipeline.getAllItems()), 3)
3960
Clark Boylandd849822015-03-02 12:38:14 -08003961 # Release jobs in order to avoid races with change A jobs
3962 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07003963 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04003964 self.executor_server.hold_jobs_in_build = False
3965 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003966 self.waitUntilSettled()
3967
3968 self.assertEqual(A.data['status'], 'NEW')
3969 self.assertEqual(B.data['status'], 'NEW')
3970 self.assertEqual(A.reported, 1)
3971 self.assertEqual(B.reported, 1)
3972
3973 self.assertEqual(self.history[0].changes, '1,1 2,1')
3974 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04003975 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003976
3977 self.assertIn('Build succeeded', A.messages[0])
3978 self.assertIn('Build succeeded', B.messages[0])
3979
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003980 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08003981 "Test cross-repo dependencies re-enqueued in independent pipelines"
3982
3983 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003984 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
3985 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08003986
3987 # A Depends-On: B
3988 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3989 A.subject, B.data['id'])
3990
3991 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3992 self.waitUntilSettled()
3993
3994 self.sched.reconfigure(self.config)
3995
3996 # Make sure the items still share a change queue, and the
3997 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05003998 tenant = self.sched.abide.tenants.get('tenant-one')
3999 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4000 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004001 first_item = queue.queue[0]
4002 for item in queue.queue:
4003 self.assertEqual(item.queue, first_item.queue)
4004 self.assertFalse(first_item.live)
4005 self.assertTrue(queue.queue[1].live)
4006
4007 self.gearman_server.hold_jobs_in_queue = False
4008 self.gearman_server.release()
4009 self.waitUntilSettled()
4010
4011 self.assertEqual(A.data['status'], 'NEW')
4012 self.assertEqual(B.data['status'], 'NEW')
4013 self.assertEqual(A.reported, 1)
4014 self.assertEqual(B.reported, 0)
4015
4016 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004017 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004018
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004019 def test_crd_check_reconfiguration(self):
4020 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4021
4022 def test_crd_undefined_project(self):
4023 """Test that undefined projects in dependencies are handled for
4024 independent pipelines"""
4025 # It's a hack for fake gerrit,
4026 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004027 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004028 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4029
Jesse Keatingea68ab42017-04-19 15:03:30 -07004030 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004031 def test_crd_check_ignore_dependencies(self):
4032 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004033
4034 self.gearman_server.hold_jobs_in_queue = True
4035 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4036 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4037 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4038
4039 # A Depends-On: B
4040 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4041 A.subject, B.data['id'])
4042 # C git-depends on B
4043 C.setDependsOn(B, 1)
4044 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4045 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4046 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4047 self.waitUntilSettled()
4048
4049 # Make sure none of the items share a change queue, and all
4050 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004051 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004052 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004053 self.assertEqual(len(check_pipeline.queues), 3)
4054 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4055 for item in check_pipeline.getAllItems():
4056 self.assertTrue(item.live)
4057
4058 self.gearman_server.hold_jobs_in_queue = False
4059 self.gearman_server.release()
4060 self.waitUntilSettled()
4061
4062 self.assertEqual(A.data['status'], 'NEW')
4063 self.assertEqual(B.data['status'], 'NEW')
4064 self.assertEqual(C.data['status'], 'NEW')
4065 self.assertEqual(A.reported, 1)
4066 self.assertEqual(B.reported, 1)
4067 self.assertEqual(C.reported, 1)
4068
4069 # Each job should have tested exactly one change
4070 for job in self.history:
4071 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004072
James E. Blair7cb84542017-04-19 13:35:05 -07004073 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004074 def test_crd_check_transitive(self):
4075 "Test transitive cross-repo dependencies"
4076 # Specifically, if A -> B -> C, and C gets a new patchset and
4077 # A gets a new patchset, ensure the test of A,2 includes B,1
4078 # and C,2 (not C,1 which would indicate stale data in the
4079 # cache for B).
4080 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4081 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4082 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4083
4084 # A Depends-On: B
4085 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4086 A.subject, B.data['id'])
4087
4088 # B Depends-On: C
4089 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4090 B.subject, C.data['id'])
4091
4092 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4093 self.waitUntilSettled()
4094 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4095
4096 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4097 self.waitUntilSettled()
4098 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4099
4100 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4101 self.waitUntilSettled()
4102 self.assertEqual(self.history[-1].changes, '3,1')
4103
4104 C.addPatchset()
4105 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4106 self.waitUntilSettled()
4107 self.assertEqual(self.history[-1].changes, '3,2')
4108
4109 A.addPatchset()
4110 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4111 self.waitUntilSettled()
4112 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004113
James E. Blaira8b90b32016-08-24 15:18:50 -07004114 def test_crd_check_unknown(self):
4115 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004116 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004117 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4118 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4119 # A Depends-On: B
4120 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4121 A.subject, B.data['id'])
4122
4123 # Make sure zuul has seen an event on B.
4124 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4125 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4126 self.waitUntilSettled()
4127
4128 self.assertEqual(A.data['status'], 'NEW')
4129 self.assertEqual(A.reported, 1)
4130 self.assertEqual(B.data['status'], 'NEW')
4131 self.assertEqual(B.reported, 0)
4132
James E. Blair92464a22016-04-05 10:21:26 -07004133 def test_crd_cycle_join(self):
4134 "Test an updated change creates a cycle"
4135 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4136
4137 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4138 self.waitUntilSettled()
4139
4140 # Create B->A
4141 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4142 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4143 B.subject, A.data['id'])
4144 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4145 self.waitUntilSettled()
4146
4147 # Update A to add A->B (a cycle).
4148 A.addPatchset()
4149 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4150 A.subject, B.data['id'])
4151 # Normally we would submit the patchset-created event for
4152 # processing here, however, we have no way of noting whether
4153 # the dependency cycle detection correctly raised an
4154 # exception, so instead, we reach into the source driver and
4155 # call the method that would ultimately be called by the event
4156 # processing.
4157
Paul Belanger0e155e22016-11-14 14:12:23 -05004158 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07004159 (trusted, project) = tenant.getProject('org/project')
4160 source = project.source
Paul Belanger0e155e22016-11-14 14:12:23 -05004161
4162 # TODO(pabelanger): As we add more source / trigger APIs we should make
4163 # it easier for users to create events for testing.
4164 event = zuul.model.TriggerEvent()
4165 event.trigger_name = 'gerrit'
4166 event.change_number = '1'
4167 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004168 with testtools.ExpectedException(
4169 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004170 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004171 self.log.debug("Got expected dependency cycle exception")
4172
4173 # Now if we update B to remove the depends-on, everything
4174 # should be okay. B; A->B
4175
4176 B.addPatchset()
4177 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004178
4179 source.getChange(event, True)
4180 event.change_number = '2'
4181 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004182
Jesse Keatingcc67b122017-04-19 14:54:57 -07004183 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004184 def test_disable_at(self):
4185 "Test a pipeline will only report to the disabled trigger when failing"
4186
Jesse Keatingcc67b122017-04-19 14:54:57 -07004187 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004188 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004189 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004190 0, tenant.layout.pipelines['check']._consecutive_failures)
4191 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004192
4193 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4194 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4195 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4196 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4197 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4198 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4199 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4200 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4201 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4202 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4203 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4204
Paul Belanger174a8272017-03-14 13:20:10 -04004205 self.executor_server.failJob('project-test1', A)
4206 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004207 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004208 self.executor_server.failJob('project-test1', D)
4209 self.executor_server.failJob('project-test1', E)
4210 self.executor_server.failJob('project-test1', F)
4211 self.executor_server.failJob('project-test1', G)
4212 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004213 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004214 self.executor_server.failJob('project-test1', J)
4215 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004216
4217 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4218 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4219 self.waitUntilSettled()
4220
4221 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004222 2, tenant.layout.pipelines['check']._consecutive_failures)
4223 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004224
4225 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4226 self.waitUntilSettled()
4227
4228 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004229 0, tenant.layout.pipelines['check']._consecutive_failures)
4230 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004231
4232 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4233 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4234 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4235 self.waitUntilSettled()
4236
4237 # We should be disabled now
4238 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004239 3, tenant.layout.pipelines['check']._consecutive_failures)
4240 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004241
4242 # We need to wait between each of these patches to make sure the
4243 # smtp messages come back in an expected order
4244 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4245 self.waitUntilSettled()
4246 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4247 self.waitUntilSettled()
4248 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4249 self.waitUntilSettled()
4250
4251 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4252 # leaving a message on each change
4253 self.assertEqual(1, len(A.messages))
4254 self.assertIn('Build failed.', A.messages[0])
4255 self.assertEqual(1, len(B.messages))
4256 self.assertIn('Build failed.', B.messages[0])
4257 self.assertEqual(1, len(C.messages))
4258 self.assertIn('Build succeeded.', C.messages[0])
4259 self.assertEqual(1, len(D.messages))
4260 self.assertIn('Build failed.', D.messages[0])
4261 self.assertEqual(1, len(E.messages))
4262 self.assertIn('Build failed.', E.messages[0])
4263 self.assertEqual(1, len(F.messages))
4264 self.assertIn('Build failed.', F.messages[0])
4265
4266 # The last 3 (GHI) would have only reported via smtp.
4267 self.assertEqual(3, len(self.smtp_messages))
4268 self.assertEqual(0, len(G.messages))
4269 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004270 self.assertIn(
4271 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004272 self.assertEqual(0, len(H.messages))
4273 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004274 self.assertIn(
4275 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004276 self.assertEqual(0, len(I.messages))
4277 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004278 self.assertIn(
4279 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004280
4281 # Now reload the configuration (simulate a HUP) to check the pipeline
4282 # comes out of disabled
4283 self.sched.reconfigure(self.config)
4284
Jesse Keatingcc67b122017-04-19 14:54:57 -07004285 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004286
4287 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004288 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004289 0, tenant.layout.pipelines['check']._consecutive_failures)
4290 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004291
4292 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4293 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4294 self.waitUntilSettled()
4295
4296 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004297 2, tenant.layout.pipelines['check']._consecutive_failures)
4298 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004299
4300 # J and K went back to gerrit
4301 self.assertEqual(1, len(J.messages))
4302 self.assertIn('Build failed.', J.messages[0])
4303 self.assertEqual(1, len(K.messages))
4304 self.assertIn('Build failed.', K.messages[0])
4305 # No more messages reported via smtp
4306 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004307
James E. Blaircc020532017-04-19 13:04:44 -07004308 @simple_layout('layouts/one-job-project.yaml')
4309 def test_one_job_project(self):
4310 "Test that queueing works with one job"
4311 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4312 'master', 'A')
4313 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4314 'master', 'B')
4315 A.addApproval('code-review', 2)
4316 B.addApproval('code-review', 2)
4317 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4318 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4319 self.waitUntilSettled()
4320
4321 self.assertEqual(A.data['status'], 'MERGED')
4322 self.assertEqual(A.reported, 2)
4323 self.assertEqual(B.data['status'], 'MERGED')
4324 self.assertEqual(B.reported, 2)
4325
Paul Belanger71d98172016-11-08 10:56:31 -05004326 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004327 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004328
Paul Belanger174a8272017-03-14 13:20:10 -04004329 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004330 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4331 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4332 self.waitUntilSettled()
4333
Paul Belanger174a8272017-03-14 13:20:10 -04004334 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004335 self.waitUntilSettled()
4336
4337 self.assertEqual(len(self.builds), 2)
4338 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004339 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004340 self.waitUntilSettled()
4341
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004342 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004343 self.assertEqual(len(self.builds), 1,
4344 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004345 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004346 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004347 self.waitUntilSettled()
4348
Paul Belanger174a8272017-03-14 13:20:10 -04004349 self.executor_server.hold_jobs_in_build = False
4350 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004351 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004352 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004353 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4354 self.assertEqual(A.reported, 1)
4355 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004356
James E. Blair15be0e12017-01-03 13:45:20 -08004357 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004358 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004359
4360 self.fake_nodepool.paused = True
4361 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4362 A.addApproval('code-review', 2)
4363 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4364 self.waitUntilSettled()
4365
4366 self.zk.client.stop()
4367 self.zk.client.start()
4368 self.fake_nodepool.paused = False
4369 self.waitUntilSettled()
4370
4371 self.assertEqual(A.data['status'], 'MERGED')
4372 self.assertEqual(A.reported, 2)
4373
James E. Blair6ab79e02017-01-06 10:10:17 -08004374 def test_nodepool_failure(self):
4375 "Test that jobs are reported after a nodepool failure"
4376
4377 self.fake_nodepool.paused = True
4378 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4379 A.addApproval('code-review', 2)
4380 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4381 self.waitUntilSettled()
4382
4383 req = self.fake_nodepool.getNodeRequests()[0]
4384 self.fake_nodepool.addFailRequest(req)
4385
4386 self.fake_nodepool.paused = False
4387 self.waitUntilSettled()
4388
4389 self.assertEqual(A.data['status'], 'NEW')
4390 self.assertEqual(A.reported, 2)
4391 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4392 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4393 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4394
Arieb6f068c2016-10-09 13:11:06 +03004395
James E. Blaira002b032017-04-18 10:35:48 -07004396class TestExecutor(ZuulTestCase):
4397 tenant_config_file = 'config/single-tenant/main.yaml'
4398
4399 def assertFinalState(self):
4400 # In this test, we expect to shut down in a non-final state,
4401 # so skip these checks.
4402 pass
4403
4404 def assertCleanShutdown(self):
4405 self.log.debug("Assert clean shutdown")
4406
4407 # After shutdown, make sure no jobs are running
4408 self.assertEqual({}, self.executor_server.job_workers)
4409
4410 # Make sure that git.Repo objects have been garbage collected.
4411 repos = []
4412 gc.collect()
4413 for obj in gc.get_objects():
4414 if isinstance(obj, git.Repo):
4415 self.log.debug("Leaked git repo object: %s" % repr(obj))
4416 repos.append(obj)
4417 self.assertEqual(len(repos), 0)
4418
4419 def test_executor_shutdown(self):
4420 "Test that the executor can shut down with jobs running"
4421
4422 self.executor_server.hold_jobs_in_build = True
4423 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4424 A.addApproval('code-review', 2)
4425 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4426 self.waitUntilSettled()
4427
4428
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004429class TestDependencyGraph(ZuulTestCase):
4430 tenant_config_file = 'config/dependency-graph/main.yaml'
4431
4432 def test_dependeny_graph_dispatch_jobs_once(self):
4433 "Test a job in a dependency graph is queued only once"
4434 # Job dependencies, starting with A
4435 # A
4436 # / \
4437 # B C
4438 # / \ / \
4439 # D F E
4440 # |
4441 # G
4442
4443 self.executor_server.hold_jobs_in_build = True
4444 change = self.fake_gerrit.addFakeChange(
4445 'org/project', 'master', 'change')
4446 change.addApproval('code-review', 2)
4447 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4448
4449 self.waitUntilSettled()
4450 self.assertEqual([b.name for b in self.builds], ['A'])
4451
4452 self.executor_server.release('A')
4453 self.waitUntilSettled()
4454 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4455
4456 self.executor_server.release('B')
4457 self.waitUntilSettled()
4458 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4459
4460 self.executor_server.release('D')
4461 self.waitUntilSettled()
4462 self.assertEqual([b.name for b in self.builds], ['C'])
4463
4464 self.executor_server.release('C')
4465 self.waitUntilSettled()
4466 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4467
4468 self.executor_server.release('F')
4469 self.waitUntilSettled()
4470 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4471
4472 self.executor_server.release('G')
4473 self.waitUntilSettled()
4474 self.assertEqual([b.name for b in self.builds], ['E'])
4475
4476 self.executor_server.release('E')
4477 self.waitUntilSettled()
4478 self.assertEqual(len(self.builds), 0)
4479
4480 self.executor_server.hold_jobs_in_build = False
4481 self.executor_server.release()
4482 self.waitUntilSettled()
4483
4484 self.assertEqual(len(self.builds), 0)
4485 self.assertEqual(len(self.history), 7)
4486
4487 self.assertEqual(change.data['status'], 'MERGED')
4488 self.assertEqual(change.reported, 2)
4489
4490 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4491 "Test that a job waits till all dependencies are successful"
4492 # Job dependencies, starting with A
4493 # A
4494 # / \
4495 # B C*
4496 # / \ / \
4497 # D F E
4498 # |
4499 # G
4500
4501 self.executor_server.hold_jobs_in_build = True
4502 change = self.fake_gerrit.addFakeChange(
4503 'org/project', 'master', 'change')
4504 change.addApproval('code-review', 2)
4505
4506 self.executor_server.failJob('C', change)
4507
4508 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4509
4510 self.waitUntilSettled()
4511 self.assertEqual([b.name for b in self.builds], ['A'])
4512
4513 self.executor_server.release('A')
4514 self.waitUntilSettled()
4515 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4516
4517 self.executor_server.release('B')
4518 self.waitUntilSettled()
4519 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4520
4521 self.executor_server.release('D')
4522 self.waitUntilSettled()
4523 self.assertEqual([b.name for b in self.builds], ['C'])
4524
4525 self.executor_server.release('C')
4526 self.waitUntilSettled()
4527 self.assertEqual(len(self.builds), 0)
4528
4529 self.executor_server.hold_jobs_in_build = False
4530 self.executor_server.release()
4531 self.waitUntilSettled()
4532
4533 self.assertEqual(len(self.builds), 0)
4534 self.assertEqual(len(self.history), 4)
4535
4536 self.assertEqual(change.data['status'], 'NEW')
4537 self.assertEqual(change.reported, 2)
4538
4539
Arieb6f068c2016-10-09 13:11:06 +03004540class TestDuplicatePipeline(ZuulTestCase):
4541 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4542
4543 def test_duplicate_pipelines(self):
4544 "Test that a change matching multiple pipelines works"
4545
4546 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4547 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4548 self.waitUntilSettled()
4549
4550 self.assertHistory([
4551 dict(name='project-test1', result='SUCCESS', changes='1,1',
4552 pipeline='dup1'),
4553 dict(name='project-test1', result='SUCCESS', changes='1,1',
4554 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004555 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004556
4557 self.assertEqual(len(A.messages), 2)
4558
Paul Belangera46a3742016-11-09 14:23:03 -05004559 if 'dup1' in A.messages[0]:
4560 self.assertIn('dup1', A.messages[0])
4561 self.assertNotIn('dup2', A.messages[0])
4562 self.assertIn('project-test1', A.messages[0])
4563 self.assertIn('dup2', A.messages[1])
4564 self.assertNotIn('dup1', A.messages[1])
4565 self.assertIn('project-test1', A.messages[1])
4566 else:
4567 self.assertIn('dup1', A.messages[1])
4568 self.assertNotIn('dup2', A.messages[1])
4569 self.assertIn('project-test1', A.messages[1])
4570 self.assertIn('dup2', A.messages[0])
4571 self.assertNotIn('dup1', A.messages[0])
4572 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004573
4574
Paul Belanger86085b32016-11-03 12:48:57 -04004575class TestSchedulerTemplatedProject(ZuulTestCase):
4576 tenant_config_file = 'config/templated-project/main.yaml'
4577
Paul Belanger174a8272017-03-14 13:20:10 -04004578 def test_job_from_templates_executed(self):
4579 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004580
4581 A = self.fake_gerrit.addFakeChange(
4582 'org/templated-project', 'master', 'A')
4583 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4584 self.waitUntilSettled()
4585
4586 self.assertEqual(self.getJobFromHistory('project-test1').result,
4587 'SUCCESS')
4588 self.assertEqual(self.getJobFromHistory('project-test2').result,
4589 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004590
4591 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004592 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004593
4594 A = self.fake_gerrit.addFakeChange(
4595 'org/layered-project', 'master', 'A')
4596 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4597 self.waitUntilSettled()
4598
4599 self.assertEqual(self.getJobFromHistory('project-test1').result,
4600 'SUCCESS')
4601 self.assertEqual(self.getJobFromHistory('project-test2').result,
4602 'SUCCESS')
4603 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4604 ).result, 'SUCCESS')
4605 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4606 ).result, 'SUCCESS')
4607 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4608 ).result, 'SUCCESS')
4609 self.assertEqual(self.getJobFromHistory('project-test6').result,
4610 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004611
4612
4613class TestSchedulerSuccessURL(ZuulTestCase):
4614 tenant_config_file = 'config/success-url/main.yaml'
4615
4616 def test_success_url(self):
4617 "Ensure bad build params are ignored"
4618 self.sched.reconfigure(self.config)
4619 self.init_repo('org/docs')
4620
4621 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4622 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4623 self.waitUntilSettled()
4624
4625 # Both builds ran: docs-draft-test + docs-draft-test2
4626 self.assertEqual(len(self.history), 2)
4627
4628 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004629 for build in self.history:
4630 if build.name == 'docs-draft-test':
4631 uuid = build.uuid[:7]
4632 break
Adam Gandelman94a60062016-11-21 16:43:14 -08004633
4634 # Two msgs: 'Starting...' + results
4635 self.assertEqual(len(self.smtp_messages), 2)
4636 body = self.smtp_messages[1]['body'].splitlines()
4637 self.assertEqual('Build succeeded.', body[0])
4638
4639 self.assertIn(
4640 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4641 'docs-draft-test/%s/publish-docs/' % uuid,
4642 body[2])
4643
Paul Belanger174a8272017-03-14 13:20:10 -04004644 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004645 self.assertIn(
4646 '- docs-draft-test2 https://server/job',
4647 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004648
4649
Adam Gandelman4da00f62016-12-09 15:47:33 -08004650class TestSchedulerMerges(ZuulTestCase):
4651 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004652
4653 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004654 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004655 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004656 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004657 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4658 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4659 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4660 A.addApproval('code-review', 2)
4661 B.addApproval('code-review', 2)
4662 C.addApproval('code-review', 2)
4663 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4664 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4665 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4666 self.waitUntilSettled()
4667
4668 build = self.builds[-1]
4669 ref = self.getParameter(build, 'ZUUL_REF')
4670
James E. Blair2a535672017-04-27 12:03:15 -07004671 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4672 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004673 repo = git.Repo(path)
4674 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4675 repo_messages.reverse()
4676
Paul Belanger174a8272017-03-14 13:20:10 -04004677 self.executor_server.hold_jobs_in_build = False
4678 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004679 self.waitUntilSettled()
4680
4681 return repo_messages
4682
4683 def _test_merge(self, mode):
4684 us_path = os.path.join(
4685 self.upstream_root, 'org/project-%s' % mode)
4686 expected_messages = [
4687 'initial commit',
4688 'add content from fixture',
4689 # the intermediate commits order is nondeterministic
4690 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4691 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4692 ]
4693 result = self._test_project_merge_mode(mode)
4694 self.assertEqual(result[:2], expected_messages[:2])
4695 self.assertEqual(result[-2:], expected_messages[-2:])
4696
4697 def test_project_merge_mode_merge(self):
4698 self._test_merge('merge')
4699
4700 def test_project_merge_mode_merge_resolve(self):
4701 self._test_merge('merge-resolve')
4702
4703 def test_project_merge_mode_cherrypick(self):
4704 expected_messages = [
4705 'initial commit',
4706 'add content from fixture',
4707 'A-1',
4708 'B-1',
4709 'C-1']
4710 result = self._test_project_merge_mode('cherry-pick')
4711 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004712
4713 def test_merge_branch(self):
4714 "Test that the right commits are on alternate branches"
4715 self.create_branch('org/project-merge-branches', 'mp')
4716
Paul Belanger174a8272017-03-14 13:20:10 -04004717 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004718 A = self.fake_gerrit.addFakeChange(
4719 'org/project-merge-branches', 'mp', 'A')
4720 B = self.fake_gerrit.addFakeChange(
4721 'org/project-merge-branches', 'mp', 'B')
4722 C = self.fake_gerrit.addFakeChange(
4723 'org/project-merge-branches', 'mp', 'C')
4724 A.addApproval('code-review', 2)
4725 B.addApproval('code-review', 2)
4726 C.addApproval('code-review', 2)
4727 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4728 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4729 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4730 self.waitUntilSettled()
4731
Paul Belanger174a8272017-03-14 13:20:10 -04004732 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004733 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004734 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004735 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004736 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004737 self.waitUntilSettled()
4738
4739 build = self.builds[-1]
4740 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
4741 ref = self.getParameter(build, 'ZUUL_REF')
James E. Blair2a535672017-04-27 12:03:15 -07004742 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4743 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004744 repo = git.Repo(path)
4745
4746 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4747 repo_messages.reverse()
4748 correct_messages = [
4749 'initial commit',
4750 'add content from fixture',
4751 'mp commit',
4752 'A-1', 'B-1', 'C-1']
4753 self.assertEqual(repo_messages, correct_messages)
4754
Paul Belanger174a8272017-03-14 13:20:10 -04004755 self.executor_server.hold_jobs_in_build = False
4756 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004757 self.waitUntilSettled()
4758
4759 def test_merge_multi_branch(self):
4760 "Test that dependent changes on multiple branches are merged"
4761 self.create_branch('org/project-merge-branches', 'mp')
4762
Paul Belanger174a8272017-03-14 13:20:10 -04004763 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004764 A = self.fake_gerrit.addFakeChange(
4765 'org/project-merge-branches', 'master', 'A')
4766 B = self.fake_gerrit.addFakeChange(
4767 'org/project-merge-branches', 'mp', 'B')
4768 C = self.fake_gerrit.addFakeChange(
4769 'org/project-merge-branches', 'master', 'C')
4770 A.addApproval('code-review', 2)
4771 B.addApproval('code-review', 2)
4772 C.addApproval('code-review', 2)
4773 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4774 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4775 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4776 self.waitUntilSettled()
4777
4778 job_A = None
4779 for job in self.builds:
4780 if 'project-merge' in job.name:
4781 job_A = job
4782 ref_A = self.getParameter(job_A, 'ZUUL_REF')
4783 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
4784 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
4785 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
4786
James E. Blair2a535672017-04-27 12:03:15 -07004787 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
4788 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004789 repo = git.Repo(path)
4790 repo_messages = [c.message.strip()
4791 for c in repo.iter_commits(ref_A)]
4792 repo_messages.reverse()
4793 correct_messages = [
4794 'initial commit', 'add content from fixture', 'A-1']
4795 self.assertEqual(repo_messages, correct_messages)
4796
Paul Belanger174a8272017-03-14 13:20:10 -04004797 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004798 self.waitUntilSettled()
4799
4800 job_B = None
4801 for job in self.builds:
4802 if 'project-merge' in job.name:
4803 job_B = job
4804 ref_B = self.getParameter(job_B, 'ZUUL_REF')
4805 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
4806 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
4807 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
4808
James E. Blair2a535672017-04-27 12:03:15 -07004809 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
4810 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004811 repo = git.Repo(path)
4812 repo_messages = [c.message.strip()
4813 for c in repo.iter_commits(ref_B)]
4814 repo_messages.reverse()
4815 correct_messages = [
4816 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4817 self.assertEqual(repo_messages, correct_messages)
4818
Paul Belanger174a8272017-03-14 13:20:10 -04004819 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004820 self.waitUntilSettled()
4821
4822 job_C = None
4823 for job in self.builds:
4824 if 'project-merge' in job.name:
4825 job_C = job
4826 ref_C = self.getParameter(job_C, 'ZUUL_REF')
4827 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
4828 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
4829 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair2a535672017-04-27 12:03:15 -07004830 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
4831 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004832 repo = git.Repo(path)
4833 repo_messages = [c.message.strip()
4834 for c in repo.iter_commits(ref_C)]
4835
4836 repo_messages.reverse()
4837 correct_messages = [
4838 'initial commit', 'add content from fixture',
4839 'A-1', 'C-1']
4840 # Ensure the right commits are in the history for this ref
4841 self.assertEqual(repo_messages, correct_messages)
4842
Paul Belanger174a8272017-03-14 13:20:10 -04004843 self.executor_server.hold_jobs_in_build = False
4844 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004845 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01004846
4847
James E. Blair9ea0d0b2017-04-20 09:27:15 -07004848class TestSemaphore(ZuulTestCase):
4849 tenant_config_file = 'config/semaphore/main.yaml'
4850
4851 def test_semaphore_one(self):
4852 "Test semaphores with max=1 (mutex)"
4853 tenant = self.sched.abide.tenants.get('tenant-one')
4854
4855 self.executor_server.hold_jobs_in_build = True
4856
4857 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4858 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4859 self.assertFalse('test-semaphore' in
4860 tenant.semaphore_handler.semaphores)
4861
4862 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4863 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4864 self.waitUntilSettled()
4865
4866 self.assertEqual(len(self.builds), 3)
4867 self.assertEqual(self.builds[0].name, 'project-test1')
4868 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
4869 self.assertEqual(self.builds[2].name, 'project-test1')
4870
4871 self.executor_server.release('semaphore-one-test1')
4872 self.waitUntilSettled()
4873
4874 self.assertEqual(len(self.builds), 3)
4875 self.assertEqual(self.builds[0].name, 'project-test1')
4876 self.assertEqual(self.builds[1].name, 'project-test1')
4877 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4878 self.assertTrue('test-semaphore' in
4879 tenant.semaphore_handler.semaphores)
4880
4881 self.executor_server.release('semaphore-one-test2')
4882 self.waitUntilSettled()
4883
4884 self.assertEqual(len(self.builds), 3)
4885 self.assertEqual(self.builds[0].name, 'project-test1')
4886 self.assertEqual(self.builds[1].name, 'project-test1')
4887 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
4888 self.assertTrue('test-semaphore' in
4889 tenant.semaphore_handler.semaphores)
4890
4891 self.executor_server.release('semaphore-one-test1')
4892 self.waitUntilSettled()
4893
4894 self.assertEqual(len(self.builds), 3)
4895 self.assertEqual(self.builds[0].name, 'project-test1')
4896 self.assertEqual(self.builds[1].name, 'project-test1')
4897 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4898 self.assertTrue('test-semaphore' in
4899 tenant.semaphore_handler.semaphores)
4900
4901 self.executor_server.release('semaphore-one-test2')
4902 self.waitUntilSettled()
4903
4904 self.assertEqual(len(self.builds), 2)
4905 self.assertEqual(self.builds[0].name, 'project-test1')
4906 self.assertEqual(self.builds[1].name, 'project-test1')
4907 self.assertFalse('test-semaphore' in
4908 tenant.semaphore_handler.semaphores)
4909
4910 self.executor_server.hold_jobs_in_build = False
4911 self.executor_server.release()
4912
4913 self.waitUntilSettled()
4914 self.assertEqual(len(self.builds), 0)
4915
4916 self.assertEqual(A.reported, 1)
4917 self.assertEqual(B.reported, 1)
4918 self.assertFalse('test-semaphore' in
4919 tenant.semaphore_handler.semaphores)
4920
4921 def test_semaphore_two(self):
4922 "Test semaphores with max>1"
4923 tenant = self.sched.abide.tenants.get('tenant-one')
4924
4925 self.executor_server.hold_jobs_in_build = True
4926 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4927 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4928 self.assertFalse('test-semaphore-two' in
4929 tenant.semaphore_handler.semaphores)
4930
4931 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4932 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4933 self.waitUntilSettled()
4934
4935 self.assertEqual(len(self.builds), 4)
4936 self.assertEqual(self.builds[0].name, 'project-test1')
4937 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
4938 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
4939 self.assertEqual(self.builds[3].name, 'project-test1')
4940 self.assertTrue('test-semaphore-two' in
4941 tenant.semaphore_handler.semaphores)
4942 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4943 'test-semaphore-two', [])), 2)
4944
4945 self.executor_server.release('semaphore-two-test1')
4946 self.waitUntilSettled()
4947
4948 self.assertEqual(len(self.builds), 4)
4949 self.assertEqual(self.builds[0].name, 'project-test1')
4950 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
4951 self.assertEqual(self.builds[2].name, 'project-test1')
4952 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
4953 self.assertTrue('test-semaphore-two' in
4954 tenant.semaphore_handler.semaphores)
4955 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4956 'test-semaphore-two', [])), 2)
4957
4958 self.executor_server.release('semaphore-two-test2')
4959 self.waitUntilSettled()
4960
4961 self.assertEqual(len(self.builds), 4)
4962 self.assertEqual(self.builds[0].name, 'project-test1')
4963 self.assertEqual(self.builds[1].name, 'project-test1')
4964 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
4965 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
4966 self.assertTrue('test-semaphore-two' in
4967 tenant.semaphore_handler.semaphores)
4968 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4969 'test-semaphore-two', [])), 2)
4970
4971 self.executor_server.release('semaphore-two-test1')
4972 self.waitUntilSettled()
4973
4974 self.assertEqual(len(self.builds), 3)
4975 self.assertEqual(self.builds[0].name, 'project-test1')
4976 self.assertEqual(self.builds[1].name, 'project-test1')
4977 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
4978 self.assertTrue('test-semaphore-two' in
4979 tenant.semaphore_handler.semaphores)
4980 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4981 'test-semaphore-two', [])), 1)
4982
4983 self.executor_server.release('semaphore-two-test2')
4984 self.waitUntilSettled()
4985
4986 self.assertEqual(len(self.builds), 2)
4987 self.assertEqual(self.builds[0].name, 'project-test1')
4988 self.assertEqual(self.builds[1].name, 'project-test1')
4989 self.assertFalse('test-semaphore-two' in
4990 tenant.semaphore_handler.semaphores)
4991
4992 self.executor_server.hold_jobs_in_build = False
4993 self.executor_server.release()
4994
4995 self.waitUntilSettled()
4996 self.assertEqual(len(self.builds), 0)
4997
4998 self.assertEqual(A.reported, 1)
4999 self.assertEqual(B.reported, 1)
5000
5001 def test_semaphore_abandon(self):
5002 "Test abandon with job semaphores"
5003 self.executor_server.hold_jobs_in_build = True
5004 tenant = self.sched.abide.tenants.get('tenant-one')
5005 check_pipeline = tenant.layout.pipelines['check']
5006
5007 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5008 self.assertFalse('test-semaphore' in
5009 tenant.semaphore_handler.semaphores)
5010
5011 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5012 self.waitUntilSettled()
5013
5014 self.assertTrue('test-semaphore' in
5015 tenant.semaphore_handler.semaphores)
5016
5017 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5018 self.waitUntilSettled()
5019
5020 # The check pipeline should be empty
5021 items = check_pipeline.getAllItems()
5022 self.assertEqual(len(items), 0)
5023
5024 # The semaphore should be released
5025 self.assertFalse('test-semaphore' in
5026 tenant.semaphore_handler.semaphores)
5027
5028 self.executor_server.hold_jobs_in_build = False
5029 self.executor_server.release()
5030 self.waitUntilSettled()
5031
5032 def test_semaphore_reconfigure(self):
5033 "Test reconfigure with job semaphores"
5034 self.executor_server.hold_jobs_in_build = True
5035 tenant = self.sched.abide.tenants.get('tenant-one')
5036 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5037 self.assertFalse('test-semaphore' in
5038 tenant.semaphore_handler.semaphores)
5039
5040 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5041 self.waitUntilSettled()
5042
5043 self.assertTrue('test-semaphore' in
5044 tenant.semaphore_handler.semaphores)
5045
5046 # reconfigure without layout change
5047 self.sched.reconfigure(self.config)
5048 self.waitUntilSettled()
5049 tenant = self.sched.abide.tenants.get('tenant-one')
5050
5051 # semaphore still must be held
5052 self.assertTrue('test-semaphore' in
5053 tenant.semaphore_handler.semaphores)
5054
5055 self.commitConfigUpdate(
5056 'common-config',
5057 'config/semaphore/zuul-reconfiguration.yaml')
5058 self.sched.reconfigure(self.config)
5059 self.waitUntilSettled()
5060 tenant = self.sched.abide.tenants.get('tenant-one')
5061
5062 self.executor_server.release('project-test1')
5063 self.waitUntilSettled()
5064
5065 # There should be no builds anymore
5066 self.assertEqual(len(self.builds), 0)
5067
5068 # The semaphore should be released
5069 self.assertFalse('test-semaphore' in
5070 tenant.semaphore_handler.semaphores)
5071
5072
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005073class TestSemaphoreMultiTenant(ZuulTestCase):
5074 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5075
5076 def test_semaphore_tenant_isolation(self):
5077 "Test semaphores in multiple tenants"
5078
5079 self.waitUntilSettled()
5080 tenant_one = self.sched.abide.tenants.get('tenant-one')
5081 tenant_two = self.sched.abide.tenants.get('tenant-two')
5082
5083 self.executor_server.hold_jobs_in_build = True
5084 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5085 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5086 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5087 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5088 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5089 self.assertFalse('test-semaphore' in
5090 tenant_one.semaphore_handler.semaphores)
5091 self.assertFalse('test-semaphore' in
5092 tenant_two.semaphore_handler.semaphores)
5093
5094 # add patches to project1 of tenant-one
5095 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5096 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5097 self.waitUntilSettled()
5098
5099 # one build of project1-test1 must run
5100 # semaphore of tenant-one must be acquired once
5101 # semaphore of tenant-two must not be acquired
5102 self.assertEqual(len(self.builds), 1)
5103 self.assertEqual(self.builds[0].name, 'project1-test1')
5104 self.assertTrue('test-semaphore' in
5105 tenant_one.semaphore_handler.semaphores)
5106 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5107 'test-semaphore', [])), 1)
5108 self.assertFalse('test-semaphore' in
5109 tenant_two.semaphore_handler.semaphores)
5110
5111 # add patches to project2 of tenant-two
5112 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5113 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5114 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5115 self.waitUntilSettled()
5116
5117 # one build of project1-test1 must run
5118 # two builds of project2-test1 must run
5119 # semaphore of tenant-one must be acquired once
5120 # semaphore of tenant-two must be acquired twice
5121 self.assertEqual(len(self.builds), 3)
5122 self.assertEqual(self.builds[0].name, 'project1-test1')
5123 self.assertEqual(self.builds[1].name, 'project2-test1')
5124 self.assertEqual(self.builds[2].name, 'project2-test1')
5125 self.assertTrue('test-semaphore' in
5126 tenant_one.semaphore_handler.semaphores)
5127 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5128 'test-semaphore', [])), 1)
5129 self.assertTrue('test-semaphore' in
5130 tenant_two.semaphore_handler.semaphores)
5131 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5132 'test-semaphore', [])), 2)
5133
5134 self.executor_server.release('project1-test1')
5135 self.waitUntilSettled()
5136
5137 # one build of project1-test1 must run
5138 # two builds of project2-test1 must run
5139 # semaphore of tenant-one must be acquired once
5140 # semaphore of tenant-two must be acquired twice
5141 self.assertEqual(len(self.builds), 3)
5142 self.assertEqual(self.builds[0].name, 'project2-test1')
5143 self.assertEqual(self.builds[1].name, 'project2-test1')
5144 self.assertEqual(self.builds[2].name, 'project1-test1')
5145 self.assertTrue('test-semaphore' in
5146 tenant_one.semaphore_handler.semaphores)
5147 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5148 'test-semaphore', [])), 1)
5149 self.assertTrue('test-semaphore' in
5150 tenant_two.semaphore_handler.semaphores)
5151 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5152 'test-semaphore', [])), 2)
5153
5154 self.executor_server.release('project2-test1')
5155 self.waitUntilSettled()
5156
5157 # one build of project1-test1 must run
5158 # one build of project2-test1 must run
5159 # semaphore of tenant-one must be acquired once
5160 # semaphore of tenant-two must be acquired once
5161 self.assertEqual(len(self.builds), 2)
5162 self.assertTrue('test-semaphore' in
5163 tenant_one.semaphore_handler.semaphores)
5164 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5165 'test-semaphore', [])), 1)
5166 self.assertTrue('test-semaphore' in
5167 tenant_two.semaphore_handler.semaphores)
5168 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5169 'test-semaphore', [])), 1)
5170
5171 self.executor_server.hold_jobs_in_build = False
5172 self.executor_server.release()
5173
5174 self.waitUntilSettled()
5175
5176 # no build must run
5177 # semaphore of tenant-one must not be acquired
5178 # semaphore of tenant-two must not be acquired
5179 self.assertEqual(len(self.builds), 0)
5180 self.assertFalse('test-semaphore' in
5181 tenant_one.semaphore_handler.semaphores)
5182 self.assertFalse('test-semaphore' in
5183 tenant_two.semaphore_handler.semaphores)
5184
5185 self.assertEqual(A.reported, 1)
5186 self.assertEqual(B.reported, 1)
5187
5188
5189class TestSemaphoreInRepo(ZuulTestCase):
5190 tenant_config_file = 'config/in-repo/main.yaml'
5191
5192 def test_semaphore_in_repo(self):
5193 "Test semaphores in repo config"
5194
5195 # This tests dynamic semaphore handling in project repos. The semaphore
5196 # max value should not be evaluated dynamically but must be updated
5197 # after the change lands.
5198
5199 self.waitUntilSettled()
5200 tenant = self.sched.abide.tenants.get('tenant-one')
5201
5202 in_repo_conf = textwrap.dedent(
5203 """
5204 - job:
5205 name: project-test2
5206 semaphore: test-semaphore
5207
5208 - project:
5209 name: org/project
5210 tenant-one-gate:
5211 jobs:
5212 - project-test2
5213
5214 # the max value in dynamic layout must be ignored
5215 - semaphore:
5216 name: test-semaphore
5217 max: 2
5218 """)
5219
5220 in_repo_playbook = textwrap.dedent(
5221 """
5222 - hosts: all
5223 tasks: []
5224 """)
5225
5226 file_dict = {'.zuul.yaml': in_repo_conf,
5227 'playbooks/project-test2.yaml': in_repo_playbook}
5228 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5229 files=file_dict)
5230 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5231 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5232 B.setDependsOn(A, 1)
5233 C.setDependsOn(A, 1)
5234
5235 self.executor_server.hold_jobs_in_build = True
5236
5237 A.addApproval('code-review', 2)
5238 B.addApproval('code-review', 2)
5239 C.addApproval('code-review', 2)
5240 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
5241 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
5242 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
5243 self.waitUntilSettled()
5244
5245 # check that the layout in a queue item still has max value of 1
5246 # for test-semaphore
5247 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5248 queue = None
5249 for queue_candidate in pipeline.queues:
5250 if queue_candidate.name == 'org/project':
5251 queue = queue_candidate
5252 break
5253 queue_item = queue.queue[0]
5254 item_dynamic_layout = queue_item.current_build_set.layout
5255 dynamic_test_semaphore = \
5256 item_dynamic_layout.semaphores.get('test-semaphore')
5257 self.assertEqual(dynamic_test_semaphore.max, 1)
5258
5259 # one build must be in queue, one semaphores acquired
5260 self.assertEqual(len(self.builds), 1)
5261 self.assertEqual(self.builds[0].name, 'project-test2')
5262 self.assertTrue('test-semaphore' in
5263 tenant.semaphore_handler.semaphores)
5264 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5265 'test-semaphore', [])), 1)
5266
5267 self.executor_server.release('project-test2')
5268 self.waitUntilSettled()
5269
5270 # change A must be merged
5271 self.assertEqual(A.data['status'], 'MERGED')
5272 self.assertEqual(A.reported, 2)
5273
5274 # send change-merged event as the gerrit mock doesn't send it
5275 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5276 self.waitUntilSettled()
5277
5278 # now that change A was merged, the new semaphore max must be effective
5279 tenant = self.sched.abide.tenants.get('tenant-one')
5280 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5281
5282 # two builds must be in queue, two semaphores acquired
5283 self.assertEqual(len(self.builds), 2)
5284 self.assertEqual(self.builds[0].name, 'project-test2')
5285 self.assertEqual(self.builds[1].name, 'project-test2')
5286 self.assertTrue('test-semaphore' in
5287 tenant.semaphore_handler.semaphores)
5288 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5289 'test-semaphore', [])), 2)
5290
5291 self.executor_server.release('project-test2')
5292 self.waitUntilSettled()
5293
5294 self.assertEqual(len(self.builds), 0)
5295 self.assertFalse('test-semaphore' in
5296 tenant.semaphore_handler.semaphores)
5297
5298 self.executor_server.hold_jobs_in_build = False
5299 self.executor_server.release()
5300
5301 self.waitUntilSettled()
5302 self.assertEqual(len(self.builds), 0)
5303
5304 self.assertEqual(A.reported, 2)
5305 self.assertEqual(B.reported, 2)
5306 self.assertEqual(C.reported, 2)