blob: afc3682c4d3bfa926b62434f98fa88a60f528c57 [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)
Paul Belanger174a8272017-03-14 13:20:10 -0400533 self.assertEqual(queue[0].name, 'executor:execute')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800534 job_args = json.loads(queue[0].arguments)
535 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(
550 json.loads(queue[0].arguments)['job'], 'project-test1')
551 self.assertEqual(
552 json.loads(queue[1].arguments)['job'], 'project-test2')
553 self.assertEqual(
554 json.loads(queue[2].arguments)['job'], 'project-test1')
555 self.assertEqual(
556 json.loads(queue[3].arguments)['job'], 'project-test2')
557 self.assertEqual(
558 json.loads(queue[4].arguments)['job'], 'project-test1')
559 self.assertEqual(
560 json.loads(queue[5].arguments)['job'], 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700561
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700562 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700563 self.waitUntilSettled()
564
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400565 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700566 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400567 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
568 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700569
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700570 self.gearman_server.hold_jobs_in_queue = False
571 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700572 self.waitUntilSettled()
573
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400574 self.assertEqual(len(self.builds), 0)
575 self.assertEqual(len(self.history), 11)
576 self.assertEqual(A.data['status'], 'NEW')
577 self.assertEqual(B.data['status'], 'MERGED')
578 self.assertEqual(C.data['status'], 'MERGED')
579 self.assertEqual(A.reported, 2)
580 self.assertEqual(B.reported, 2)
581 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700582
James E. Blairec056492016-07-22 09:45:56 -0700583 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700584 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400585 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700586 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -0700587 A.addApproval('code-review', 2)
588 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700589 self.waitUntilSettled()
590 time.sleep(2)
591
592 data = json.loads(self.sched.formatStatusJSON())
593 found_job = None
594 for pipeline in data['pipelines']:
595 if pipeline['name'] != 'gate':
596 continue
597 for queue in pipeline['change_queues']:
598 for head in queue['heads']:
599 for item in head:
600 for job in item['jobs']:
601 if job['name'] == 'project-merge':
602 found_job = job
603 break
604
605 self.assertIsNotNone(found_job)
606 if iteration == 1:
607 self.assertIsNotNone(found_job['estimated_time'])
608 self.assertIsNone(found_job['remaining_time'])
609 else:
610 self.assertIsNotNone(found_job['estimated_time'])
611 self.assertTrue(found_job['estimated_time'] >= 2)
612 self.assertIsNotNone(found_job['remaining_time'])
613
Paul Belanger174a8272017-03-14 13:20:10 -0400614 self.executor_server.hold_jobs_in_build = False
615 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700616 self.waitUntilSettled()
617
James E. Blairec056492016-07-22 09:45:56 -0700618 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700619 def test_time_database(self):
620 "Test the time database"
621
622 self._test_time_database(1)
623 self._test_time_database(2)
624
James E. Blairfef71632013-09-23 11:15:47 -0700625 def test_two_failed_changes_at_head(self):
626 "Test that changes are reparented correctly if 2 fail at head"
627
Paul Belanger174a8272017-03-14 13:20:10 -0400628 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700629 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
630 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
631 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700632 A.addApproval('code-review', 2)
633 B.addApproval('code-review', 2)
634 C.addApproval('code-review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700635
Paul Belanger174a8272017-03-14 13:20:10 -0400636 self.executor_server.failJob('project-test1', A)
637 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700638
James E. Blair8b5408c2016-08-08 15:37:46 -0700639 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
640 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
641 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700642 self.waitUntilSettled()
643
Paul Belanger174a8272017-03-14 13:20:10 -0400644 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700645 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400646 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700647 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400648 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700649 self.waitUntilSettled()
650
651 self.assertEqual(len(self.builds), 6)
652 self.assertEqual(self.builds[0].name, 'project-test1')
653 self.assertEqual(self.builds[1].name, 'project-test2')
654 self.assertEqual(self.builds[2].name, 'project-test1')
655 self.assertEqual(self.builds[3].name, 'project-test2')
656 self.assertEqual(self.builds[4].name, 'project-test1')
657 self.assertEqual(self.builds[5].name, 'project-test2')
658
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400659 self.assertTrue(self.builds[0].hasChanges(A))
660 self.assertTrue(self.builds[2].hasChanges(A))
661 self.assertTrue(self.builds[2].hasChanges(B))
662 self.assertTrue(self.builds[4].hasChanges(A))
663 self.assertTrue(self.builds[4].hasChanges(B))
664 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700665
666 # Fail change B first
667 self.release(self.builds[2])
668 self.waitUntilSettled()
669
670 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400671 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700672 self.waitUntilSettled()
673
674 self.assertEqual(len(self.builds), 5)
675 self.assertEqual(self.builds[0].name, 'project-test1')
676 self.assertEqual(self.builds[1].name, 'project-test2')
677 self.assertEqual(self.builds[2].name, 'project-test2')
678 self.assertEqual(self.builds[3].name, 'project-test1')
679 self.assertEqual(self.builds[4].name, 'project-test2')
680
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400681 self.assertTrue(self.builds[1].hasChanges(A))
682 self.assertTrue(self.builds[2].hasChanges(A))
683 self.assertTrue(self.builds[2].hasChanges(B))
684 self.assertTrue(self.builds[4].hasChanges(A))
685 self.assertFalse(self.builds[4].hasChanges(B))
686 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700687
688 # Finish running all passing jobs for change A
689 self.release(self.builds[1])
690 self.waitUntilSettled()
691 # Fail and report change A
692 self.release(self.builds[0])
693 self.waitUntilSettled()
694
695 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400696 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700697 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400698 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700699 self.waitUntilSettled()
700
701 self.assertEqual(len(self.builds), 4)
702 self.assertEqual(self.builds[0].name, 'project-test1') # B
703 self.assertEqual(self.builds[1].name, 'project-test2') # B
704 self.assertEqual(self.builds[2].name, 'project-test1') # C
705 self.assertEqual(self.builds[3].name, 'project-test2') # C
706
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400707 self.assertFalse(self.builds[1].hasChanges(A))
708 self.assertTrue(self.builds[1].hasChanges(B))
709 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700710
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400711 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700712 # After A failed and B and C restarted, B should be back in
713 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400714 self.assertTrue(self.builds[2].hasChanges(B))
715 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700716
Paul Belanger174a8272017-03-14 13:20:10 -0400717 self.executor_server.hold_jobs_in_build = False
718 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700719 self.waitUntilSettled()
720
721 self.assertEqual(len(self.builds), 0)
722 self.assertEqual(len(self.history), 21)
723 self.assertEqual(A.data['status'], 'NEW')
724 self.assertEqual(B.data['status'], 'NEW')
725 self.assertEqual(C.data['status'], 'MERGED')
726 self.assertEqual(A.reported, 2)
727 self.assertEqual(B.reported, 2)
728 self.assertEqual(C.reported, 2)
729
James E. Blair8c803f82012-07-31 16:25:42 -0700730 def test_patch_order(self):
731 "Test that dependent patches are tested in the right order"
732 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
733 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
734 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700735 A.addApproval('code-review', 2)
736 B.addApproval('code-review', 2)
737 C.addApproval('code-review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700738
739 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
740 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
741 M2.setMerged()
742 M1.setMerged()
743
744 # C -> B -> A -> M1 -> M2
745 # M2 is here to make sure it is never queried. If it is, it
746 # means zuul is walking down the entire history of merged
747 # changes.
748
749 C.setDependsOn(B, 1)
750 B.setDependsOn(A, 1)
751 A.setDependsOn(M1, 1)
752 M1.setDependsOn(M2, 1)
753
James E. Blair8b5408c2016-08-08 15:37:46 -0700754 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700755
756 self.waitUntilSettled()
757
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400758 self.assertEqual(A.data['status'], 'NEW')
759 self.assertEqual(B.data['status'], 'NEW')
760 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700761
James E. Blair8b5408c2016-08-08 15:37:46 -0700762 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
763 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700764
765 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400766 self.assertEqual(M2.queried, 0)
767 self.assertEqual(A.data['status'], 'MERGED')
768 self.assertEqual(B.data['status'], 'MERGED')
769 self.assertEqual(C.data['status'], 'MERGED')
770 self.assertEqual(A.reported, 2)
771 self.assertEqual(B.reported, 2)
772 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700773
James E. Blair063672f2015-01-29 13:09:12 -0800774 def test_needed_changes_enqueue(self):
775 "Test that a needed change is enqueued ahead"
776 # A Given a git tree like this, if we enqueue
777 # / \ change C, we should walk up and down the tree
778 # B G and enqueue changes in the order ABCDEFG.
779 # /|\ This is also the order that you would get if
780 # *C E F you enqueued changes in the order ABCDEFG, so
781 # / the ordering is stable across re-enqueue events.
782 # D
783
784 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
785 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
786 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
787 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
788 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
789 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
790 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
791 B.setDependsOn(A, 1)
792 C.setDependsOn(B, 1)
793 D.setDependsOn(C, 1)
794 E.setDependsOn(B, 1)
795 F.setDependsOn(B, 1)
796 G.setDependsOn(A, 1)
797
James E. Blair8b5408c2016-08-08 15:37:46 -0700798 A.addApproval('code-review', 2)
799 B.addApproval('code-review', 2)
800 C.addApproval('code-review', 2)
801 D.addApproval('code-review', 2)
802 E.addApproval('code-review', 2)
803 F.addApproval('code-review', 2)
804 G.addApproval('code-review', 2)
805 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800806
807 self.waitUntilSettled()
808
809 self.assertEqual(A.data['status'], 'NEW')
810 self.assertEqual(B.data['status'], 'NEW')
811 self.assertEqual(C.data['status'], 'NEW')
812 self.assertEqual(D.data['status'], 'NEW')
813 self.assertEqual(E.data['status'], 'NEW')
814 self.assertEqual(F.data['status'], 'NEW')
815 self.assertEqual(G.data['status'], 'NEW')
816
817 # We're about to add approvals to changes without adding the
818 # triggering events to Zuul, so that we can be sure that it is
819 # enqueing the changes based on dependencies, not because of
820 # triggering events. Since it will have the changes cached
821 # already (without approvals), we need to clear the cache
822 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400823 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100824 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800825
Paul Belanger174a8272017-03-14 13:20:10 -0400826 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -0700827 A.addApproval('approved', 1)
828 B.addApproval('approved', 1)
829 D.addApproval('approved', 1)
830 E.addApproval('approved', 1)
831 F.addApproval('approved', 1)
832 G.addApproval('approved', 1)
833 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800834
835 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400836 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800837 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400838 self.executor_server.hold_jobs_in_build = False
839 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800840 self.waitUntilSettled()
841
842 self.assertEqual(A.data['status'], 'MERGED')
843 self.assertEqual(B.data['status'], 'MERGED')
844 self.assertEqual(C.data['status'], 'MERGED')
845 self.assertEqual(D.data['status'], 'MERGED')
846 self.assertEqual(E.data['status'], 'MERGED')
847 self.assertEqual(F.data['status'], 'MERGED')
848 self.assertEqual(G.data['status'], 'MERGED')
849 self.assertEqual(A.reported, 2)
850 self.assertEqual(B.reported, 2)
851 self.assertEqual(C.reported, 2)
852 self.assertEqual(D.reported, 2)
853 self.assertEqual(E.reported, 2)
854 self.assertEqual(F.reported, 2)
855 self.assertEqual(G.reported, 2)
856 self.assertEqual(self.history[6].changes,
857 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
858
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100859 def test_source_cache(self):
860 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400861 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700862
863 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
864 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
865 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
James E. Blair8b5408c2016-08-08 15:37:46 -0700866 A.addApproval('code-review', 2)
867 B.addApproval('code-review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700868
869 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
870 M1.setMerged()
871
872 B.setDependsOn(A, 1)
873 A.setDependsOn(M1, 1)
874
James E. Blair8b5408c2016-08-08 15:37:46 -0700875 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700876 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
877
878 self.waitUntilSettled()
879
880 for build in self.builds:
881 if build.parameters['ZUUL_PIPELINE'] == 'check':
882 build.release()
883 self.waitUntilSettled()
884 for build in self.builds:
885 if build.parameters['ZUUL_PIPELINE'] == 'check':
886 build.release()
887 self.waitUntilSettled()
888
James E. Blair8b5408c2016-08-08 15:37:46 -0700889 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700890 self.waitUntilSettled()
891
Joshua Hesketh352264b2015-08-11 23:42:08 +1000892 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700893 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000894 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700895
Paul Belanger174a8272017-03-14 13:20:10 -0400896 self.executor_server.hold_jobs_in_build = False
897 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700898 self.waitUntilSettled()
899
900 self.assertEqual(A.data['status'], 'MERGED')
901 self.assertEqual(B.data['status'], 'MERGED')
902 self.assertEqual(A.queried, 2) # Initial and isMerged
903 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
904
James E. Blair8c803f82012-07-31 16:25:42 -0700905 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700906 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700907 # TODO: move to test_gerrit (this is a unit test!)
908 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400909 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -0700910 (trusted, project) = tenant.getProject('org/project')
911 source = project.source
Paul Belanger9bba4902016-11-02 16:07:33 -0400912
913 # TODO(pabelanger): As we add more source / trigger APIs we should make
914 # it easier for users to create events for testing.
915 event = zuul.model.TriggerEvent()
916 event.trigger_name = 'gerrit'
917 event.change_number = '1'
918 event.patch_number = '2'
919
920 a = source.getChange(event)
921 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700922 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700923
James E. Blair8b5408c2016-08-08 15:37:46 -0700924 A.addApproval('code-review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400925 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700926 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700927
James E. Blair8b5408c2016-08-08 15:37:46 -0700928 A.addApproval('approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400929 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700930 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700931
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700932 def test_project_merge_conflict(self):
933 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700934
935 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700936 A = self.fake_gerrit.addFakeChange('org/project',
937 'master', 'A',
938 files={'conflict': 'foo'})
939 B = self.fake_gerrit.addFakeChange('org/project',
940 'master', 'B',
941 files={'conflict': 'bar'})
942 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -0700943 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700944 A.addApproval('code-review', 2)
945 B.addApproval('code-review', 2)
946 C.addApproval('code-review', 2)
947 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
948 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
949 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700950 self.waitUntilSettled()
951
James E. Blair6736beb2013-07-11 15:18:15 -0700952 self.assertEqual(A.reported, 1)
953 self.assertEqual(B.reported, 1)
954 self.assertEqual(C.reported, 1)
955
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700956 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700957 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700958 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700959 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700960 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700961 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700962
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700963 self.gearman_server.hold_jobs_in_queue = False
964 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700965 self.waitUntilSettled()
966
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400967 self.assertEqual(A.data['status'], 'MERGED')
968 self.assertEqual(B.data['status'], 'NEW')
969 self.assertEqual(C.data['status'], 'MERGED')
970 self.assertEqual(A.reported, 2)
971 self.assertEqual(B.reported, 2)
972 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700973
974 self.assertHistory([
975 dict(name='project-merge', result='SUCCESS', changes='1,1'),
976 dict(name='project-test1', result='SUCCESS', changes='1,1'),
977 dict(name='project-test2', result='SUCCESS', changes='1,1'),
978 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
979 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
980 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
981 ], ordered=False)
982
983 def test_delayed_merge_conflict(self):
984 "Test that delayed check merge conflicts are handled properly"
985
986 # Hold jobs in the gearman queue so that we can test whether
987 # the executor returns a merge failure after the scheduler has
988 # successfully merged.
989 self.gearman_server.hold_jobs_in_queue = True
990 A = self.fake_gerrit.addFakeChange('org/project',
991 'master', 'A',
992 files={'conflict': 'foo'})
993 B = self.fake_gerrit.addFakeChange('org/project',
994 'master', 'B',
995 files={'conflict': 'bar'})
996 C = self.fake_gerrit.addFakeChange('org/project',
997 'master', 'C')
998 C.setDependsOn(B, 1)
999
1000 # A enters the gate queue; B and C enter the check queue
1001 A.addApproval('code-review', 2)
1002 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1003 self.waitUntilSettled()
1004 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1005 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1006 self.waitUntilSettled()
1007
1008 self.assertEqual(A.reported, 1)
1009 self.assertEqual(B.reported, 0) # Check does not report start
1010 self.assertEqual(C.reported, 0) # Check does not report start
1011
1012 # A merges while B and C are queued in check
1013 # Release A project-merge
1014 queue = self.gearman_server.getQueue()
1015 self.release(queue[0])
1016 self.waitUntilSettled()
1017
1018 # Release A project-test*
1019 # gate has higher precedence, so A's test jobs are added in
1020 # front of the merge jobs for B and C
1021 queue = self.gearman_server.getQueue()
1022 self.release(queue[0])
1023 self.release(queue[1])
1024 self.waitUntilSettled()
1025
1026 self.assertEqual(A.data['status'], 'MERGED')
1027 self.assertEqual(B.data['status'], 'NEW')
1028 self.assertEqual(C.data['status'], 'NEW')
1029 self.assertEqual(A.reported, 2)
1030 self.assertEqual(B.reported, 0)
1031 self.assertEqual(C.reported, 0)
1032 self.assertHistory([
1033 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1034 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1035 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1036 ], ordered=False)
1037
1038 # B and C report merge conflicts
1039 # Release B project-merge
1040 queue = self.gearman_server.getQueue()
1041 self.release(queue[0])
1042 self.waitUntilSettled()
1043
1044 # Release C
1045 self.gearman_server.hold_jobs_in_queue = False
1046 self.gearman_server.release()
1047 self.waitUntilSettled()
1048
1049 self.assertEqual(A.data['status'], 'MERGED')
1050 self.assertEqual(B.data['status'], 'NEW')
1051 self.assertEqual(C.data['status'], 'NEW')
1052 self.assertEqual(A.reported, 2)
1053 self.assertEqual(B.reported, 1)
1054 self.assertEqual(C.reported, 1)
1055
1056 self.assertHistory([
1057 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1058 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1059 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1060 dict(name='project-merge', result='MERGER_FAILURE', changes='2,1'),
1061 dict(name='project-merge', result='MERGER_FAILURE',
1062 changes='2,1 3,1'),
1063 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001064
James E. Blairdaabed22012-08-15 15:38:57 -07001065 def test_post(self):
1066 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001067
Zhongyue Luo5d556072012-09-21 02:00:47 +09001068 e = {
1069 "type": "ref-updated",
1070 "submitter": {
1071 "name": "User Name",
1072 },
1073 "refUpdate": {
1074 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1075 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1076 "refName": "master",
1077 "project": "org/project",
1078 }
1079 }
James E. Blairdaabed22012-08-15 15:38:57 -07001080 self.fake_gerrit.addEvent(e)
1081 self.waitUntilSettled()
1082
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001083 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001084 self.assertEqual(len(self.history), 1)
1085 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001086
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001087 def test_post_ignore_deletes(self):
1088 "Test that deleting refs does not trigger post jobs"
1089
1090 e = {
1091 "type": "ref-updated",
1092 "submitter": {
1093 "name": "User Name",
1094 },
1095 "refUpdate": {
1096 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1097 "newRev": "0000000000000000000000000000000000000000",
1098 "refName": "master",
1099 "project": "org/project",
1100 }
1101 }
1102 self.fake_gerrit.addEvent(e)
1103 self.waitUntilSettled()
1104
1105 job_names = [x.name for x in self.history]
1106 self.assertEqual(len(self.history), 0)
1107 self.assertNotIn('project-post', job_names)
1108
James E. Blaircc324192017-04-19 14:36:08 -07001109 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001110 def test_post_ignore_deletes_negative(self):
1111 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001112 e = {
1113 "type": "ref-updated",
1114 "submitter": {
1115 "name": "User Name",
1116 },
1117 "refUpdate": {
1118 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1119 "newRev": "0000000000000000000000000000000000000000",
1120 "refName": "master",
1121 "project": "org/project",
1122 }
1123 }
1124 self.fake_gerrit.addEvent(e)
1125 self.waitUntilSettled()
1126
1127 job_names = [x.name for x in self.history]
1128 self.assertEqual(len(self.history), 1)
1129 self.assertIn('project-post', job_names)
1130
James E. Blairec056492016-07-22 09:45:56 -07001131 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001132 def test_build_configuration_branch_interaction(self):
1133 "Test that switching between branches works"
1134 self.test_build_configuration()
1135 self.test_build_configuration_branch()
1136 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001137 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001138 repo = git.Repo(path)
1139 repo.heads.master.commit = repo.commit('init')
1140 self.test_build_configuration()
1141
James E. Blaircaec0c52012-08-22 14:52:22 -07001142 def test_dependent_changes_dequeue(self):
1143 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001144
James E. Blaircaec0c52012-08-22 14:52:22 -07001145 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1146 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1147 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001148 A.addApproval('code-review', 2)
1149 B.addApproval('code-review', 2)
1150 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001151
1152 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1153 M1.setMerged()
1154
1155 # C -> B -> A -> M1
1156
1157 C.setDependsOn(B, 1)
1158 B.setDependsOn(A, 1)
1159 A.setDependsOn(M1, 1)
1160
Paul Belanger174a8272017-03-14 13:20:10 -04001161 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001162
James E. Blair8b5408c2016-08-08 15:37:46 -07001163 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1164 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1165 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001166
1167 self.waitUntilSettled()
1168
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001169 self.assertEqual(A.data['status'], 'NEW')
1170 self.assertEqual(A.reported, 2)
1171 self.assertEqual(B.data['status'], 'NEW')
1172 self.assertEqual(B.reported, 2)
1173 self.assertEqual(C.data['status'], 'NEW')
1174 self.assertEqual(C.reported, 2)
1175 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001176
James E. Blair972e3c72013-08-29 12:04:55 -07001177 def test_failing_dependent_changes(self):
1178 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001179 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001180 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1181 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1182 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1183 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1184 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001185 A.addApproval('code-review', 2)
1186 B.addApproval('code-review', 2)
1187 C.addApproval('code-review', 2)
1188 D.addApproval('code-review', 2)
1189 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001190
1191 # E, D -> C -> B, A
1192
1193 D.setDependsOn(C, 1)
1194 C.setDependsOn(B, 1)
1195
Paul Belanger174a8272017-03-14 13:20:10 -04001196 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001197
James E. Blair8b5408c2016-08-08 15:37:46 -07001198 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1199 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1200 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1201 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1202 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001203
1204 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001205 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001206 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001207 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001208 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001209 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001210 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()
1215
Paul Belanger174a8272017-03-14 13:20:10 -04001216 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001217 for build in self.builds:
1218 if build.parameters['ZUUL_CHANGE'] != '1':
1219 build.release()
1220 self.waitUntilSettled()
1221
Paul Belanger174a8272017-03-14 13:20:10 -04001222 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001223 self.waitUntilSettled()
1224
1225 self.assertEqual(A.data['status'], 'MERGED')
1226 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001227 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001228 self.assertEqual(B.data['status'], 'NEW')
1229 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001230 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001231 self.assertEqual(C.data['status'], 'NEW')
1232 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001233 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001234 self.assertEqual(D.data['status'], 'NEW')
1235 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001236 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001237 self.assertEqual(E.data['status'], 'MERGED')
1238 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001239 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001240 self.assertEqual(len(self.history), 18)
1241
James E. Blairec590122012-08-22 15:19:31 -07001242 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001243 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001244 # If it's dequeued more than once, we should see extra
1245 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001246
Paul Belanger174a8272017-03-14 13:20:10 -04001247 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001248 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1249 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1250 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001251 A.addApproval('code-review', 2)
1252 B.addApproval('code-review', 2)
1253 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001254
Paul Belanger174a8272017-03-14 13:20:10 -04001255 self.executor_server.failJob('project-test1', A)
1256 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001257
James E. Blair8b5408c2016-08-08 15:37:46 -07001258 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1259 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1260 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001261
1262 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001263
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001264 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001265 self.assertEqual(self.builds[0].name, 'project-merge')
1266 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001267
Paul Belanger174a8272017-03-14 13:20:10 -04001268 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001269 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001270 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001271 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001272 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001273 self.waitUntilSettled()
1274
James E. Blair6df67132017-04-19 13:53:20 -07001275 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001276 self.assertEqual(self.builds[0].name, 'project-test1')
1277 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001278 self.assertEqual(self.builds[2].name, 'project-test1')
1279 self.assertEqual(self.builds[3].name, 'project-test2')
1280 self.assertEqual(self.builds[4].name, 'project-test1')
1281 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001282
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001283 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001284 self.waitUntilSettled()
1285
James E. Blair6df67132017-04-19 13:53:20 -07001286 self.assertEqual(len(self.builds), 2) # test2, merge for B
1287 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001288
Paul Belanger174a8272017-03-14 13:20:10 -04001289 self.executor_server.hold_jobs_in_build = False
1290 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001291 self.waitUntilSettled()
1292
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001293 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001294 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001295
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001296 self.assertEqual(A.data['status'], 'NEW')
1297 self.assertEqual(B.data['status'], 'MERGED')
1298 self.assertEqual(C.data['status'], 'MERGED')
1299 self.assertEqual(A.reported, 2)
1300 self.assertEqual(B.reported, 2)
1301 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001302
James E. Blaircc020532017-04-19 13:04:44 -07001303 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001304 def test_nonvoting_job(self):
1305 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001306
James E. Blair4ec821f2012-08-23 15:28:28 -07001307 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1308 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001309 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001310 self.executor_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001311 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001312
1313 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001314
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001315 self.assertEqual(A.data['status'], 'MERGED')
1316 self.assertEqual(A.reported, 2)
1317 self.assertEqual(
1318 self.getJobFromHistory('nonvoting-project-merge').result,
1319 'SUCCESS')
1320 self.assertEqual(
1321 self.getJobFromHistory('nonvoting-project-test1').result,
1322 'SUCCESS')
1323 self.assertEqual(
1324 self.getJobFromHistory('nonvoting-project-test2').result,
1325 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001326
James E. Blair5821bd92015-09-16 08:48:15 -07001327 for build in self.builds:
1328 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1329
James E. Blaire0487072012-08-29 17:38:31 -07001330 def test_check_queue_success(self):
1331 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001332
James E. Blaire0487072012-08-29 17:38:31 -07001333 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1334 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1335
1336 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001337
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001338 self.assertEqual(A.data['status'], 'NEW')
1339 self.assertEqual(A.reported, 1)
1340 self.assertEqual(self.getJobFromHistory('project-merge').result,
1341 'SUCCESS')
1342 self.assertEqual(self.getJobFromHistory('project-test1').result,
1343 'SUCCESS')
1344 self.assertEqual(self.getJobFromHistory('project-test2').result,
1345 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001346
1347 def test_check_queue_failure(self):
1348 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001349
James E. Blaire0487072012-08-29 17:38:31 -07001350 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001351 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001352 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1353
1354 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001355
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001356 self.assertEqual(A.data['status'], 'NEW')
1357 self.assertEqual(A.reported, 1)
1358 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001359 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001360 self.assertEqual(self.getJobFromHistory('project-test1').result,
1361 'SUCCESS')
1362 self.assertEqual(self.getJobFromHistory('project-test2').result,
1363 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001364
James E. Blair7cb84542017-04-19 13:35:05 -07001365 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001366 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001367 # This particular test does a large amount of merges and needs a little
1368 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001369 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001370 "test that dependent changes behind dequeued changes work"
1371 # This complicated test is a reproduction of a real life bug
1372 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001373
Paul Belanger174a8272017-03-14 13:20:10 -04001374 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001375 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1376 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1377 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1378 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1379 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1380 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1381 D.setDependsOn(C, 1)
1382 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001383 A.addApproval('code-review', 2)
1384 B.addApproval('code-review', 2)
1385 C.addApproval('code-review', 2)
1386 D.addApproval('code-review', 2)
1387 E.addApproval('code-review', 2)
1388 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001389
1390 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001391
1392 # Change object re-use in the gerrit trigger is hidden if
1393 # changes are added in quick succession; waiting makes it more
1394 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001395 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001396 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001397 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001398 self.waitUntilSettled()
1399
Paul Belanger174a8272017-03-14 13:20:10 -04001400 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001401 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001402 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001403 self.waitUntilSettled()
1404
James E. Blair8b5408c2016-08-08 15:37:46 -07001405 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001406 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001407 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001408 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001409 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001410 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001411 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001412 self.waitUntilSettled()
1413
Paul Belanger174a8272017-03-14 13:20:10 -04001414 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001415 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001416 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001417 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001418 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001419 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001420 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001421 self.waitUntilSettled()
1422
1423 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001424
1425 # Grab pointers to the jobs we want to release before
1426 # releasing any, because list indexes may change as
1427 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001428 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001429 a.release()
1430 b.release()
1431 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001432 self.waitUntilSettled()
1433
Paul Belanger174a8272017-03-14 13:20:10 -04001434 self.executor_server.hold_jobs_in_build = False
1435 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001436 self.waitUntilSettled()
1437
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001438 self.assertEqual(A.data['status'], 'NEW')
1439 self.assertEqual(B.data['status'], 'MERGED')
1440 self.assertEqual(C.data['status'], 'MERGED')
1441 self.assertEqual(D.data['status'], 'MERGED')
1442 self.assertEqual(E.data['status'], 'MERGED')
1443 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001444
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001445 self.assertEqual(A.reported, 2)
1446 self.assertEqual(B.reported, 2)
1447 self.assertEqual(C.reported, 2)
1448 self.assertEqual(D.reported, 2)
1449 self.assertEqual(E.reported, 2)
1450 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001451
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001452 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1453 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001454
1455 def test_merger_repack(self):
1456 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001457
James E. Blair05fed602012-09-07 12:45:24 -07001458 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001459 A.addApproval('code-review', 2)
1460 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001461 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001462 self.assertEqual(self.getJobFromHistory('project-merge').result,
1463 'SUCCESS')
1464 self.assertEqual(self.getJobFromHistory('project-test1').result,
1465 'SUCCESS')
1466 self.assertEqual(self.getJobFromHistory('project-test2').result,
1467 'SUCCESS')
1468 self.assertEqual(A.data['status'], 'MERGED')
1469 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001470 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001471 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001472
James E. Blair2a535672017-04-27 12:03:15 -07001473 path = os.path.join(self.merger_src_root, "review.example.com",
1474 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001475 if os.path.exists(path):
1476 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001477 path = os.path.join(self.executor_src_root, "review.example.com",
1478 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001479 if os.path.exists(path):
1480 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001481
1482 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001483 A.addApproval('code-review', 2)
1484 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001485 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001486 self.assertEqual(self.getJobFromHistory('project-merge').result,
1487 'SUCCESS')
1488 self.assertEqual(self.getJobFromHistory('project-test1').result,
1489 'SUCCESS')
1490 self.assertEqual(self.getJobFromHistory('project-test2').result,
1491 'SUCCESS')
1492 self.assertEqual(A.data['status'], 'MERGED')
1493 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001494
James E. Blair4886f282012-11-15 09:27:33 -08001495 def test_merger_repack_large_change(self):
1496 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001497 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001498 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001499 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001500 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001501 url = self.fake_gerrit.getGitUrl(project)
James E. Blair2a535672017-04-27 12:03:15 -07001502 self.merge_server.merger._addProject('review.example.com',
1503 'org/project', url)
James E. Blair6df67132017-04-19 13:53:20 -07001504 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001505 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001506 # TODOv3(jeblair): add hostname to upstream root
1507 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001508 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001509 path = os.path.join(self.merger_src_root, 'review.example.com',
1510 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001511 if os.path.exists(path):
1512 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001513 path = os.path.join(self.executor_src_root, 'review.example.com',
1514 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001515 if os.path.exists(path):
1516 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001517
James E. Blair8b5408c2016-08-08 15:37:46 -07001518 A.addApproval('code-review', 2)
1519 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001520 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001521 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001522 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001523 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001524 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001525 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001526 'SUCCESS')
1527 self.assertEqual(A.data['status'], 'MERGED')
1528 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001529
James E. Blair2fa50962013-01-30 21:50:41 -08001530 def test_new_patchset_dequeues_old(self):
1531 "Test that a new patchset causes the old to be dequeued"
1532 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001533 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001534 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1535 M.setMerged()
1536
1537 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1538 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1539 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1540 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001541 A.addApproval('code-review', 2)
1542 B.addApproval('code-review', 2)
1543 C.addApproval('code-review', 2)
1544 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001545
1546 C.setDependsOn(B, 1)
1547 B.setDependsOn(A, 1)
1548 A.setDependsOn(M, 1)
1549
James E. Blair8b5408c2016-08-08 15:37:46 -07001550 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1551 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1552 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1553 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001554 self.waitUntilSettled()
1555
1556 B.addPatchset()
1557 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1558 self.waitUntilSettled()
1559
Paul Belanger174a8272017-03-14 13:20:10 -04001560 self.executor_server.hold_jobs_in_build = False
1561 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001562 self.waitUntilSettled()
1563
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001564 self.assertEqual(A.data['status'], 'MERGED')
1565 self.assertEqual(A.reported, 2)
1566 self.assertEqual(B.data['status'], 'NEW')
1567 self.assertEqual(B.reported, 2)
1568 self.assertEqual(C.data['status'], 'NEW')
1569 self.assertEqual(C.reported, 2)
1570 self.assertEqual(D.data['status'], 'MERGED')
1571 self.assertEqual(D.reported, 2)
1572 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001573
James E. Blairba437362015-02-07 11:41:52 -08001574 def test_new_patchset_check(self):
1575 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001576
Paul Belanger174a8272017-03-14 13:20:10 -04001577 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001578
1579 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001580 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001581 tenant = self.sched.abide.tenants.get('tenant-one')
1582 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001583
1584 # Add two git-dependent changes
1585 B.setDependsOn(A, 1)
1586 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1587 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001588 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1589 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001590
1591 # A live item, and a non-live/live pair
1592 items = check_pipeline.getAllItems()
1593 self.assertEqual(len(items), 3)
1594
1595 self.assertEqual(items[0].change.number, '1')
1596 self.assertEqual(items[0].change.patchset, '1')
1597 self.assertFalse(items[0].live)
1598
1599 self.assertEqual(items[1].change.number, '2')
1600 self.assertEqual(items[1].change.patchset, '1')
1601 self.assertTrue(items[1].live)
1602
1603 self.assertEqual(items[2].change.number, '1')
1604 self.assertEqual(items[2].change.patchset, '1')
1605 self.assertTrue(items[2].live)
1606
1607 # Add a new patchset to A
1608 A.addPatchset()
1609 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1610 self.waitUntilSettled()
1611
1612 # The live copy of A,1 should be gone, but the non-live and B
1613 # should continue, and we should have a new A,2
1614 items = check_pipeline.getAllItems()
1615 self.assertEqual(len(items), 3)
1616
1617 self.assertEqual(items[0].change.number, '1')
1618 self.assertEqual(items[0].change.patchset, '1')
1619 self.assertFalse(items[0].live)
1620
1621 self.assertEqual(items[1].change.number, '2')
1622 self.assertEqual(items[1].change.patchset, '1')
1623 self.assertTrue(items[1].live)
1624
1625 self.assertEqual(items[2].change.number, '1')
1626 self.assertEqual(items[2].change.patchset, '2')
1627 self.assertTrue(items[2].live)
1628
1629 # Add a new patchset to B
1630 B.addPatchset()
1631 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1632 self.waitUntilSettled()
1633
1634 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1635 # but we should have a new B,2 (still based on A,1)
1636 items = check_pipeline.getAllItems()
1637 self.assertEqual(len(items), 3)
1638
1639 self.assertEqual(items[0].change.number, '1')
1640 self.assertEqual(items[0].change.patchset, '2')
1641 self.assertTrue(items[0].live)
1642
1643 self.assertEqual(items[1].change.number, '1')
1644 self.assertEqual(items[1].change.patchset, '1')
1645 self.assertFalse(items[1].live)
1646
1647 self.assertEqual(items[2].change.number, '2')
1648 self.assertEqual(items[2].change.patchset, '2')
1649 self.assertTrue(items[2].live)
1650
1651 self.builds[0].release()
1652 self.waitUntilSettled()
1653 self.builds[0].release()
1654 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001655 self.executor_server.hold_jobs_in_build = False
1656 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001657 self.waitUntilSettled()
1658
1659 self.assertEqual(A.reported, 1)
1660 self.assertEqual(B.reported, 1)
1661 self.assertEqual(self.history[0].result, 'ABORTED')
1662 self.assertEqual(self.history[0].changes, '1,1')
1663 self.assertEqual(self.history[1].result, 'ABORTED')
1664 self.assertEqual(self.history[1].changes, '1,1 2,1')
1665 self.assertEqual(self.history[2].result, 'SUCCESS')
1666 self.assertEqual(self.history[2].changes, '1,2')
1667 self.assertEqual(self.history[3].result, 'SUCCESS')
1668 self.assertEqual(self.history[3].changes, '1,1 2,2')
1669
1670 def test_abandoned_gate(self):
1671 "Test that an abandoned change is dequeued from gate"
1672
Paul Belanger174a8272017-03-14 13:20:10 -04001673 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001674
1675 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001676 A.addApproval('code-review', 2)
1677 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001678 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001679 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1680 self.assertEqual(self.builds[0].name, 'project-merge')
1681
1682 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1683 self.waitUntilSettled()
1684
Paul Belanger174a8272017-03-14 13:20:10 -04001685 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001686 self.waitUntilSettled()
1687
Arie5b4048c2016-10-05 16:12:06 +03001688 self.assertBuilds([])
1689 self.assertHistory([
1690 dict(name='project-merge', result='ABORTED', changes='1,1')],
1691 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001692 self.assertEqual(A.reported, 1,
1693 "Abandoned gate change should report only start")
1694
1695 def test_abandoned_check(self):
1696 "Test that an abandoned change is dequeued from check"
1697
Paul Belanger174a8272017-03-14 13:20:10 -04001698 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001699
1700 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1701 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001702 tenant = self.sched.abide.tenants.get('tenant-one')
1703 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001704
1705 # Add two git-dependent changes
1706 B.setDependsOn(A, 1)
1707 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1708 self.waitUntilSettled()
1709 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1710 self.waitUntilSettled()
1711 # A live item, and a non-live/live pair
1712 items = check_pipeline.getAllItems()
1713 self.assertEqual(len(items), 3)
1714
1715 self.assertEqual(items[0].change.number, '1')
1716 self.assertFalse(items[0].live)
1717
1718 self.assertEqual(items[1].change.number, '2')
1719 self.assertTrue(items[1].live)
1720
1721 self.assertEqual(items[2].change.number, '1')
1722 self.assertTrue(items[2].live)
1723
1724 # Abandon A
1725 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1726 self.waitUntilSettled()
1727
1728 # The live copy of A should be gone, but the non-live and B
1729 # should continue
1730 items = check_pipeline.getAllItems()
1731 self.assertEqual(len(items), 2)
1732
1733 self.assertEqual(items[0].change.number, '1')
1734 self.assertFalse(items[0].live)
1735
1736 self.assertEqual(items[1].change.number, '2')
1737 self.assertTrue(items[1].live)
1738
Paul Belanger174a8272017-03-14 13:20:10 -04001739 self.executor_server.hold_jobs_in_build = False
1740 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001741 self.waitUntilSettled()
1742
1743 self.assertEqual(len(self.history), 4)
1744 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001745 'Build should have been aborted')
1746 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001747 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001748
Steve Varnau7b78b312015-04-03 14:49:46 -07001749 def test_abandoned_not_timer(self):
1750 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001751 # This test can not use simple_layout because it must start
1752 # with a configuration which does not include a
1753 # timer-triggered job so that we have an opportunity to set
1754 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001755 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001756 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001757 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001758 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001759 # The pipeline triggers every second, so we should have seen
1760 # several by now.
1761 time.sleep(5)
1762 self.waitUntilSettled()
1763 # Stop queuing timer triggered jobs so that the assertions
1764 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001765 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001766 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001767 self.sched.reconfigure(self.config)
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001768
Steve Varnau7b78b312015-04-03 14:49:46 -07001769 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1770
1771 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1772 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1773 self.waitUntilSettled()
1774 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1775
1776 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1777 self.waitUntilSettled()
1778
1779 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1780
Paul Belanger174a8272017-03-14 13:20:10 -04001781 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001782 self.waitUntilSettled()
1783
Arx Cruzb1b010d2013-10-28 19:49:59 -02001784 def test_zuul_url_return(self):
1785 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001786 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Paul Belanger174a8272017-03-14 13:20:10 -04001787 self.executor_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001788
1789 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001790 A.addApproval('code-review', 2)
1791 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001792 self.waitUntilSettled()
1793
1794 self.assertEqual(len(self.builds), 1)
1795 for build in self.builds:
1796 self.assertTrue('ZUUL_URL' in build.parameters)
1797
Paul Belanger174a8272017-03-14 13:20:10 -04001798 self.executor_server.hold_jobs_in_build = False
1799 self.executor_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001800 self.waitUntilSettled()
1801
James E. Blair2fa50962013-01-30 21:50:41 -08001802 def test_new_patchset_dequeues_old_on_head(self):
1803 "Test that a new patchset causes the old to be dequeued (at head)"
1804 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001805 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001806 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1807 M.setMerged()
1808 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1809 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1810 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1811 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001812 A.addApproval('code-review', 2)
1813 B.addApproval('code-review', 2)
1814 C.addApproval('code-review', 2)
1815 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001816
1817 C.setDependsOn(B, 1)
1818 B.setDependsOn(A, 1)
1819 A.setDependsOn(M, 1)
1820
James E. Blair8b5408c2016-08-08 15:37:46 -07001821 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1822 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1823 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1824 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001825 self.waitUntilSettled()
1826
1827 A.addPatchset()
1828 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1829 self.waitUntilSettled()
1830
Paul Belanger174a8272017-03-14 13:20:10 -04001831 self.executor_server.hold_jobs_in_build = False
1832 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001833 self.waitUntilSettled()
1834
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001835 self.assertEqual(A.data['status'], 'NEW')
1836 self.assertEqual(A.reported, 2)
1837 self.assertEqual(B.data['status'], 'NEW')
1838 self.assertEqual(B.reported, 2)
1839 self.assertEqual(C.data['status'], 'NEW')
1840 self.assertEqual(C.reported, 2)
1841 self.assertEqual(D.data['status'], 'MERGED')
1842 self.assertEqual(D.reported, 2)
1843 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001844
1845 def test_new_patchset_dequeues_old_without_dependents(self):
1846 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001847 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001848 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1849 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1850 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001851 A.addApproval('code-review', 2)
1852 B.addApproval('code-review', 2)
1853 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001854
James E. Blair8b5408c2016-08-08 15:37:46 -07001855 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1856 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1857 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001858 self.waitUntilSettled()
1859
1860 B.addPatchset()
1861 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1862 self.waitUntilSettled()
1863
Paul Belanger174a8272017-03-14 13:20:10 -04001864 self.executor_server.hold_jobs_in_build = False
1865 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001866 self.waitUntilSettled()
1867
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001868 self.assertEqual(A.data['status'], 'MERGED')
1869 self.assertEqual(A.reported, 2)
1870 self.assertEqual(B.data['status'], 'NEW')
1871 self.assertEqual(B.reported, 2)
1872 self.assertEqual(C.data['status'], 'MERGED')
1873 self.assertEqual(C.reported, 2)
1874 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001875
1876 def test_new_patchset_dequeues_old_independent_queue(self):
1877 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001878 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001879 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1880 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1881 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1882 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1883 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1884 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1885 self.waitUntilSettled()
1886
1887 B.addPatchset()
1888 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1889 self.waitUntilSettled()
1890
Paul Belanger174a8272017-03-14 13:20:10 -04001891 self.executor_server.hold_jobs_in_build = False
1892 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001893 self.waitUntilSettled()
1894
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001895 self.assertEqual(A.data['status'], 'NEW')
1896 self.assertEqual(A.reported, 1)
1897 self.assertEqual(B.data['status'], 'NEW')
1898 self.assertEqual(B.reported, 1)
1899 self.assertEqual(C.data['status'], 'NEW')
1900 self.assertEqual(C.reported, 1)
1901 self.assertEqual(len(self.history), 10)
1902 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001903
James E. Blaircc020532017-04-19 13:04:44 -07001904 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07001905 def test_noop_job(self):
1906 "Test that the internal noop job works"
1907 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001908 A.addApproval('code-review', 2)
1909 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001910 self.waitUntilSettled()
1911
1912 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1913 self.assertTrue(self.sched._areAllBuildsComplete())
1914 self.assertEqual(len(self.history), 0)
1915 self.assertEqual(A.data['status'], 'MERGED')
1916 self.assertEqual(A.reported, 2)
1917
James E. Blair06cc3922017-04-19 10:08:10 -07001918 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001919 def test_no_job_project(self):
1920 "Test that reports with no jobs don't get sent"
1921 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1922 'master', 'A')
1923 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1924 self.waitUntilSettled()
1925
1926 # Change wasn't reported to
1927 self.assertEqual(A.reported, False)
1928
1929 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001930 tenant = self.sched.abide.tenants.get('tenant-one')
1931 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001932 items = check_pipeline.getAllItems()
1933 self.assertEqual(len(items), 0)
1934
1935 self.assertEqual(len(self.history), 0)
1936
James E. Blair7d0dedc2013-02-21 17:26:09 -08001937 def test_zuul_refs(self):
1938 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04001939 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001940 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1941 M1.setMerged()
1942 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1943 M2.setMerged()
1944
1945 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1946 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1947 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1948 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001949 A.addApproval('code-review', 2)
1950 B.addApproval('code-review', 2)
1951 C.addApproval('code-review', 2)
1952 D.addApproval('code-review', 2)
1953 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1954 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1955 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1956 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001957
1958 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001959 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001960 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001961 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001962 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001963 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001964 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()
1967
James E. Blair7d0dedc2013-02-21 17:26:09 -08001968 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08001969 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001970 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001971 if x.parameters['ZUUL_CHANGE'] == '3':
1972 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001973 a_build = x
1974 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001975 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001976 b_build = x
1977 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001978 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001979 c_build = x
1980 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001981 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001982 d_build = x
1983 if a_build and b_build and c_build and d_build:
1984 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08001985
1986 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001987 self.assertIsNotNone(a_zref)
1988 self.assertIsNotNone(b_zref)
1989 self.assertIsNotNone(c_zref)
1990 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001991
1992 # And they should all be different
1993 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001994 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001995
Clint Byrum3343e3e2016-11-15 16:05:03 -08001996 # should have a, not b, and should not be in project2
1997 self.assertTrue(a_build.hasChanges(A))
1998 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001999
Clint Byrum3343e3e2016-11-15 16:05:03 -08002000 # should have a and b, and should not be in project2
2001 self.assertTrue(b_build.hasChanges(A, B))
2002 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002003
Clint Byrum3343e3e2016-11-15 16:05:03 -08002004 # should have a and b in 1, c in 2
2005 self.assertTrue(c_build.hasChanges(A, B, C))
2006 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002007
Clint Byrum3343e3e2016-11-15 16:05:03 -08002008 # should have a and b in 1, c and d in 2
2009 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002010
Paul Belanger174a8272017-03-14 13:20:10 -04002011 self.executor_server.hold_jobs_in_build = False
2012 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002013 self.waitUntilSettled()
2014
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002015 self.assertEqual(A.data['status'], 'MERGED')
2016 self.assertEqual(A.reported, 2)
2017 self.assertEqual(B.data['status'], 'MERGED')
2018 self.assertEqual(B.reported, 2)
2019 self.assertEqual(C.data['status'], 'MERGED')
2020 self.assertEqual(C.reported, 2)
2021 self.assertEqual(D.data['status'], 'MERGED')
2022 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002023
James E. Blair4a28a882013-08-23 15:17:33 -07002024 def test_rerun_on_error(self):
2025 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002026 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002027 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002028 A.addApproval('code-review', 2)
2029 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002030 self.waitUntilSettled()
2031
James E. Blair412fba82017-01-26 15:00:50 -08002032 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002033 self.executor_server.hold_jobs_in_build = False
2034 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002035 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002036 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002037 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2038
James E. Blair412e5582013-04-22 15:50:12 -07002039 def test_statsd(self):
2040 "Test each of the statsd methods used in the scheduler"
2041 import extras
2042 statsd = extras.try_import('statsd.statsd')
2043 statsd.incr('test-incr')
2044 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002045 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002046 self.assertReportedStat('test-incr', '1|c')
2047 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002048 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002049
James E. Blairdad52252014-02-07 16:59:17 -08002050 def test_stuck_job_cleanup(self):
2051 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002052
2053 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002054 self.gearman_server.hold_jobs_in_queue = True
2055 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002056 A.addApproval('code-review', 2)
2057 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002058 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002059 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002060 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2061
James E. Blair31cb3e82017-04-20 13:08:56 -07002062 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002063 self.sched.reconfigure(self.config)
2064 self.waitUntilSettled()
2065
James E. Blair18c64442014-03-18 10:14:45 -07002066 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002067 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002068 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002069 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2070 self.assertTrue(self.sched._areAllBuildsComplete())
2071
2072 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002073 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002074 self.assertEqual(self.history[0].result, 'SUCCESS')
2075
James E. Blair879dafb2015-07-17 14:04:49 -07002076 def test_file_head(self):
2077 # This is a regression test for an observed bug. A change
2078 # with a file named "HEAD" in the root directory of the repo
2079 # was processed by a merger. It then was unable to reset the
2080 # repo because of:
2081 # GitCommandError: 'git reset --hard HEAD' returned
2082 # with exit code 128
2083 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2084 # and filename
2085 # Use '--' to separate filenames from revisions'
2086
2087 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002088 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002089 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2090
2091 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2092 self.waitUntilSettled()
2093
2094 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2095 self.waitUntilSettled()
2096
2097 self.assertIn('Build succeeded', A.messages[0])
2098 self.assertIn('Build succeeded', B.messages[0])
2099
James E. Blair70c71582013-03-06 08:50:50 -08002100 def test_file_jobs(self):
2101 "Test that file jobs run only when appropriate"
2102 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002103 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002104 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002105 A.addApproval('code-review', 2)
2106 B.addApproval('code-review', 2)
2107 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2108 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002109 self.waitUntilSettled()
2110
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002111 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002112 if x.name == 'project-testfile']
2113
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002114 self.assertEqual(len(testfile_jobs), 1)
2115 self.assertEqual(testfile_jobs[0].changes, '1,2')
2116 self.assertEqual(A.data['status'], 'MERGED')
2117 self.assertEqual(A.reported, 2)
2118 self.assertEqual(B.data['status'], 'MERGED')
2119 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002120
Clint Byrum3ee39f32016-11-17 23:45:07 -08002121 def _test_irrelevant_files_jobs(self, should_skip):
2122 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002123 if should_skip:
2124 files = {'ignoreme': 'ignored\n'}
2125 else:
2126 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002127
2128 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002129 'master',
2130 'test irrelevant-files',
2131 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002132 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2133 self.waitUntilSettled()
2134
2135 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002136 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002137
2138 if should_skip:
2139 self.assertEqual([], tested_change_ids)
2140 else:
2141 self.assertIn(change.data['number'], tested_change_ids)
2142
James E. Blaircc324192017-04-19 14:36:08 -07002143 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002144 def test_irrelevant_files_match_skips_job(self):
2145 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002146
James E. Blaircc324192017-04-19 14:36:08 -07002147 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002148 def test_irrelevant_files_no_match_runs_job(self):
2149 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002150
James E. Blaircc324192017-04-19 14:36:08 -07002151 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002152 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002153 files = {'ignoreme': 'ignored\n'}
2154
2155 change = self.fake_gerrit.addFakeChange('org/project',
2156 'master',
2157 'test irrelevant-files',
2158 files=files)
2159 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2160 self.waitUntilSettled()
2161
2162 run_jobs = set([build.name for build in self.history])
2163
2164 self.assertEqual(set(['project-test-nomatch-starts-empty',
2165 'project-test-nomatch-starts-full']), run_jobs)
2166
James E. Blairec056492016-07-22 09:45:56 -07002167 @skip("Disabled for early v3 development")
James E. Blair3c5e5b52013-04-26 11:17:03 -07002168 def test_test_config(self):
2169 "Test that we can test the config"
James E. Blairf84026c2015-12-08 16:11:46 -08002170 self.sched.testConfig(self.config.get('zuul', 'tenant_config'),
Joshua Hesketh352264b2015-08-11 23:42:08 +10002171 self.connections)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002172
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)
James E. Blair1843a552013-07-03 14:19:52 -07002236 data = f.read()
2237
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. Blairec056492016-07-22 09:45:56 -07002354 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002355 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002356 # An extrapolation of test_live_reconfiguration_merge_conflict
2357 # that tests a job added to a job tree with a failed root does
2358 # not run.
2359 self.worker.registerFunction('build:project-test3')
Paul Belanger174a8272017-03-14 13:20:10 -04002360 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002361
2362 # This change is fine. It's here to stop the queue long
2363 # enough for the next change to be subject to the
2364 # reconfiguration. This change will succeed and merge.
2365 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2366 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002367 A.addApproval('code-review', 2)
2368 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002369 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002370 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002371 self.waitUntilSettled()
2372
2373 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002374 self.executor_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002375 B.addApproval('code-review', 2)
2376 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002377 self.waitUntilSettled()
2378
Paul Belanger174a8272017-03-14 13:20:10 -04002379 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002380 self.waitUntilSettled()
2381
2382 # Both -merge jobs have run, but no others.
2383 self.assertEqual(A.data['status'], 'NEW')
2384 self.assertEqual(A.reported, 1)
2385 self.assertEqual(B.data['status'], 'NEW')
2386 self.assertEqual(B.reported, 1)
2387 self.assertEqual(self.history[0].result, 'SUCCESS')
2388 self.assertEqual(self.history[0].name, 'project-merge')
2389 self.assertEqual(self.history[1].result, 'FAILURE')
2390 self.assertEqual(self.history[1].name, 'project-merge')
2391 self.assertEqual(len(self.history), 2)
2392
2393 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002394 self.updateConfigLayout(
2395 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002396 self.sched.reconfigure(self.config)
2397 self.waitUntilSettled()
2398
Paul Belanger174a8272017-03-14 13:20:10 -04002399 self.executor_server.hold_jobs_in_build = False
2400 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002401 self.waitUntilSettled()
2402
2403 self.assertEqual(A.data['status'], 'MERGED')
2404 self.assertEqual(A.reported, 2)
2405 self.assertEqual(B.data['status'], 'NEW')
2406 self.assertEqual(B.reported, 2)
2407 self.assertEqual(self.history[0].result, 'SUCCESS')
2408 self.assertEqual(self.history[0].name, 'project-merge')
2409 self.assertEqual(self.history[1].result, 'FAILURE')
2410 self.assertEqual(self.history[1].name, 'project-merge')
2411 self.assertEqual(self.history[2].result, 'SUCCESS')
2412 self.assertEqual(self.history[3].result, 'SUCCESS')
2413 self.assertEqual(self.history[4].result, 'SUCCESS')
2414 self.assertEqual(len(self.history), 5)
2415
James E. Blairec056492016-07-22 09:45:56 -07002416 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002417 def test_live_reconfiguration_failed_job(self):
2418 # Test that a change with a removed failing job does not
2419 # disrupt reconfiguration. If a change has a failed job and
2420 # that job is removed during a reconfiguration, we observed a
2421 # bug where the code to re-set build statuses would run on
2422 # that build and raise an exception because the job no longer
2423 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002424 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002425
2426 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2427
2428 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002429 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002430
2431 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2432 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002433 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002434 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002435 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002436 self.waitUntilSettled()
2437
2438 self.assertEqual(A.data['status'], 'NEW')
2439 self.assertEqual(A.reported, 0)
2440
2441 self.assertEqual(self.getJobFromHistory('project-merge').result,
2442 'SUCCESS')
2443 self.assertEqual(self.getJobFromHistory('project-test1').result,
2444 'FAILURE')
2445 self.assertEqual(len(self.history), 2)
2446
2447 # Remove the test1 job.
James E. Blairf84026c2015-12-08 16:11:46 -08002448 self.updateConfigLayout(
2449 'tests/fixtures/layout-live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002450 self.sched.reconfigure(self.config)
2451 self.waitUntilSettled()
2452
Paul Belanger174a8272017-03-14 13:20:10 -04002453 self.executor_server.hold_jobs_in_build = False
2454 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002455 self.waitUntilSettled()
2456
2457 self.assertEqual(self.getJobFromHistory('project-test2').result,
2458 'SUCCESS')
2459 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2460 'SUCCESS')
2461 self.assertEqual(len(self.history), 4)
2462
2463 self.assertEqual(A.data['status'], 'NEW')
2464 self.assertEqual(A.reported, 1)
2465 self.assertIn('Build succeeded', A.messages[0])
2466 # Ensure the removed job was not included in the report.
2467 self.assertNotIn('project-test1', A.messages[0])
2468
James E. Blairec056492016-07-22 09:45:56 -07002469 @skip("Disabled for early v3 development")
James E. Blairfe707d12015-08-05 15:18:15 -07002470 def test_live_reconfiguration_shared_queue(self):
2471 # Test that a change with a failing job which was removed from
2472 # this project but otherwise still exists in the system does
2473 # not disrupt reconfiguration.
2474
Paul Belanger174a8272017-03-14 13:20:10 -04002475 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002476
2477 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2478
Paul Belanger174a8272017-03-14 13:20:10 -04002479 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002480
2481 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2482 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002483 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002484 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002485 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002486 self.waitUntilSettled()
2487
2488 self.assertEqual(A.data['status'], 'NEW')
2489 self.assertEqual(A.reported, 0)
2490
2491 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2492 'SUCCESS')
2493 self.assertEqual(self.getJobFromHistory(
2494 'project1-project2-integration').result, 'FAILURE')
2495 self.assertEqual(len(self.history), 2)
2496
2497 # Remove the integration job.
James E. Blairf84026c2015-12-08 16:11:46 -08002498 self.updateConfigLayout(
2499 'tests/fixtures/layout-live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002500 self.sched.reconfigure(self.config)
2501 self.waitUntilSettled()
2502
Paul Belanger174a8272017-03-14 13:20:10 -04002503 self.executor_server.hold_jobs_in_build = False
2504 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002505 self.waitUntilSettled()
2506
2507 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2508 'SUCCESS')
2509 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2510 'SUCCESS')
2511 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2512 'SUCCESS')
2513 self.assertEqual(self.getJobFromHistory(
2514 'project1-project2-integration').result, 'FAILURE')
2515 self.assertEqual(len(self.history), 4)
2516
2517 self.assertEqual(A.data['status'], 'NEW')
2518 self.assertEqual(A.reported, 1)
2519 self.assertIn('Build succeeded', A.messages[0])
2520 # Ensure the removed job was not included in the report.
2521 self.assertNotIn('project1-project2-integration', A.messages[0])
2522
James E. Blairec056492016-07-22 09:45:56 -07002523 @skip("Disabled for early v3 development")
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002524 def test_double_live_reconfiguration_shared_queue(self):
2525 # This was a real-world regression. A change is added to
2526 # gate; a reconfigure happens, a second change which depends
2527 # on the first is added, and a second reconfiguration happens.
2528 # Ensure that both changes merge.
2529
2530 # A failure may indicate incorrect caching or cleaning up of
2531 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002532 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002533
2534 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2535 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2536 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002537 A.addApproval('code-review', 2)
2538 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002539
2540 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002541 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002542 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002543 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002544 self.waitUntilSettled()
2545
2546 # Reconfigure (with only one change in the pipeline).
2547 self.sched.reconfigure(self.config)
2548 self.waitUntilSettled()
2549
2550 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002551 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002552 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002553 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002554 self.waitUntilSettled()
2555
2556 # Reconfigure (with both in the pipeline).
2557 self.sched.reconfigure(self.config)
2558 self.waitUntilSettled()
2559
Paul Belanger174a8272017-03-14 13:20:10 -04002560 self.executor_server.hold_jobs_in_build = False
2561 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002562 self.waitUntilSettled()
2563
2564 self.assertEqual(len(self.history), 8)
2565
2566 self.assertEqual(A.data['status'], 'MERGED')
2567 self.assertEqual(A.reported, 2)
2568 self.assertEqual(B.data['status'], 'MERGED')
2569 self.assertEqual(B.reported, 2)
2570
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002571 def test_live_reconfiguration_del_project(self):
2572 # Test project deletion from layout
2573 # while changes are enqueued
2574
Paul Belanger174a8272017-03-14 13:20:10 -04002575 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002576 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2577 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2578 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2579
2580 # A Depends-On: B
2581 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2582 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002583 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002584
2585 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2586 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2587 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002588 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002589 self.waitUntilSettled()
2590 self.assertEqual(len(self.builds), 5)
2591
2592 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002593 self.commitConfigUpdate(
2594 'common-config',
2595 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002596 self.sched.reconfigure(self.config)
2597 self.waitUntilSettled()
2598
2599 # Builds for C aborted, builds for A succeed,
2600 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002601 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002602 self.assertEqual(job_c.changes, '3,1')
2603 self.assertEqual(job_c.result, 'ABORTED')
2604
Paul Belanger174a8272017-03-14 13:20:10 -04002605 self.executor_server.hold_jobs_in_build = False
2606 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002607 self.waitUntilSettled()
2608
Clint Byrum0149ada2017-04-04 12:22:01 -07002609 self.assertEqual(
2610 self.getJobFromHistory('project-test1', 'org/project').changes,
2611 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002612
2613 self.assertEqual(A.data['status'], 'NEW')
2614 self.assertEqual(B.data['status'], 'NEW')
2615 self.assertEqual(C.data['status'], 'NEW')
2616 self.assertEqual(A.reported, 1)
2617 self.assertEqual(B.reported, 0)
2618 self.assertEqual(C.reported, 0)
2619
Clint Byrum0149ada2017-04-04 12:22:01 -07002620 tenant = self.sched.abide.tenants.get('tenant-one')
2621 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002622 self.assertIn('Build succeeded', A.messages[0])
2623
James E. Blairec056492016-07-22 09:45:56 -07002624 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002625 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002626 self.updateConfigLayout(
2627 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002628 self.sched.reconfigure(self.config)
2629
2630 self.init_repo("org/new-project")
2631 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2632
James E. Blair8b5408c2016-08-08 15:37:46 -07002633 A.addApproval('code-review', 2)
2634 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002635 self.waitUntilSettled()
2636 self.assertEqual(self.getJobFromHistory('project-merge').result,
2637 'SUCCESS')
2638 self.assertEqual(self.getJobFromHistory('project-test1').result,
2639 'SUCCESS')
2640 self.assertEqual(self.getJobFromHistory('project-test2').result,
2641 'SUCCESS')
2642 self.assertEqual(A.data['status'], 'MERGED')
2643 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002644
James E. Blair9ea70072017-04-19 16:05:30 -07002645 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002646 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002647 self.init_repo("org/delete-project")
2648 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2649
James E. Blair8b5408c2016-08-08 15:37:46 -07002650 A.addApproval('code-review', 2)
2651 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002652 self.waitUntilSettled()
2653 self.assertEqual(self.getJobFromHistory('project-merge').result,
2654 'SUCCESS')
2655 self.assertEqual(self.getJobFromHistory('project-test1').result,
2656 'SUCCESS')
2657 self.assertEqual(self.getJobFromHistory('project-test2').result,
2658 'SUCCESS')
2659 self.assertEqual(A.data['status'], 'MERGED')
2660 self.assertEqual(A.reported, 2)
2661
2662 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002663 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002664 if os.path.exists(os.path.join(self.merger_src_root, p)):
2665 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002666 if os.path.exists(os.path.join(self.executor_src_root, p)):
2667 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002668
2669 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2670
James E. Blair8b5408c2016-08-08 15:37:46 -07002671 B.addApproval('code-review', 2)
2672 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002673 self.waitUntilSettled()
2674 self.assertEqual(self.getJobFromHistory('project-merge').result,
2675 'SUCCESS')
2676 self.assertEqual(self.getJobFromHistory('project-test1').result,
2677 'SUCCESS')
2678 self.assertEqual(self.getJobFromHistory('project-test2').result,
2679 'SUCCESS')
2680 self.assertEqual(B.data['status'], 'MERGED')
2681 self.assertEqual(B.reported, 2)
2682
James E. Blaire68d43f2017-04-19 14:03:30 -07002683 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002684 def test_tags(self):
2685 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002686 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2687 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2688 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2689 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2690 self.waitUntilSettled()
2691
James E. Blaire68d43f2017-04-19 14:03:30 -07002692 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002693
2694 results = {self.getJobFromHistory('merge',
2695 project='org/project1').uuid: 'extratag merge',
2696 self.getJobFromHistory('merge',
2697 project='org/project2').uuid: 'merge'}
James E. Blair456f2fb2016-02-09 09:29:33 -08002698
2699 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002700 self.assertEqual(results.get(build.uuid, ''),
2701 build.parameters['vars']['zuul'].get('tags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002702
James E. Blair63bb0ef2013-07-29 17:14:51 -07002703 def test_timer(self):
2704 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002705 # This test can not use simple_layout because it must start
2706 # with a configuration which does not include a
2707 # timer-triggered job so that we have an opportunity to set
2708 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002709 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002710 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002711 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002712
Clark Boylan3ee090a2014-04-03 20:55:09 -07002713 # The pipeline triggers every second, so we should have seen
2714 # several by now.
2715 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002716 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002717
2718 self.assertEqual(len(self.builds), 2)
2719
James E. Blair63bb0ef2013-07-29 17:14:51 -07002720 port = self.webapp.server.socket.getsockname()[1]
2721
Clint Byrum5e729e92017-02-08 06:34:57 -08002722 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002723 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002724 f = urllib.request.urlopen(req)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002725 data = f.read()
2726
Paul Belanger174a8272017-03-14 13:20:10 -04002727 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002728 # Stop queuing timer triggered jobs so that the assertions
2729 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002730 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002731 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04002732 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002733 self.waitUntilSettled()
2734
2735 self.assertEqual(self.getJobFromHistory(
2736 'project-bitrot-stable-old').result, 'SUCCESS')
2737 self.assertEqual(self.getJobFromHistory(
2738 'project-bitrot-stable-older').result, 'SUCCESS')
2739
2740 data = json.loads(data)
2741 status_jobs = set()
2742 for p in data['pipelines']:
2743 for q in p['change_queues']:
2744 for head in q['heads']:
2745 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002746 for job in change['jobs']:
2747 status_jobs.add(job['name'])
2748 self.assertIn('project-bitrot-stable-old', status_jobs)
2749 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002750
James E. Blair4f6033c2014-03-27 15:49:09 -07002751 def test_idle(self):
2752 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002753 # This test can not use simple_layout because it must start
2754 # with a configuration which does not include a
2755 # timer-triggered job so that we have an opportunity to set
2756 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002757 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002758
Clark Boylan3ee090a2014-04-03 20:55:09 -07002759 for x in range(1, 3):
2760 # Test that timer triggers periodic jobs even across
2761 # layout config reloads.
2762 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002763 self.commitConfigUpdate('common-config',
2764 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002765 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002766 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002767
Clark Boylan3ee090a2014-04-03 20:55:09 -07002768 # The pipeline triggers every second, so we should have seen
2769 # several by now.
2770 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002771
2772 # Stop queuing timer triggered jobs so that the assertions
2773 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002774 self.commitConfigUpdate('common-config',
2775 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002776 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002777 self.waitUntilSettled()
Clint Byrum58264dc2017-02-07 21:21:22 -08002778 self.assertEqual(len(self.builds), 2,
2779 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002780 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002781 self.waitUntilSettled()
2782 self.assertEqual(len(self.builds), 0)
2783 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002784
James E. Blaircc324192017-04-19 14:36:08 -07002785 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002786 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002787 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2788 self.waitUntilSettled()
2789
2790 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2791 self.waitUntilSettled()
2792
James E. Blairff80a2f2013-12-27 13:24:06 -08002793 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002794
2795 # A.messages only holds what FakeGerrit places in it. Thus we
2796 # work on the knowledge of what the first message should be as
2797 # it is only configured to go to SMTP.
2798
2799 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002800 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002801 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002802 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002803 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002804 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002805
2806 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002807 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002808 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002809 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002810 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002811 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002812
James E. Blaire5910202013-12-27 09:50:31 -08002813 def test_timer_smtp(self):
2814 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002815 # This test can not use simple_layout because it must start
2816 # with a configuration which does not include a
2817 # timer-triggered job so that we have an opportunity to set
2818 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002819 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002820 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002821 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002822
Clark Boylan3ee090a2014-04-03 20:55:09 -07002823 # The pipeline triggers every second, so we should have seen
2824 # several by now.
2825 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002826 self.waitUntilSettled()
2827
Clark Boylan3ee090a2014-04-03 20:55:09 -07002828 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04002829 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002830 self.waitUntilSettled()
2831 self.assertEqual(len(self.history), 2)
2832
James E. Blaire5910202013-12-27 09:50:31 -08002833 self.assertEqual(self.getJobFromHistory(
2834 'project-bitrot-stable-old').result, 'SUCCESS')
2835 self.assertEqual(self.getJobFromHistory(
2836 'project-bitrot-stable-older').result, 'SUCCESS')
2837
James E. Blairff80a2f2013-12-27 13:24:06 -08002838 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002839
2840 # A.messages only holds what FakeGerrit places in it. Thus we
2841 # work on the knowledge of what the first message should be as
2842 # it is only configured to go to SMTP.
2843
2844 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002845 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002846 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002847 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002848 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002849 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002850
Clark Boylan3ee090a2014-04-03 20:55:09 -07002851 # Stop queuing timer triggered jobs and let any that may have
2852 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002853 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002854 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07002855 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002856 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002857 self.waitUntilSettled()
2858
Joshua Hesketh25695cb2017-03-06 12:50:04 +11002859 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00002860 def test_timer_sshkey(self):
2861 "Test that a periodic job can setup SSH key authentication"
2862 self.worker.hold_jobs_in_build = True
2863 self.config.set('zuul', 'layout_config',
2864 'tests/fixtures/layout-timer.yaml')
2865 self.sched.reconfigure(self.config)
2866 self.registerJobs()
2867
2868 # The pipeline triggers every second, so we should have seen
2869 # several by now.
2870 time.sleep(5)
2871 self.waitUntilSettled()
2872
2873 self.assertEqual(len(self.builds), 2)
2874
2875 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
2876 self.assertTrue(os.path.isfile(ssh_wrapper))
2877 with open(ssh_wrapper) as f:
2878 ssh_wrapper_content = f.read()
2879 self.assertIn("fake_id_rsa", ssh_wrapper_content)
2880 # In the unit tests Merger runs in the same process,
2881 # so we see its' environment variables
2882 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
2883
2884 self.worker.release('.*')
2885 self.waitUntilSettled()
2886 self.assertEqual(len(self.history), 2)
2887
2888 self.assertEqual(self.getJobFromHistory(
2889 'project-bitrot-stable-old').result, 'SUCCESS')
2890 self.assertEqual(self.getJobFromHistory(
2891 'project-bitrot-stable-older').result, 'SUCCESS')
2892
2893 # Stop queuing timer triggered jobs and let any that may have
2894 # queued through so that end of test assertions pass.
2895 self.config.set('zuul', 'layout_config',
2896 'tests/fixtures/layout-no-timer.yaml')
2897 self.sched.reconfigure(self.config)
2898 self.registerJobs()
2899 self.waitUntilSettled()
2900 self.worker.release('.*')
2901 self.waitUntilSettled()
2902
James E. Blair91e34592015-07-31 16:45:59 -07002903 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08002904 "Test that the RPC client can enqueue a change"
2905 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002906 A.addApproval('code-review', 2)
2907 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08002908
2909 client = zuul.rpcclient.RPCClient('127.0.0.1',
2910 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002911 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04002912 r = client.enqueue(tenant='tenant-one',
2913 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002914 project='org/project',
2915 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002916 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002917 self.waitUntilSettled()
2918 self.assertEqual(self.getJobFromHistory('project-merge').result,
2919 'SUCCESS')
2920 self.assertEqual(self.getJobFromHistory('project-test1').result,
2921 'SUCCESS')
2922 self.assertEqual(self.getJobFromHistory('project-test2').result,
2923 'SUCCESS')
2924 self.assertEqual(A.data['status'], 'MERGED')
2925 self.assertEqual(A.reported, 2)
2926 self.assertEqual(r, True)
2927
James E. Blair91e34592015-07-31 16:45:59 -07002928 def test_client_enqueue_ref(self):
2929 "Test that the RPC client can enqueue a ref"
2930
2931 client = zuul.rpcclient.RPCClient('127.0.0.1',
2932 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002933 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07002934 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04002935 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07002936 pipeline='post',
2937 project='org/project',
2938 trigger='gerrit',
2939 ref='master',
2940 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
2941 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
2942 self.waitUntilSettled()
2943 job_names = [x.name for x in self.history]
2944 self.assertEqual(len(self.history), 1)
2945 self.assertIn('project-post', job_names)
2946 self.assertEqual(r, True)
2947
James E. Blairad28e912013-11-27 10:43:22 -08002948 def test_client_enqueue_negative(self):
2949 "Test that the RPC client returns errors"
2950 client = zuul.rpcclient.RPCClient('127.0.0.1',
2951 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002952 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08002953 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04002954 "Invalid tenant"):
2955 r = client.enqueue(tenant='tenant-foo',
2956 pipeline='gate',
2957 project='org/project',
2958 trigger='gerrit',
2959 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04002960 self.assertEqual(r, False)
2961
2962 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08002963 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002964 r = client.enqueue(tenant='tenant-one',
2965 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002966 project='project-does-not-exist',
2967 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002968 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002969 self.assertEqual(r, False)
2970
2971 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2972 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002973 r = client.enqueue(tenant='tenant-one',
2974 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08002975 project='org/project',
2976 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002977 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002978 self.assertEqual(r, False)
2979
2980 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2981 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002982 r = client.enqueue(tenant='tenant-one',
2983 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002984 project='org/project',
2985 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08002986 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002987 self.assertEqual(r, False)
2988
2989 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2990 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002991 r = client.enqueue(tenant='tenant-one',
2992 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002993 project='org/project',
2994 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002995 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002996 self.assertEqual(r, False)
2997
2998 self.waitUntilSettled()
2999 self.assertEqual(len(self.history), 0)
3000 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003001
3002 def test_client_promote(self):
3003 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003004 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003005 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3006 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3007 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003008 A.addApproval('code-review', 2)
3009 B.addApproval('code-review', 2)
3010 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003011
James E. Blair8b5408c2016-08-08 15:37:46 -07003012 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3013 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3014 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003015
3016 self.waitUntilSettled()
3017
Paul Belangerbaca3132016-11-04 12:49:54 -04003018 tenant = self.sched.abide.tenants.get('tenant-one')
3019 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003020 enqueue_times = {}
3021 for item in items:
3022 enqueue_times[str(item.change)] = item.enqueue_time
3023
James E. Blair36658cf2013-12-06 17:53:48 -08003024 client = zuul.rpcclient.RPCClient('127.0.0.1',
3025 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003026 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003027 r = client.promote(tenant='tenant-one',
3028 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003029 change_ids=['2,1', '3,1'])
3030
Sean Daguef39b9ca2014-01-10 21:34:35 -05003031 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003032 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003033 for item in items:
3034 self.assertEqual(
3035 enqueue_times[str(item.change)], item.enqueue_time)
3036
James E. Blair78acec92014-02-06 07:11:32 -08003037 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003038 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003039 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003040 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003041 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003042 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003043 self.waitUntilSettled()
3044
3045 self.assertEqual(len(self.builds), 6)
3046 self.assertEqual(self.builds[0].name, 'project-test1')
3047 self.assertEqual(self.builds[1].name, 'project-test2')
3048 self.assertEqual(self.builds[2].name, 'project-test1')
3049 self.assertEqual(self.builds[3].name, 'project-test2')
3050 self.assertEqual(self.builds[4].name, 'project-test1')
3051 self.assertEqual(self.builds[5].name, 'project-test2')
3052
Paul Belangerbaca3132016-11-04 12:49:54 -04003053 self.assertTrue(self.builds[0].hasChanges(B))
3054 self.assertFalse(self.builds[0].hasChanges(A))
3055 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003056
Paul Belangerbaca3132016-11-04 12:49:54 -04003057 self.assertTrue(self.builds[2].hasChanges(B))
3058 self.assertTrue(self.builds[2].hasChanges(C))
3059 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003060
Paul Belangerbaca3132016-11-04 12:49:54 -04003061 self.assertTrue(self.builds[4].hasChanges(B))
3062 self.assertTrue(self.builds[4].hasChanges(C))
3063 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003064
Paul Belanger174a8272017-03-14 13:20:10 -04003065 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003066 self.waitUntilSettled()
3067
3068 self.assertEqual(A.data['status'], 'MERGED')
3069 self.assertEqual(A.reported, 2)
3070 self.assertEqual(B.data['status'], 'MERGED')
3071 self.assertEqual(B.reported, 2)
3072 self.assertEqual(C.data['status'], 'MERGED')
3073 self.assertEqual(C.reported, 2)
3074
James E. Blair36658cf2013-12-06 17:53:48 -08003075 self.assertEqual(r, True)
3076
3077 def test_client_promote_dependent(self):
3078 "Test that the RPC client can promote a dependent change"
3079 # C (depends on B) -> B -> A ; then promote C to get:
3080 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003081 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003082 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3083 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3084 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3085
3086 C.setDependsOn(B, 1)
3087
James E. Blair8b5408c2016-08-08 15:37:46 -07003088 A.addApproval('code-review', 2)
3089 B.addApproval('code-review', 2)
3090 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003091
James E. Blair8b5408c2016-08-08 15:37:46 -07003092 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3093 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3094 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003095
3096 self.waitUntilSettled()
3097
3098 client = zuul.rpcclient.RPCClient('127.0.0.1',
3099 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003100 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003101 r = client.promote(tenant='tenant-one',
3102 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003103 change_ids=['3,1'])
3104
James E. Blair78acec92014-02-06 07:11:32 -08003105 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003106 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003107 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003108 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003109 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003110 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003111 self.waitUntilSettled()
3112
3113 self.assertEqual(len(self.builds), 6)
3114 self.assertEqual(self.builds[0].name, 'project-test1')
3115 self.assertEqual(self.builds[1].name, 'project-test2')
3116 self.assertEqual(self.builds[2].name, 'project-test1')
3117 self.assertEqual(self.builds[3].name, 'project-test2')
3118 self.assertEqual(self.builds[4].name, 'project-test1')
3119 self.assertEqual(self.builds[5].name, 'project-test2')
3120
Paul Belangerbaca3132016-11-04 12:49:54 -04003121 self.assertTrue(self.builds[0].hasChanges(B))
3122 self.assertFalse(self.builds[0].hasChanges(A))
3123 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003124
Paul Belangerbaca3132016-11-04 12:49:54 -04003125 self.assertTrue(self.builds[2].hasChanges(B))
3126 self.assertTrue(self.builds[2].hasChanges(C))
3127 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003128
Paul Belangerbaca3132016-11-04 12:49:54 -04003129 self.assertTrue(self.builds[4].hasChanges(B))
3130 self.assertTrue(self.builds[4].hasChanges(C))
3131 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003132
Paul Belanger174a8272017-03-14 13:20:10 -04003133 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003134 self.waitUntilSettled()
3135
3136 self.assertEqual(A.data['status'], 'MERGED')
3137 self.assertEqual(A.reported, 2)
3138 self.assertEqual(B.data['status'], 'MERGED')
3139 self.assertEqual(B.reported, 2)
3140 self.assertEqual(C.data['status'], 'MERGED')
3141 self.assertEqual(C.reported, 2)
3142
James E. Blair36658cf2013-12-06 17:53:48 -08003143 self.assertEqual(r, True)
3144
3145 def test_client_promote_negative(self):
3146 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003147 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003148 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003149 A.addApproval('code-review', 2)
3150 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003151 self.waitUntilSettled()
3152
3153 client = zuul.rpcclient.RPCClient('127.0.0.1',
3154 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003155 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003156
3157 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003158 r = client.promote(tenant='tenant-one',
3159 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003160 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003161 self.assertEqual(r, False)
3162
3163 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003164 r = client.promote(tenant='tenant-one',
3165 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003166 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003167 self.assertEqual(r, False)
3168
Paul Belanger174a8272017-03-14 13:20:10 -04003169 self.executor_server.hold_jobs_in_build = False
3170 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003171 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003172
James E. Blaircc324192017-04-19 14:36:08 -07003173 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003174 def test_queue_rate_limiting(self):
3175 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003176 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003177 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3178 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3179 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3180
3181 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003182 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003183
James E. Blair8b5408c2016-08-08 15:37:46 -07003184 A.addApproval('code-review', 2)
3185 B.addApproval('code-review', 2)
3186 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003187
James E. Blair8b5408c2016-08-08 15:37:46 -07003188 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3189 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3190 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003191 self.waitUntilSettled()
3192
3193 # Only A and B will have their merge jobs queued because
3194 # window is 2.
3195 self.assertEqual(len(self.builds), 2)
3196 self.assertEqual(self.builds[0].name, 'project-merge')
3197 self.assertEqual(self.builds[1].name, 'project-merge')
3198
James E. Blair732ee862017-04-21 07:49:26 -07003199 # Release the merge jobs one at a time.
3200 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003201 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003202 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003203 self.waitUntilSettled()
3204
3205 # Only A and B will have their test jobs queued because
3206 # window is 2.
3207 self.assertEqual(len(self.builds), 4)
3208 self.assertEqual(self.builds[0].name, 'project-test1')
3209 self.assertEqual(self.builds[1].name, 'project-test2')
3210 self.assertEqual(self.builds[2].name, 'project-test1')
3211 self.assertEqual(self.builds[3].name, 'project-test2')
3212
Paul Belanger174a8272017-03-14 13:20:10 -04003213 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003214 self.waitUntilSettled()
3215
James E. Blaircc324192017-04-19 14:36:08 -07003216 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003217 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003218 # A failed so window is reduced by 1 to 1.
3219 self.assertEqual(queue.window, 1)
3220 self.assertEqual(queue.window_floor, 1)
3221 self.assertEqual(A.data['status'], 'NEW')
3222
3223 # Gate is reset and only B's merge job is queued because
3224 # window shrunk to 1.
3225 self.assertEqual(len(self.builds), 1)
3226 self.assertEqual(self.builds[0].name, 'project-merge')
3227
Paul Belanger174a8272017-03-14 13:20:10 -04003228 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003229 self.waitUntilSettled()
3230
3231 # Only B's test jobs are queued because window is still 1.
3232 self.assertEqual(len(self.builds), 2)
3233 self.assertEqual(self.builds[0].name, 'project-test1')
3234 self.assertEqual(self.builds[1].name, 'project-test2')
3235
Paul Belanger174a8272017-03-14 13:20:10 -04003236 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003237 self.waitUntilSettled()
3238
3239 # B was successfully merged so window is increased to 2.
3240 self.assertEqual(queue.window, 2)
3241 self.assertEqual(queue.window_floor, 1)
3242 self.assertEqual(B.data['status'], 'MERGED')
3243
3244 # Only C is left and its merge job is queued.
3245 self.assertEqual(len(self.builds), 1)
3246 self.assertEqual(self.builds[0].name, 'project-merge')
3247
Paul Belanger174a8272017-03-14 13:20:10 -04003248 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003249 self.waitUntilSettled()
3250
3251 # After successful merge job the test jobs for C are queued.
3252 self.assertEqual(len(self.builds), 2)
3253 self.assertEqual(self.builds[0].name, 'project-test1')
3254 self.assertEqual(self.builds[1].name, 'project-test2')
3255
Paul Belanger174a8272017-03-14 13:20:10 -04003256 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003257 self.waitUntilSettled()
3258
3259 # C successfully merged so window is bumped to 3.
3260 self.assertEqual(queue.window, 3)
3261 self.assertEqual(queue.window_floor, 1)
3262 self.assertEqual(C.data['status'], 'MERGED')
3263
James E. Blaircc324192017-04-19 14:36:08 -07003264 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003265 def test_queue_rate_limiting_dependent(self):
3266 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003267 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003268 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3269 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3270 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3271
3272 B.setDependsOn(A, 1)
3273
Paul Belanger174a8272017-03-14 13:20:10 -04003274 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003275
James E. Blair8b5408c2016-08-08 15:37:46 -07003276 A.addApproval('code-review', 2)
3277 B.addApproval('code-review', 2)
3278 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003279
James E. Blair8b5408c2016-08-08 15:37:46 -07003280 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3281 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3282 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003283 self.waitUntilSettled()
3284
3285 # Only A and B will have their merge jobs queued because
3286 # window is 2.
3287 self.assertEqual(len(self.builds), 2)
3288 self.assertEqual(self.builds[0].name, 'project-merge')
3289 self.assertEqual(self.builds[1].name, 'project-merge')
3290
James E. Blairf4a5f022017-04-18 14:01:10 -07003291 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003292
3293 # Only A and B will have their test jobs queued because
3294 # window is 2.
3295 self.assertEqual(len(self.builds), 4)
3296 self.assertEqual(self.builds[0].name, 'project-test1')
3297 self.assertEqual(self.builds[1].name, 'project-test2')
3298 self.assertEqual(self.builds[2].name, 'project-test1')
3299 self.assertEqual(self.builds[3].name, 'project-test2')
3300
Paul Belanger174a8272017-03-14 13:20:10 -04003301 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003302 self.waitUntilSettled()
3303
James E. Blaircc324192017-04-19 14:36:08 -07003304 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003305 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003306 # A failed so window is reduced by 1 to 1.
3307 self.assertEqual(queue.window, 1)
3308 self.assertEqual(queue.window_floor, 1)
3309 self.assertEqual(A.data['status'], 'NEW')
3310 self.assertEqual(B.data['status'], 'NEW')
3311
3312 # Gate is reset and only C's merge job is queued because
3313 # window shrunk to 1 and A and B were dequeued.
3314 self.assertEqual(len(self.builds), 1)
3315 self.assertEqual(self.builds[0].name, 'project-merge')
3316
James E. Blairf4a5f022017-04-18 14:01:10 -07003317 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003318
3319 # Only C's test jobs are queued because window is still 1.
3320 self.assertEqual(len(self.builds), 2)
3321 self.assertEqual(self.builds[0].name, 'project-test1')
3322 self.assertEqual(self.builds[1].name, 'project-test2')
3323
Paul Belanger174a8272017-03-14 13:20:10 -04003324 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003325 self.waitUntilSettled()
3326
3327 # C was successfully merged so window is increased to 2.
3328 self.assertEqual(queue.window, 2)
3329 self.assertEqual(queue.window_floor, 1)
3330 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003331
James E. Blairec056492016-07-22 09:45:56 -07003332 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003333 def test_worker_update_metadata(self):
3334 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003335 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003336
3337 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003338 A.addApproval('code-review', 2)
3339 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003340 self.waitUntilSettled()
3341
Paul Belanger174a8272017-03-14 13:20:10 -04003342 self.assertEqual(len(self.executor.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003343
3344 self.log.debug('Current builds:')
Paul Belanger174a8272017-03-14 13:20:10 -04003345 self.log.debug(self.executor.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003346
3347 start = time.time()
3348 while True:
3349 if time.time() - start > 10:
3350 raise Exception("Timeout waiting for gearman server to report "
3351 + "back to the client")
Paul Belanger174a8272017-03-14 13:20:10 -04003352 build = self.executor.builds.values()[0]
Joshua Heskethba8776a2014-01-12 14:35:40 +08003353 if build.worker.name == "My Worker":
3354 break
3355 else:
3356 time.sleep(0)
3357
3358 self.log.debug(build)
3359 self.assertEqual("My Worker", build.worker.name)
3360 self.assertEqual("localhost", build.worker.hostname)
3361 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3362 self.assertEqual("zuul.example.org", build.worker.fqdn)
3363 self.assertEqual("FakeBuilder", build.worker.program)
3364 self.assertEqual("v1.1", build.worker.version)
3365 self.assertEqual({'something': 'else'}, build.worker.extra)
3366
Paul Belanger174a8272017-03-14 13:20:10 -04003367 self.executor_server.hold_jobs_in_build = False
3368 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003369 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003370
James E. Blaircc324192017-04-19 14:36:08 -07003371 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003372 def test_footer_message(self):
3373 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003374 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003375 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003376 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003377 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003378 self.waitUntilSettled()
3379
3380 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003381 B.addApproval('code-review', 2)
3382 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003383 self.waitUntilSettled()
3384
3385 self.assertEqual(2, len(self.smtp_messages))
3386
Adam Gandelman82258fd2017-02-07 16:51:58 -08003387 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003388Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003389http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003390
Adam Gandelman82258fd2017-02-07 16:51:58 -08003391 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003392For CI problems and help debugging, contact ci@example.org"""
3393
Adam Gandelman82258fd2017-02-07 16:51:58 -08003394 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3395 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3396 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3397 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003398
James E. Blairec056492016-07-22 09:45:56 -07003399 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003400 def test_merge_failure_reporters(self):
3401 """Check that the config is set up correctly"""
3402
James E. Blairf84026c2015-12-08 16:11:46 -08003403 self.updateConfigLayout(
3404 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003405 self.sched.reconfigure(self.config)
3406 self.registerJobs()
3407
3408 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003409 "Merge Failed.\n\nThis change or one of its cross-repo "
3410 "dependencies was unable to be automatically merged with the "
3411 "current state of its repository. Please rebase the change and "
3412 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003413 self.sched.layout.pipelines['check'].merge_failure_message)
3414 self.assertEqual(
3415 "The merge failed! For more information...",
3416 self.sched.layout.pipelines['gate'].merge_failure_message)
3417
3418 self.assertEqual(
3419 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3420 self.assertEqual(
3421 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3422
3423 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003424 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3425 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003426
3427 self.assertTrue(
3428 (
3429 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003430 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003431 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003432 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003433 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003434 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003435 ) or (
3436 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003437 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003438 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003439 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003440 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003441 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003442 )
3443 )
3444
James E. Blairec056492016-07-22 09:45:56 -07003445 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003446 def test_merge_failure_reports(self):
3447 """Check that when a change fails to merge the correct message is sent
3448 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003449 self.updateConfigLayout(
3450 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003451 self.sched.reconfigure(self.config)
3452 self.registerJobs()
3453
3454 # Check a test failure isn't reported to SMTP
3455 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003456 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003457 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003458 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003459 self.waitUntilSettled()
3460
3461 self.assertEqual(3, len(self.history)) # 3 jobs
3462 self.assertEqual(0, len(self.smtp_messages))
3463
3464 # Check a merge failure is reported to SMTP
3465 # B should be merged, but C will conflict with B
3466 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3467 B.addPatchset(['conflict'])
3468 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3469 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003470 B.addApproval('code-review', 2)
3471 C.addApproval('code-review', 2)
3472 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3473 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003474 self.waitUntilSettled()
3475
3476 self.assertEqual(6, len(self.history)) # A and B jobs
3477 self.assertEqual(1, len(self.smtp_messages))
3478 self.assertEqual('The merge failed! For more information...',
3479 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003480
James E. Blairec056492016-07-22 09:45:56 -07003481 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003482 def test_default_merge_failure_reports(self):
3483 """Check that the default merge failure reports are correct."""
3484
3485 # A should report success, B should report merge failure.
3486 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3487 A.addPatchset(['conflict'])
3488 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3489 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003490 A.addApproval('code-review', 2)
3491 B.addApproval('code-review', 2)
3492 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3493 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003494 self.waitUntilSettled()
3495
3496 self.assertEqual(3, len(self.history)) # A jobs
3497 self.assertEqual(A.reported, 2)
3498 self.assertEqual(B.reported, 2)
3499 self.assertEqual(A.data['status'], 'MERGED')
3500 self.assertEqual(B.data['status'], 'NEW')
3501 self.assertIn('Build succeeded', A.messages[1])
3502 self.assertIn('Merge Failed', B.messages[1])
3503 self.assertIn('automatically merged', B.messages[1])
3504 self.assertNotIn('logs.example.com', B.messages[1])
3505 self.assertNotIn('SKIPPED', B.messages[1])
3506
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003507 def test_client_get_running_jobs(self):
3508 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003509 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003510 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003511 A.addApproval('code-review', 2)
3512 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003513 self.waitUntilSettled()
3514
3515 client = zuul.rpcclient.RPCClient('127.0.0.1',
3516 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003517 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003518
3519 # Wait for gearman server to send the initial workData back to zuul
3520 start = time.time()
3521 while True:
3522 if time.time() - start > 10:
3523 raise Exception("Timeout waiting for gearman server to report "
3524 + "back to the client")
Paul Belanger174a8272017-03-14 13:20:10 -04003525 build = self.executor_client.builds.values()[0]
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003526 if build.worker.name == "My Worker":
3527 break
3528 else:
3529 time.sleep(0)
3530
3531 running_items = client.get_running_jobs()
3532
3533 self.assertEqual(1, len(running_items))
3534 running_item = running_items[0]
3535 self.assertEqual([], running_item['failing_reasons'])
3536 self.assertEqual([], running_item['items_behind'])
3537 self.assertEqual('https://hostname/1', running_item['url'])
3538 self.assertEqual(None, running_item['item_ahead'])
3539 self.assertEqual('org/project', running_item['project'])
3540 self.assertEqual(None, running_item['remaining_time'])
3541 self.assertEqual(True, running_item['active'])
3542 self.assertEqual('1,1', running_item['id'])
3543
3544 self.assertEqual(3, len(running_item['jobs']))
3545 for job in running_item['jobs']:
3546 if job['name'] == 'project-merge':
3547 self.assertEqual('project-merge', job['name'])
3548 self.assertEqual('gate', job['pipeline'])
3549 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003550 self.assertEqual('https://server/job/project-merge/0/',
3551 job['url'])
3552 self.assertEqual(7, len(job['worker']))
3553 self.assertEqual(False, job['canceled'])
3554 self.assertEqual(True, job['voting'])
3555 self.assertEqual(None, job['result'])
3556 self.assertEqual('gate', job['pipeline'])
3557 break
3558
Paul Belanger174a8272017-03-14 13:20:10 -04003559 self.executor_server.hold_jobs_in_build = False
3560 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003561 self.waitUntilSettled()
3562
3563 running_items = client.get_running_jobs()
3564 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003565
James E. Blairb70e55a2017-04-19 12:57:02 -07003566 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003567 def test_nonvoting_pipeline(self):
3568 "Test that a nonvoting pipeline (experimental) can still report"
3569
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003570 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3571 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003572 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3573 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003574
3575 self.assertEqual(self.getJobFromHistory('project-merge').result,
3576 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003577 self.assertEqual(
3578 self.getJobFromHistory('experimental-project-test').result,
3579 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003580 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003581
3582 def test_crd_gate(self):
3583 "Test cross-repo dependencies"
3584 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3585 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003586 A.addApproval('code-review', 2)
3587 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003588
3589 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3590 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3591 AM2.setMerged()
3592 AM1.setMerged()
3593
3594 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3595 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3596 BM2.setMerged()
3597 BM1.setMerged()
3598
3599 # A -> AM1 -> AM2
3600 # B -> BM1 -> BM2
3601 # A Depends-On: B
3602 # M2 is here to make sure it is never queried. If it is, it
3603 # means zuul is walking down the entire history of merged
3604 # changes.
3605
3606 B.setDependsOn(BM1, 1)
3607 BM1.setDependsOn(BM2, 1)
3608
3609 A.setDependsOn(AM1, 1)
3610 AM1.setDependsOn(AM2, 1)
3611
3612 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3613 A.subject, B.data['id'])
3614
James E. Blair8b5408c2016-08-08 15:37:46 -07003615 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003616 self.waitUntilSettled()
3617
3618 self.assertEqual(A.data['status'], 'NEW')
3619 self.assertEqual(B.data['status'], 'NEW')
3620
Paul Belanger5dccbe72016-11-14 11:17:37 -05003621 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003622 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003623
Paul Belanger174a8272017-03-14 13:20:10 -04003624 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003625 B.addApproval('approved', 1)
3626 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003627 self.waitUntilSettled()
3628
Paul Belanger174a8272017-03-14 13:20:10 -04003629 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003630 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003631 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003632 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003633 self.executor_server.hold_jobs_in_build = False
3634 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003635 self.waitUntilSettled()
3636
3637 self.assertEqual(AM2.queried, 0)
3638 self.assertEqual(BM2.queried, 0)
3639 self.assertEqual(A.data['status'], 'MERGED')
3640 self.assertEqual(B.data['status'], 'MERGED')
3641 self.assertEqual(A.reported, 2)
3642 self.assertEqual(B.reported, 2)
3643
Paul Belanger5dccbe72016-11-14 11:17:37 -05003644 changes = self.getJobFromHistory(
3645 'project-merge', 'org/project1').changes
3646 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003647
3648 def test_crd_branch(self):
3649 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003650
3651 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003652 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3653 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3654 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3655 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003656 A.addApproval('code-review', 2)
3657 B.addApproval('code-review', 2)
3658 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003659
3660 # A Depends-On: B+C
3661 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3662 A.subject, B.data['id'])
3663
Paul Belanger174a8272017-03-14 13:20:10 -04003664 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003665 B.addApproval('approved', 1)
3666 C.addApproval('approved', 1)
3667 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003668 self.waitUntilSettled()
3669
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.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003673 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003674 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003675 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003676 self.executor_server.hold_jobs_in_build = False
3677 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003678 self.waitUntilSettled()
3679
3680 self.assertEqual(A.data['status'], 'MERGED')
3681 self.assertEqual(B.data['status'], 'MERGED')
3682 self.assertEqual(C.data['status'], 'MERGED')
3683 self.assertEqual(A.reported, 2)
3684 self.assertEqual(B.reported, 2)
3685 self.assertEqual(C.reported, 2)
3686
Paul Belanger6379db12016-11-14 13:57:54 -05003687 changes = self.getJobFromHistory(
3688 'project-merge', 'org/project1').changes
3689 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003690
3691 def test_crd_multiline(self):
3692 "Test multiple depends-on lines in commit"
3693 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3694 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3695 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003696 A.addApproval('code-review', 2)
3697 B.addApproval('code-review', 2)
3698 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003699
3700 # A Depends-On: B+C
3701 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3702 A.subject, B.data['id'], C.data['id'])
3703
Paul Belanger174a8272017-03-14 13:20:10 -04003704 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003705 B.addApproval('approved', 1)
3706 C.addApproval('approved', 1)
3707 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003708 self.waitUntilSettled()
3709
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.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003713 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003714 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003715 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003716 self.executor_server.hold_jobs_in_build = False
3717 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003718 self.waitUntilSettled()
3719
3720 self.assertEqual(A.data['status'], 'MERGED')
3721 self.assertEqual(B.data['status'], 'MERGED')
3722 self.assertEqual(C.data['status'], 'MERGED')
3723 self.assertEqual(A.reported, 2)
3724 self.assertEqual(B.reported, 2)
3725 self.assertEqual(C.reported, 2)
3726
Paul Belanger5dccbe72016-11-14 11:17:37 -05003727 changes = self.getJobFromHistory(
3728 'project-merge', 'org/project1').changes
3729 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003730
3731 def test_crd_unshared_gate(self):
3732 "Test cross-repo dependencies in unshared gate queues"
3733 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3734 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003735 A.addApproval('code-review', 2)
3736 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003737
3738 # A Depends-On: B
3739 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3740 A.subject, B.data['id'])
3741
3742 # A and B do not share a queue, make sure that A is unable to
3743 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003744 B.addApproval('approved', 1)
3745 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003746 self.waitUntilSettled()
3747
3748 self.assertEqual(A.data['status'], 'NEW')
3749 self.assertEqual(B.data['status'], 'NEW')
3750 self.assertEqual(A.reported, 0)
3751 self.assertEqual(B.reported, 0)
3752 self.assertEqual(len(self.history), 0)
3753
3754 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003755 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003756 self.waitUntilSettled()
3757
3758 self.assertEqual(B.data['status'], 'MERGED')
3759 self.assertEqual(B.reported, 2)
3760
3761 # Now that B is merged, A should be able to be enqueued and
3762 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003763 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003764 self.waitUntilSettled()
3765
3766 self.assertEqual(A.data['status'], 'MERGED')
3767 self.assertEqual(A.reported, 2)
3768
James E. Blair96698e22015-04-02 07:48:21 -07003769 def test_crd_gate_reverse(self):
3770 "Test reverse cross-repo dependencies"
3771 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3772 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003773 A.addApproval('code-review', 2)
3774 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003775
3776 # A Depends-On: B
3777
3778 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3779 A.subject, B.data['id'])
3780
James E. Blair8b5408c2016-08-08 15:37:46 -07003781 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003782 self.waitUntilSettled()
3783
3784 self.assertEqual(A.data['status'], 'NEW')
3785 self.assertEqual(B.data['status'], 'NEW')
3786
Paul Belanger174a8272017-03-14 13:20:10 -04003787 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003788 A.addApproval('approved', 1)
3789 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003790 self.waitUntilSettled()
3791
Paul Belanger174a8272017-03-14 13:20:10 -04003792 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003793 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003794 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003795 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003796 self.executor_server.hold_jobs_in_build = False
3797 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003798 self.waitUntilSettled()
3799
3800 self.assertEqual(A.data['status'], 'MERGED')
3801 self.assertEqual(B.data['status'], 'MERGED')
3802 self.assertEqual(A.reported, 2)
3803 self.assertEqual(B.reported, 2)
3804
Paul Belanger5dccbe72016-11-14 11:17:37 -05003805 changes = self.getJobFromHistory(
3806 'project-merge', 'org/project1').changes
3807 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003808
James E. Blair5ee24252014-12-30 10:12:29 -08003809 def test_crd_cycle(self):
3810 "Test cross-repo dependency cycles"
3811 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3812 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003813 A.addApproval('code-review', 2)
3814 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003815
3816 # A -> B -> A (via commit-depends)
3817
3818 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3819 A.subject, B.data['id'])
3820 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3821 B.subject, A.data['id'])
3822
James E. Blair8b5408c2016-08-08 15:37:46 -07003823 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003824 self.waitUntilSettled()
3825
3826 self.assertEqual(A.reported, 0)
3827 self.assertEqual(B.reported, 0)
3828 self.assertEqual(A.data['status'], 'NEW')
3829 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003830
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003831 def test_crd_gate_unknown(self):
3832 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07003833 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003834 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3835 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003836 A.addApproval('code-review', 2)
3837 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003838
3839 # A Depends-On: B
3840 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3841 A.subject, B.data['id'])
3842
James E. Blair8b5408c2016-08-08 15:37:46 -07003843 B.addApproval('approved', 1)
3844 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003845 self.waitUntilSettled()
3846
3847 # Unknown projects cannot share a queue with any other
3848 # since they don't have common jobs with any other (they have no jobs).
3849 # Changes which depend on unknown project changes
3850 # should not be processed in dependent pipeline
3851 self.assertEqual(A.data['status'], 'NEW')
3852 self.assertEqual(B.data['status'], 'NEW')
3853 self.assertEqual(A.reported, 0)
3854 self.assertEqual(B.reported, 0)
3855 self.assertEqual(len(self.history), 0)
3856
3857 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07003858 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003859 B.setMerged()
3860 self.waitUntilSettled()
3861 self.assertEqual(len(self.history), 0)
3862
3863 # Now that B is merged, A should be able to be enqueued and
3864 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003865 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003866 self.waitUntilSettled()
3867
3868 self.assertEqual(A.data['status'], 'MERGED')
3869 self.assertEqual(A.reported, 2)
3870 self.assertEqual(B.data['status'], 'MERGED')
3871 self.assertEqual(B.reported, 0)
3872
James E. Blairbfb8e042014-12-30 17:01:44 -08003873 def test_crd_check(self):
3874 "Test cross-repo dependencies in independent pipelines"
3875
Paul Belanger174a8272017-03-14 13:20:10 -04003876 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08003877 self.gearman_server.hold_jobs_in_queue = True
3878 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3879 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3880
3881 # A Depends-On: B
3882 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3883 A.subject, B.data['id'])
3884
3885 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3886 self.waitUntilSettled()
3887
James E. Blairbfb8e042014-12-30 17:01:44 -08003888 self.gearman_server.hold_jobs_in_queue = False
3889 self.gearman_server.release()
3890 self.waitUntilSettled()
3891
Paul Belanger174a8272017-03-14 13:20:10 -04003892 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05003893 self.waitUntilSettled()
3894
James E. Blair2a535672017-04-27 12:03:15 -07003895 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08003896
Paul Belanger174a8272017-03-14 13:20:10 -04003897 self.executor_server.hold_jobs_in_build = False
3898 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05003899 self.waitUntilSettled()
3900
James E. Blairbfb8e042014-12-30 17:01:44 -08003901 self.assertEqual(A.data['status'], 'NEW')
3902 self.assertEqual(B.data['status'], 'NEW')
3903 self.assertEqual(A.reported, 1)
3904 self.assertEqual(B.reported, 0)
3905
3906 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05003907 tenant = self.sched.abide.tenants.get('tenant-one')
3908 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08003909
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003910 def test_crd_check_git_depends(self):
3911 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07003912 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003913 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3914 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3915
3916 # Add two git-dependent changes and make sure they both report
3917 # success.
3918 B.setDependsOn(A, 1)
3919 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3920 self.waitUntilSettled()
3921 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3922 self.waitUntilSettled()
3923
James E. Blairb8c16472015-05-05 14:55:26 -07003924 self.orderedRelease()
3925 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003926 self.waitUntilSettled()
3927
3928 self.assertEqual(A.data['status'], 'NEW')
3929 self.assertEqual(B.data['status'], 'NEW')
3930 self.assertEqual(A.reported, 1)
3931 self.assertEqual(B.reported, 1)
3932
3933 self.assertEqual(self.history[0].changes, '1,1')
3934 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04003935 tenant = self.sched.abide.tenants.get('tenant-one')
3936 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003937
3938 self.assertIn('Build succeeded', A.messages[0])
3939 self.assertIn('Build succeeded', B.messages[0])
3940
3941 def test_crd_check_duplicate(self):
3942 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04003943 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003944 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3945 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04003946 tenant = self.sched.abide.tenants.get('tenant-one')
3947 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003948
3949 # Add two git-dependent changes...
3950 B.setDependsOn(A, 1)
3951 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3952 self.waitUntilSettled()
3953 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3954
3955 # ...make sure the live one is not duplicated...
3956 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3957 self.waitUntilSettled()
3958 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3959
3960 # ...but the non-live one is able to be.
3961 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3962 self.waitUntilSettled()
3963 self.assertEqual(len(check_pipeline.getAllItems()), 3)
3964
Clark Boylandd849822015-03-02 12:38:14 -08003965 # Release jobs in order to avoid races with change A jobs
3966 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07003967 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04003968 self.executor_server.hold_jobs_in_build = False
3969 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003970 self.waitUntilSettled()
3971
3972 self.assertEqual(A.data['status'], 'NEW')
3973 self.assertEqual(B.data['status'], 'NEW')
3974 self.assertEqual(A.reported, 1)
3975 self.assertEqual(B.reported, 1)
3976
3977 self.assertEqual(self.history[0].changes, '1,1 2,1')
3978 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04003979 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003980
3981 self.assertIn('Build succeeded', A.messages[0])
3982 self.assertIn('Build succeeded', B.messages[0])
3983
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003984 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08003985 "Test cross-repo dependencies re-enqueued in independent pipelines"
3986
3987 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003988 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
3989 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08003990
3991 # A Depends-On: B
3992 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3993 A.subject, B.data['id'])
3994
3995 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3996 self.waitUntilSettled()
3997
3998 self.sched.reconfigure(self.config)
3999
4000 # Make sure the items still share a change queue, and the
4001 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004002 tenant = self.sched.abide.tenants.get('tenant-one')
4003 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4004 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004005 first_item = queue.queue[0]
4006 for item in queue.queue:
4007 self.assertEqual(item.queue, first_item.queue)
4008 self.assertFalse(first_item.live)
4009 self.assertTrue(queue.queue[1].live)
4010
4011 self.gearman_server.hold_jobs_in_queue = False
4012 self.gearman_server.release()
4013 self.waitUntilSettled()
4014
4015 self.assertEqual(A.data['status'], 'NEW')
4016 self.assertEqual(B.data['status'], 'NEW')
4017 self.assertEqual(A.reported, 1)
4018 self.assertEqual(B.reported, 0)
4019
4020 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004021 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004022
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004023 def test_crd_check_reconfiguration(self):
4024 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4025
4026 def test_crd_undefined_project(self):
4027 """Test that undefined projects in dependencies are handled for
4028 independent pipelines"""
4029 # It's a hack for fake gerrit,
4030 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004031 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004032 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4033
Jesse Keatingea68ab42017-04-19 15:03:30 -07004034 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004035 def test_crd_check_ignore_dependencies(self):
4036 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004037
4038 self.gearman_server.hold_jobs_in_queue = True
4039 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4040 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4041 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4042
4043 # A Depends-On: B
4044 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4045 A.subject, B.data['id'])
4046 # C git-depends on B
4047 C.setDependsOn(B, 1)
4048 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4049 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4050 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4051 self.waitUntilSettled()
4052
4053 # Make sure none of the items share a change queue, and all
4054 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004055 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004056 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004057 self.assertEqual(len(check_pipeline.queues), 3)
4058 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4059 for item in check_pipeline.getAllItems():
4060 self.assertTrue(item.live)
4061
4062 self.gearman_server.hold_jobs_in_queue = False
4063 self.gearman_server.release()
4064 self.waitUntilSettled()
4065
4066 self.assertEqual(A.data['status'], 'NEW')
4067 self.assertEqual(B.data['status'], 'NEW')
4068 self.assertEqual(C.data['status'], 'NEW')
4069 self.assertEqual(A.reported, 1)
4070 self.assertEqual(B.reported, 1)
4071 self.assertEqual(C.reported, 1)
4072
4073 # Each job should have tested exactly one change
4074 for job in self.history:
4075 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004076
James E. Blair7cb84542017-04-19 13:35:05 -07004077 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004078 def test_crd_check_transitive(self):
4079 "Test transitive cross-repo dependencies"
4080 # Specifically, if A -> B -> C, and C gets a new patchset and
4081 # A gets a new patchset, ensure the test of A,2 includes B,1
4082 # and C,2 (not C,1 which would indicate stale data in the
4083 # cache for B).
4084 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4085 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4086 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4087
4088 # A Depends-On: B
4089 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4090 A.subject, B.data['id'])
4091
4092 # B Depends-On: C
4093 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4094 B.subject, C.data['id'])
4095
4096 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4097 self.waitUntilSettled()
4098 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4099
4100 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4101 self.waitUntilSettled()
4102 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4103
4104 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4105 self.waitUntilSettled()
4106 self.assertEqual(self.history[-1].changes, '3,1')
4107
4108 C.addPatchset()
4109 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4110 self.waitUntilSettled()
4111 self.assertEqual(self.history[-1].changes, '3,2')
4112
4113 A.addPatchset()
4114 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4115 self.waitUntilSettled()
4116 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004117
James E. Blaira8b90b32016-08-24 15:18:50 -07004118 def test_crd_check_unknown(self):
4119 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004120 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004121 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4122 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4123 # A Depends-On: B
4124 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4125 A.subject, B.data['id'])
4126
4127 # Make sure zuul has seen an event on B.
4128 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4129 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4130 self.waitUntilSettled()
4131
4132 self.assertEqual(A.data['status'], 'NEW')
4133 self.assertEqual(A.reported, 1)
4134 self.assertEqual(B.data['status'], 'NEW')
4135 self.assertEqual(B.reported, 0)
4136
James E. Blair92464a22016-04-05 10:21:26 -07004137 def test_crd_cycle_join(self):
4138 "Test an updated change creates a cycle"
4139 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4140
4141 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4142 self.waitUntilSettled()
4143
4144 # Create B->A
4145 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4146 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4147 B.subject, A.data['id'])
4148 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4149 self.waitUntilSettled()
4150
4151 # Update A to add A->B (a cycle).
4152 A.addPatchset()
4153 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4154 A.subject, B.data['id'])
4155 # Normally we would submit the patchset-created event for
4156 # processing here, however, we have no way of noting whether
4157 # the dependency cycle detection correctly raised an
4158 # exception, so instead, we reach into the source driver and
4159 # call the method that would ultimately be called by the event
4160 # processing.
4161
Paul Belanger0e155e22016-11-14 14:12:23 -05004162 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07004163 (trusted, project) = tenant.getProject('org/project')
4164 source = project.source
Paul Belanger0e155e22016-11-14 14:12:23 -05004165
4166 # TODO(pabelanger): As we add more source / trigger APIs we should make
4167 # it easier for users to create events for testing.
4168 event = zuul.model.TriggerEvent()
4169 event.trigger_name = 'gerrit'
4170 event.change_number = '1'
4171 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004172 with testtools.ExpectedException(
4173 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004174 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004175 self.log.debug("Got expected dependency cycle exception")
4176
4177 # Now if we update B to remove the depends-on, everything
4178 # should be okay. B; A->B
4179
4180 B.addPatchset()
4181 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004182
4183 source.getChange(event, True)
4184 event.change_number = '2'
4185 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004186
Jesse Keatingcc67b122017-04-19 14:54:57 -07004187 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004188 def test_disable_at(self):
4189 "Test a pipeline will only report to the disabled trigger when failing"
4190
Jesse Keatingcc67b122017-04-19 14:54:57 -07004191 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004192 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004193 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004194 0, tenant.layout.pipelines['check']._consecutive_failures)
4195 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004196
4197 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4198 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4199 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4200 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4201 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4202 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4203 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4204 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4205 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4206 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4207 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4208
Paul Belanger174a8272017-03-14 13:20:10 -04004209 self.executor_server.failJob('project-test1', A)
4210 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004211 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004212 self.executor_server.failJob('project-test1', D)
4213 self.executor_server.failJob('project-test1', E)
4214 self.executor_server.failJob('project-test1', F)
4215 self.executor_server.failJob('project-test1', G)
4216 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004217 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004218 self.executor_server.failJob('project-test1', J)
4219 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004220
4221 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4222 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4223 self.waitUntilSettled()
4224
4225 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004226 2, tenant.layout.pipelines['check']._consecutive_failures)
4227 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004228
4229 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4230 self.waitUntilSettled()
4231
4232 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004233 0, tenant.layout.pipelines['check']._consecutive_failures)
4234 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004235
4236 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4237 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4238 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4239 self.waitUntilSettled()
4240
4241 # We should be disabled now
4242 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004243 3, tenant.layout.pipelines['check']._consecutive_failures)
4244 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004245
4246 # We need to wait between each of these patches to make sure the
4247 # smtp messages come back in an expected order
4248 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4249 self.waitUntilSettled()
4250 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4251 self.waitUntilSettled()
4252 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4253 self.waitUntilSettled()
4254
4255 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4256 # leaving a message on each change
4257 self.assertEqual(1, len(A.messages))
4258 self.assertIn('Build failed.', A.messages[0])
4259 self.assertEqual(1, len(B.messages))
4260 self.assertIn('Build failed.', B.messages[0])
4261 self.assertEqual(1, len(C.messages))
4262 self.assertIn('Build succeeded.', C.messages[0])
4263 self.assertEqual(1, len(D.messages))
4264 self.assertIn('Build failed.', D.messages[0])
4265 self.assertEqual(1, len(E.messages))
4266 self.assertIn('Build failed.', E.messages[0])
4267 self.assertEqual(1, len(F.messages))
4268 self.assertIn('Build failed.', F.messages[0])
4269
4270 # The last 3 (GHI) would have only reported via smtp.
4271 self.assertEqual(3, len(self.smtp_messages))
4272 self.assertEqual(0, len(G.messages))
4273 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004274 self.assertIn(
4275 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004276 self.assertEqual(0, len(H.messages))
4277 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004278 self.assertIn(
4279 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004280 self.assertEqual(0, len(I.messages))
4281 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004282 self.assertIn(
4283 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004284
4285 # Now reload the configuration (simulate a HUP) to check the pipeline
4286 # comes out of disabled
4287 self.sched.reconfigure(self.config)
4288
Jesse Keatingcc67b122017-04-19 14:54:57 -07004289 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004290
4291 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004292 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004293 0, tenant.layout.pipelines['check']._consecutive_failures)
4294 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004295
4296 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4297 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4298 self.waitUntilSettled()
4299
4300 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004301 2, tenant.layout.pipelines['check']._consecutive_failures)
4302 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004303
4304 # J and K went back to gerrit
4305 self.assertEqual(1, len(J.messages))
4306 self.assertIn('Build failed.', J.messages[0])
4307 self.assertEqual(1, len(K.messages))
4308 self.assertIn('Build failed.', K.messages[0])
4309 # No more messages reported via smtp
4310 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004311
James E. Blaircc020532017-04-19 13:04:44 -07004312 @simple_layout('layouts/one-job-project.yaml')
4313 def test_one_job_project(self):
4314 "Test that queueing works with one job"
4315 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4316 'master', 'A')
4317 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4318 'master', 'B')
4319 A.addApproval('code-review', 2)
4320 B.addApproval('code-review', 2)
4321 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4322 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4323 self.waitUntilSettled()
4324
4325 self.assertEqual(A.data['status'], 'MERGED')
4326 self.assertEqual(A.reported, 2)
4327 self.assertEqual(B.data['status'], 'MERGED')
4328 self.assertEqual(B.reported, 2)
4329
Paul Belanger71d98172016-11-08 10:56:31 -05004330 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004331 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004332
Paul Belanger174a8272017-03-14 13:20:10 -04004333 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004334 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4335 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4336 self.waitUntilSettled()
4337
Paul Belanger174a8272017-03-14 13:20:10 -04004338 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004339 self.waitUntilSettled()
4340
4341 self.assertEqual(len(self.builds), 2)
4342 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004343 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004344 self.waitUntilSettled()
4345
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004346 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004347 self.assertEqual(len(self.builds), 1,
4348 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004349 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004350 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004351 self.waitUntilSettled()
4352
Paul Belanger174a8272017-03-14 13:20:10 -04004353 self.executor_server.hold_jobs_in_build = False
4354 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004355 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004356 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004357 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4358 self.assertEqual(A.reported, 1)
4359 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004360
James E. Blair15be0e12017-01-03 13:45:20 -08004361 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004362 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004363
4364 self.fake_nodepool.paused = True
4365 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4366 A.addApproval('code-review', 2)
4367 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4368 self.waitUntilSettled()
4369
4370 self.zk.client.stop()
4371 self.zk.client.start()
4372 self.fake_nodepool.paused = False
4373 self.waitUntilSettled()
4374
4375 self.assertEqual(A.data['status'], 'MERGED')
4376 self.assertEqual(A.reported, 2)
4377
James E. Blair6ab79e02017-01-06 10:10:17 -08004378 def test_nodepool_failure(self):
4379 "Test that jobs are reported after a nodepool failure"
4380
4381 self.fake_nodepool.paused = True
4382 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4383 A.addApproval('code-review', 2)
4384 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4385 self.waitUntilSettled()
4386
4387 req = self.fake_nodepool.getNodeRequests()[0]
4388 self.fake_nodepool.addFailRequest(req)
4389
4390 self.fake_nodepool.paused = False
4391 self.waitUntilSettled()
4392
4393 self.assertEqual(A.data['status'], 'NEW')
4394 self.assertEqual(A.reported, 2)
4395 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4396 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4397 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4398
Arieb6f068c2016-10-09 13:11:06 +03004399
James E. Blaira002b032017-04-18 10:35:48 -07004400class TestExecutor(ZuulTestCase):
4401 tenant_config_file = 'config/single-tenant/main.yaml'
4402
4403 def assertFinalState(self):
4404 # In this test, we expect to shut down in a non-final state,
4405 # so skip these checks.
4406 pass
4407
4408 def assertCleanShutdown(self):
4409 self.log.debug("Assert clean shutdown")
4410
4411 # After shutdown, make sure no jobs are running
4412 self.assertEqual({}, self.executor_server.job_workers)
4413
4414 # Make sure that git.Repo objects have been garbage collected.
4415 repos = []
4416 gc.collect()
4417 for obj in gc.get_objects():
4418 if isinstance(obj, git.Repo):
4419 self.log.debug("Leaked git repo object: %s" % repr(obj))
4420 repos.append(obj)
4421 self.assertEqual(len(repos), 0)
4422
4423 def test_executor_shutdown(self):
4424 "Test that the executor can shut down with jobs running"
4425
4426 self.executor_server.hold_jobs_in_build = True
4427 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4428 A.addApproval('code-review', 2)
4429 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4430 self.waitUntilSettled()
4431
4432
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004433class TestDependencyGraph(ZuulTestCase):
4434 tenant_config_file = 'config/dependency-graph/main.yaml'
4435
4436 def test_dependeny_graph_dispatch_jobs_once(self):
4437 "Test a job in a dependency graph is queued only once"
4438 # Job dependencies, starting with A
4439 # A
4440 # / \
4441 # B C
4442 # / \ / \
4443 # D F E
4444 # |
4445 # G
4446
4447 self.executor_server.hold_jobs_in_build = True
4448 change = self.fake_gerrit.addFakeChange(
4449 'org/project', 'master', 'change')
4450 change.addApproval('code-review', 2)
4451 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4452
4453 self.waitUntilSettled()
4454 self.assertEqual([b.name for b in self.builds], ['A'])
4455
4456 self.executor_server.release('A')
4457 self.waitUntilSettled()
4458 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4459
4460 self.executor_server.release('B')
4461 self.waitUntilSettled()
4462 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4463
4464 self.executor_server.release('D')
4465 self.waitUntilSettled()
4466 self.assertEqual([b.name for b in self.builds], ['C'])
4467
4468 self.executor_server.release('C')
4469 self.waitUntilSettled()
4470 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4471
4472 self.executor_server.release('F')
4473 self.waitUntilSettled()
4474 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4475
4476 self.executor_server.release('G')
4477 self.waitUntilSettled()
4478 self.assertEqual([b.name for b in self.builds], ['E'])
4479
4480 self.executor_server.release('E')
4481 self.waitUntilSettled()
4482 self.assertEqual(len(self.builds), 0)
4483
4484 self.executor_server.hold_jobs_in_build = False
4485 self.executor_server.release()
4486 self.waitUntilSettled()
4487
4488 self.assertEqual(len(self.builds), 0)
4489 self.assertEqual(len(self.history), 7)
4490
4491 self.assertEqual(change.data['status'], 'MERGED')
4492 self.assertEqual(change.reported, 2)
4493
4494 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4495 "Test that a job waits till all dependencies are successful"
4496 # Job dependencies, starting with A
4497 # A
4498 # / \
4499 # B C*
4500 # / \ / \
4501 # D F E
4502 # |
4503 # G
4504
4505 self.executor_server.hold_jobs_in_build = True
4506 change = self.fake_gerrit.addFakeChange(
4507 'org/project', 'master', 'change')
4508 change.addApproval('code-review', 2)
4509
4510 self.executor_server.failJob('C', change)
4511
4512 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4513
4514 self.waitUntilSettled()
4515 self.assertEqual([b.name for b in self.builds], ['A'])
4516
4517 self.executor_server.release('A')
4518 self.waitUntilSettled()
4519 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4520
4521 self.executor_server.release('B')
4522 self.waitUntilSettled()
4523 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4524
4525 self.executor_server.release('D')
4526 self.waitUntilSettled()
4527 self.assertEqual([b.name for b in self.builds], ['C'])
4528
4529 self.executor_server.release('C')
4530 self.waitUntilSettled()
4531 self.assertEqual(len(self.builds), 0)
4532
4533 self.executor_server.hold_jobs_in_build = False
4534 self.executor_server.release()
4535 self.waitUntilSettled()
4536
4537 self.assertEqual(len(self.builds), 0)
4538 self.assertEqual(len(self.history), 4)
4539
4540 self.assertEqual(change.data['status'], 'NEW')
4541 self.assertEqual(change.reported, 2)
4542
4543
Arieb6f068c2016-10-09 13:11:06 +03004544class TestDuplicatePipeline(ZuulTestCase):
4545 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4546
4547 def test_duplicate_pipelines(self):
4548 "Test that a change matching multiple pipelines works"
4549
4550 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4551 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4552 self.waitUntilSettled()
4553
4554 self.assertHistory([
4555 dict(name='project-test1', result='SUCCESS', changes='1,1',
4556 pipeline='dup1'),
4557 dict(name='project-test1', result='SUCCESS', changes='1,1',
4558 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004559 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004560
4561 self.assertEqual(len(A.messages), 2)
4562
Paul Belangera46a3742016-11-09 14:23:03 -05004563 if 'dup1' in A.messages[0]:
4564 self.assertIn('dup1', A.messages[0])
4565 self.assertNotIn('dup2', A.messages[0])
4566 self.assertIn('project-test1', A.messages[0])
4567 self.assertIn('dup2', A.messages[1])
4568 self.assertNotIn('dup1', A.messages[1])
4569 self.assertIn('project-test1', A.messages[1])
4570 else:
4571 self.assertIn('dup1', A.messages[1])
4572 self.assertNotIn('dup2', A.messages[1])
4573 self.assertIn('project-test1', A.messages[1])
4574 self.assertIn('dup2', A.messages[0])
4575 self.assertNotIn('dup1', A.messages[0])
4576 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004577
4578
Paul Belanger86085b32016-11-03 12:48:57 -04004579class TestSchedulerTemplatedProject(ZuulTestCase):
4580 tenant_config_file = 'config/templated-project/main.yaml'
4581
Paul Belanger174a8272017-03-14 13:20:10 -04004582 def test_job_from_templates_executed(self):
4583 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004584
4585 A = self.fake_gerrit.addFakeChange(
4586 'org/templated-project', 'master', 'A')
4587 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4588 self.waitUntilSettled()
4589
4590 self.assertEqual(self.getJobFromHistory('project-test1').result,
4591 'SUCCESS')
4592 self.assertEqual(self.getJobFromHistory('project-test2').result,
4593 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004594
4595 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004596 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004597
4598 A = self.fake_gerrit.addFakeChange(
4599 'org/layered-project', 'master', 'A')
4600 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4601 self.waitUntilSettled()
4602
4603 self.assertEqual(self.getJobFromHistory('project-test1').result,
4604 'SUCCESS')
4605 self.assertEqual(self.getJobFromHistory('project-test2').result,
4606 'SUCCESS')
4607 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4608 ).result, 'SUCCESS')
4609 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4610 ).result, 'SUCCESS')
4611 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4612 ).result, 'SUCCESS')
4613 self.assertEqual(self.getJobFromHistory('project-test6').result,
4614 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004615
4616
4617class TestSchedulerSuccessURL(ZuulTestCase):
4618 tenant_config_file = 'config/success-url/main.yaml'
4619
4620 def test_success_url(self):
4621 "Ensure bad build params are ignored"
4622 self.sched.reconfigure(self.config)
4623 self.init_repo('org/docs')
4624
4625 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4626 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4627 self.waitUntilSettled()
4628
4629 # Both builds ran: docs-draft-test + docs-draft-test2
4630 self.assertEqual(len(self.history), 2)
4631
4632 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004633 for build in self.history:
4634 if build.name == 'docs-draft-test':
4635 uuid = build.uuid[:7]
4636 break
Adam Gandelman94a60062016-11-21 16:43:14 -08004637
4638 # Two msgs: 'Starting...' + results
4639 self.assertEqual(len(self.smtp_messages), 2)
4640 body = self.smtp_messages[1]['body'].splitlines()
4641 self.assertEqual('Build succeeded.', body[0])
4642
4643 self.assertIn(
4644 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4645 'docs-draft-test/%s/publish-docs/' % uuid,
4646 body[2])
4647
Paul Belanger174a8272017-03-14 13:20:10 -04004648 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004649 self.assertIn(
4650 '- docs-draft-test2 https://server/job',
4651 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004652
4653
Adam Gandelman4da00f62016-12-09 15:47:33 -08004654class TestSchedulerMerges(ZuulTestCase):
4655 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004656
4657 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004658 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004659 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004660 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004661 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4662 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4663 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4664 A.addApproval('code-review', 2)
4665 B.addApproval('code-review', 2)
4666 C.addApproval('code-review', 2)
4667 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4668 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4669 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4670 self.waitUntilSettled()
4671
4672 build = self.builds[-1]
4673 ref = self.getParameter(build, 'ZUUL_REF')
4674
James E. Blair2a535672017-04-27 12:03:15 -07004675 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4676 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004677 repo = git.Repo(path)
4678 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4679 repo_messages.reverse()
4680
Paul Belanger174a8272017-03-14 13:20:10 -04004681 self.executor_server.hold_jobs_in_build = False
4682 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004683 self.waitUntilSettled()
4684
4685 return repo_messages
4686
4687 def _test_merge(self, mode):
4688 us_path = os.path.join(
4689 self.upstream_root, 'org/project-%s' % mode)
4690 expected_messages = [
4691 'initial commit',
4692 'add content from fixture',
4693 # the intermediate commits order is nondeterministic
4694 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4695 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4696 ]
4697 result = self._test_project_merge_mode(mode)
4698 self.assertEqual(result[:2], expected_messages[:2])
4699 self.assertEqual(result[-2:], expected_messages[-2:])
4700
4701 def test_project_merge_mode_merge(self):
4702 self._test_merge('merge')
4703
4704 def test_project_merge_mode_merge_resolve(self):
4705 self._test_merge('merge-resolve')
4706
4707 def test_project_merge_mode_cherrypick(self):
4708 expected_messages = [
4709 'initial commit',
4710 'add content from fixture',
4711 'A-1',
4712 'B-1',
4713 'C-1']
4714 result = self._test_project_merge_mode('cherry-pick')
4715 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004716
4717 def test_merge_branch(self):
4718 "Test that the right commits are on alternate branches"
4719 self.create_branch('org/project-merge-branches', 'mp')
4720
Paul Belanger174a8272017-03-14 13:20:10 -04004721 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004722 A = self.fake_gerrit.addFakeChange(
4723 'org/project-merge-branches', 'mp', 'A')
4724 B = self.fake_gerrit.addFakeChange(
4725 'org/project-merge-branches', 'mp', 'B')
4726 C = self.fake_gerrit.addFakeChange(
4727 'org/project-merge-branches', 'mp', 'C')
4728 A.addApproval('code-review', 2)
4729 B.addApproval('code-review', 2)
4730 C.addApproval('code-review', 2)
4731 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4732 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4733 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4734 self.waitUntilSettled()
4735
Paul Belanger174a8272017-03-14 13:20:10 -04004736 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004737 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004738 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004739 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004740 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004741 self.waitUntilSettled()
4742
4743 build = self.builds[-1]
4744 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
4745 ref = self.getParameter(build, 'ZUUL_REF')
James E. Blair2a535672017-04-27 12:03:15 -07004746 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4747 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004748 repo = git.Repo(path)
4749
4750 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4751 repo_messages.reverse()
4752 correct_messages = [
4753 'initial commit',
4754 'add content from fixture',
4755 'mp commit',
4756 'A-1', 'B-1', 'C-1']
4757 self.assertEqual(repo_messages, correct_messages)
4758
Paul Belanger174a8272017-03-14 13:20:10 -04004759 self.executor_server.hold_jobs_in_build = False
4760 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004761 self.waitUntilSettled()
4762
4763 def test_merge_multi_branch(self):
4764 "Test that dependent changes on multiple branches are merged"
4765 self.create_branch('org/project-merge-branches', 'mp')
4766
Paul Belanger174a8272017-03-14 13:20:10 -04004767 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004768 A = self.fake_gerrit.addFakeChange(
4769 'org/project-merge-branches', 'master', 'A')
4770 B = self.fake_gerrit.addFakeChange(
4771 'org/project-merge-branches', 'mp', 'B')
4772 C = self.fake_gerrit.addFakeChange(
4773 'org/project-merge-branches', 'master', 'C')
4774 A.addApproval('code-review', 2)
4775 B.addApproval('code-review', 2)
4776 C.addApproval('code-review', 2)
4777 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4778 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4779 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4780 self.waitUntilSettled()
4781
4782 job_A = None
4783 for job in self.builds:
4784 if 'project-merge' in job.name:
4785 job_A = job
4786 ref_A = self.getParameter(job_A, 'ZUUL_REF')
4787 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
4788 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
4789 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
4790
James E. Blair2a535672017-04-27 12:03:15 -07004791 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
4792 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004793 repo = git.Repo(path)
4794 repo_messages = [c.message.strip()
4795 for c in repo.iter_commits(ref_A)]
4796 repo_messages.reverse()
4797 correct_messages = [
4798 'initial commit', 'add content from fixture', 'A-1']
4799 self.assertEqual(repo_messages, correct_messages)
4800
Paul Belanger174a8272017-03-14 13:20:10 -04004801 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004802 self.waitUntilSettled()
4803
4804 job_B = None
4805 for job in self.builds:
4806 if 'project-merge' in job.name:
4807 job_B = job
4808 ref_B = self.getParameter(job_B, 'ZUUL_REF')
4809 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
4810 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
4811 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
4812
James E. Blair2a535672017-04-27 12:03:15 -07004813 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
4814 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004815 repo = git.Repo(path)
4816 repo_messages = [c.message.strip()
4817 for c in repo.iter_commits(ref_B)]
4818 repo_messages.reverse()
4819 correct_messages = [
4820 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4821 self.assertEqual(repo_messages, correct_messages)
4822
Paul Belanger174a8272017-03-14 13:20:10 -04004823 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004824 self.waitUntilSettled()
4825
4826 job_C = None
4827 for job in self.builds:
4828 if 'project-merge' in job.name:
4829 job_C = job
4830 ref_C = self.getParameter(job_C, 'ZUUL_REF')
4831 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
4832 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
4833 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair2a535672017-04-27 12:03:15 -07004834 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
4835 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004836 repo = git.Repo(path)
4837 repo_messages = [c.message.strip()
4838 for c in repo.iter_commits(ref_C)]
4839
4840 repo_messages.reverse()
4841 correct_messages = [
4842 'initial commit', 'add content from fixture',
4843 'A-1', 'C-1']
4844 # Ensure the right commits are in the history for this ref
4845 self.assertEqual(repo_messages, correct_messages)
4846
Paul Belanger174a8272017-03-14 13:20:10 -04004847 self.executor_server.hold_jobs_in_build = False
4848 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004849 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01004850
4851
James E. Blair9ea0d0b2017-04-20 09:27:15 -07004852class TestSemaphore(ZuulTestCase):
4853 tenant_config_file = 'config/semaphore/main.yaml'
4854
4855 def test_semaphore_one(self):
4856 "Test semaphores with max=1 (mutex)"
4857 tenant = self.sched.abide.tenants.get('tenant-one')
4858
4859 self.executor_server.hold_jobs_in_build = True
4860
4861 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4862 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4863 self.assertFalse('test-semaphore' in
4864 tenant.semaphore_handler.semaphores)
4865
4866 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4867 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4868 self.waitUntilSettled()
4869
4870 self.assertEqual(len(self.builds), 3)
4871 self.assertEqual(self.builds[0].name, 'project-test1')
4872 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
4873 self.assertEqual(self.builds[2].name, 'project-test1')
4874
4875 self.executor_server.release('semaphore-one-test1')
4876 self.waitUntilSettled()
4877
4878 self.assertEqual(len(self.builds), 3)
4879 self.assertEqual(self.builds[0].name, 'project-test1')
4880 self.assertEqual(self.builds[1].name, 'project-test1')
4881 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4882 self.assertTrue('test-semaphore' in
4883 tenant.semaphore_handler.semaphores)
4884
4885 self.executor_server.release('semaphore-one-test2')
4886 self.waitUntilSettled()
4887
4888 self.assertEqual(len(self.builds), 3)
4889 self.assertEqual(self.builds[0].name, 'project-test1')
4890 self.assertEqual(self.builds[1].name, 'project-test1')
4891 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
4892 self.assertTrue('test-semaphore' in
4893 tenant.semaphore_handler.semaphores)
4894
4895 self.executor_server.release('semaphore-one-test1')
4896 self.waitUntilSettled()
4897
4898 self.assertEqual(len(self.builds), 3)
4899 self.assertEqual(self.builds[0].name, 'project-test1')
4900 self.assertEqual(self.builds[1].name, 'project-test1')
4901 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4902 self.assertTrue('test-semaphore' in
4903 tenant.semaphore_handler.semaphores)
4904
4905 self.executor_server.release('semaphore-one-test2')
4906 self.waitUntilSettled()
4907
4908 self.assertEqual(len(self.builds), 2)
4909 self.assertEqual(self.builds[0].name, 'project-test1')
4910 self.assertEqual(self.builds[1].name, 'project-test1')
4911 self.assertFalse('test-semaphore' in
4912 tenant.semaphore_handler.semaphores)
4913
4914 self.executor_server.hold_jobs_in_build = False
4915 self.executor_server.release()
4916
4917 self.waitUntilSettled()
4918 self.assertEqual(len(self.builds), 0)
4919
4920 self.assertEqual(A.reported, 1)
4921 self.assertEqual(B.reported, 1)
4922 self.assertFalse('test-semaphore' in
4923 tenant.semaphore_handler.semaphores)
4924
4925 def test_semaphore_two(self):
4926 "Test semaphores with max>1"
4927 tenant = self.sched.abide.tenants.get('tenant-one')
4928
4929 self.executor_server.hold_jobs_in_build = True
4930 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4931 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4932 self.assertFalse('test-semaphore-two' in
4933 tenant.semaphore_handler.semaphores)
4934
4935 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4936 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4937 self.waitUntilSettled()
4938
4939 self.assertEqual(len(self.builds), 4)
4940 self.assertEqual(self.builds[0].name, 'project-test1')
4941 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
4942 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
4943 self.assertEqual(self.builds[3].name, 'project-test1')
4944 self.assertTrue('test-semaphore-two' in
4945 tenant.semaphore_handler.semaphores)
4946 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4947 'test-semaphore-two', [])), 2)
4948
4949 self.executor_server.release('semaphore-two-test1')
4950 self.waitUntilSettled()
4951
4952 self.assertEqual(len(self.builds), 4)
4953 self.assertEqual(self.builds[0].name, 'project-test1')
4954 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
4955 self.assertEqual(self.builds[2].name, 'project-test1')
4956 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
4957 self.assertTrue('test-semaphore-two' in
4958 tenant.semaphore_handler.semaphores)
4959 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4960 'test-semaphore-two', [])), 2)
4961
4962 self.executor_server.release('semaphore-two-test2')
4963 self.waitUntilSettled()
4964
4965 self.assertEqual(len(self.builds), 4)
4966 self.assertEqual(self.builds[0].name, 'project-test1')
4967 self.assertEqual(self.builds[1].name, 'project-test1')
4968 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
4969 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
4970 self.assertTrue('test-semaphore-two' in
4971 tenant.semaphore_handler.semaphores)
4972 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4973 'test-semaphore-two', [])), 2)
4974
4975 self.executor_server.release('semaphore-two-test1')
4976 self.waitUntilSettled()
4977
4978 self.assertEqual(len(self.builds), 3)
4979 self.assertEqual(self.builds[0].name, 'project-test1')
4980 self.assertEqual(self.builds[1].name, 'project-test1')
4981 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
4982 self.assertTrue('test-semaphore-two' in
4983 tenant.semaphore_handler.semaphores)
4984 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4985 'test-semaphore-two', [])), 1)
4986
4987 self.executor_server.release('semaphore-two-test2')
4988 self.waitUntilSettled()
4989
4990 self.assertEqual(len(self.builds), 2)
4991 self.assertEqual(self.builds[0].name, 'project-test1')
4992 self.assertEqual(self.builds[1].name, 'project-test1')
4993 self.assertFalse('test-semaphore-two' in
4994 tenant.semaphore_handler.semaphores)
4995
4996 self.executor_server.hold_jobs_in_build = False
4997 self.executor_server.release()
4998
4999 self.waitUntilSettled()
5000 self.assertEqual(len(self.builds), 0)
5001
5002 self.assertEqual(A.reported, 1)
5003 self.assertEqual(B.reported, 1)
5004
5005 def test_semaphore_abandon(self):
5006 "Test abandon with job semaphores"
5007 self.executor_server.hold_jobs_in_build = True
5008 tenant = self.sched.abide.tenants.get('tenant-one')
5009 check_pipeline = tenant.layout.pipelines['check']
5010
5011 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5012 self.assertFalse('test-semaphore' in
5013 tenant.semaphore_handler.semaphores)
5014
5015 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5016 self.waitUntilSettled()
5017
5018 self.assertTrue('test-semaphore' in
5019 tenant.semaphore_handler.semaphores)
5020
5021 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5022 self.waitUntilSettled()
5023
5024 # The check pipeline should be empty
5025 items = check_pipeline.getAllItems()
5026 self.assertEqual(len(items), 0)
5027
5028 # The semaphore should be released
5029 self.assertFalse('test-semaphore' in
5030 tenant.semaphore_handler.semaphores)
5031
5032 self.executor_server.hold_jobs_in_build = False
5033 self.executor_server.release()
5034 self.waitUntilSettled()
5035
5036 def test_semaphore_reconfigure(self):
5037 "Test reconfigure with job semaphores"
5038 self.executor_server.hold_jobs_in_build = True
5039 tenant = self.sched.abide.tenants.get('tenant-one')
5040 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5041 self.assertFalse('test-semaphore' in
5042 tenant.semaphore_handler.semaphores)
5043
5044 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5045 self.waitUntilSettled()
5046
5047 self.assertTrue('test-semaphore' in
5048 tenant.semaphore_handler.semaphores)
5049
5050 # reconfigure without layout change
5051 self.sched.reconfigure(self.config)
5052 self.waitUntilSettled()
5053 tenant = self.sched.abide.tenants.get('tenant-one')
5054
5055 # semaphore still must be held
5056 self.assertTrue('test-semaphore' in
5057 tenant.semaphore_handler.semaphores)
5058
5059 self.commitConfigUpdate(
5060 'common-config',
5061 'config/semaphore/zuul-reconfiguration.yaml')
5062 self.sched.reconfigure(self.config)
5063 self.waitUntilSettled()
5064 tenant = self.sched.abide.tenants.get('tenant-one')
5065
5066 self.executor_server.release('project-test1')
5067 self.waitUntilSettled()
5068
5069 # There should be no builds anymore
5070 self.assertEqual(len(self.builds), 0)
5071
5072 # The semaphore should be released
5073 self.assertFalse('test-semaphore' in
5074 tenant.semaphore_handler.semaphores)
5075
5076
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005077class TestSemaphoreMultiTenant(ZuulTestCase):
5078 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5079
5080 def test_semaphore_tenant_isolation(self):
5081 "Test semaphores in multiple tenants"
5082
5083 self.waitUntilSettled()
5084 tenant_one = self.sched.abide.tenants.get('tenant-one')
5085 tenant_two = self.sched.abide.tenants.get('tenant-two')
5086
5087 self.executor_server.hold_jobs_in_build = True
5088 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5089 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5090 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5091 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5092 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5093 self.assertFalse('test-semaphore' in
5094 tenant_one.semaphore_handler.semaphores)
5095 self.assertFalse('test-semaphore' in
5096 tenant_two.semaphore_handler.semaphores)
5097
5098 # add patches to project1 of tenant-one
5099 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5100 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5101 self.waitUntilSettled()
5102
5103 # one build of project1-test1 must run
5104 # semaphore of tenant-one must be acquired once
5105 # semaphore of tenant-two must not be acquired
5106 self.assertEqual(len(self.builds), 1)
5107 self.assertEqual(self.builds[0].name, 'project1-test1')
5108 self.assertTrue('test-semaphore' in
5109 tenant_one.semaphore_handler.semaphores)
5110 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5111 'test-semaphore', [])), 1)
5112 self.assertFalse('test-semaphore' in
5113 tenant_two.semaphore_handler.semaphores)
5114
5115 # add patches to project2 of tenant-two
5116 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5117 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5118 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5119 self.waitUntilSettled()
5120
5121 # one build of project1-test1 must run
5122 # two builds of project2-test1 must run
5123 # semaphore of tenant-one must be acquired once
5124 # semaphore of tenant-two must be acquired twice
5125 self.assertEqual(len(self.builds), 3)
5126 self.assertEqual(self.builds[0].name, 'project1-test1')
5127 self.assertEqual(self.builds[1].name, 'project2-test1')
5128 self.assertEqual(self.builds[2].name, 'project2-test1')
5129 self.assertTrue('test-semaphore' in
5130 tenant_one.semaphore_handler.semaphores)
5131 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5132 'test-semaphore', [])), 1)
5133 self.assertTrue('test-semaphore' in
5134 tenant_two.semaphore_handler.semaphores)
5135 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5136 'test-semaphore', [])), 2)
5137
5138 self.executor_server.release('project1-test1')
5139 self.waitUntilSettled()
5140
5141 # one build of project1-test1 must run
5142 # two builds of project2-test1 must run
5143 # semaphore of tenant-one must be acquired once
5144 # semaphore of tenant-two must be acquired twice
5145 self.assertEqual(len(self.builds), 3)
5146 self.assertEqual(self.builds[0].name, 'project2-test1')
5147 self.assertEqual(self.builds[1].name, 'project2-test1')
5148 self.assertEqual(self.builds[2].name, 'project1-test1')
5149 self.assertTrue('test-semaphore' in
5150 tenant_one.semaphore_handler.semaphores)
5151 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5152 'test-semaphore', [])), 1)
5153 self.assertTrue('test-semaphore' in
5154 tenant_two.semaphore_handler.semaphores)
5155 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5156 'test-semaphore', [])), 2)
5157
5158 self.executor_server.release('project2-test1')
5159 self.waitUntilSettled()
5160
5161 # one build of project1-test1 must run
5162 # one build of project2-test1 must run
5163 # semaphore of tenant-one must be acquired once
5164 # semaphore of tenant-two must be acquired once
5165 self.assertEqual(len(self.builds), 2)
5166 self.assertTrue('test-semaphore' in
5167 tenant_one.semaphore_handler.semaphores)
5168 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5169 'test-semaphore', [])), 1)
5170 self.assertTrue('test-semaphore' in
5171 tenant_two.semaphore_handler.semaphores)
5172 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5173 'test-semaphore', [])), 1)
5174
5175 self.executor_server.hold_jobs_in_build = False
5176 self.executor_server.release()
5177
5178 self.waitUntilSettled()
5179
5180 # no build must run
5181 # semaphore of tenant-one must not be acquired
5182 # semaphore of tenant-two must not be acquired
5183 self.assertEqual(len(self.builds), 0)
5184 self.assertFalse('test-semaphore' in
5185 tenant_one.semaphore_handler.semaphores)
5186 self.assertFalse('test-semaphore' in
5187 tenant_two.semaphore_handler.semaphores)
5188
5189 self.assertEqual(A.reported, 1)
5190 self.assertEqual(B.reported, 1)
5191
5192
5193class TestSemaphoreInRepo(ZuulTestCase):
5194 tenant_config_file = 'config/in-repo/main.yaml'
5195
5196 def test_semaphore_in_repo(self):
5197 "Test semaphores in repo config"
5198
5199 # This tests dynamic semaphore handling in project repos. The semaphore
5200 # max value should not be evaluated dynamically but must be updated
5201 # after the change lands.
5202
5203 self.waitUntilSettled()
5204 tenant = self.sched.abide.tenants.get('tenant-one')
5205
5206 in_repo_conf = textwrap.dedent(
5207 """
5208 - job:
5209 name: project-test2
5210 semaphore: test-semaphore
5211
5212 - project:
5213 name: org/project
5214 tenant-one-gate:
5215 jobs:
5216 - project-test2
5217
5218 # the max value in dynamic layout must be ignored
5219 - semaphore:
5220 name: test-semaphore
5221 max: 2
5222 """)
5223
5224 in_repo_playbook = textwrap.dedent(
5225 """
5226 - hosts: all
5227 tasks: []
5228 """)
5229
5230 file_dict = {'.zuul.yaml': in_repo_conf,
5231 'playbooks/project-test2.yaml': in_repo_playbook}
5232 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5233 files=file_dict)
5234 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5235 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5236 B.setDependsOn(A, 1)
5237 C.setDependsOn(A, 1)
5238
5239 self.executor_server.hold_jobs_in_build = True
5240
5241 A.addApproval('code-review', 2)
5242 B.addApproval('code-review', 2)
5243 C.addApproval('code-review', 2)
5244 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
5245 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
5246 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
5247 self.waitUntilSettled()
5248
5249 # check that the layout in a queue item still has max value of 1
5250 # for test-semaphore
5251 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5252 queue = None
5253 for queue_candidate in pipeline.queues:
5254 if queue_candidate.name == 'org/project':
5255 queue = queue_candidate
5256 break
5257 queue_item = queue.queue[0]
5258 item_dynamic_layout = queue_item.current_build_set.layout
5259 dynamic_test_semaphore = \
5260 item_dynamic_layout.semaphores.get('test-semaphore')
5261 self.assertEqual(dynamic_test_semaphore.max, 1)
5262
5263 # one build must be in queue, one semaphores acquired
5264 self.assertEqual(len(self.builds), 1)
5265 self.assertEqual(self.builds[0].name, 'project-test2')
5266 self.assertTrue('test-semaphore' in
5267 tenant.semaphore_handler.semaphores)
5268 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5269 'test-semaphore', [])), 1)
5270
5271 self.executor_server.release('project-test2')
5272 self.waitUntilSettled()
5273
5274 # change A must be merged
5275 self.assertEqual(A.data['status'], 'MERGED')
5276 self.assertEqual(A.reported, 2)
5277
5278 # send change-merged event as the gerrit mock doesn't send it
5279 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5280 self.waitUntilSettled()
5281
5282 # now that change A was merged, the new semaphore max must be effective
5283 tenant = self.sched.abide.tenants.get('tenant-one')
5284 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5285
5286 # two builds must be in queue, two semaphores acquired
5287 self.assertEqual(len(self.builds), 2)
5288 self.assertEqual(self.builds[0].name, 'project-test2')
5289 self.assertEqual(self.builds[1].name, 'project-test2')
5290 self.assertTrue('test-semaphore' in
5291 tenant.semaphore_handler.semaphores)
5292 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5293 'test-semaphore', [])), 2)
5294
5295 self.executor_server.release('project-test2')
5296 self.waitUntilSettled()
5297
5298 self.assertEqual(len(self.builds), 0)
5299 self.assertFalse('test-semaphore' in
5300 tenant.semaphore_handler.semaphores)
5301
5302 self.executor_server.hold_jobs_in_build = False
5303 self.executor_server.release()
5304
5305 self.waitUntilSettled()
5306 self.assertEqual(len(self.builds), 0)
5307
5308 self.assertEqual(A.reported, 2)
5309 self.assertEqual(B.reported, 2)
5310 self.assertEqual(C.reported, 2)