blob: 0ac42c177f9d4647299f26cd3e2f1815fd0dd737 [file] [log] [blame]
James E. Blairb0fcae42012-07-17 11:12:10 -07001#!/usr/bin/env python
2
3# Copyright 2012 Hewlett-Packard Development Company, L.P.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
James E. Blaira002b032017-04-18 10:35:48 -070017import gc
James E. Blairb0fcae42012-07-17 11:12:10 -070018import json
Tobias Henkel9a0e1942017-03-20 16:16:02 +010019import textwrap
20
Monty Taylorbc758832013-06-17 17:22:42 -040021import os
James E. Blairb0fcae42012-07-17 11:12:10 -070022import re
James E. Blair4886cc12012-07-18 15:39:41 -070023import shutil
Monty Taylorbc758832013-06-17 17:22:42 -040024import time
Clint Byrum03454a52017-05-26 17:14:02 -070025from unittest import skip
Monty Taylorbc758832013-06-17 17:22:42 -040026
James E. Blair4886cc12012-07-18 15:39:41 -070027import git
Morgan Fainberg293f7f82016-05-30 14:01:22 -070028from six.moves import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040029import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070030
Maru Newby3fe5f852015-01-13 04:22:14 +000031import zuul.change_matcher
James E. Blairb0fcae42012-07-17 11:12:10 -070032import zuul.scheduler
James E. Blairad28e912013-11-27 10:43:22 -080033import zuul.rpcclient
Paul Belanger9bba4902016-11-02 16:07:33 -040034import zuul.model
James E. Blairb0fcae42012-07-17 11:12:10 -070035
Maru Newby3fe5f852015-01-13 04:22:14 +000036from tests.base import (
Maru Newby3fe5f852015-01-13 04:22:14 +000037 ZuulTestCase,
38 repack_repo,
James E. Blair06cc3922017-04-19 10:08:10 -070039 simple_layout,
Maru Newby3fe5f852015-01-13 04:22:14 +000040)
James E. Blairb0fcae42012-07-17 11:12:10 -070041
James E. Blairb0fcae42012-07-17 11:12:10 -070042
Clark Boylanb640e052014-04-03 16:41:46 -070043class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070044 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010045
Paul Belanger174a8272017-03-14 13:20:10 -040046 def test_jobs_executed(self):
47 "Test that jobs are executed and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070048
James E. Blairb0fcae42012-07-17 11:12:10 -070049 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070050 A.addApproval('code-review', 2)
51 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070052 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040053 self.assertEqual(self.getJobFromHistory('project-merge').result,
54 'SUCCESS')
55 self.assertEqual(self.getJobFromHistory('project-test1').result,
56 'SUCCESS')
57 self.assertEqual(self.getJobFromHistory('project-test2').result,
58 'SUCCESS')
59 self.assertEqual(A.data['status'], 'MERGED')
60 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070061 self.assertEqual(self.getJobFromHistory('project-test1').node,
62 'image1')
Adam Gandelmaneace4692017-02-07 15:18:36 -080063 self.assertIsNone(self.getJobFromHistory('project-test2').node)
James E. Blairb0fcae42012-07-17 11:12:10 -070064
James E. Blair552b54f2016-07-22 13:55:32 -070065 # TODOv3(jeblair): we may want to report stats by tenant (also?).
Monty Taylorf1385042017-05-27 12:25:05 -050066 # Per-driver
67 self.assertReportedStat('zuul.event.gerrit.comment-added', value='1|c')
68 # Per-driver per-connection
69 self.assertReportedStat('zuul.event.gerrit.gerrit.comment-added',
70 value='1|c')
James E. Blair66eeebf2013-07-27 17:44:32 -070071 self.assertReportedStat('zuul.pipeline.gate.current_changes',
72 value='1|g')
73 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
74 kind='ms')
75 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
76 value='1|c')
77 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
78 self.assertReportedStat('zuul.pipeline.gate.total_changes',
79 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070080 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070081 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -070082 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070083 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070084
James E. Blair5821bd92015-09-16 08:48:15 -070085 for build in self.builds:
86 self.assertEqual(build.parameters['ZUUL_VOTING'], '1')
87
James E. Blair3cb10702013-08-24 08:56:03 -070088 def test_initial_pipeline_gauges(self):
89 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -070090 self.assertReportedStat('zuul.pipeline.gate.current_changes',
91 value='0|g')
92 self.assertReportedStat('zuul.pipeline.check.current_changes',
93 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -070094
James E. Blair34776ee2016-08-25 13:53:54 -070095 def test_job_branch(self):
96 "Test the correct variant of a job runs on a branch"
97 self.create_branch('org/project', 'stable')
98 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
99 A.addApproval('code-review', 2)
100 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
101 self.waitUntilSettled()
102 self.assertEqual(self.getJobFromHistory('project-test1').result,
103 'SUCCESS')
104 self.assertEqual(self.getJobFromHistory('project-test2').result,
105 'SUCCESS')
106 self.assertEqual(A.data['status'], 'MERGED')
107 self.assertEqual(A.reported, 2,
108 "A should report start and success")
109 self.assertIn('gate', A.messages[1],
110 "A should transit gate")
111 self.assertEqual(self.getJobFromHistory('project-test1').node,
112 'image2')
113
James E. Blairb0fcae42012-07-17 11:12:10 -0700114 def test_parallel_changes(self):
115 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700116
Paul Belanger174a8272017-03-14 13:20:10 -0400117 self.executor_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700118 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
119 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
120 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700121 A.addApproval('code-review', 2)
122 B.addApproval('code-review', 2)
123 C.addApproval('code-review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700124
James E. Blair8b5408c2016-08-08 15:37:46 -0700125 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
126 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
127 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700128
129 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400130 self.assertEqual(len(self.builds), 1)
131 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700132 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700133
Paul Belanger174a8272017-03-14 13:20:10 -0400134 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700135 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400136 self.assertEqual(len(self.builds), 3)
137 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700138 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400139 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700140 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400141 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700142 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700143
Paul Belanger174a8272017-03-14 13:20:10 -0400144 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700145 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400146 self.assertEqual(len(self.builds), 5)
147 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700148 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400149 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700150 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700151
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400152 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700153 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400154 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700155 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700156
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400157 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700158 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700159
Paul Belanger174a8272017-03-14 13:20:10 -0400160 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700161 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400162 self.assertEqual(len(self.builds), 6)
163 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700164 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400165 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700166 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700167
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400168 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700169 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400170 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700171 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700172
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400173 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700174 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400175 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700176 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700177
Paul Belanger174a8272017-03-14 13:20:10 -0400178 self.executor_server.hold_jobs_in_build = False
179 self.executor_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700180 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400181 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700182
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400183 self.assertEqual(len(self.history), 9)
184 self.assertEqual(A.data['status'], 'MERGED')
185 self.assertEqual(B.data['status'], 'MERGED')
186 self.assertEqual(C.data['status'], 'MERGED')
187 self.assertEqual(A.reported, 2)
188 self.assertEqual(B.reported, 2)
189 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700190
191 def test_failed_changes(self):
192 "Test that a change behind a failed change is retested"
Paul Belanger174a8272017-03-14 13:20:10 -0400193 self.executor_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700194
James E. Blairb02a3bb2012-07-30 17:49:55 -0700195 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
196 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -0700197 A.addApproval('code-review', 2)
198 B.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700199
Paul Belanger174a8272017-03-14 13:20:10 -0400200 self.executor_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700201
James E. Blair8b5408c2016-08-08 15:37:46 -0700202 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
203 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700204 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700205 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400206
Paul Belanger174a8272017-03-14 13:20:10 -0400207 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400208 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700209 # A/project-merge is complete
210 self.assertBuilds([
211 dict(name='project-test1', changes='1,1'),
212 dict(name='project-test2', changes='1,1'),
213 dict(name='project-merge', changes='1,1 2,1'),
214 ])
James E. Blaire2819012013-06-28 17:17:26 -0400215
Paul Belanger174a8272017-03-14 13:20:10 -0400216 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400217 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700218 # A/project-merge is complete
219 # B/project-merge is complete
220 self.assertBuilds([
221 dict(name='project-test1', changes='1,1'),
222 dict(name='project-test2', changes='1,1'),
223 dict(name='project-test1', changes='1,1 2,1'),
224 dict(name='project-test2', changes='1,1 2,1'),
225 ])
226
227 # Release project-test1 for A which will fail. This will
Paul Belanger174a8272017-03-14 13:20:10 -0400228 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700229 self.builds[0].release()
230 self.waitUntilSettled()
231
232 self.orderedRelease()
233 self.assertHistory([
234 dict(name='project-merge', result='SUCCESS', changes='1,1'),
235 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
236 dict(name='project-test1', result='FAILURE', changes='1,1'),
237 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
238 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
239 dict(name='project-test2', result='SUCCESS', changes='1,1'),
240 dict(name='project-merge', result='SUCCESS', changes='2,1'),
241 dict(name='project-test1', result='SUCCESS', changes='2,1'),
242 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700243 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700244
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400245 self.assertEqual(A.data['status'], 'NEW')
246 self.assertEqual(B.data['status'], 'MERGED')
247 self.assertEqual(A.reported, 2)
248 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700249
250 def test_independent_queues(self):
251 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700252
Paul Belanger174a8272017-03-14 13:20:10 -0400253 self.executor_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900254 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700255 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
256 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700257 A.addApproval('code-review', 2)
258 B.addApproval('code-review', 2)
259 C.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700260
James E. Blair8b5408c2016-08-08 15:37:46 -0700261 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800262 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700263
James E. Blairf59f3cf2017-02-19 14:50:26 -0800264 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
265 self.waitUntilSettled()
266
267 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700268 self.waitUntilSettled()
269
270 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700271 self.assertBuilds([
272 dict(name='project-merge', changes='1,1'),
273 dict(name='project-merge', changes='2,1'),
274 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700275
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700276 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700277 self.builds[0].release()
278 self.waitUntilSettled()
279 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700280 self.waitUntilSettled()
281 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400282 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700283 self.waitUntilSettled()
284
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700285 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700286 self.assertBuilds([
287 dict(name='project-test1', changes='1,1'),
288 dict(name='project-test2', changes='1,1'),
289 dict(name='project-test1', changes='2,1'),
290 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500291 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700292 dict(name='project-test1', changes='2,1 3,1'),
293 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800294 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700295 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700296
James E. Blair5f0d49e2016-08-19 09:36:50 -0700297 self.orderedRelease()
298 self.assertHistory([
299 dict(name='project-merge', result='SUCCESS', changes='1,1'),
300 dict(name='project-merge', result='SUCCESS', changes='2,1'),
301 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
302 dict(name='project-test1', result='SUCCESS', changes='1,1'),
303 dict(name='project-test2', result='SUCCESS', changes='1,1'),
304 dict(name='project-test1', result='SUCCESS', changes='2,1'),
305 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500306 dict(
307 name='project1-project2-integration',
308 result='SUCCESS',
309 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700310 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
311 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800312 dict(name='project1-project2-integration',
313 result='SUCCESS',
314 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700315 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700316
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400317 self.assertEqual(A.data['status'], 'MERGED')
318 self.assertEqual(B.data['status'], 'MERGED')
319 self.assertEqual(C.data['status'], 'MERGED')
320 self.assertEqual(A.reported, 2)
321 self.assertEqual(B.reported, 2)
322 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700323
324 def test_failed_change_at_head(self):
325 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700326
Paul Belanger174a8272017-03-14 13:20:10 -0400327 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700328 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
329 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
330 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700331 A.addApproval('code-review', 2)
332 B.addApproval('code-review', 2)
333 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700334
Paul Belanger174a8272017-03-14 13:20:10 -0400335 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700336
James E. Blair8b5408c2016-08-08 15:37:46 -0700337 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
338 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
339 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700340
341 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700342
James E. Blairb536ecc2016-08-31 10:11:42 -0700343 self.assertBuilds([
344 dict(name='project-merge', changes='1,1'),
345 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700346
Paul Belanger174a8272017-03-14 13:20:10 -0400347 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700348 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400349 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700350 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400351 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700352 self.waitUntilSettled()
353
James E. Blairb536ecc2016-08-31 10:11:42 -0700354 self.assertBuilds([
355 dict(name='project-test1', changes='1,1'),
356 dict(name='project-test2', changes='1,1'),
357 dict(name='project-test1', changes='1,1 2,1'),
358 dict(name='project-test2', changes='1,1 2,1'),
359 dict(name='project-test1', changes='1,1 2,1 3,1'),
360 dict(name='project-test2', changes='1,1 2,1 3,1'),
361 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700362
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400363 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700364 self.waitUntilSettled()
365
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400366 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700367 self.assertBuilds([
368 dict(name='project-test2', changes='1,1'),
369 dict(name='project-merge', changes='2,1'),
370 ])
371 # Unordered history comparison because the aborts can finish
372 # in any order.
373 self.assertHistory([
374 dict(name='project-merge', result='SUCCESS',
375 changes='1,1'),
376 dict(name='project-merge', result='SUCCESS',
377 changes='1,1 2,1'),
378 dict(name='project-merge', result='SUCCESS',
379 changes='1,1 2,1 3,1'),
380 dict(name='project-test1', result='FAILURE',
381 changes='1,1'),
382 dict(name='project-test1', result='ABORTED',
383 changes='1,1 2,1'),
384 dict(name='project-test2', result='ABORTED',
385 changes='1,1 2,1'),
386 dict(name='project-test1', result='ABORTED',
387 changes='1,1 2,1 3,1'),
388 dict(name='project-test2', result='ABORTED',
389 changes='1,1 2,1 3,1'),
390 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700391
Paul Belanger174a8272017-03-14 13:20:10 -0400392 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700393 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400394 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700395 self.waitUntilSettled()
396 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700397
James E. Blairb536ecc2016-08-31 10:11:42 -0700398 self.assertBuilds([])
399 self.assertHistory([
400 dict(name='project-merge', result='SUCCESS',
401 changes='1,1'),
402 dict(name='project-merge', result='SUCCESS',
403 changes='1,1 2,1'),
404 dict(name='project-merge', result='SUCCESS',
405 changes='1,1 2,1 3,1'),
406 dict(name='project-test1', result='FAILURE',
407 changes='1,1'),
408 dict(name='project-test1', result='ABORTED',
409 changes='1,1 2,1'),
410 dict(name='project-test2', result='ABORTED',
411 changes='1,1 2,1'),
412 dict(name='project-test1', result='ABORTED',
413 changes='1,1 2,1 3,1'),
414 dict(name='project-test2', result='ABORTED',
415 changes='1,1 2,1 3,1'),
416 dict(name='project-merge', result='SUCCESS',
417 changes='2,1'),
418 dict(name='project-merge', result='SUCCESS',
419 changes='2,1 3,1'),
420 dict(name='project-test2', result='SUCCESS',
421 changes='1,1'),
422 dict(name='project-test1', result='SUCCESS',
423 changes='2,1'),
424 dict(name='project-test2', result='SUCCESS',
425 changes='2,1'),
426 dict(name='project-test1', result='SUCCESS',
427 changes='2,1 3,1'),
428 dict(name='project-test2', result='SUCCESS',
429 changes='2,1 3,1'),
430 ], ordered=False)
431
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400432 self.assertEqual(A.data['status'], 'NEW')
433 self.assertEqual(B.data['status'], 'MERGED')
434 self.assertEqual(C.data['status'], 'MERGED')
435 self.assertEqual(A.reported, 2)
436 self.assertEqual(B.reported, 2)
437 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700438
James E. Blair0aac4872013-08-23 14:02:38 -0700439 def test_failed_change_in_middle(self):
440 "Test a failed change in the middle of the queue"
441
Paul Belanger174a8272017-03-14 13:20:10 -0400442 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700443 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
444 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
445 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700446 A.addApproval('code-review', 2)
447 B.addApproval('code-review', 2)
448 C.addApproval('code-review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700449
Paul Belanger174a8272017-03-14 13:20:10 -0400450 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700451
James E. Blair8b5408c2016-08-08 15:37:46 -0700452 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
453 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
454 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700455
456 self.waitUntilSettled()
457
Paul Belanger174a8272017-03-14 13:20:10 -0400458 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700459 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400460 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700461 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400462 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700463 self.waitUntilSettled()
464
465 self.assertEqual(len(self.builds), 6)
466 self.assertEqual(self.builds[0].name, 'project-test1')
467 self.assertEqual(self.builds[1].name, 'project-test2')
468 self.assertEqual(self.builds[2].name, 'project-test1')
469 self.assertEqual(self.builds[3].name, 'project-test2')
470 self.assertEqual(self.builds[4].name, 'project-test1')
471 self.assertEqual(self.builds[5].name, 'project-test2')
472
473 self.release(self.builds[2])
474 self.waitUntilSettled()
475
James E. Blair972e3c72013-08-29 12:04:55 -0700476 # project-test1 and project-test2 for A
477 # project-test2 for B
478 # project-merge for C (without B)
479 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700480 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
481
Paul Belanger174a8272017-03-14 13:20:10 -0400482 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700483 self.waitUntilSettled()
484
485 # project-test1 and project-test2 for A
486 # project-test2 for B
487 # project-test1 and project-test2 for C
488 self.assertEqual(len(self.builds), 5)
489
Paul Belanger2e2a0492016-10-30 18:33:37 -0400490 tenant = self.sched.abide.tenants.get('tenant-one')
491 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700492 builds = items[0].current_build_set.getBuilds()
493 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
494 self.assertEqual(self.countJobResults(builds, None), 2)
495 builds = items[1].current_build_set.getBuilds()
496 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
497 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
498 self.assertEqual(self.countJobResults(builds, None), 1)
499 builds = items[2].current_build_set.getBuilds()
500 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700501 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700502
Paul Belanger174a8272017-03-14 13:20:10 -0400503 self.executor_server.hold_jobs_in_build = False
504 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700505 self.waitUntilSettled()
506
507 self.assertEqual(len(self.builds), 0)
508 self.assertEqual(len(self.history), 12)
509 self.assertEqual(A.data['status'], 'MERGED')
510 self.assertEqual(B.data['status'], 'NEW')
511 self.assertEqual(C.data['status'], 'MERGED')
512 self.assertEqual(A.reported, 2)
513 self.assertEqual(B.reported, 2)
514 self.assertEqual(C.reported, 2)
515
James E. Blaird466dc42012-07-31 10:42:56 -0700516 def test_failed_change_at_head_with_queue(self):
517 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700518
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700519 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700520 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
521 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
522 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700523 A.addApproval('code-review', 2)
524 B.addApproval('code-review', 2)
525 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700526
Paul Belanger174a8272017-03-14 13:20:10 -0400527 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700528
James E. Blair8b5408c2016-08-08 15:37:46 -0700529 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
530 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
531 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700532
533 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700534 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400535 self.assertEqual(len(self.builds), 0)
536 self.assertEqual(len(queue), 1)
Clint Byrumf322fe22017-05-10 20:53:12 -0700537 self.assertEqual(queue[0].name, b'executor:execute')
538 job_args = json.loads(queue[0].arguments.decode('utf8'))
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800539 self.assertEqual(job_args['job'], 'project-merge')
540 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700541
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. Blair1f4c2bb2013-04-26 08:40:46 -0700544 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700545 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700546 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700547 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700548 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700549
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400550 self.assertEqual(len(self.builds), 0)
551 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800552
553 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700554 json.loads(queue[0].arguments.decode('utf8'))['job'],
555 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800556 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700557 json.loads(queue[1].arguments.decode('utf8'))['job'],
558 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800559 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700560 json.loads(queue[2].arguments.decode('utf8'))['job'],
561 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800562 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700563 json.loads(queue[3].arguments.decode('utf8'))['job'],
564 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800565 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700566 json.loads(queue[4].arguments.decode('utf8'))['job'],
567 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800568 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700569 json.loads(queue[5].arguments.decode('utf8'))['job'],
570 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700571
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700572 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700573 self.waitUntilSettled()
574
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400575 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700576 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400577 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
578 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700579
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700580 self.gearman_server.hold_jobs_in_queue = False
581 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700582 self.waitUntilSettled()
583
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400584 self.assertEqual(len(self.builds), 0)
585 self.assertEqual(len(self.history), 11)
586 self.assertEqual(A.data['status'], 'NEW')
587 self.assertEqual(B.data['status'], 'MERGED')
588 self.assertEqual(C.data['status'], 'MERGED')
589 self.assertEqual(A.reported, 2)
590 self.assertEqual(B.reported, 2)
591 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700592
James E. Blairec056492016-07-22 09:45:56 -0700593 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700594 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400595 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700596 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -0700597 A.addApproval('code-review', 2)
598 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700599 self.waitUntilSettled()
600 time.sleep(2)
601
602 data = json.loads(self.sched.formatStatusJSON())
603 found_job = None
604 for pipeline in data['pipelines']:
605 if pipeline['name'] != 'gate':
606 continue
607 for queue in pipeline['change_queues']:
608 for head in queue['heads']:
609 for item in head:
610 for job in item['jobs']:
611 if job['name'] == 'project-merge':
612 found_job = job
613 break
614
615 self.assertIsNotNone(found_job)
616 if iteration == 1:
617 self.assertIsNotNone(found_job['estimated_time'])
618 self.assertIsNone(found_job['remaining_time'])
619 else:
620 self.assertIsNotNone(found_job['estimated_time'])
621 self.assertTrue(found_job['estimated_time'] >= 2)
622 self.assertIsNotNone(found_job['remaining_time'])
623
Paul Belanger174a8272017-03-14 13:20:10 -0400624 self.executor_server.hold_jobs_in_build = False
625 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700626 self.waitUntilSettled()
627
James E. Blairec056492016-07-22 09:45:56 -0700628 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700629 def test_time_database(self):
630 "Test the time database"
631
632 self._test_time_database(1)
633 self._test_time_database(2)
634
James E. Blairfef71632013-09-23 11:15:47 -0700635 def test_two_failed_changes_at_head(self):
636 "Test that changes are reparented correctly if 2 fail at head"
637
Paul Belanger174a8272017-03-14 13:20:10 -0400638 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700639 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
640 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
641 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700642 A.addApproval('code-review', 2)
643 B.addApproval('code-review', 2)
644 C.addApproval('code-review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700645
Paul Belanger174a8272017-03-14 13:20:10 -0400646 self.executor_server.failJob('project-test1', A)
647 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700648
James E. Blair8b5408c2016-08-08 15:37:46 -0700649 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
650 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
651 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700652 self.waitUntilSettled()
653
Paul Belanger174a8272017-03-14 13:20:10 -0400654 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700655 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400656 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700657 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400658 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700659 self.waitUntilSettled()
660
661 self.assertEqual(len(self.builds), 6)
662 self.assertEqual(self.builds[0].name, 'project-test1')
663 self.assertEqual(self.builds[1].name, 'project-test2')
664 self.assertEqual(self.builds[2].name, 'project-test1')
665 self.assertEqual(self.builds[3].name, 'project-test2')
666 self.assertEqual(self.builds[4].name, 'project-test1')
667 self.assertEqual(self.builds[5].name, 'project-test2')
668
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400669 self.assertTrue(self.builds[0].hasChanges(A))
670 self.assertTrue(self.builds[2].hasChanges(A))
671 self.assertTrue(self.builds[2].hasChanges(B))
672 self.assertTrue(self.builds[4].hasChanges(A))
673 self.assertTrue(self.builds[4].hasChanges(B))
674 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700675
676 # Fail change B first
677 self.release(self.builds[2])
678 self.waitUntilSettled()
679
680 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400681 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700682 self.waitUntilSettled()
683
684 self.assertEqual(len(self.builds), 5)
685 self.assertEqual(self.builds[0].name, 'project-test1')
686 self.assertEqual(self.builds[1].name, 'project-test2')
687 self.assertEqual(self.builds[2].name, 'project-test2')
688 self.assertEqual(self.builds[3].name, 'project-test1')
689 self.assertEqual(self.builds[4].name, 'project-test2')
690
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400691 self.assertTrue(self.builds[1].hasChanges(A))
692 self.assertTrue(self.builds[2].hasChanges(A))
693 self.assertTrue(self.builds[2].hasChanges(B))
694 self.assertTrue(self.builds[4].hasChanges(A))
695 self.assertFalse(self.builds[4].hasChanges(B))
696 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700697
698 # Finish running all passing jobs for change A
699 self.release(self.builds[1])
700 self.waitUntilSettled()
701 # Fail and report change A
702 self.release(self.builds[0])
703 self.waitUntilSettled()
704
705 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400706 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700707 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400708 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700709 self.waitUntilSettled()
710
711 self.assertEqual(len(self.builds), 4)
712 self.assertEqual(self.builds[0].name, 'project-test1') # B
713 self.assertEqual(self.builds[1].name, 'project-test2') # B
714 self.assertEqual(self.builds[2].name, 'project-test1') # C
715 self.assertEqual(self.builds[3].name, 'project-test2') # C
716
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400717 self.assertFalse(self.builds[1].hasChanges(A))
718 self.assertTrue(self.builds[1].hasChanges(B))
719 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700720
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400721 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700722 # After A failed and B and C restarted, B should be back in
723 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400724 self.assertTrue(self.builds[2].hasChanges(B))
725 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700726
Paul Belanger174a8272017-03-14 13:20:10 -0400727 self.executor_server.hold_jobs_in_build = False
728 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700729 self.waitUntilSettled()
730
731 self.assertEqual(len(self.builds), 0)
732 self.assertEqual(len(self.history), 21)
733 self.assertEqual(A.data['status'], 'NEW')
734 self.assertEqual(B.data['status'], 'NEW')
735 self.assertEqual(C.data['status'], 'MERGED')
736 self.assertEqual(A.reported, 2)
737 self.assertEqual(B.reported, 2)
738 self.assertEqual(C.reported, 2)
739
James E. Blair8c803f82012-07-31 16:25:42 -0700740 def test_patch_order(self):
741 "Test that dependent patches are tested in the right order"
742 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
743 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
744 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700745 A.addApproval('code-review', 2)
746 B.addApproval('code-review', 2)
747 C.addApproval('code-review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700748
749 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
750 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
751 M2.setMerged()
752 M1.setMerged()
753
754 # C -> B -> A -> M1 -> M2
755 # M2 is here to make sure it is never queried. If it is, it
756 # means zuul is walking down the entire history of merged
757 # changes.
758
759 C.setDependsOn(B, 1)
760 B.setDependsOn(A, 1)
761 A.setDependsOn(M1, 1)
762 M1.setDependsOn(M2, 1)
763
James E. Blair8b5408c2016-08-08 15:37:46 -0700764 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700765
766 self.waitUntilSettled()
767
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400768 self.assertEqual(A.data['status'], 'NEW')
769 self.assertEqual(B.data['status'], 'NEW')
770 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700771
James E. Blair8b5408c2016-08-08 15:37:46 -0700772 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
773 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700774
775 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400776 self.assertEqual(M2.queried, 0)
777 self.assertEqual(A.data['status'], 'MERGED')
778 self.assertEqual(B.data['status'], 'MERGED')
779 self.assertEqual(C.data['status'], 'MERGED')
780 self.assertEqual(A.reported, 2)
781 self.assertEqual(B.reported, 2)
782 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700783
James E. Blair063672f2015-01-29 13:09:12 -0800784 def test_needed_changes_enqueue(self):
785 "Test that a needed change is enqueued ahead"
786 # A Given a git tree like this, if we enqueue
787 # / \ change C, we should walk up and down the tree
788 # B G and enqueue changes in the order ABCDEFG.
789 # /|\ This is also the order that you would get if
790 # *C E F you enqueued changes in the order ABCDEFG, so
791 # / the ordering is stable across re-enqueue events.
792 # D
793
794 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
795 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
796 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
797 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
798 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
799 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
800 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
801 B.setDependsOn(A, 1)
802 C.setDependsOn(B, 1)
803 D.setDependsOn(C, 1)
804 E.setDependsOn(B, 1)
805 F.setDependsOn(B, 1)
806 G.setDependsOn(A, 1)
807
James E. Blair8b5408c2016-08-08 15:37:46 -0700808 A.addApproval('code-review', 2)
809 B.addApproval('code-review', 2)
810 C.addApproval('code-review', 2)
811 D.addApproval('code-review', 2)
812 E.addApproval('code-review', 2)
813 F.addApproval('code-review', 2)
814 G.addApproval('code-review', 2)
815 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800816
817 self.waitUntilSettled()
818
819 self.assertEqual(A.data['status'], 'NEW')
820 self.assertEqual(B.data['status'], 'NEW')
821 self.assertEqual(C.data['status'], 'NEW')
822 self.assertEqual(D.data['status'], 'NEW')
823 self.assertEqual(E.data['status'], 'NEW')
824 self.assertEqual(F.data['status'], 'NEW')
825 self.assertEqual(G.data['status'], 'NEW')
826
827 # We're about to add approvals to changes without adding the
828 # triggering events to Zuul, so that we can be sure that it is
829 # enqueing the changes based on dependencies, not because of
830 # triggering events. Since it will have the changes cached
831 # already (without approvals), we need to clear the cache
832 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400833 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100834 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800835
Paul Belanger174a8272017-03-14 13:20:10 -0400836 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -0700837 A.addApproval('approved', 1)
838 B.addApproval('approved', 1)
839 D.addApproval('approved', 1)
840 E.addApproval('approved', 1)
841 F.addApproval('approved', 1)
842 G.addApproval('approved', 1)
843 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800844
845 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400846 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800847 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400848 self.executor_server.hold_jobs_in_build = False
849 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800850 self.waitUntilSettled()
851
852 self.assertEqual(A.data['status'], 'MERGED')
853 self.assertEqual(B.data['status'], 'MERGED')
854 self.assertEqual(C.data['status'], 'MERGED')
855 self.assertEqual(D.data['status'], 'MERGED')
856 self.assertEqual(E.data['status'], 'MERGED')
857 self.assertEqual(F.data['status'], 'MERGED')
858 self.assertEqual(G.data['status'], 'MERGED')
859 self.assertEqual(A.reported, 2)
860 self.assertEqual(B.reported, 2)
861 self.assertEqual(C.reported, 2)
862 self.assertEqual(D.reported, 2)
863 self.assertEqual(E.reported, 2)
864 self.assertEqual(F.reported, 2)
865 self.assertEqual(G.reported, 2)
866 self.assertEqual(self.history[6].changes,
867 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
868
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100869 def test_source_cache(self):
870 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400871 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700872
873 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
874 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
875 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
James E. Blair8b5408c2016-08-08 15:37:46 -0700876 A.addApproval('code-review', 2)
877 B.addApproval('code-review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700878
879 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
880 M1.setMerged()
881
882 B.setDependsOn(A, 1)
883 A.setDependsOn(M1, 1)
884
James E. Blair8b5408c2016-08-08 15:37:46 -0700885 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700886 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
887
888 self.waitUntilSettled()
889
890 for build in self.builds:
891 if build.parameters['ZUUL_PIPELINE'] == 'check':
892 build.release()
893 self.waitUntilSettled()
894 for build in self.builds:
895 if build.parameters['ZUUL_PIPELINE'] == 'check':
896 build.release()
897 self.waitUntilSettled()
898
James E. Blair8b5408c2016-08-08 15:37:46 -0700899 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700900 self.waitUntilSettled()
901
Joshua Hesketh352264b2015-08-11 23:42:08 +1000902 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700903 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000904 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700905
Paul Belanger174a8272017-03-14 13:20:10 -0400906 self.executor_server.hold_jobs_in_build = False
907 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700908 self.waitUntilSettled()
909
910 self.assertEqual(A.data['status'], 'MERGED')
911 self.assertEqual(B.data['status'], 'MERGED')
912 self.assertEqual(A.queried, 2) # Initial and isMerged
913 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
914
James E. Blair8c803f82012-07-31 16:25:42 -0700915 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700916 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700917 # TODO: move to test_gerrit (this is a unit test!)
918 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400919 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -0700920 (trusted, project) = tenant.getProject('org/project')
921 source = project.source
Paul Belanger9bba4902016-11-02 16:07:33 -0400922
923 # TODO(pabelanger): As we add more source / trigger APIs we should make
924 # it easier for users to create events for testing.
925 event = zuul.model.TriggerEvent()
926 event.trigger_name = 'gerrit'
927 event.change_number = '1'
928 event.patch_number = '2'
929
930 a = source.getChange(event)
931 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700932 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700933
James E. Blair8b5408c2016-08-08 15:37:46 -0700934 A.addApproval('code-review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400935 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700936 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700937
James E. Blair8b5408c2016-08-08 15:37:46 -0700938 A.addApproval('approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400939 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700940 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700941
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700942 def test_project_merge_conflict(self):
943 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700944
945 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700946 A = self.fake_gerrit.addFakeChange('org/project',
947 'master', 'A',
948 files={'conflict': 'foo'})
949 B = self.fake_gerrit.addFakeChange('org/project',
950 'master', 'B',
951 files={'conflict': 'bar'})
952 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -0700953 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700954 A.addApproval('code-review', 2)
955 B.addApproval('code-review', 2)
956 C.addApproval('code-review', 2)
957 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
958 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
959 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700960 self.waitUntilSettled()
961
James E. Blair6736beb2013-07-11 15:18:15 -0700962 self.assertEqual(A.reported, 1)
James E. Blair6736beb2013-07-11 15:18:15 -0700963 self.assertEqual(C.reported, 1)
964
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700965 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700966 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700967 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700968 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700969 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700970 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700971
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700972 self.gearman_server.hold_jobs_in_queue = False
973 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700974 self.waitUntilSettled()
975
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400976 self.assertEqual(A.data['status'], 'MERGED')
977 self.assertEqual(B.data['status'], 'NEW')
978 self.assertEqual(C.data['status'], 'MERGED')
979 self.assertEqual(A.reported, 2)
Tobias Henkel9842bd72017-05-16 13:40:03 +0200980 self.assertIn('Merge Failed', B.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400981 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700982
983 self.assertHistory([
984 dict(name='project-merge', result='SUCCESS', changes='1,1'),
985 dict(name='project-test1', result='SUCCESS', changes='1,1'),
986 dict(name='project-test2', result='SUCCESS', changes='1,1'),
987 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
988 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
989 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
990 ], ordered=False)
991
992 def test_delayed_merge_conflict(self):
993 "Test that delayed check merge conflicts are handled properly"
994
995 # Hold jobs in the gearman queue so that we can test whether
James E. Blair1960d682017-04-28 15:44:14 -0700996 # the executor sucesfully merges a change based on an old
997 # repo state (frozen by the scheduler) which would otherwise
998 # conflict.
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700999 self.gearman_server.hold_jobs_in_queue = True
1000 A = self.fake_gerrit.addFakeChange('org/project',
1001 'master', 'A',
1002 files={'conflict': 'foo'})
1003 B = self.fake_gerrit.addFakeChange('org/project',
1004 'master', 'B',
1005 files={'conflict': 'bar'})
1006 C = self.fake_gerrit.addFakeChange('org/project',
1007 'master', 'C')
1008 C.setDependsOn(B, 1)
1009
1010 # A enters the gate queue; B and C enter the check queue
1011 A.addApproval('code-review', 2)
1012 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1013 self.waitUntilSettled()
1014 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
James E. Blair8d144dc2017-05-05 10:13:45 -07001015 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001016 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1017 self.waitUntilSettled()
1018
1019 self.assertEqual(A.reported, 1)
1020 self.assertEqual(B.reported, 0) # Check does not report start
1021 self.assertEqual(C.reported, 0) # Check does not report start
1022
1023 # A merges while B and C are queued in check
1024 # Release A project-merge
1025 queue = self.gearman_server.getQueue()
1026 self.release(queue[0])
1027 self.waitUntilSettled()
1028
1029 # Release A project-test*
1030 # gate has higher precedence, so A's test jobs are added in
1031 # front of the merge jobs for B and C
1032 queue = self.gearman_server.getQueue()
1033 self.release(queue[0])
1034 self.release(queue[1])
1035 self.waitUntilSettled()
1036
1037 self.assertEqual(A.data['status'], 'MERGED')
1038 self.assertEqual(B.data['status'], 'NEW')
1039 self.assertEqual(C.data['status'], 'NEW')
1040 self.assertEqual(A.reported, 2)
1041 self.assertEqual(B.reported, 0)
1042 self.assertEqual(C.reported, 0)
1043 self.assertHistory([
1044 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1045 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1046 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1047 ], ordered=False)
1048
1049 # B and C report merge conflicts
1050 # Release B project-merge
1051 queue = self.gearman_server.getQueue()
1052 self.release(queue[0])
1053 self.waitUntilSettled()
1054
1055 # Release C
1056 self.gearman_server.hold_jobs_in_queue = False
1057 self.gearman_server.release()
1058 self.waitUntilSettled()
1059
1060 self.assertEqual(A.data['status'], 'MERGED')
1061 self.assertEqual(B.data['status'], 'NEW')
1062 self.assertEqual(C.data['status'], 'NEW')
1063 self.assertEqual(A.reported, 2)
1064 self.assertEqual(B.reported, 1)
1065 self.assertEqual(C.reported, 1)
1066
1067 self.assertHistory([
1068 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1069 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1070 dict(name='project-test2', result='SUCCESS', changes='1,1'),
James E. Blair1960d682017-04-28 15:44:14 -07001071 dict(name='project-merge', result='SUCCESS', changes='2,1'),
1072 dict(name='project-test1', result='SUCCESS', changes='2,1'),
1073 dict(name='project-test2', result='SUCCESS', changes='2,1'),
1074 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
1075 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
1076 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001077 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001078
James E. Blairdaabed22012-08-15 15:38:57 -07001079 def test_post(self):
1080 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001081
Zhongyue Luo5d556072012-09-21 02:00:47 +09001082 e = {
1083 "type": "ref-updated",
1084 "submitter": {
1085 "name": "User Name",
1086 },
1087 "refUpdate": {
1088 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1089 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1090 "refName": "master",
1091 "project": "org/project",
1092 }
1093 }
James E. Blairdaabed22012-08-15 15:38:57 -07001094 self.fake_gerrit.addEvent(e)
1095 self.waitUntilSettled()
1096
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001097 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001098 self.assertEqual(len(self.history), 1)
1099 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001100
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001101 def test_post_ignore_deletes(self):
1102 "Test that deleting refs does not trigger post jobs"
1103
1104 e = {
1105 "type": "ref-updated",
1106 "submitter": {
1107 "name": "User Name",
1108 },
1109 "refUpdate": {
1110 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1111 "newRev": "0000000000000000000000000000000000000000",
1112 "refName": "master",
1113 "project": "org/project",
1114 }
1115 }
1116 self.fake_gerrit.addEvent(e)
1117 self.waitUntilSettled()
1118
1119 job_names = [x.name for x in self.history]
1120 self.assertEqual(len(self.history), 0)
1121 self.assertNotIn('project-post', job_names)
1122
James E. Blaircc324192017-04-19 14:36:08 -07001123 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001124 def test_post_ignore_deletes_negative(self):
1125 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001126 e = {
1127 "type": "ref-updated",
1128 "submitter": {
1129 "name": "User Name",
1130 },
1131 "refUpdate": {
1132 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1133 "newRev": "0000000000000000000000000000000000000000",
1134 "refName": "master",
1135 "project": "org/project",
1136 }
1137 }
1138 self.fake_gerrit.addEvent(e)
1139 self.waitUntilSettled()
1140
1141 job_names = [x.name for x in self.history]
1142 self.assertEqual(len(self.history), 1)
1143 self.assertIn('project-post', job_names)
1144
James E. Blairec056492016-07-22 09:45:56 -07001145 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001146 def test_build_configuration_branch_interaction(self):
1147 "Test that switching between branches works"
1148 self.test_build_configuration()
1149 self.test_build_configuration_branch()
1150 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001151 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001152 repo = git.Repo(path)
1153 repo.heads.master.commit = repo.commit('init')
1154 self.test_build_configuration()
1155
James E. Blaircaec0c52012-08-22 14:52:22 -07001156 def test_dependent_changes_dequeue(self):
1157 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001158
James E. Blaircaec0c52012-08-22 14:52:22 -07001159 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1160 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1161 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001162 A.addApproval('code-review', 2)
1163 B.addApproval('code-review', 2)
1164 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001165
1166 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1167 M1.setMerged()
1168
1169 # C -> B -> A -> M1
1170
1171 C.setDependsOn(B, 1)
1172 B.setDependsOn(A, 1)
1173 A.setDependsOn(M1, 1)
1174
Paul Belanger174a8272017-03-14 13:20:10 -04001175 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001176
James E. Blair8b5408c2016-08-08 15:37:46 -07001177 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1178 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1179 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001180
1181 self.waitUntilSettled()
1182
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001183 self.assertEqual(A.data['status'], 'NEW')
1184 self.assertEqual(A.reported, 2)
1185 self.assertEqual(B.data['status'], 'NEW')
1186 self.assertEqual(B.reported, 2)
1187 self.assertEqual(C.data['status'], 'NEW')
James E. Blair0d3e83b2017-06-05 13:51:57 -07001188 self.assertIn('This change depends on a change that failed to merge.',
1189 C.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001190 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001191
James E. Blair972e3c72013-08-29 12:04:55 -07001192 def test_failing_dependent_changes(self):
1193 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001194 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001195 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1196 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1197 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1198 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1199 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001200 A.addApproval('code-review', 2)
1201 B.addApproval('code-review', 2)
1202 C.addApproval('code-review', 2)
1203 D.addApproval('code-review', 2)
1204 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001205
1206 # E, D -> C -> B, A
1207
1208 D.setDependsOn(C, 1)
1209 C.setDependsOn(B, 1)
1210
Paul Belanger174a8272017-03-14 13:20:10 -04001211 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001212
James E. Blair8b5408c2016-08-08 15:37:46 -07001213 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1214 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1215 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1216 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1217 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001218
1219 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001220 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001221 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001222 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001223 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001224 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001225 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001226 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001227 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001228 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001229 self.waitUntilSettled()
1230
Paul Belanger174a8272017-03-14 13:20:10 -04001231 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001232 for build in self.builds:
1233 if build.parameters['ZUUL_CHANGE'] != '1':
1234 build.release()
1235 self.waitUntilSettled()
1236
Paul Belanger174a8272017-03-14 13:20:10 -04001237 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001238 self.waitUntilSettled()
1239
1240 self.assertEqual(A.data['status'], 'MERGED')
1241 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001242 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001243 self.assertEqual(B.data['status'], 'NEW')
1244 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001245 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001246 self.assertEqual(C.data['status'], 'NEW')
1247 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001248 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001249 self.assertEqual(D.data['status'], 'NEW')
1250 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001251 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001252 self.assertEqual(E.data['status'], 'MERGED')
1253 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001254 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001255 self.assertEqual(len(self.history), 18)
1256
James E. Blairec590122012-08-22 15:19:31 -07001257 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001258 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001259 # If it's dequeued more than once, we should see extra
1260 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001261
Paul Belanger174a8272017-03-14 13:20:10 -04001262 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001263 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1264 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1265 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001266 A.addApproval('code-review', 2)
1267 B.addApproval('code-review', 2)
1268 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001269
Paul Belanger174a8272017-03-14 13:20:10 -04001270 self.executor_server.failJob('project-test1', A)
1271 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001272
James E. Blair8b5408c2016-08-08 15:37:46 -07001273 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1274 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1275 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001276
1277 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001278
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001279 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001280 self.assertEqual(self.builds[0].name, 'project-merge')
1281 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001282
Paul Belanger174a8272017-03-14 13:20:10 -04001283 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001284 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001285 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001286 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001287 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001288 self.waitUntilSettled()
1289
James E. Blair6df67132017-04-19 13:53:20 -07001290 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001291 self.assertEqual(self.builds[0].name, 'project-test1')
1292 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001293 self.assertEqual(self.builds[2].name, 'project-test1')
1294 self.assertEqual(self.builds[3].name, 'project-test2')
1295 self.assertEqual(self.builds[4].name, 'project-test1')
1296 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001297
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001298 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001299 self.waitUntilSettled()
1300
James E. Blair6df67132017-04-19 13:53:20 -07001301 self.assertEqual(len(self.builds), 2) # test2, merge for B
1302 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001303
Paul Belanger174a8272017-03-14 13:20:10 -04001304 self.executor_server.hold_jobs_in_build = False
1305 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001306 self.waitUntilSettled()
1307
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001308 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001309 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001310
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001311 self.assertEqual(A.data['status'], 'NEW')
1312 self.assertEqual(B.data['status'], 'MERGED')
1313 self.assertEqual(C.data['status'], 'MERGED')
1314 self.assertEqual(A.reported, 2)
1315 self.assertEqual(B.reported, 2)
1316 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001317
James E. Blaircc020532017-04-19 13:04:44 -07001318 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001319 def test_nonvoting_job(self):
1320 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001321
James E. Blair4ec821f2012-08-23 15:28:28 -07001322 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1323 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001324 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001325 self.executor_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001326 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001327
1328 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001329
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001330 self.assertEqual(A.data['status'], 'MERGED')
1331 self.assertEqual(A.reported, 2)
1332 self.assertEqual(
1333 self.getJobFromHistory('nonvoting-project-merge').result,
1334 'SUCCESS')
1335 self.assertEqual(
1336 self.getJobFromHistory('nonvoting-project-test1').result,
1337 'SUCCESS')
1338 self.assertEqual(
1339 self.getJobFromHistory('nonvoting-project-test2').result,
1340 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001341
James E. Blair5821bd92015-09-16 08:48:15 -07001342 for build in self.builds:
1343 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1344
James E. Blaire0487072012-08-29 17:38:31 -07001345 def test_check_queue_success(self):
1346 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001347
James E. Blaire0487072012-08-29 17:38:31 -07001348 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1349 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1350
1351 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001352
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001353 self.assertEqual(A.data['status'], 'NEW')
1354 self.assertEqual(A.reported, 1)
1355 self.assertEqual(self.getJobFromHistory('project-merge').result,
1356 'SUCCESS')
1357 self.assertEqual(self.getJobFromHistory('project-test1').result,
1358 'SUCCESS')
1359 self.assertEqual(self.getJobFromHistory('project-test2').result,
1360 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001361
1362 def test_check_queue_failure(self):
1363 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001364
James E. Blaire0487072012-08-29 17:38:31 -07001365 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001366 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001367 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1368
1369 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001370
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001371 self.assertEqual(A.data['status'], 'NEW')
1372 self.assertEqual(A.reported, 1)
1373 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001374 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001375 self.assertEqual(self.getJobFromHistory('project-test1').result,
1376 'SUCCESS')
1377 self.assertEqual(self.getJobFromHistory('project-test2').result,
1378 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001379
James E. Blair7cb84542017-04-19 13:35:05 -07001380 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001381 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001382 # This particular test does a large amount of merges and needs a little
1383 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001384 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001385 "test that dependent changes behind dequeued changes work"
1386 # This complicated test is a reproduction of a real life bug
1387 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001388
Paul Belanger174a8272017-03-14 13:20:10 -04001389 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001390 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1391 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1392 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1393 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1394 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1395 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1396 D.setDependsOn(C, 1)
1397 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001398 A.addApproval('code-review', 2)
1399 B.addApproval('code-review', 2)
1400 C.addApproval('code-review', 2)
1401 D.addApproval('code-review', 2)
1402 E.addApproval('code-review', 2)
1403 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001404
1405 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001406
1407 # Change object re-use in the gerrit trigger is hidden if
1408 # changes are added in quick succession; waiting makes it more
1409 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001410 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001411 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001412 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001413 self.waitUntilSettled()
1414
Paul Belanger174a8272017-03-14 13:20:10 -04001415 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001416 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001417 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001418 self.waitUntilSettled()
1419
James E. Blair8b5408c2016-08-08 15:37:46 -07001420 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001421 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001422 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001423 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001424 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001425 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001426 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001427 self.waitUntilSettled()
1428
Paul Belanger174a8272017-03-14 13:20:10 -04001429 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001430 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001431 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001432 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001433 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001434 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001435 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001436 self.waitUntilSettled()
1437
1438 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001439
1440 # Grab pointers to the jobs we want to release before
1441 # releasing any, because list indexes may change as
1442 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001443 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001444 a.release()
1445 b.release()
1446 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001447 self.waitUntilSettled()
1448
Paul Belanger174a8272017-03-14 13:20:10 -04001449 self.executor_server.hold_jobs_in_build = False
1450 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001451 self.waitUntilSettled()
1452
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001453 self.assertEqual(A.data['status'], 'NEW')
1454 self.assertEqual(B.data['status'], 'MERGED')
1455 self.assertEqual(C.data['status'], 'MERGED')
1456 self.assertEqual(D.data['status'], 'MERGED')
1457 self.assertEqual(E.data['status'], 'MERGED')
1458 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001459
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001460 self.assertEqual(A.reported, 2)
1461 self.assertEqual(B.reported, 2)
1462 self.assertEqual(C.reported, 2)
1463 self.assertEqual(D.reported, 2)
1464 self.assertEqual(E.reported, 2)
1465 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001466
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001467 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1468 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001469
1470 def test_merger_repack(self):
1471 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001472
James E. Blair05fed602012-09-07 12:45:24 -07001473 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001474 A.addApproval('code-review', 2)
1475 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001476 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001477 self.assertEqual(self.getJobFromHistory('project-merge').result,
1478 'SUCCESS')
1479 self.assertEqual(self.getJobFromHistory('project-test1').result,
1480 'SUCCESS')
1481 self.assertEqual(self.getJobFromHistory('project-test2').result,
1482 'SUCCESS')
1483 self.assertEqual(A.data['status'], 'MERGED')
1484 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001485 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001486 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001487
James E. Blair2a535672017-04-27 12:03:15 -07001488 path = os.path.join(self.merger_src_root, "review.example.com",
1489 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001490 if os.path.exists(path):
1491 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001492 path = os.path.join(self.executor_src_root, "review.example.com",
1493 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001494 if os.path.exists(path):
1495 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001496
1497 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001498 A.addApproval('code-review', 2)
1499 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001500 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001501 self.assertEqual(self.getJobFromHistory('project-merge').result,
1502 'SUCCESS')
1503 self.assertEqual(self.getJobFromHistory('project-test1').result,
1504 'SUCCESS')
1505 self.assertEqual(self.getJobFromHistory('project-test2').result,
1506 'SUCCESS')
1507 self.assertEqual(A.data['status'], 'MERGED')
1508 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001509
James E. Blair4886f282012-11-15 09:27:33 -08001510 def test_merger_repack_large_change(self):
1511 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001512 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001513 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001514 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001515 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001516 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001517 self.executor_server.merger._addProject('review.example.com',
1518 'org/project', url)
James E. Blair6df67132017-04-19 13:53:20 -07001519 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001520 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001521 # TODOv3(jeblair): add hostname to upstream root
1522 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001523 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001524 path = os.path.join(self.merger_src_root, 'review.example.com',
1525 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001526 if os.path.exists(path):
1527 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001528 path = os.path.join(self.executor_src_root, 'review.example.com',
1529 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001530 if os.path.exists(path):
1531 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001532
James E. Blair8b5408c2016-08-08 15:37:46 -07001533 A.addApproval('code-review', 2)
1534 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001535 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001536 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001537 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001538 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001539 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001540 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001541 'SUCCESS')
1542 self.assertEqual(A.data['status'], 'MERGED')
1543 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001544
James E. Blair2fa50962013-01-30 21:50:41 -08001545 def test_new_patchset_dequeues_old(self):
1546 "Test that a new patchset causes the old to be dequeued"
1547 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001548 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001549 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1550 M.setMerged()
1551
1552 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1553 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1554 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1555 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001556 A.addApproval('code-review', 2)
1557 B.addApproval('code-review', 2)
1558 C.addApproval('code-review', 2)
1559 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001560
1561 C.setDependsOn(B, 1)
1562 B.setDependsOn(A, 1)
1563 A.setDependsOn(M, 1)
1564
James E. Blair8b5408c2016-08-08 15:37:46 -07001565 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1566 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1567 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1568 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001569 self.waitUntilSettled()
1570
1571 B.addPatchset()
1572 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1573 self.waitUntilSettled()
1574
Paul Belanger174a8272017-03-14 13:20:10 -04001575 self.executor_server.hold_jobs_in_build = False
1576 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001577 self.waitUntilSettled()
1578
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001579 self.assertEqual(A.data['status'], 'MERGED')
1580 self.assertEqual(A.reported, 2)
1581 self.assertEqual(B.data['status'], 'NEW')
1582 self.assertEqual(B.reported, 2)
1583 self.assertEqual(C.data['status'], 'NEW')
1584 self.assertEqual(C.reported, 2)
1585 self.assertEqual(D.data['status'], 'MERGED')
1586 self.assertEqual(D.reported, 2)
1587 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001588
James E. Blairba437362015-02-07 11:41:52 -08001589 def test_new_patchset_check(self):
1590 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001591
Paul Belanger174a8272017-03-14 13:20:10 -04001592 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001593
1594 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001595 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001596 tenant = self.sched.abide.tenants.get('tenant-one')
1597 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001598
1599 # Add two git-dependent changes
1600 B.setDependsOn(A, 1)
1601 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1602 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001603 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1604 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001605
1606 # A live item, and a non-live/live pair
1607 items = check_pipeline.getAllItems()
1608 self.assertEqual(len(items), 3)
1609
1610 self.assertEqual(items[0].change.number, '1')
1611 self.assertEqual(items[0].change.patchset, '1')
1612 self.assertFalse(items[0].live)
1613
1614 self.assertEqual(items[1].change.number, '2')
1615 self.assertEqual(items[1].change.patchset, '1')
1616 self.assertTrue(items[1].live)
1617
1618 self.assertEqual(items[2].change.number, '1')
1619 self.assertEqual(items[2].change.patchset, '1')
1620 self.assertTrue(items[2].live)
1621
1622 # Add a new patchset to A
1623 A.addPatchset()
1624 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1625 self.waitUntilSettled()
1626
1627 # The live copy of A,1 should be gone, but the non-live and B
1628 # should continue, and we should have a new A,2
1629 items = check_pipeline.getAllItems()
1630 self.assertEqual(len(items), 3)
1631
1632 self.assertEqual(items[0].change.number, '1')
1633 self.assertEqual(items[0].change.patchset, '1')
1634 self.assertFalse(items[0].live)
1635
1636 self.assertEqual(items[1].change.number, '2')
1637 self.assertEqual(items[1].change.patchset, '1')
1638 self.assertTrue(items[1].live)
1639
1640 self.assertEqual(items[2].change.number, '1')
1641 self.assertEqual(items[2].change.patchset, '2')
1642 self.assertTrue(items[2].live)
1643
1644 # Add a new patchset to B
1645 B.addPatchset()
1646 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1647 self.waitUntilSettled()
1648
1649 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1650 # but we should have a new B,2 (still based on A,1)
1651 items = check_pipeline.getAllItems()
1652 self.assertEqual(len(items), 3)
1653
1654 self.assertEqual(items[0].change.number, '1')
1655 self.assertEqual(items[0].change.patchset, '2')
1656 self.assertTrue(items[0].live)
1657
1658 self.assertEqual(items[1].change.number, '1')
1659 self.assertEqual(items[1].change.patchset, '1')
1660 self.assertFalse(items[1].live)
1661
1662 self.assertEqual(items[2].change.number, '2')
1663 self.assertEqual(items[2].change.patchset, '2')
1664 self.assertTrue(items[2].live)
1665
1666 self.builds[0].release()
1667 self.waitUntilSettled()
1668 self.builds[0].release()
1669 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001670 self.executor_server.hold_jobs_in_build = False
1671 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001672 self.waitUntilSettled()
1673
1674 self.assertEqual(A.reported, 1)
1675 self.assertEqual(B.reported, 1)
1676 self.assertEqual(self.history[0].result, 'ABORTED')
1677 self.assertEqual(self.history[0].changes, '1,1')
1678 self.assertEqual(self.history[1].result, 'ABORTED')
1679 self.assertEqual(self.history[1].changes, '1,1 2,1')
1680 self.assertEqual(self.history[2].result, 'SUCCESS')
1681 self.assertEqual(self.history[2].changes, '1,2')
1682 self.assertEqual(self.history[3].result, 'SUCCESS')
1683 self.assertEqual(self.history[3].changes, '1,1 2,2')
1684
1685 def test_abandoned_gate(self):
1686 "Test that an abandoned change is dequeued from gate"
1687
Paul Belanger174a8272017-03-14 13:20:10 -04001688 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001689
1690 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001691 A.addApproval('code-review', 2)
1692 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001693 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001694 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1695 self.assertEqual(self.builds[0].name, 'project-merge')
1696
1697 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1698 self.waitUntilSettled()
1699
Paul Belanger174a8272017-03-14 13:20:10 -04001700 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001701 self.waitUntilSettled()
1702
Arie5b4048c2016-10-05 16:12:06 +03001703 self.assertBuilds([])
1704 self.assertHistory([
1705 dict(name='project-merge', result='ABORTED', changes='1,1')],
1706 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001707 self.assertEqual(A.reported, 1,
1708 "Abandoned gate change should report only start")
1709
1710 def test_abandoned_check(self):
1711 "Test that an abandoned change is dequeued from check"
1712
Paul Belanger174a8272017-03-14 13:20:10 -04001713 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001714
1715 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1716 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001717 tenant = self.sched.abide.tenants.get('tenant-one')
1718 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001719
1720 # Add two git-dependent changes
1721 B.setDependsOn(A, 1)
1722 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1723 self.waitUntilSettled()
1724 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1725 self.waitUntilSettled()
1726 # A live item, and a non-live/live pair
1727 items = check_pipeline.getAllItems()
1728 self.assertEqual(len(items), 3)
1729
1730 self.assertEqual(items[0].change.number, '1')
1731 self.assertFalse(items[0].live)
1732
1733 self.assertEqual(items[1].change.number, '2')
1734 self.assertTrue(items[1].live)
1735
1736 self.assertEqual(items[2].change.number, '1')
1737 self.assertTrue(items[2].live)
1738
1739 # Abandon A
1740 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1741 self.waitUntilSettled()
1742
1743 # The live copy of A should be gone, but the non-live and B
1744 # should continue
1745 items = check_pipeline.getAllItems()
1746 self.assertEqual(len(items), 2)
1747
1748 self.assertEqual(items[0].change.number, '1')
1749 self.assertFalse(items[0].live)
1750
1751 self.assertEqual(items[1].change.number, '2')
1752 self.assertTrue(items[1].live)
1753
Paul Belanger174a8272017-03-14 13:20:10 -04001754 self.executor_server.hold_jobs_in_build = False
1755 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001756 self.waitUntilSettled()
1757
1758 self.assertEqual(len(self.history), 4)
1759 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001760 'Build should have been aborted')
1761 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001762 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001763
Steve Varnau7b78b312015-04-03 14:49:46 -07001764 def test_abandoned_not_timer(self):
1765 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001766 # This test can not use simple_layout because it must start
1767 # with a configuration which does not include a
1768 # timer-triggered job so that we have an opportunity to set
1769 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001770 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001771 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001772 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001773 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001774 # The pipeline triggers every second, so we should have seen
1775 # several by now.
1776 time.sleep(5)
1777 self.waitUntilSettled()
1778 # Stop queuing timer triggered jobs so that the assertions
1779 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001780 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001781 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001782 self.sched.reconfigure(self.config)
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001783
Steve Varnau7b78b312015-04-03 14:49:46 -07001784 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1785
1786 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1787 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1788 self.waitUntilSettled()
1789 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1790
1791 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1792 self.waitUntilSettled()
1793
1794 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1795
Paul Belanger174a8272017-03-14 13:20:10 -04001796 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001797 self.waitUntilSettled()
1798
Arx Cruzb1b010d2013-10-28 19:49:59 -02001799 def test_zuul_url_return(self):
1800 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001801 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Paul Belanger174a8272017-03-14 13:20:10 -04001802 self.executor_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001803
1804 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001805 A.addApproval('code-review', 2)
1806 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001807 self.waitUntilSettled()
1808
1809 self.assertEqual(len(self.builds), 1)
1810 for build in self.builds:
1811 self.assertTrue('ZUUL_URL' in build.parameters)
1812
Paul Belanger174a8272017-03-14 13:20:10 -04001813 self.executor_server.hold_jobs_in_build = False
1814 self.executor_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001815 self.waitUntilSettled()
1816
James E. Blair2fa50962013-01-30 21:50:41 -08001817 def test_new_patchset_dequeues_old_on_head(self):
1818 "Test that a new patchset causes the old to be dequeued (at head)"
1819 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001820 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001821 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1822 M.setMerged()
1823 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1824 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1825 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1826 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001827 A.addApproval('code-review', 2)
1828 B.addApproval('code-review', 2)
1829 C.addApproval('code-review', 2)
1830 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001831
1832 C.setDependsOn(B, 1)
1833 B.setDependsOn(A, 1)
1834 A.setDependsOn(M, 1)
1835
James E. Blair8b5408c2016-08-08 15:37:46 -07001836 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1837 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1838 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1839 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001840 self.waitUntilSettled()
1841
1842 A.addPatchset()
1843 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1844 self.waitUntilSettled()
1845
Paul Belanger174a8272017-03-14 13:20:10 -04001846 self.executor_server.hold_jobs_in_build = False
1847 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001848 self.waitUntilSettled()
1849
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001850 self.assertEqual(A.data['status'], 'NEW')
1851 self.assertEqual(A.reported, 2)
1852 self.assertEqual(B.data['status'], 'NEW')
1853 self.assertEqual(B.reported, 2)
1854 self.assertEqual(C.data['status'], 'NEW')
1855 self.assertEqual(C.reported, 2)
1856 self.assertEqual(D.data['status'], 'MERGED')
1857 self.assertEqual(D.reported, 2)
1858 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001859
1860 def test_new_patchset_dequeues_old_without_dependents(self):
1861 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001862 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001863 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1864 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1865 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001866 A.addApproval('code-review', 2)
1867 B.addApproval('code-review', 2)
1868 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001869
James E. Blair8b5408c2016-08-08 15:37:46 -07001870 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1871 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1872 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001873 self.waitUntilSettled()
1874
1875 B.addPatchset()
1876 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1877 self.waitUntilSettled()
1878
Paul Belanger174a8272017-03-14 13:20:10 -04001879 self.executor_server.hold_jobs_in_build = False
1880 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001881 self.waitUntilSettled()
1882
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001883 self.assertEqual(A.data['status'], 'MERGED')
1884 self.assertEqual(A.reported, 2)
1885 self.assertEqual(B.data['status'], 'NEW')
1886 self.assertEqual(B.reported, 2)
1887 self.assertEqual(C.data['status'], 'MERGED')
1888 self.assertEqual(C.reported, 2)
1889 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001890
1891 def test_new_patchset_dequeues_old_independent_queue(self):
1892 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001893 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001894 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1895 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1896 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1897 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1898 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1899 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1900 self.waitUntilSettled()
1901
1902 B.addPatchset()
1903 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1904 self.waitUntilSettled()
1905
Paul Belanger174a8272017-03-14 13:20:10 -04001906 self.executor_server.hold_jobs_in_build = False
1907 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001908 self.waitUntilSettled()
1909
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001910 self.assertEqual(A.data['status'], 'NEW')
1911 self.assertEqual(A.reported, 1)
1912 self.assertEqual(B.data['status'], 'NEW')
1913 self.assertEqual(B.reported, 1)
1914 self.assertEqual(C.data['status'], 'NEW')
1915 self.assertEqual(C.reported, 1)
1916 self.assertEqual(len(self.history), 10)
1917 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001918
James E. Blaircc020532017-04-19 13:04:44 -07001919 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07001920 def test_noop_job(self):
1921 "Test that the internal noop job works"
1922 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001923 A.addApproval('code-review', 2)
1924 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001925 self.waitUntilSettled()
1926
1927 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1928 self.assertTrue(self.sched._areAllBuildsComplete())
1929 self.assertEqual(len(self.history), 0)
1930 self.assertEqual(A.data['status'], 'MERGED')
1931 self.assertEqual(A.reported, 2)
1932
James E. Blair06cc3922017-04-19 10:08:10 -07001933 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001934 def test_no_job_project(self):
1935 "Test that reports with no jobs don't get sent"
1936 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1937 'master', 'A')
1938 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1939 self.waitUntilSettled()
1940
1941 # Change wasn't reported to
1942 self.assertEqual(A.reported, False)
1943
1944 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001945 tenant = self.sched.abide.tenants.get('tenant-one')
1946 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001947 items = check_pipeline.getAllItems()
1948 self.assertEqual(len(items), 0)
1949
1950 self.assertEqual(len(self.history), 0)
1951
James E. Blair7d0dedc2013-02-21 17:26:09 -08001952 def test_zuul_refs(self):
1953 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04001954 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001955 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1956 M1.setMerged()
1957 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1958 M2.setMerged()
1959
1960 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1961 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1962 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1963 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001964 A.addApproval('code-review', 2)
1965 B.addApproval('code-review', 2)
1966 C.addApproval('code-review', 2)
1967 D.addApproval('code-review', 2)
1968 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1969 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1970 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1971 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001972
1973 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001974 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001975 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001976 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001977 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001978 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001979 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001980 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001981 self.waitUntilSettled()
1982
James E. Blair7d0dedc2013-02-21 17:26:09 -08001983 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08001984 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001985 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001986 if x.parameters['ZUUL_CHANGE'] == '3':
1987 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001988 a_build = x
1989 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001990 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001991 b_build = x
1992 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001993 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001994 c_build = x
1995 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001996 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001997 d_build = x
1998 if a_build and b_build and c_build and d_build:
1999 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002000
2001 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002002 self.assertIsNotNone(a_zref)
2003 self.assertIsNotNone(b_zref)
2004 self.assertIsNotNone(c_zref)
2005 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002006
2007 # And they should all be different
2008 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002009 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002010
Clint Byrum3343e3e2016-11-15 16:05:03 -08002011 # should have a, not b, and should not be in project2
2012 self.assertTrue(a_build.hasChanges(A))
2013 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002014
Clint Byrum3343e3e2016-11-15 16:05:03 -08002015 # should have a and b, and should not be in project2
2016 self.assertTrue(b_build.hasChanges(A, B))
2017 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002018
Clint Byrum3343e3e2016-11-15 16:05:03 -08002019 # should have a and b in 1, c in 2
2020 self.assertTrue(c_build.hasChanges(A, B, C))
2021 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002022
Clint Byrum3343e3e2016-11-15 16:05:03 -08002023 # should have a and b in 1, c and d in 2
2024 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002025
Paul Belanger174a8272017-03-14 13:20:10 -04002026 self.executor_server.hold_jobs_in_build = False
2027 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002028 self.waitUntilSettled()
2029
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002030 self.assertEqual(A.data['status'], 'MERGED')
2031 self.assertEqual(A.reported, 2)
2032 self.assertEqual(B.data['status'], 'MERGED')
2033 self.assertEqual(B.reported, 2)
2034 self.assertEqual(C.data['status'], 'MERGED')
2035 self.assertEqual(C.reported, 2)
2036 self.assertEqual(D.data['status'], 'MERGED')
2037 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002038
James E. Blair4a28a882013-08-23 15:17:33 -07002039 def test_rerun_on_error(self):
2040 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002041 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002042 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002043 A.addApproval('code-review', 2)
2044 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002045 self.waitUntilSettled()
2046
James E. Blair412fba82017-01-26 15:00:50 -08002047 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002048 self.executor_server.hold_jobs_in_build = False
2049 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002050 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002051 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002052 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2053
James E. Blair412e5582013-04-22 15:50:12 -07002054 def test_statsd(self):
2055 "Test each of the statsd methods used in the scheduler"
2056 import extras
2057 statsd = extras.try_import('statsd.statsd')
2058 statsd.incr('test-incr')
2059 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002060 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002061 self.assertReportedStat('test-incr', '1|c')
2062 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002063 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002064
James E. Blairdad52252014-02-07 16:59:17 -08002065 def test_stuck_job_cleanup(self):
2066 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002067
2068 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002069 self.gearman_server.hold_jobs_in_queue = True
2070 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002071 A.addApproval('code-review', 2)
2072 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002073 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002074 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002075 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2076
James E. Blair31cb3e82017-04-20 13:08:56 -07002077 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002078 self.sched.reconfigure(self.config)
2079 self.waitUntilSettled()
2080
James E. Blair18c64442014-03-18 10:14:45 -07002081 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002082 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002083 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002084 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2085 self.assertTrue(self.sched._areAllBuildsComplete())
2086
2087 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002088 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002089 self.assertEqual(self.history[0].result, 'SUCCESS')
2090
James E. Blair879dafb2015-07-17 14:04:49 -07002091 def test_file_head(self):
2092 # This is a regression test for an observed bug. A change
2093 # with a file named "HEAD" in the root directory of the repo
2094 # was processed by a merger. It then was unable to reset the
2095 # repo because of:
2096 # GitCommandError: 'git reset --hard HEAD' returned
2097 # with exit code 128
2098 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2099 # and filename
2100 # Use '--' to separate filenames from revisions'
2101
2102 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002103 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002104 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2105
2106 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2107 self.waitUntilSettled()
2108
2109 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2110 self.waitUntilSettled()
2111
2112 self.assertIn('Build succeeded', A.messages[0])
2113 self.assertIn('Build succeeded', B.messages[0])
2114
James E. Blair70c71582013-03-06 08:50:50 -08002115 def test_file_jobs(self):
2116 "Test that file jobs run only when appropriate"
2117 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002118 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002119 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002120 A.addApproval('code-review', 2)
2121 B.addApproval('code-review', 2)
2122 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2123 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002124 self.waitUntilSettled()
2125
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002126 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002127 if x.name == 'project-testfile']
2128
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002129 self.assertEqual(len(testfile_jobs), 1)
2130 self.assertEqual(testfile_jobs[0].changes, '1,2')
2131 self.assertEqual(A.data['status'], 'MERGED')
2132 self.assertEqual(A.reported, 2)
2133 self.assertEqual(B.data['status'], 'MERGED')
2134 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002135
Clint Byrum3ee39f32016-11-17 23:45:07 -08002136 def _test_irrelevant_files_jobs(self, should_skip):
2137 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002138 if should_skip:
2139 files = {'ignoreme': 'ignored\n'}
2140 else:
2141 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002142
2143 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002144 'master',
2145 'test irrelevant-files',
2146 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002147 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2148 self.waitUntilSettled()
2149
2150 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002151 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002152
2153 if should_skip:
2154 self.assertEqual([], tested_change_ids)
2155 else:
2156 self.assertIn(change.data['number'], tested_change_ids)
2157
James E. Blaircc324192017-04-19 14:36:08 -07002158 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002159 def test_irrelevant_files_match_skips_job(self):
2160 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002161
James E. Blaircc324192017-04-19 14:36:08 -07002162 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002163 def test_irrelevant_files_no_match_runs_job(self):
2164 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002165
James E. Blaircc324192017-04-19 14:36:08 -07002166 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002167 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002168 files = {'ignoreme': 'ignored\n'}
2169
2170 change = self.fake_gerrit.addFakeChange('org/project',
2171 'master',
2172 'test irrelevant-files',
2173 files=files)
2174 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2175 self.waitUntilSettled()
2176
2177 run_jobs = set([build.name for build in self.history])
2178
2179 self.assertEqual(set(['project-test-nomatch-starts-empty',
2180 'project-test-nomatch-starts-full']), run_jobs)
2181
James E. Blairc8a1e052014-02-25 09:29:26 -08002182 def test_queue_names(self):
2183 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002184 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002185 (trusted, project1) = tenant.getProject('org/project1')
2186 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002187 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2188 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2189 self.assertEqual(q1.name, 'integrated')
2190 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002191
James E. Blair64ed6f22013-07-10 14:07:23 -07002192 def test_queue_precedence(self):
2193 "Test that queue precedence works"
2194
2195 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002196 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002197 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2198 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002199 A.addApproval('code-review', 2)
2200 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002201
2202 self.waitUntilSettled()
2203 self.gearman_server.hold_jobs_in_queue = False
2204 self.gearman_server.release()
2205 self.waitUntilSettled()
2206
James E. Blair8de58bd2013-07-18 16:23:33 -07002207 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002208 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002209 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002210 self.waitUntilSettled()
2211
James E. Blair64ed6f22013-07-10 14:07:23 -07002212 self.log.debug(self.history)
2213 self.assertEqual(self.history[0].pipeline, 'gate')
2214 self.assertEqual(self.history[1].pipeline, 'check')
2215 self.assertEqual(self.history[2].pipeline, 'gate')
2216 self.assertEqual(self.history[3].pipeline, 'gate')
2217 self.assertEqual(self.history[4].pipeline, 'check')
2218 self.assertEqual(self.history[5].pipeline, 'check')
2219
Clark Boylana5edbe42014-06-03 16:39:10 -07002220 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002221 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002222 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002223 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002224 A.addApproval('code-review', 2)
2225 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002226 self.waitUntilSettled()
2227
Paul Belanger174a8272017-03-14 13:20:10 -04002228 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002229 self.waitUntilSettled()
2230
James E. Blair1843a552013-07-03 14:19:52 -07002231 port = self.webapp.server.socket.getsockname()[1]
2232
Adam Gandelman77a12c72017-02-03 17:43:43 -08002233 req = urllib.request.Request(
2234 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002235 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002236 headers = f.info()
2237 self.assertIn('Content-Length', headers)
2238 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002239 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2240 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002241 self.assertIn('Access-Control-Allow-Origin', headers)
2242 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002243 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002244 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002245 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002246
Paul Belanger174a8272017-03-14 13:20:10 -04002247 self.executor_server.hold_jobs_in_build = False
2248 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002249 self.waitUntilSettled()
2250
2251 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002252 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002253 for p in data['pipelines']:
2254 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002255 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002256 self.assertEqual(q['window'], 20)
2257 else:
2258 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002259 for head in q['heads']:
2260 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002261 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002262 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002263 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002264 status_jobs.append(job)
2265 self.assertEqual('project-merge', status_jobs[0]['name'])
2266 self.assertEqual('https://server/job/project-merge/0/',
2267 status_jobs[0]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002268 self.assertEqual('https://server/job/project-merge/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002269 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002270 self.assertEqual('project-test1', status_jobs[1]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002271 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002272 status_jobs[1]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002273 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002274 status_jobs[1]['report_url'])
2275
2276 self.assertEqual('project-test2', status_jobs[2]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002277 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002278 status_jobs[2]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002279 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002280 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002281
James E. Blaircdccd972013-07-01 12:10:22 -07002282 def test_live_reconfiguration(self):
2283 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002284 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002285 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002286 A.addApproval('code-review', 2)
2287 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002288 self.waitUntilSettled()
2289
2290 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002291 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002292
Paul Belanger174a8272017-03-14 13:20:10 -04002293 self.executor_server.hold_jobs_in_build = False
2294 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002295 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002296 self.assertEqual(self.getJobFromHistory('project-merge').result,
2297 'SUCCESS')
2298 self.assertEqual(self.getJobFromHistory('project-test1').result,
2299 'SUCCESS')
2300 self.assertEqual(self.getJobFromHistory('project-test2').result,
2301 'SUCCESS')
2302 self.assertEqual(A.data['status'], 'MERGED')
2303 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002304
James E. Blair6bc782d2015-07-17 16:20:21 -07002305 def test_live_reconfiguration_merge_conflict(self):
2306 # A real-world bug: a change in a gate queue has a merge
2307 # conflict and a job is added to its project while it's
2308 # sitting in the queue. The job gets added to the change and
2309 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002310 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002311
2312 # This change is fine. It's here to stop the queue long
2313 # enough for the next change to be subject to the
2314 # reconfiguration, as well as to provide a conflict for the
2315 # next change. This change will succeed and merge.
2316 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002317 A.addPatchset({'conflict': 'A'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002318 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002319
2320 # This change will be in merge conflict. During the
2321 # reconfiguration, we will add a job. We want to make sure
2322 # that doesn't cause it to get stuck.
2323 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002324 B.addPatchset({'conflict': 'B'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002325 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002326
James E. Blair8b5408c2016-08-08 15:37:46 -07002327 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2328 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002329
2330 self.waitUntilSettled()
2331
2332 # No jobs have run yet
2333 self.assertEqual(A.data['status'], 'NEW')
2334 self.assertEqual(A.reported, 1)
2335 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002336 self.assertEqual(len(self.history), 0)
2337
2338 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002339 self.commitConfigUpdate('common-config',
2340 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002341 self.sched.reconfigure(self.config)
2342 self.waitUntilSettled()
2343
Paul Belanger174a8272017-03-14 13:20:10 -04002344 self.executor_server.hold_jobs_in_build = False
2345 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002346 self.waitUntilSettled()
2347
2348 self.assertEqual(A.data['status'], 'MERGED')
2349 self.assertEqual(A.reported, 2)
2350 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002351 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002352 self.assertEqual(self.getJobFromHistory('project-merge').result,
2353 'SUCCESS')
2354 self.assertEqual(self.getJobFromHistory('project-test1').result,
2355 'SUCCESS')
2356 self.assertEqual(self.getJobFromHistory('project-test2').result,
2357 'SUCCESS')
2358 self.assertEqual(self.getJobFromHistory('project-test3').result,
2359 'SUCCESS')
2360 self.assertEqual(len(self.history), 4)
2361
James E. Blair400e8fd2015-07-30 17:44:45 -07002362 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002363 # An extrapolation of test_live_reconfiguration_merge_conflict
2364 # that tests a job added to a job tree with a failed root does
2365 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002366 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002367
2368 # This change is fine. It's here to stop the queue long
2369 # enough for the next change to be subject to the
2370 # reconfiguration. This change will succeed and merge.
2371 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002372 A.addPatchset({'conflict': 'A'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002373 A.addApproval('code-review', 2)
2374 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002375 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002376 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002377 self.waitUntilSettled()
2378
2379 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002380 self.executor_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002381 B.addApproval('code-review', 2)
2382 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002383 self.waitUntilSettled()
2384
Paul Belanger174a8272017-03-14 13:20:10 -04002385 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002386 self.waitUntilSettled()
2387
2388 # Both -merge jobs have run, but no others.
2389 self.assertEqual(A.data['status'], 'NEW')
2390 self.assertEqual(A.reported, 1)
2391 self.assertEqual(B.data['status'], 'NEW')
2392 self.assertEqual(B.reported, 1)
2393 self.assertEqual(self.history[0].result, 'SUCCESS')
2394 self.assertEqual(self.history[0].name, 'project-merge')
2395 self.assertEqual(self.history[1].result, 'FAILURE')
2396 self.assertEqual(self.history[1].name, 'project-merge')
2397 self.assertEqual(len(self.history), 2)
2398
2399 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002400 self.commitConfigUpdate('common-config',
2401 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002402 self.sched.reconfigure(self.config)
2403 self.waitUntilSettled()
2404
Paul Belanger174a8272017-03-14 13:20:10 -04002405 self.executor_server.hold_jobs_in_build = False
2406 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002407 self.waitUntilSettled()
2408
2409 self.assertEqual(A.data['status'], 'MERGED')
2410 self.assertEqual(A.reported, 2)
2411 self.assertEqual(B.data['status'], 'NEW')
2412 self.assertEqual(B.reported, 2)
2413 self.assertEqual(self.history[0].result, 'SUCCESS')
2414 self.assertEqual(self.history[0].name, 'project-merge')
2415 self.assertEqual(self.history[1].result, 'FAILURE')
2416 self.assertEqual(self.history[1].name, 'project-merge')
2417 self.assertEqual(self.history[2].result, 'SUCCESS')
2418 self.assertEqual(self.history[3].result, 'SUCCESS')
2419 self.assertEqual(self.history[4].result, 'SUCCESS')
2420 self.assertEqual(len(self.history), 5)
2421
James E. Blair400e8fd2015-07-30 17:44:45 -07002422 def test_live_reconfiguration_failed_job(self):
2423 # Test that a change with a removed failing job does not
2424 # disrupt reconfiguration. If a change has a failed job and
2425 # that job is removed during a reconfiguration, we observed a
2426 # bug where the code to re-set build statuses would run on
2427 # that build and raise an exception because the job no longer
2428 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002429 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002430
2431 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2432
2433 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002434 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002435
2436 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2437 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002438 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002439 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002440 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002441 self.waitUntilSettled()
2442
2443 self.assertEqual(A.data['status'], 'NEW')
2444 self.assertEqual(A.reported, 0)
2445
2446 self.assertEqual(self.getJobFromHistory('project-merge').result,
2447 'SUCCESS')
2448 self.assertEqual(self.getJobFromHistory('project-test1').result,
2449 'FAILURE')
2450 self.assertEqual(len(self.history), 2)
2451
2452 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002453 self.commitConfigUpdate('common-config',
2454 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002455 self.sched.reconfigure(self.config)
2456 self.waitUntilSettled()
2457
Paul Belanger174a8272017-03-14 13:20:10 -04002458 self.executor_server.hold_jobs_in_build = False
2459 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002460 self.waitUntilSettled()
2461
2462 self.assertEqual(self.getJobFromHistory('project-test2').result,
2463 'SUCCESS')
2464 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2465 'SUCCESS')
2466 self.assertEqual(len(self.history), 4)
2467
2468 self.assertEqual(A.data['status'], 'NEW')
2469 self.assertEqual(A.reported, 1)
2470 self.assertIn('Build succeeded', A.messages[0])
2471 # Ensure the removed job was not included in the report.
2472 self.assertNotIn('project-test1', A.messages[0])
2473
James E. Blairfe707d12015-08-05 15:18:15 -07002474 def test_live_reconfiguration_shared_queue(self):
2475 # Test that a change with a failing job which was removed from
2476 # this project but otherwise still exists in the system does
2477 # not disrupt reconfiguration.
2478
Paul Belanger174a8272017-03-14 13:20:10 -04002479 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002480
2481 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2482
Paul Belanger174a8272017-03-14 13:20:10 -04002483 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002484
2485 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2486 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002487 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002488 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002489 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002490 self.waitUntilSettled()
2491
2492 self.assertEqual(A.data['status'], 'NEW')
2493 self.assertEqual(A.reported, 0)
2494
Clint Byrum69663d42017-05-01 14:51:04 -07002495 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002496 'SUCCESS')
2497 self.assertEqual(self.getJobFromHistory(
2498 'project1-project2-integration').result, 'FAILURE')
2499 self.assertEqual(len(self.history), 2)
2500
2501 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002502 self.commitConfigUpdate(
2503 'common-config',
2504 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002505 self.sched.reconfigure(self.config)
2506 self.waitUntilSettled()
2507
Paul Belanger174a8272017-03-14 13:20:10 -04002508 self.executor_server.hold_jobs_in_build = False
2509 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002510 self.waitUntilSettled()
2511
Clint Byrum69663d42017-05-01 14:51:04 -07002512 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002513 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002514 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002515 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002516 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002517 'SUCCESS')
2518 self.assertEqual(self.getJobFromHistory(
2519 'project1-project2-integration').result, 'FAILURE')
2520 self.assertEqual(len(self.history), 4)
2521
2522 self.assertEqual(A.data['status'], 'NEW')
2523 self.assertEqual(A.reported, 1)
2524 self.assertIn('Build succeeded', A.messages[0])
2525 # Ensure the removed job was not included in the report.
2526 self.assertNotIn('project1-project2-integration', A.messages[0])
2527
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002528 def test_double_live_reconfiguration_shared_queue(self):
2529 # This was a real-world regression. A change is added to
2530 # gate; a reconfigure happens, a second change which depends
2531 # on the first is added, and a second reconfiguration happens.
2532 # Ensure that both changes merge.
2533
2534 # A failure may indicate incorrect caching or cleaning up of
2535 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002536 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002537
2538 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2539 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2540 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002541 A.addApproval('code-review', 2)
2542 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002543
2544 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002545 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002546 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002547 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002548 self.waitUntilSettled()
2549
2550 # Reconfigure (with only one change in the pipeline).
2551 self.sched.reconfigure(self.config)
2552 self.waitUntilSettled()
2553
2554 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002555 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002556 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002557 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002558 self.waitUntilSettled()
2559
2560 # Reconfigure (with both in the pipeline).
2561 self.sched.reconfigure(self.config)
2562 self.waitUntilSettled()
2563
Paul Belanger174a8272017-03-14 13:20:10 -04002564 self.executor_server.hold_jobs_in_build = False
2565 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002566 self.waitUntilSettled()
2567
2568 self.assertEqual(len(self.history), 8)
2569
2570 self.assertEqual(A.data['status'], 'MERGED')
2571 self.assertEqual(A.reported, 2)
2572 self.assertEqual(B.data['status'], 'MERGED')
2573 self.assertEqual(B.reported, 2)
2574
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002575 def test_live_reconfiguration_del_project(self):
2576 # Test project deletion from layout
2577 # while changes are enqueued
2578
Paul Belanger174a8272017-03-14 13:20:10 -04002579 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002580 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2581 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2582 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2583
2584 # A Depends-On: B
2585 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2586 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002587 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002588
2589 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2590 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2591 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002592 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002593 self.waitUntilSettled()
2594 self.assertEqual(len(self.builds), 5)
2595
2596 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002597 self.commitConfigUpdate(
2598 'common-config',
2599 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002600 self.sched.reconfigure(self.config)
2601 self.waitUntilSettled()
2602
2603 # Builds for C aborted, builds for A succeed,
2604 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002605 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002606 self.assertEqual(job_c.changes, '3,1')
2607 self.assertEqual(job_c.result, 'ABORTED')
2608
Paul Belanger174a8272017-03-14 13:20:10 -04002609 self.executor_server.hold_jobs_in_build = False
2610 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002611 self.waitUntilSettled()
2612
Clint Byrum0149ada2017-04-04 12:22:01 -07002613 self.assertEqual(
2614 self.getJobFromHistory('project-test1', 'org/project').changes,
2615 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002616
2617 self.assertEqual(A.data['status'], 'NEW')
2618 self.assertEqual(B.data['status'], 'NEW')
2619 self.assertEqual(C.data['status'], 'NEW')
2620 self.assertEqual(A.reported, 1)
2621 self.assertEqual(B.reported, 0)
2622 self.assertEqual(C.reported, 0)
2623
Clint Byrum0149ada2017-04-04 12:22:01 -07002624 tenant = self.sched.abide.tenants.get('tenant-one')
2625 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002626 self.assertIn('Build succeeded', A.messages[0])
2627
James E. Blairec056492016-07-22 09:45:56 -07002628 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002629 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002630 self.updateConfigLayout(
2631 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002632 self.sched.reconfigure(self.config)
2633
2634 self.init_repo("org/new-project")
2635 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2636
James E. Blair8b5408c2016-08-08 15:37:46 -07002637 A.addApproval('code-review', 2)
2638 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002639 self.waitUntilSettled()
2640 self.assertEqual(self.getJobFromHistory('project-merge').result,
2641 'SUCCESS')
2642 self.assertEqual(self.getJobFromHistory('project-test1').result,
2643 'SUCCESS')
2644 self.assertEqual(self.getJobFromHistory('project-test2').result,
2645 'SUCCESS')
2646 self.assertEqual(A.data['status'], 'MERGED')
2647 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002648
James E. Blair9ea70072017-04-19 16:05:30 -07002649 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002650 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002651 self.init_repo("org/delete-project")
2652 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2653
James E. Blair8b5408c2016-08-08 15:37:46 -07002654 A.addApproval('code-review', 2)
2655 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002656 self.waitUntilSettled()
2657 self.assertEqual(self.getJobFromHistory('project-merge').result,
2658 'SUCCESS')
2659 self.assertEqual(self.getJobFromHistory('project-test1').result,
2660 'SUCCESS')
2661 self.assertEqual(self.getJobFromHistory('project-test2').result,
2662 'SUCCESS')
2663 self.assertEqual(A.data['status'], 'MERGED')
2664 self.assertEqual(A.reported, 2)
2665
2666 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002667 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002668 if os.path.exists(os.path.join(self.merger_src_root, p)):
2669 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002670 if os.path.exists(os.path.join(self.executor_src_root, p)):
2671 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002672
2673 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2674
James E. Blair8b5408c2016-08-08 15:37:46 -07002675 B.addApproval('code-review', 2)
2676 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002677 self.waitUntilSettled()
2678 self.assertEqual(self.getJobFromHistory('project-merge').result,
2679 'SUCCESS')
2680 self.assertEqual(self.getJobFromHistory('project-test1').result,
2681 'SUCCESS')
2682 self.assertEqual(self.getJobFromHistory('project-test2').result,
2683 'SUCCESS')
2684 self.assertEqual(B.data['status'], 'MERGED')
2685 self.assertEqual(B.reported, 2)
2686
James E. Blaire68d43f2017-04-19 14:03:30 -07002687 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002688 def test_tags(self):
2689 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002690 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2691 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2692 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2693 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2694 self.waitUntilSettled()
2695
James E. Blaire68d43f2017-04-19 14:03:30 -07002696 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002697
2698 results = {self.getJobFromHistory('merge',
2699 project='org/project1').uuid: 'extratag merge',
2700 self.getJobFromHistory('merge',
2701 project='org/project2').uuid: 'merge'}
James E. Blair456f2fb2016-02-09 09:29:33 -08002702
2703 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002704 self.assertEqual(results.get(build.uuid, ''),
2705 build.parameters['vars']['zuul'].get('tags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002706
James E. Blair63bb0ef2013-07-29 17:14:51 -07002707 def test_timer(self):
2708 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002709 # This test can not use simple_layout because it must start
2710 # with a configuration which does not include a
2711 # timer-triggered job so that we have an opportunity to set
2712 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002713 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002714 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002715 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002716
Clark Boylan3ee090a2014-04-03 20:55:09 -07002717 # The pipeline triggers every second, so we should have seen
2718 # several by now.
2719 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002720 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002721
2722 self.assertEqual(len(self.builds), 2)
2723
James E. Blair63bb0ef2013-07-29 17:14:51 -07002724 port = self.webapp.server.socket.getsockname()[1]
2725
Clint Byrum5e729e92017-02-08 06:34:57 -08002726 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002727 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002728 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002729 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002730
Paul Belanger174a8272017-03-14 13:20:10 -04002731 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002732 # Stop queuing timer triggered jobs so that the assertions
2733 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002734 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002735 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04002736 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002737 self.waitUntilSettled()
2738
2739 self.assertEqual(self.getJobFromHistory(
2740 'project-bitrot-stable-old').result, 'SUCCESS')
2741 self.assertEqual(self.getJobFromHistory(
2742 'project-bitrot-stable-older').result, 'SUCCESS')
2743
2744 data = json.loads(data)
2745 status_jobs = set()
2746 for p in data['pipelines']:
2747 for q in p['change_queues']:
2748 for head in q['heads']:
2749 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002750 for job in change['jobs']:
2751 status_jobs.add(job['name'])
2752 self.assertIn('project-bitrot-stable-old', status_jobs)
2753 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002754
James E. Blair4f6033c2014-03-27 15:49:09 -07002755 def test_idle(self):
2756 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002757 # This test can not use simple_layout because it must start
2758 # with a configuration which does not include a
2759 # timer-triggered job so that we have an opportunity to set
2760 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002761 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002762
Clark Boylan3ee090a2014-04-03 20:55:09 -07002763 for x in range(1, 3):
2764 # Test that timer triggers periodic jobs even across
2765 # layout config reloads.
2766 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002767 self.commitConfigUpdate('common-config',
2768 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002769 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002770 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002771
Clark Boylan3ee090a2014-04-03 20:55:09 -07002772 # The pipeline triggers every second, so we should have seen
2773 # several by now.
2774 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002775
2776 # Stop queuing timer triggered jobs so that the assertions
2777 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002778 self.commitConfigUpdate('common-config',
2779 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002780 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002781 self.waitUntilSettled()
Clint Byrum58264dc2017-02-07 21:21:22 -08002782 self.assertEqual(len(self.builds), 2,
2783 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002784 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002785 self.waitUntilSettled()
2786 self.assertEqual(len(self.builds), 0)
2787 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002788
James E. Blaircc324192017-04-19 14:36:08 -07002789 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002790 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002791 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2792 self.waitUntilSettled()
2793
2794 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2795 self.waitUntilSettled()
2796
James E. Blairff80a2f2013-12-27 13:24:06 -08002797 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002798
2799 # A.messages only holds what FakeGerrit places in it. Thus we
2800 # work on the knowledge of what the first message should be as
2801 # it is only configured to go to SMTP.
2802
2803 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002804 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002805 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002806 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002807 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002808 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002809
2810 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002811 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002812 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002813 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002814 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002815 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002816
James E. Blaire5910202013-12-27 09:50:31 -08002817 def test_timer_smtp(self):
2818 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002819 # This test can not use simple_layout because it must start
2820 # with a configuration which does not include a
2821 # timer-triggered job so that we have an opportunity to set
2822 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002823 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002824 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002825 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002826
Clark Boylan3ee090a2014-04-03 20:55:09 -07002827 # The pipeline triggers every second, so we should have seen
2828 # several by now.
2829 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002830 self.waitUntilSettled()
2831
Clark Boylan3ee090a2014-04-03 20:55:09 -07002832 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04002833 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002834 self.waitUntilSettled()
2835 self.assertEqual(len(self.history), 2)
2836
James E. Blaire5910202013-12-27 09:50:31 -08002837 self.assertEqual(self.getJobFromHistory(
2838 'project-bitrot-stable-old').result, 'SUCCESS')
2839 self.assertEqual(self.getJobFromHistory(
2840 'project-bitrot-stable-older').result, 'SUCCESS')
2841
James E. Blairff80a2f2013-12-27 13:24:06 -08002842 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002843
2844 # A.messages only holds what FakeGerrit places in it. Thus we
2845 # work on the knowledge of what the first message should be as
2846 # it is only configured to go to SMTP.
2847
2848 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002849 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002850 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002851 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002852 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002853 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002854
Clark Boylan3ee090a2014-04-03 20:55:09 -07002855 # Stop queuing timer triggered jobs and let any that may have
2856 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002857 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002858 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07002859 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002860 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002861 self.waitUntilSettled()
2862
Joshua Hesketh25695cb2017-03-06 12:50:04 +11002863 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00002864 def test_timer_sshkey(self):
2865 "Test that a periodic job can setup SSH key authentication"
2866 self.worker.hold_jobs_in_build = True
2867 self.config.set('zuul', 'layout_config',
2868 'tests/fixtures/layout-timer.yaml')
2869 self.sched.reconfigure(self.config)
2870 self.registerJobs()
2871
2872 # The pipeline triggers every second, so we should have seen
2873 # several by now.
2874 time.sleep(5)
2875 self.waitUntilSettled()
2876
2877 self.assertEqual(len(self.builds), 2)
2878
2879 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
2880 self.assertTrue(os.path.isfile(ssh_wrapper))
2881 with open(ssh_wrapper) as f:
2882 ssh_wrapper_content = f.read()
2883 self.assertIn("fake_id_rsa", ssh_wrapper_content)
2884 # In the unit tests Merger runs in the same process,
2885 # so we see its' environment variables
2886 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
2887
2888 self.worker.release('.*')
2889 self.waitUntilSettled()
2890 self.assertEqual(len(self.history), 2)
2891
2892 self.assertEqual(self.getJobFromHistory(
2893 'project-bitrot-stable-old').result, 'SUCCESS')
2894 self.assertEqual(self.getJobFromHistory(
2895 'project-bitrot-stable-older').result, 'SUCCESS')
2896
2897 # Stop queuing timer triggered jobs and let any that may have
2898 # queued through so that end of test assertions pass.
2899 self.config.set('zuul', 'layout_config',
2900 'tests/fixtures/layout-no-timer.yaml')
2901 self.sched.reconfigure(self.config)
2902 self.registerJobs()
2903 self.waitUntilSettled()
2904 self.worker.release('.*')
2905 self.waitUntilSettled()
2906
James E. Blair91e34592015-07-31 16:45:59 -07002907 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08002908 "Test that the RPC client can enqueue a change"
2909 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002910 A.addApproval('code-review', 2)
2911 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08002912
2913 client = zuul.rpcclient.RPCClient('127.0.0.1',
2914 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002915 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04002916 r = client.enqueue(tenant='tenant-one',
2917 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002918 project='org/project',
2919 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002920 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002921 self.waitUntilSettled()
2922 self.assertEqual(self.getJobFromHistory('project-merge').result,
2923 'SUCCESS')
2924 self.assertEqual(self.getJobFromHistory('project-test1').result,
2925 'SUCCESS')
2926 self.assertEqual(self.getJobFromHistory('project-test2').result,
2927 'SUCCESS')
2928 self.assertEqual(A.data['status'], 'MERGED')
2929 self.assertEqual(A.reported, 2)
2930 self.assertEqual(r, True)
2931
James E. Blair91e34592015-07-31 16:45:59 -07002932 def test_client_enqueue_ref(self):
2933 "Test that the RPC client can enqueue a ref"
2934
2935 client = zuul.rpcclient.RPCClient('127.0.0.1',
2936 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002937 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07002938 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04002939 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07002940 pipeline='post',
2941 project='org/project',
2942 trigger='gerrit',
2943 ref='master',
2944 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
2945 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
2946 self.waitUntilSettled()
2947 job_names = [x.name for x in self.history]
2948 self.assertEqual(len(self.history), 1)
2949 self.assertIn('project-post', job_names)
2950 self.assertEqual(r, True)
2951
James E. Blairad28e912013-11-27 10:43:22 -08002952 def test_client_enqueue_negative(self):
2953 "Test that the RPC client returns errors"
2954 client = zuul.rpcclient.RPCClient('127.0.0.1',
2955 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002956 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08002957 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04002958 "Invalid tenant"):
2959 r = client.enqueue(tenant='tenant-foo',
2960 pipeline='gate',
2961 project='org/project',
2962 trigger='gerrit',
2963 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04002964 self.assertEqual(r, False)
2965
2966 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08002967 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002968 r = client.enqueue(tenant='tenant-one',
2969 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002970 project='project-does-not-exist',
2971 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002972 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002973 self.assertEqual(r, False)
2974
2975 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2976 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002977 r = client.enqueue(tenant='tenant-one',
2978 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08002979 project='org/project',
2980 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002981 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002982 self.assertEqual(r, False)
2983
2984 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2985 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002986 r = client.enqueue(tenant='tenant-one',
2987 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002988 project='org/project',
2989 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08002990 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002991 self.assertEqual(r, False)
2992
2993 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2994 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002995 r = client.enqueue(tenant='tenant-one',
2996 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002997 project='org/project',
2998 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002999 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003000 self.assertEqual(r, False)
3001
3002 self.waitUntilSettled()
3003 self.assertEqual(len(self.history), 0)
3004 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003005
3006 def test_client_promote(self):
3007 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003008 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003009 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3010 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3011 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003012 A.addApproval('code-review', 2)
3013 B.addApproval('code-review', 2)
3014 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003015
James E. Blair8b5408c2016-08-08 15:37:46 -07003016 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3017 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3018 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003019
3020 self.waitUntilSettled()
3021
Paul Belangerbaca3132016-11-04 12:49:54 -04003022 tenant = self.sched.abide.tenants.get('tenant-one')
3023 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003024 enqueue_times = {}
3025 for item in items:
3026 enqueue_times[str(item.change)] = item.enqueue_time
3027
James E. Blair36658cf2013-12-06 17:53:48 -08003028 client = zuul.rpcclient.RPCClient('127.0.0.1',
3029 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003030 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003031 r = client.promote(tenant='tenant-one',
3032 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003033 change_ids=['2,1', '3,1'])
3034
Sean Daguef39b9ca2014-01-10 21:34:35 -05003035 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003036 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003037 for item in items:
3038 self.assertEqual(
3039 enqueue_times[str(item.change)], item.enqueue_time)
3040
James E. Blair78acec92014-02-06 07:11:32 -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()
Paul Belanger174a8272017-03-14 13:20:10 -04003044 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003045 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003046 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003047 self.waitUntilSettled()
3048
3049 self.assertEqual(len(self.builds), 6)
3050 self.assertEqual(self.builds[0].name, 'project-test1')
3051 self.assertEqual(self.builds[1].name, 'project-test2')
3052 self.assertEqual(self.builds[2].name, 'project-test1')
3053 self.assertEqual(self.builds[3].name, 'project-test2')
3054 self.assertEqual(self.builds[4].name, 'project-test1')
3055 self.assertEqual(self.builds[5].name, 'project-test2')
3056
Paul Belangerbaca3132016-11-04 12:49:54 -04003057 self.assertTrue(self.builds[0].hasChanges(B))
3058 self.assertFalse(self.builds[0].hasChanges(A))
3059 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003060
Paul Belangerbaca3132016-11-04 12:49:54 -04003061 self.assertTrue(self.builds[2].hasChanges(B))
3062 self.assertTrue(self.builds[2].hasChanges(C))
3063 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003064
Paul Belangerbaca3132016-11-04 12:49:54 -04003065 self.assertTrue(self.builds[4].hasChanges(B))
3066 self.assertTrue(self.builds[4].hasChanges(C))
3067 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003068
Paul Belanger174a8272017-03-14 13:20:10 -04003069 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003070 self.waitUntilSettled()
3071
3072 self.assertEqual(A.data['status'], 'MERGED')
3073 self.assertEqual(A.reported, 2)
3074 self.assertEqual(B.data['status'], 'MERGED')
3075 self.assertEqual(B.reported, 2)
3076 self.assertEqual(C.data['status'], 'MERGED')
3077 self.assertEqual(C.reported, 2)
3078
James E. Blair36658cf2013-12-06 17:53:48 -08003079 self.assertEqual(r, True)
3080
3081 def test_client_promote_dependent(self):
3082 "Test that the RPC client can promote a dependent change"
3083 # C (depends on B) -> B -> A ; then promote C to get:
3084 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003085 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003086 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3087 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3088 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3089
3090 C.setDependsOn(B, 1)
3091
James E. Blair8b5408c2016-08-08 15:37:46 -07003092 A.addApproval('code-review', 2)
3093 B.addApproval('code-review', 2)
3094 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003095
James E. Blair8b5408c2016-08-08 15:37:46 -07003096 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3097 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3098 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003099
3100 self.waitUntilSettled()
3101
3102 client = zuul.rpcclient.RPCClient('127.0.0.1',
3103 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003104 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003105 r = client.promote(tenant='tenant-one',
3106 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003107 change_ids=['3,1'])
3108
James E. Blair78acec92014-02-06 07:11:32 -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()
Paul Belanger174a8272017-03-14 13:20:10 -04003112 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003113 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003114 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003115 self.waitUntilSettled()
3116
3117 self.assertEqual(len(self.builds), 6)
3118 self.assertEqual(self.builds[0].name, 'project-test1')
3119 self.assertEqual(self.builds[1].name, 'project-test2')
3120 self.assertEqual(self.builds[2].name, 'project-test1')
3121 self.assertEqual(self.builds[3].name, 'project-test2')
3122 self.assertEqual(self.builds[4].name, 'project-test1')
3123 self.assertEqual(self.builds[5].name, 'project-test2')
3124
Paul Belangerbaca3132016-11-04 12:49:54 -04003125 self.assertTrue(self.builds[0].hasChanges(B))
3126 self.assertFalse(self.builds[0].hasChanges(A))
3127 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003128
Paul Belangerbaca3132016-11-04 12:49:54 -04003129 self.assertTrue(self.builds[2].hasChanges(B))
3130 self.assertTrue(self.builds[2].hasChanges(C))
3131 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003132
Paul Belangerbaca3132016-11-04 12:49:54 -04003133 self.assertTrue(self.builds[4].hasChanges(B))
3134 self.assertTrue(self.builds[4].hasChanges(C))
3135 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003136
Paul Belanger174a8272017-03-14 13:20:10 -04003137 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003138 self.waitUntilSettled()
3139
3140 self.assertEqual(A.data['status'], 'MERGED')
3141 self.assertEqual(A.reported, 2)
3142 self.assertEqual(B.data['status'], 'MERGED')
3143 self.assertEqual(B.reported, 2)
3144 self.assertEqual(C.data['status'], 'MERGED')
3145 self.assertEqual(C.reported, 2)
3146
James E. Blair36658cf2013-12-06 17:53:48 -08003147 self.assertEqual(r, True)
3148
3149 def test_client_promote_negative(self):
3150 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003151 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003152 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003153 A.addApproval('code-review', 2)
3154 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003155 self.waitUntilSettled()
3156
3157 client = zuul.rpcclient.RPCClient('127.0.0.1',
3158 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003159 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003160
3161 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003162 r = client.promote(tenant='tenant-one',
3163 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003164 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003165 self.assertEqual(r, False)
3166
3167 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003168 r = client.promote(tenant='tenant-one',
3169 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003170 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003171 self.assertEqual(r, False)
3172
Paul Belanger174a8272017-03-14 13:20:10 -04003173 self.executor_server.hold_jobs_in_build = False
3174 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003175 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003176
James E. Blaircc324192017-04-19 14:36:08 -07003177 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003178 def test_queue_rate_limiting(self):
3179 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003180 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003181 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3182 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3183 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3184
3185 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003186 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003187
James E. Blair8b5408c2016-08-08 15:37:46 -07003188 A.addApproval('code-review', 2)
3189 B.addApproval('code-review', 2)
3190 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003191
James E. Blair8b5408c2016-08-08 15:37:46 -07003192 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3193 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3194 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003195 self.waitUntilSettled()
3196
3197 # Only A and B will have their merge jobs queued because
3198 # window is 2.
3199 self.assertEqual(len(self.builds), 2)
3200 self.assertEqual(self.builds[0].name, 'project-merge')
3201 self.assertEqual(self.builds[1].name, 'project-merge')
3202
James E. Blair732ee862017-04-21 07:49:26 -07003203 # Release the merge jobs one at a time.
3204 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003205 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003206 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003207 self.waitUntilSettled()
3208
3209 # Only A and B will have their test jobs queued because
3210 # window is 2.
3211 self.assertEqual(len(self.builds), 4)
3212 self.assertEqual(self.builds[0].name, 'project-test1')
3213 self.assertEqual(self.builds[1].name, 'project-test2')
3214 self.assertEqual(self.builds[2].name, 'project-test1')
3215 self.assertEqual(self.builds[3].name, 'project-test2')
3216
Paul Belanger174a8272017-03-14 13:20:10 -04003217 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003218 self.waitUntilSettled()
3219
James E. Blaircc324192017-04-19 14:36:08 -07003220 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003221 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003222 # A failed so window is reduced by 1 to 1.
3223 self.assertEqual(queue.window, 1)
3224 self.assertEqual(queue.window_floor, 1)
3225 self.assertEqual(A.data['status'], 'NEW')
3226
3227 # Gate is reset and only B's merge job is queued because
3228 # window shrunk to 1.
3229 self.assertEqual(len(self.builds), 1)
3230 self.assertEqual(self.builds[0].name, 'project-merge')
3231
Paul Belanger174a8272017-03-14 13:20:10 -04003232 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003233 self.waitUntilSettled()
3234
3235 # Only B's test jobs are queued because window is still 1.
3236 self.assertEqual(len(self.builds), 2)
3237 self.assertEqual(self.builds[0].name, 'project-test1')
3238 self.assertEqual(self.builds[1].name, 'project-test2')
3239
Paul Belanger174a8272017-03-14 13:20:10 -04003240 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003241 self.waitUntilSettled()
3242
3243 # B was successfully merged so window is increased to 2.
3244 self.assertEqual(queue.window, 2)
3245 self.assertEqual(queue.window_floor, 1)
3246 self.assertEqual(B.data['status'], 'MERGED')
3247
3248 # Only C is left and its merge job is queued.
3249 self.assertEqual(len(self.builds), 1)
3250 self.assertEqual(self.builds[0].name, 'project-merge')
3251
Paul Belanger174a8272017-03-14 13:20:10 -04003252 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003253 self.waitUntilSettled()
3254
3255 # After successful merge job the test jobs for C are queued.
3256 self.assertEqual(len(self.builds), 2)
3257 self.assertEqual(self.builds[0].name, 'project-test1')
3258 self.assertEqual(self.builds[1].name, 'project-test2')
3259
Paul Belanger174a8272017-03-14 13:20:10 -04003260 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003261 self.waitUntilSettled()
3262
3263 # C successfully merged so window is bumped to 3.
3264 self.assertEqual(queue.window, 3)
3265 self.assertEqual(queue.window_floor, 1)
3266 self.assertEqual(C.data['status'], 'MERGED')
3267
James E. Blaircc324192017-04-19 14:36:08 -07003268 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003269 def test_queue_rate_limiting_dependent(self):
3270 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003271 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003272 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3273 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3274 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3275
3276 B.setDependsOn(A, 1)
3277
Paul Belanger174a8272017-03-14 13:20:10 -04003278 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003279
James E. Blair8b5408c2016-08-08 15:37:46 -07003280 A.addApproval('code-review', 2)
3281 B.addApproval('code-review', 2)
3282 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003283
James E. Blair8b5408c2016-08-08 15:37:46 -07003284 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3285 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3286 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003287 self.waitUntilSettled()
3288
3289 # Only A and B will have their merge jobs queued because
3290 # window is 2.
3291 self.assertEqual(len(self.builds), 2)
3292 self.assertEqual(self.builds[0].name, 'project-merge')
3293 self.assertEqual(self.builds[1].name, 'project-merge')
3294
James E. Blairf4a5f022017-04-18 14:01:10 -07003295 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003296
3297 # Only A and B will have their test jobs queued because
3298 # window is 2.
3299 self.assertEqual(len(self.builds), 4)
3300 self.assertEqual(self.builds[0].name, 'project-test1')
3301 self.assertEqual(self.builds[1].name, 'project-test2')
3302 self.assertEqual(self.builds[2].name, 'project-test1')
3303 self.assertEqual(self.builds[3].name, 'project-test2')
3304
Paul Belanger174a8272017-03-14 13:20:10 -04003305 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003306 self.waitUntilSettled()
3307
James E. Blaircc324192017-04-19 14:36:08 -07003308 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003309 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003310 # A failed so window is reduced by 1 to 1.
3311 self.assertEqual(queue.window, 1)
3312 self.assertEqual(queue.window_floor, 1)
3313 self.assertEqual(A.data['status'], 'NEW')
3314 self.assertEqual(B.data['status'], 'NEW')
3315
3316 # Gate is reset and only C's merge job is queued because
3317 # window shrunk to 1 and A and B were dequeued.
3318 self.assertEqual(len(self.builds), 1)
3319 self.assertEqual(self.builds[0].name, 'project-merge')
3320
James E. Blairf4a5f022017-04-18 14:01:10 -07003321 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003322
3323 # Only C's test jobs are queued because window is still 1.
3324 self.assertEqual(len(self.builds), 2)
3325 self.assertEqual(self.builds[0].name, 'project-test1')
3326 self.assertEqual(self.builds[1].name, 'project-test2')
3327
Paul Belanger174a8272017-03-14 13:20:10 -04003328 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003329 self.waitUntilSettled()
3330
3331 # C was successfully merged so window is increased to 2.
3332 self.assertEqual(queue.window, 2)
3333 self.assertEqual(queue.window_floor, 1)
3334 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003335
James E. Blairec056492016-07-22 09:45:56 -07003336 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003337 def test_worker_update_metadata(self):
3338 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003339 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003340
3341 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003342 A.addApproval('code-review', 2)
3343 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003344 self.waitUntilSettled()
3345
Paul Belanger174a8272017-03-14 13:20:10 -04003346 self.assertEqual(len(self.executor.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003347
3348 self.log.debug('Current builds:')
Paul Belanger174a8272017-03-14 13:20:10 -04003349 self.log.debug(self.executor.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003350
3351 start = time.time()
3352 while True:
3353 if time.time() - start > 10:
3354 raise Exception("Timeout waiting for gearman server to report "
3355 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003356 build = list(self.executor.builds.values())[0]
Joshua Heskethba8776a2014-01-12 14:35:40 +08003357 if build.worker.name == "My Worker":
3358 break
3359 else:
3360 time.sleep(0)
3361
3362 self.log.debug(build)
3363 self.assertEqual("My Worker", build.worker.name)
3364 self.assertEqual("localhost", build.worker.hostname)
3365 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3366 self.assertEqual("zuul.example.org", build.worker.fqdn)
3367 self.assertEqual("FakeBuilder", build.worker.program)
3368 self.assertEqual("v1.1", build.worker.version)
3369 self.assertEqual({'something': 'else'}, build.worker.extra)
3370
Paul Belanger174a8272017-03-14 13:20:10 -04003371 self.executor_server.hold_jobs_in_build = False
3372 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003373 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003374
James E. Blaircc324192017-04-19 14:36:08 -07003375 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003376 def test_footer_message(self):
3377 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003378 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003379 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003380 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003381 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003382 self.waitUntilSettled()
3383
3384 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003385 B.addApproval('code-review', 2)
3386 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003387 self.waitUntilSettled()
3388
3389 self.assertEqual(2, len(self.smtp_messages))
3390
Adam Gandelman82258fd2017-02-07 16:51:58 -08003391 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003392Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003393http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003394
Adam Gandelman82258fd2017-02-07 16:51:58 -08003395 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003396For CI problems and help debugging, contact ci@example.org"""
3397
Adam Gandelman82258fd2017-02-07 16:51:58 -08003398 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3399 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3400 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3401 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003402
Tobias Henkel9842bd72017-05-16 13:40:03 +02003403 @simple_layout('layouts/unmanaged-project.yaml')
3404 def test_unmanaged_project_start_message(self):
3405 "Test start reporting is not done for unmanaged projects."
3406 self.init_repo("org/project", tag='init')
3407 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3408 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3409 self.waitUntilSettled()
3410
3411 self.assertEqual(0, len(A.messages))
3412
James E. Blairec056492016-07-22 09:45:56 -07003413 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003414 def test_merge_failure_reporters(self):
3415 """Check that the config is set up correctly"""
3416
James E. Blairf84026c2015-12-08 16:11:46 -08003417 self.updateConfigLayout(
3418 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003419 self.sched.reconfigure(self.config)
3420 self.registerJobs()
3421
3422 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003423 "Merge Failed.\n\nThis change or one of its cross-repo "
3424 "dependencies was unable to be automatically merged with the "
3425 "current state of its repository. Please rebase the change and "
3426 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003427 self.sched.layout.pipelines['check'].merge_failure_message)
3428 self.assertEqual(
3429 "The merge failed! For more information...",
3430 self.sched.layout.pipelines['gate'].merge_failure_message)
3431
3432 self.assertEqual(
3433 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3434 self.assertEqual(
3435 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3436
3437 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003438 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3439 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003440
3441 self.assertTrue(
3442 (
3443 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003444 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003445 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003446 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003447 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003448 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003449 ) or (
3450 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003451 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003452 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003453 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003454 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003455 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003456 )
3457 )
3458
James E. Blairec056492016-07-22 09:45:56 -07003459 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003460 def test_merge_failure_reports(self):
3461 """Check that when a change fails to merge the correct message is sent
3462 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003463 self.updateConfigLayout(
3464 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003465 self.sched.reconfigure(self.config)
3466 self.registerJobs()
3467
3468 # Check a test failure isn't reported to SMTP
3469 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003470 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003471 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003472 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003473 self.waitUntilSettled()
3474
3475 self.assertEqual(3, len(self.history)) # 3 jobs
3476 self.assertEqual(0, len(self.smtp_messages))
3477
3478 # Check a merge failure is reported to SMTP
3479 # B should be merged, but C will conflict with B
3480 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3481 B.addPatchset(['conflict'])
3482 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3483 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003484 B.addApproval('code-review', 2)
3485 C.addApproval('code-review', 2)
3486 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3487 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003488 self.waitUntilSettled()
3489
3490 self.assertEqual(6, len(self.history)) # A and B jobs
3491 self.assertEqual(1, len(self.smtp_messages))
3492 self.assertEqual('The merge failed! For more information...',
3493 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003494
James E. Blairec056492016-07-22 09:45:56 -07003495 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003496 def test_default_merge_failure_reports(self):
3497 """Check that the default merge failure reports are correct."""
3498
3499 # A should report success, B should report merge failure.
3500 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3501 A.addPatchset(['conflict'])
3502 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3503 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003504 A.addApproval('code-review', 2)
3505 B.addApproval('code-review', 2)
3506 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3507 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003508 self.waitUntilSettled()
3509
3510 self.assertEqual(3, len(self.history)) # A jobs
3511 self.assertEqual(A.reported, 2)
3512 self.assertEqual(B.reported, 2)
3513 self.assertEqual(A.data['status'], 'MERGED')
3514 self.assertEqual(B.data['status'], 'NEW')
3515 self.assertIn('Build succeeded', A.messages[1])
3516 self.assertIn('Merge Failed', B.messages[1])
3517 self.assertIn('automatically merged', B.messages[1])
3518 self.assertNotIn('logs.example.com', B.messages[1])
3519 self.assertNotIn('SKIPPED', B.messages[1])
3520
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003521 def test_client_get_running_jobs(self):
3522 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003523 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003524 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003525 A.addApproval('code-review', 2)
3526 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003527 self.waitUntilSettled()
3528
3529 client = zuul.rpcclient.RPCClient('127.0.0.1',
3530 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003531 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003532
3533 # Wait for gearman server to send the initial workData back to zuul
3534 start = time.time()
3535 while True:
3536 if time.time() - start > 10:
3537 raise Exception("Timeout waiting for gearman server to report "
3538 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003539 build = list(self.executor_client.builds.values())[0]
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003540 if build.worker.name == "My Worker":
3541 break
3542 else:
3543 time.sleep(0)
3544
3545 running_items = client.get_running_jobs()
3546
3547 self.assertEqual(1, len(running_items))
3548 running_item = running_items[0]
3549 self.assertEqual([], running_item['failing_reasons'])
3550 self.assertEqual([], running_item['items_behind'])
3551 self.assertEqual('https://hostname/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003552 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003553 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003554 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003555 self.assertEqual(True, running_item['active'])
3556 self.assertEqual('1,1', running_item['id'])
3557
3558 self.assertEqual(3, len(running_item['jobs']))
3559 for job in running_item['jobs']:
3560 if job['name'] == 'project-merge':
3561 self.assertEqual('project-merge', job['name'])
3562 self.assertEqual('gate', job['pipeline'])
3563 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003564 self.assertEqual('https://server/job/project-merge/0/',
3565 job['url'])
3566 self.assertEqual(7, len(job['worker']))
3567 self.assertEqual(False, job['canceled'])
3568 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003569 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003570 self.assertEqual('gate', job['pipeline'])
3571 break
3572
Paul Belanger174a8272017-03-14 13:20:10 -04003573 self.executor_server.hold_jobs_in_build = False
3574 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003575 self.waitUntilSettled()
3576
3577 running_items = client.get_running_jobs()
3578 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003579
James E. Blairb70e55a2017-04-19 12:57:02 -07003580 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003581 def test_nonvoting_pipeline(self):
3582 "Test that a nonvoting pipeline (experimental) can still report"
3583
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003584 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3585 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003586 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3587 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003588
3589 self.assertEqual(self.getJobFromHistory('project-merge').result,
3590 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003591 self.assertEqual(
3592 self.getJobFromHistory('experimental-project-test').result,
3593 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003594 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003595
3596 def test_crd_gate(self):
3597 "Test cross-repo dependencies"
3598 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3599 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003600 A.addApproval('code-review', 2)
3601 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003602
3603 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3604 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3605 AM2.setMerged()
3606 AM1.setMerged()
3607
3608 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3609 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3610 BM2.setMerged()
3611 BM1.setMerged()
3612
3613 # A -> AM1 -> AM2
3614 # B -> BM1 -> BM2
3615 # A Depends-On: B
3616 # M2 is here to make sure it is never queried. If it is, it
3617 # means zuul is walking down the entire history of merged
3618 # changes.
3619
3620 B.setDependsOn(BM1, 1)
3621 BM1.setDependsOn(BM2, 1)
3622
3623 A.setDependsOn(AM1, 1)
3624 AM1.setDependsOn(AM2, 1)
3625
3626 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3627 A.subject, B.data['id'])
3628
James E. Blair8b5408c2016-08-08 15:37:46 -07003629 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003630 self.waitUntilSettled()
3631
3632 self.assertEqual(A.data['status'], 'NEW')
3633 self.assertEqual(B.data['status'], 'NEW')
3634
Paul Belanger5dccbe72016-11-14 11:17:37 -05003635 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003636 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003637
Paul Belanger174a8272017-03-14 13:20:10 -04003638 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003639 B.addApproval('approved', 1)
3640 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003641 self.waitUntilSettled()
3642
Paul Belanger174a8272017-03-14 13:20:10 -04003643 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003644 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003645 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003646 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003647 self.executor_server.hold_jobs_in_build = False
3648 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003649 self.waitUntilSettled()
3650
3651 self.assertEqual(AM2.queried, 0)
3652 self.assertEqual(BM2.queried, 0)
3653 self.assertEqual(A.data['status'], 'MERGED')
3654 self.assertEqual(B.data['status'], 'MERGED')
3655 self.assertEqual(A.reported, 2)
3656 self.assertEqual(B.reported, 2)
3657
Paul Belanger5dccbe72016-11-14 11:17:37 -05003658 changes = self.getJobFromHistory(
3659 'project-merge', 'org/project1').changes
3660 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003661
3662 def test_crd_branch(self):
3663 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003664
3665 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003666 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3667 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3668 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3669 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003670 A.addApproval('code-review', 2)
3671 B.addApproval('code-review', 2)
3672 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003673
3674 # A Depends-On: B+C
3675 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3676 A.subject, B.data['id'])
3677
Paul Belanger174a8272017-03-14 13:20:10 -04003678 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003679 B.addApproval('approved', 1)
3680 C.addApproval('approved', 1)
3681 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003682 self.waitUntilSettled()
3683
Paul Belanger174a8272017-03-14 13:20:10 -04003684 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003685 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003686 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003687 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003688 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003689 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003690 self.executor_server.hold_jobs_in_build = False
3691 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003692 self.waitUntilSettled()
3693
3694 self.assertEqual(A.data['status'], 'MERGED')
3695 self.assertEqual(B.data['status'], 'MERGED')
3696 self.assertEqual(C.data['status'], 'MERGED')
3697 self.assertEqual(A.reported, 2)
3698 self.assertEqual(B.reported, 2)
3699 self.assertEqual(C.reported, 2)
3700
Paul Belanger6379db12016-11-14 13:57:54 -05003701 changes = self.getJobFromHistory(
3702 'project-merge', 'org/project1').changes
3703 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003704
3705 def test_crd_multiline(self):
3706 "Test multiple depends-on lines in commit"
3707 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3708 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3709 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003710 A.addApproval('code-review', 2)
3711 B.addApproval('code-review', 2)
3712 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003713
3714 # A Depends-On: B+C
3715 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3716 A.subject, B.data['id'], C.data['id'])
3717
Paul Belanger174a8272017-03-14 13:20:10 -04003718 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003719 B.addApproval('approved', 1)
3720 C.addApproval('approved', 1)
3721 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003722 self.waitUntilSettled()
3723
Paul Belanger174a8272017-03-14 13:20:10 -04003724 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003725 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003726 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003727 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003728 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003729 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003730 self.executor_server.hold_jobs_in_build = False
3731 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003732 self.waitUntilSettled()
3733
3734 self.assertEqual(A.data['status'], 'MERGED')
3735 self.assertEqual(B.data['status'], 'MERGED')
3736 self.assertEqual(C.data['status'], 'MERGED')
3737 self.assertEqual(A.reported, 2)
3738 self.assertEqual(B.reported, 2)
3739 self.assertEqual(C.reported, 2)
3740
Paul Belanger5dccbe72016-11-14 11:17:37 -05003741 changes = self.getJobFromHistory(
3742 'project-merge', 'org/project1').changes
3743 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003744
3745 def test_crd_unshared_gate(self):
3746 "Test cross-repo dependencies in unshared gate queues"
3747 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3748 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003749 A.addApproval('code-review', 2)
3750 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003751
3752 # A Depends-On: B
3753 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3754 A.subject, B.data['id'])
3755
3756 # A and B do not share a queue, make sure that A is unable to
3757 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003758 B.addApproval('approved', 1)
3759 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003760 self.waitUntilSettled()
3761
3762 self.assertEqual(A.data['status'], 'NEW')
3763 self.assertEqual(B.data['status'], 'NEW')
3764 self.assertEqual(A.reported, 0)
3765 self.assertEqual(B.reported, 0)
3766 self.assertEqual(len(self.history), 0)
3767
3768 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003769 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003770 self.waitUntilSettled()
3771
3772 self.assertEqual(B.data['status'], 'MERGED')
3773 self.assertEqual(B.reported, 2)
3774
3775 # Now that B is merged, A should be able to be enqueued and
3776 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003777 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003778 self.waitUntilSettled()
3779
3780 self.assertEqual(A.data['status'], 'MERGED')
3781 self.assertEqual(A.reported, 2)
3782
James E. Blair96698e22015-04-02 07:48:21 -07003783 def test_crd_gate_reverse(self):
3784 "Test reverse cross-repo dependencies"
3785 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3786 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003787 A.addApproval('code-review', 2)
3788 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003789
3790 # A Depends-On: B
3791
3792 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3793 A.subject, B.data['id'])
3794
James E. Blair8b5408c2016-08-08 15:37:46 -07003795 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003796 self.waitUntilSettled()
3797
3798 self.assertEqual(A.data['status'], 'NEW')
3799 self.assertEqual(B.data['status'], 'NEW')
3800
Paul Belanger174a8272017-03-14 13:20:10 -04003801 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003802 A.addApproval('approved', 1)
3803 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003804 self.waitUntilSettled()
3805
Paul Belanger174a8272017-03-14 13:20:10 -04003806 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003807 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003808 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003809 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003810 self.executor_server.hold_jobs_in_build = False
3811 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003812 self.waitUntilSettled()
3813
3814 self.assertEqual(A.data['status'], 'MERGED')
3815 self.assertEqual(B.data['status'], 'MERGED')
3816 self.assertEqual(A.reported, 2)
3817 self.assertEqual(B.reported, 2)
3818
Paul Belanger5dccbe72016-11-14 11:17:37 -05003819 changes = self.getJobFromHistory(
3820 'project-merge', 'org/project1').changes
3821 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003822
James E. Blair5ee24252014-12-30 10:12:29 -08003823 def test_crd_cycle(self):
3824 "Test cross-repo dependency cycles"
3825 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3826 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003827 A.addApproval('code-review', 2)
3828 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003829
3830 # A -> B -> A (via commit-depends)
3831
3832 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3833 A.subject, B.data['id'])
3834 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3835 B.subject, A.data['id'])
3836
James E. Blair8b5408c2016-08-08 15:37:46 -07003837 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003838 self.waitUntilSettled()
3839
3840 self.assertEqual(A.reported, 0)
3841 self.assertEqual(B.reported, 0)
3842 self.assertEqual(A.data['status'], 'NEW')
3843 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003844
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003845 def test_crd_gate_unknown(self):
3846 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07003847 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003848 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3849 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003850 A.addApproval('code-review', 2)
3851 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003852
3853 # A Depends-On: B
3854 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3855 A.subject, B.data['id'])
3856
James E. Blair8b5408c2016-08-08 15:37:46 -07003857 B.addApproval('approved', 1)
3858 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003859 self.waitUntilSettled()
3860
3861 # Unknown projects cannot share a queue with any other
3862 # since they don't have common jobs with any other (they have no jobs).
3863 # Changes which depend on unknown project changes
3864 # should not be processed in dependent pipeline
3865 self.assertEqual(A.data['status'], 'NEW')
3866 self.assertEqual(B.data['status'], 'NEW')
3867 self.assertEqual(A.reported, 0)
3868 self.assertEqual(B.reported, 0)
3869 self.assertEqual(len(self.history), 0)
3870
3871 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07003872 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003873 B.setMerged()
3874 self.waitUntilSettled()
3875 self.assertEqual(len(self.history), 0)
3876
3877 # Now that B is merged, A should be able to be enqueued and
3878 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003879 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003880 self.waitUntilSettled()
3881
3882 self.assertEqual(A.data['status'], 'MERGED')
3883 self.assertEqual(A.reported, 2)
3884 self.assertEqual(B.data['status'], 'MERGED')
3885 self.assertEqual(B.reported, 0)
3886
James E. Blairbfb8e042014-12-30 17:01:44 -08003887 def test_crd_check(self):
3888 "Test cross-repo dependencies in independent pipelines"
3889
Paul Belanger174a8272017-03-14 13:20:10 -04003890 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08003891 self.gearman_server.hold_jobs_in_queue = True
3892 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3893 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3894
3895 # A Depends-On: B
3896 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3897 A.subject, B.data['id'])
3898
3899 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3900 self.waitUntilSettled()
3901
James E. Blairbfb8e042014-12-30 17:01:44 -08003902 self.gearman_server.hold_jobs_in_queue = False
3903 self.gearman_server.release()
3904 self.waitUntilSettled()
3905
Paul Belanger174a8272017-03-14 13:20:10 -04003906 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05003907 self.waitUntilSettled()
3908
James E. Blair2a535672017-04-27 12:03:15 -07003909 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08003910
Paul Belanger174a8272017-03-14 13:20:10 -04003911 self.executor_server.hold_jobs_in_build = False
3912 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05003913 self.waitUntilSettled()
3914
James E. Blairbfb8e042014-12-30 17:01:44 -08003915 self.assertEqual(A.data['status'], 'NEW')
3916 self.assertEqual(B.data['status'], 'NEW')
3917 self.assertEqual(A.reported, 1)
3918 self.assertEqual(B.reported, 0)
3919
3920 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05003921 tenant = self.sched.abide.tenants.get('tenant-one')
3922 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08003923
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003924 def test_crd_check_git_depends(self):
3925 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07003926 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003927 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3928 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3929
3930 # Add two git-dependent changes and make sure they both report
3931 # success.
3932 B.setDependsOn(A, 1)
3933 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3934 self.waitUntilSettled()
3935 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3936 self.waitUntilSettled()
3937
James E. Blairb8c16472015-05-05 14:55:26 -07003938 self.orderedRelease()
3939 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003940 self.waitUntilSettled()
3941
3942 self.assertEqual(A.data['status'], 'NEW')
3943 self.assertEqual(B.data['status'], 'NEW')
3944 self.assertEqual(A.reported, 1)
3945 self.assertEqual(B.reported, 1)
3946
3947 self.assertEqual(self.history[0].changes, '1,1')
3948 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04003949 tenant = self.sched.abide.tenants.get('tenant-one')
3950 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003951
3952 self.assertIn('Build succeeded', A.messages[0])
3953 self.assertIn('Build succeeded', B.messages[0])
3954
3955 def test_crd_check_duplicate(self):
3956 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04003957 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003958 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3959 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04003960 tenant = self.sched.abide.tenants.get('tenant-one')
3961 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003962
3963 # Add two git-dependent changes...
3964 B.setDependsOn(A, 1)
3965 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3966 self.waitUntilSettled()
3967 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3968
3969 # ...make sure the live one is not duplicated...
3970 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3971 self.waitUntilSettled()
3972 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3973
3974 # ...but the non-live one is able to be.
3975 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3976 self.waitUntilSettled()
3977 self.assertEqual(len(check_pipeline.getAllItems()), 3)
3978
Clark Boylandd849822015-03-02 12:38:14 -08003979 # Release jobs in order to avoid races with change A jobs
3980 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07003981 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04003982 self.executor_server.hold_jobs_in_build = False
3983 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003984 self.waitUntilSettled()
3985
3986 self.assertEqual(A.data['status'], 'NEW')
3987 self.assertEqual(B.data['status'], 'NEW')
3988 self.assertEqual(A.reported, 1)
3989 self.assertEqual(B.reported, 1)
3990
3991 self.assertEqual(self.history[0].changes, '1,1 2,1')
3992 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04003993 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003994
3995 self.assertIn('Build succeeded', A.messages[0])
3996 self.assertIn('Build succeeded', B.messages[0])
3997
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003998 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08003999 "Test cross-repo dependencies re-enqueued in independent pipelines"
4000
4001 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004002 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4003 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004004
4005 # A Depends-On: B
4006 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4007 A.subject, B.data['id'])
4008
4009 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4010 self.waitUntilSettled()
4011
4012 self.sched.reconfigure(self.config)
4013
4014 # Make sure the items still share a change queue, and the
4015 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004016 tenant = self.sched.abide.tenants.get('tenant-one')
4017 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4018 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004019 first_item = queue.queue[0]
4020 for item in queue.queue:
4021 self.assertEqual(item.queue, first_item.queue)
4022 self.assertFalse(first_item.live)
4023 self.assertTrue(queue.queue[1].live)
4024
4025 self.gearman_server.hold_jobs_in_queue = False
4026 self.gearman_server.release()
4027 self.waitUntilSettled()
4028
4029 self.assertEqual(A.data['status'], 'NEW')
4030 self.assertEqual(B.data['status'], 'NEW')
4031 self.assertEqual(A.reported, 1)
4032 self.assertEqual(B.reported, 0)
4033
4034 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004035 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004036
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004037 def test_crd_check_reconfiguration(self):
4038 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4039
4040 def test_crd_undefined_project(self):
4041 """Test that undefined projects in dependencies are handled for
4042 independent pipelines"""
4043 # It's a hack for fake gerrit,
4044 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004045 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004046 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4047
Jesse Keatingea68ab42017-04-19 15:03:30 -07004048 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004049 def test_crd_check_ignore_dependencies(self):
4050 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004051
4052 self.gearman_server.hold_jobs_in_queue = True
4053 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4054 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4055 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4056
4057 # A Depends-On: B
4058 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4059 A.subject, B.data['id'])
4060 # C git-depends on B
4061 C.setDependsOn(B, 1)
4062 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4063 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4064 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4065 self.waitUntilSettled()
4066
4067 # Make sure none of the items share a change queue, and all
4068 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004069 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004070 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004071 self.assertEqual(len(check_pipeline.queues), 3)
4072 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4073 for item in check_pipeline.getAllItems():
4074 self.assertTrue(item.live)
4075
4076 self.gearman_server.hold_jobs_in_queue = False
4077 self.gearman_server.release()
4078 self.waitUntilSettled()
4079
4080 self.assertEqual(A.data['status'], 'NEW')
4081 self.assertEqual(B.data['status'], 'NEW')
4082 self.assertEqual(C.data['status'], 'NEW')
4083 self.assertEqual(A.reported, 1)
4084 self.assertEqual(B.reported, 1)
4085 self.assertEqual(C.reported, 1)
4086
4087 # Each job should have tested exactly one change
4088 for job in self.history:
4089 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004090
James E. Blair7cb84542017-04-19 13:35:05 -07004091 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004092 def test_crd_check_transitive(self):
4093 "Test transitive cross-repo dependencies"
4094 # Specifically, if A -> B -> C, and C gets a new patchset and
4095 # A gets a new patchset, ensure the test of A,2 includes B,1
4096 # and C,2 (not C,1 which would indicate stale data in the
4097 # cache for B).
4098 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4099 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4100 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4101
4102 # A Depends-On: B
4103 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4104 A.subject, B.data['id'])
4105
4106 # B Depends-On: C
4107 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4108 B.subject, C.data['id'])
4109
4110 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4111 self.waitUntilSettled()
4112 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4113
4114 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4115 self.waitUntilSettled()
4116 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4117
4118 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4119 self.waitUntilSettled()
4120 self.assertEqual(self.history[-1].changes, '3,1')
4121
4122 C.addPatchset()
4123 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4124 self.waitUntilSettled()
4125 self.assertEqual(self.history[-1].changes, '3,2')
4126
4127 A.addPatchset()
4128 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4129 self.waitUntilSettled()
4130 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004131
James E. Blaira8b90b32016-08-24 15:18:50 -07004132 def test_crd_check_unknown(self):
4133 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004134 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004135 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4136 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4137 # A Depends-On: B
4138 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4139 A.subject, B.data['id'])
4140
4141 # Make sure zuul has seen an event on B.
4142 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4143 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4144 self.waitUntilSettled()
4145
4146 self.assertEqual(A.data['status'], 'NEW')
4147 self.assertEqual(A.reported, 1)
4148 self.assertEqual(B.data['status'], 'NEW')
4149 self.assertEqual(B.reported, 0)
4150
James E. Blair92464a22016-04-05 10:21:26 -07004151 def test_crd_cycle_join(self):
4152 "Test an updated change creates a cycle"
4153 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4154
4155 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4156 self.waitUntilSettled()
4157
4158 # Create B->A
4159 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4160 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4161 B.subject, A.data['id'])
4162 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4163 self.waitUntilSettled()
4164
4165 # Update A to add A->B (a cycle).
4166 A.addPatchset()
4167 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4168 A.subject, B.data['id'])
4169 # Normally we would submit the patchset-created event for
4170 # processing here, however, we have no way of noting whether
4171 # the dependency cycle detection correctly raised an
4172 # exception, so instead, we reach into the source driver and
4173 # call the method that would ultimately be called by the event
4174 # processing.
4175
Paul Belanger0e155e22016-11-14 14:12:23 -05004176 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07004177 (trusted, project) = tenant.getProject('org/project')
4178 source = project.source
Paul Belanger0e155e22016-11-14 14:12:23 -05004179
4180 # TODO(pabelanger): As we add more source / trigger APIs we should make
4181 # it easier for users to create events for testing.
4182 event = zuul.model.TriggerEvent()
4183 event.trigger_name = 'gerrit'
4184 event.change_number = '1'
4185 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004186 with testtools.ExpectedException(
4187 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004188 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004189 self.log.debug("Got expected dependency cycle exception")
4190
4191 # Now if we update B to remove the depends-on, everything
4192 # should be okay. B; A->B
4193
4194 B.addPatchset()
4195 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004196
4197 source.getChange(event, True)
4198 event.change_number = '2'
4199 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004200
Jesse Keatingcc67b122017-04-19 14:54:57 -07004201 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004202 def test_disable_at(self):
4203 "Test a pipeline will only report to the disabled trigger when failing"
4204
Jesse Keatingcc67b122017-04-19 14:54:57 -07004205 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004206 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004207 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004208 0, tenant.layout.pipelines['check']._consecutive_failures)
4209 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004210
4211 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4212 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4213 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4214 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4215 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4216 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4217 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4218 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4219 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4220 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4221 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4222
Paul Belanger174a8272017-03-14 13:20:10 -04004223 self.executor_server.failJob('project-test1', A)
4224 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004225 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004226 self.executor_server.failJob('project-test1', D)
4227 self.executor_server.failJob('project-test1', E)
4228 self.executor_server.failJob('project-test1', F)
4229 self.executor_server.failJob('project-test1', G)
4230 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004231 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004232 self.executor_server.failJob('project-test1', J)
4233 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004234
4235 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4236 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4237 self.waitUntilSettled()
4238
4239 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004240 2, tenant.layout.pipelines['check']._consecutive_failures)
4241 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004242
4243 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4244 self.waitUntilSettled()
4245
4246 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004247 0, tenant.layout.pipelines['check']._consecutive_failures)
4248 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004249
4250 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4251 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4252 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4253 self.waitUntilSettled()
4254
4255 # We should be disabled now
4256 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004257 3, tenant.layout.pipelines['check']._consecutive_failures)
4258 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004259
4260 # We need to wait between each of these patches to make sure the
4261 # smtp messages come back in an expected order
4262 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4263 self.waitUntilSettled()
4264 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4265 self.waitUntilSettled()
4266 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4267 self.waitUntilSettled()
4268
4269 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4270 # leaving a message on each change
4271 self.assertEqual(1, len(A.messages))
4272 self.assertIn('Build failed.', A.messages[0])
4273 self.assertEqual(1, len(B.messages))
4274 self.assertIn('Build failed.', B.messages[0])
4275 self.assertEqual(1, len(C.messages))
4276 self.assertIn('Build succeeded.', C.messages[0])
4277 self.assertEqual(1, len(D.messages))
4278 self.assertIn('Build failed.', D.messages[0])
4279 self.assertEqual(1, len(E.messages))
4280 self.assertIn('Build failed.', E.messages[0])
4281 self.assertEqual(1, len(F.messages))
4282 self.assertIn('Build failed.', F.messages[0])
4283
4284 # The last 3 (GHI) would have only reported via smtp.
4285 self.assertEqual(3, len(self.smtp_messages))
4286 self.assertEqual(0, len(G.messages))
4287 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004288 self.assertIn(
4289 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004290 self.assertEqual(0, len(H.messages))
4291 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004292 self.assertIn(
4293 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004294 self.assertEqual(0, len(I.messages))
4295 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004296 self.assertIn(
4297 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004298
4299 # Now reload the configuration (simulate a HUP) to check the pipeline
4300 # comes out of disabled
4301 self.sched.reconfigure(self.config)
4302
Jesse Keatingcc67b122017-04-19 14:54:57 -07004303 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004304
4305 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004306 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004307 0, tenant.layout.pipelines['check']._consecutive_failures)
4308 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004309
4310 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4311 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4312 self.waitUntilSettled()
4313
4314 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004315 2, tenant.layout.pipelines['check']._consecutive_failures)
4316 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004317
4318 # J and K went back to gerrit
4319 self.assertEqual(1, len(J.messages))
4320 self.assertIn('Build failed.', J.messages[0])
4321 self.assertEqual(1, len(K.messages))
4322 self.assertIn('Build failed.', K.messages[0])
4323 # No more messages reported via smtp
4324 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004325
James E. Blaircc020532017-04-19 13:04:44 -07004326 @simple_layout('layouts/one-job-project.yaml')
4327 def test_one_job_project(self):
4328 "Test that queueing works with one job"
4329 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4330 'master', 'A')
4331 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4332 'master', 'B')
4333 A.addApproval('code-review', 2)
4334 B.addApproval('code-review', 2)
4335 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4336 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4337 self.waitUntilSettled()
4338
4339 self.assertEqual(A.data['status'], 'MERGED')
4340 self.assertEqual(A.reported, 2)
4341 self.assertEqual(B.data['status'], 'MERGED')
4342 self.assertEqual(B.reported, 2)
4343
Paul Belanger71d98172016-11-08 10:56:31 -05004344 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004345 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004346
Paul Belanger174a8272017-03-14 13:20:10 -04004347 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004348 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4349 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4350 self.waitUntilSettled()
4351
Paul Belanger174a8272017-03-14 13:20:10 -04004352 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004353 self.waitUntilSettled()
4354
4355 self.assertEqual(len(self.builds), 2)
4356 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004357 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004358 self.waitUntilSettled()
4359
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004360 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004361 self.assertEqual(len(self.builds), 1,
4362 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004363 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004364 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004365 self.waitUntilSettled()
4366
Paul Belanger174a8272017-03-14 13:20:10 -04004367 self.executor_server.hold_jobs_in_build = False
4368 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004369 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004370 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004371 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4372 self.assertEqual(A.reported, 1)
4373 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004374
James E. Blair15be0e12017-01-03 13:45:20 -08004375 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004376 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004377
4378 self.fake_nodepool.paused = True
4379 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4380 A.addApproval('code-review', 2)
4381 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4382 self.waitUntilSettled()
4383
4384 self.zk.client.stop()
4385 self.zk.client.start()
4386 self.fake_nodepool.paused = False
4387 self.waitUntilSettled()
4388
4389 self.assertEqual(A.data['status'], 'MERGED')
4390 self.assertEqual(A.reported, 2)
4391
James E. Blair6ab79e02017-01-06 10:10:17 -08004392 def test_nodepool_failure(self):
4393 "Test that jobs are reported after a nodepool failure"
4394
4395 self.fake_nodepool.paused = True
4396 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4397 A.addApproval('code-review', 2)
4398 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4399 self.waitUntilSettled()
4400
4401 req = self.fake_nodepool.getNodeRequests()[0]
4402 self.fake_nodepool.addFailRequest(req)
4403
4404 self.fake_nodepool.paused = False
4405 self.waitUntilSettled()
4406
4407 self.assertEqual(A.data['status'], 'NEW')
4408 self.assertEqual(A.reported, 2)
4409 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4410 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4411 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4412
Arieb6f068c2016-10-09 13:11:06 +03004413
James E. Blaira002b032017-04-18 10:35:48 -07004414class TestExecutor(ZuulTestCase):
4415 tenant_config_file = 'config/single-tenant/main.yaml'
4416
4417 def assertFinalState(self):
4418 # In this test, we expect to shut down in a non-final state,
4419 # so skip these checks.
4420 pass
4421
4422 def assertCleanShutdown(self):
4423 self.log.debug("Assert clean shutdown")
4424
4425 # After shutdown, make sure no jobs are running
4426 self.assertEqual({}, self.executor_server.job_workers)
4427
4428 # Make sure that git.Repo objects have been garbage collected.
4429 repos = []
4430 gc.collect()
4431 for obj in gc.get_objects():
4432 if isinstance(obj, git.Repo):
4433 self.log.debug("Leaked git repo object: %s" % repr(obj))
4434 repos.append(obj)
4435 self.assertEqual(len(repos), 0)
4436
4437 def test_executor_shutdown(self):
4438 "Test that the executor can shut down with jobs running"
4439
4440 self.executor_server.hold_jobs_in_build = True
4441 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4442 A.addApproval('code-review', 2)
4443 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4444 self.waitUntilSettled()
4445
4446
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004447class TestDependencyGraph(ZuulTestCase):
4448 tenant_config_file = 'config/dependency-graph/main.yaml'
4449
4450 def test_dependeny_graph_dispatch_jobs_once(self):
4451 "Test a job in a dependency graph is queued only once"
4452 # Job dependencies, starting with A
4453 # A
4454 # / \
4455 # B C
4456 # / \ / \
4457 # D F E
4458 # |
4459 # G
4460
4461 self.executor_server.hold_jobs_in_build = True
4462 change = self.fake_gerrit.addFakeChange(
4463 'org/project', 'master', 'change')
4464 change.addApproval('code-review', 2)
4465 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4466
4467 self.waitUntilSettled()
4468 self.assertEqual([b.name for b in self.builds], ['A'])
4469
4470 self.executor_server.release('A')
4471 self.waitUntilSettled()
4472 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4473
4474 self.executor_server.release('B')
4475 self.waitUntilSettled()
4476 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4477
4478 self.executor_server.release('D')
4479 self.waitUntilSettled()
4480 self.assertEqual([b.name for b in self.builds], ['C'])
4481
4482 self.executor_server.release('C')
4483 self.waitUntilSettled()
4484 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4485
4486 self.executor_server.release('F')
4487 self.waitUntilSettled()
4488 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4489
4490 self.executor_server.release('G')
4491 self.waitUntilSettled()
4492 self.assertEqual([b.name for b in self.builds], ['E'])
4493
4494 self.executor_server.release('E')
4495 self.waitUntilSettled()
4496 self.assertEqual(len(self.builds), 0)
4497
4498 self.executor_server.hold_jobs_in_build = False
4499 self.executor_server.release()
4500 self.waitUntilSettled()
4501
4502 self.assertEqual(len(self.builds), 0)
4503 self.assertEqual(len(self.history), 7)
4504
4505 self.assertEqual(change.data['status'], 'MERGED')
4506 self.assertEqual(change.reported, 2)
4507
4508 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4509 "Test that a job waits till all dependencies are successful"
4510 # Job dependencies, starting with A
4511 # A
4512 # / \
4513 # B C*
4514 # / \ / \
4515 # D F E
4516 # |
4517 # G
4518
4519 self.executor_server.hold_jobs_in_build = True
4520 change = self.fake_gerrit.addFakeChange(
4521 'org/project', 'master', 'change')
4522 change.addApproval('code-review', 2)
4523
4524 self.executor_server.failJob('C', change)
4525
4526 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4527
4528 self.waitUntilSettled()
4529 self.assertEqual([b.name for b in self.builds], ['A'])
4530
4531 self.executor_server.release('A')
4532 self.waitUntilSettled()
4533 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4534
4535 self.executor_server.release('B')
4536 self.waitUntilSettled()
4537 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4538
4539 self.executor_server.release('D')
4540 self.waitUntilSettled()
4541 self.assertEqual([b.name for b in self.builds], ['C'])
4542
4543 self.executor_server.release('C')
4544 self.waitUntilSettled()
4545 self.assertEqual(len(self.builds), 0)
4546
4547 self.executor_server.hold_jobs_in_build = False
4548 self.executor_server.release()
4549 self.waitUntilSettled()
4550
4551 self.assertEqual(len(self.builds), 0)
4552 self.assertEqual(len(self.history), 4)
4553
4554 self.assertEqual(change.data['status'], 'NEW')
4555 self.assertEqual(change.reported, 2)
4556
4557
Arieb6f068c2016-10-09 13:11:06 +03004558class TestDuplicatePipeline(ZuulTestCase):
4559 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4560
4561 def test_duplicate_pipelines(self):
4562 "Test that a change matching multiple pipelines works"
4563
4564 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4565 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4566 self.waitUntilSettled()
4567
4568 self.assertHistory([
4569 dict(name='project-test1', result='SUCCESS', changes='1,1',
4570 pipeline='dup1'),
4571 dict(name='project-test1', result='SUCCESS', changes='1,1',
4572 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004573 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004574
4575 self.assertEqual(len(A.messages), 2)
4576
Paul Belangera46a3742016-11-09 14:23:03 -05004577 if 'dup1' in A.messages[0]:
4578 self.assertIn('dup1', A.messages[0])
4579 self.assertNotIn('dup2', A.messages[0])
4580 self.assertIn('project-test1', A.messages[0])
4581 self.assertIn('dup2', A.messages[1])
4582 self.assertNotIn('dup1', A.messages[1])
4583 self.assertIn('project-test1', A.messages[1])
4584 else:
4585 self.assertIn('dup1', A.messages[1])
4586 self.assertNotIn('dup2', A.messages[1])
4587 self.assertIn('project-test1', A.messages[1])
4588 self.assertIn('dup2', A.messages[0])
4589 self.assertNotIn('dup1', A.messages[0])
4590 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004591
4592
Paul Belanger86085b32016-11-03 12:48:57 -04004593class TestSchedulerTemplatedProject(ZuulTestCase):
4594 tenant_config_file = 'config/templated-project/main.yaml'
4595
Paul Belanger174a8272017-03-14 13:20:10 -04004596 def test_job_from_templates_executed(self):
4597 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004598
4599 A = self.fake_gerrit.addFakeChange(
4600 'org/templated-project', 'master', 'A')
4601 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4602 self.waitUntilSettled()
4603
4604 self.assertEqual(self.getJobFromHistory('project-test1').result,
4605 'SUCCESS')
4606 self.assertEqual(self.getJobFromHistory('project-test2').result,
4607 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004608
4609 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004610 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004611
4612 A = self.fake_gerrit.addFakeChange(
4613 'org/layered-project', 'master', 'A')
4614 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4615 self.waitUntilSettled()
4616
4617 self.assertEqual(self.getJobFromHistory('project-test1').result,
4618 'SUCCESS')
4619 self.assertEqual(self.getJobFromHistory('project-test2').result,
4620 'SUCCESS')
4621 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4622 ).result, 'SUCCESS')
4623 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4624 ).result, 'SUCCESS')
4625 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4626 ).result, 'SUCCESS')
4627 self.assertEqual(self.getJobFromHistory('project-test6').result,
4628 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004629
4630
4631class TestSchedulerSuccessURL(ZuulTestCase):
4632 tenant_config_file = 'config/success-url/main.yaml'
4633
4634 def test_success_url(self):
4635 "Ensure bad build params are ignored"
4636 self.sched.reconfigure(self.config)
4637 self.init_repo('org/docs')
4638
4639 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4640 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4641 self.waitUntilSettled()
4642
4643 # Both builds ran: docs-draft-test + docs-draft-test2
4644 self.assertEqual(len(self.history), 2)
4645
4646 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004647 for build in self.history:
4648 if build.name == 'docs-draft-test':
4649 uuid = build.uuid[:7]
4650 break
Adam Gandelman94a60062016-11-21 16:43:14 -08004651
4652 # Two msgs: 'Starting...' + results
4653 self.assertEqual(len(self.smtp_messages), 2)
4654 body = self.smtp_messages[1]['body'].splitlines()
4655 self.assertEqual('Build succeeded.', body[0])
4656
4657 self.assertIn(
4658 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4659 'docs-draft-test/%s/publish-docs/' % uuid,
4660 body[2])
4661
Paul Belanger174a8272017-03-14 13:20:10 -04004662 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004663 self.assertIn(
4664 '- docs-draft-test2 https://server/job',
4665 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004666
4667
Adam Gandelman4da00f62016-12-09 15:47:33 -08004668class TestSchedulerMerges(ZuulTestCase):
4669 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004670
4671 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004672 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004673 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004674 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004675 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4676 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4677 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4678 A.addApproval('code-review', 2)
4679 B.addApproval('code-review', 2)
4680 C.addApproval('code-review', 2)
4681 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4682 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4683 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4684 self.waitUntilSettled()
4685
4686 build = self.builds[-1]
4687 ref = self.getParameter(build, 'ZUUL_REF')
4688
James E. Blair2a535672017-04-27 12:03:15 -07004689 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4690 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004691 repo = git.Repo(path)
4692 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4693 repo_messages.reverse()
4694
Paul Belanger174a8272017-03-14 13:20:10 -04004695 self.executor_server.hold_jobs_in_build = False
4696 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004697 self.waitUntilSettled()
4698
4699 return repo_messages
4700
4701 def _test_merge(self, mode):
4702 us_path = os.path.join(
4703 self.upstream_root, 'org/project-%s' % mode)
4704 expected_messages = [
4705 'initial commit',
4706 'add content from fixture',
4707 # the intermediate commits order is nondeterministic
4708 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4709 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4710 ]
4711 result = self._test_project_merge_mode(mode)
4712 self.assertEqual(result[:2], expected_messages[:2])
4713 self.assertEqual(result[-2:], expected_messages[-2:])
4714
4715 def test_project_merge_mode_merge(self):
4716 self._test_merge('merge')
4717
4718 def test_project_merge_mode_merge_resolve(self):
4719 self._test_merge('merge-resolve')
4720
4721 def test_project_merge_mode_cherrypick(self):
4722 expected_messages = [
4723 'initial commit',
4724 'add content from fixture',
4725 'A-1',
4726 'B-1',
4727 'C-1']
4728 result = self._test_project_merge_mode('cherry-pick')
4729 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004730
4731 def test_merge_branch(self):
4732 "Test that the right commits are on alternate branches"
4733 self.create_branch('org/project-merge-branches', 'mp')
4734
Paul Belanger174a8272017-03-14 13:20:10 -04004735 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004736 A = self.fake_gerrit.addFakeChange(
4737 'org/project-merge-branches', 'mp', 'A')
4738 B = self.fake_gerrit.addFakeChange(
4739 'org/project-merge-branches', 'mp', 'B')
4740 C = self.fake_gerrit.addFakeChange(
4741 'org/project-merge-branches', 'mp', 'C')
4742 A.addApproval('code-review', 2)
4743 B.addApproval('code-review', 2)
4744 C.addApproval('code-review', 2)
4745 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4746 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4747 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4748 self.waitUntilSettled()
4749
Paul Belanger174a8272017-03-14 13:20:10 -04004750 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004751 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004752 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004753 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004754 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004755 self.waitUntilSettled()
4756
4757 build = self.builds[-1]
4758 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
4759 ref = self.getParameter(build, 'ZUUL_REF')
James E. Blair2a535672017-04-27 12:03:15 -07004760 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4761 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004762 repo = git.Repo(path)
4763
4764 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4765 repo_messages.reverse()
4766 correct_messages = [
4767 'initial commit',
4768 'add content from fixture',
4769 'mp commit',
4770 'A-1', 'B-1', 'C-1']
4771 self.assertEqual(repo_messages, correct_messages)
4772
Paul Belanger174a8272017-03-14 13:20:10 -04004773 self.executor_server.hold_jobs_in_build = False
4774 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004775 self.waitUntilSettled()
4776
4777 def test_merge_multi_branch(self):
4778 "Test that dependent changes on multiple branches are merged"
4779 self.create_branch('org/project-merge-branches', 'mp')
4780
Paul Belanger174a8272017-03-14 13:20:10 -04004781 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004782 A = self.fake_gerrit.addFakeChange(
4783 'org/project-merge-branches', 'master', 'A')
4784 B = self.fake_gerrit.addFakeChange(
4785 'org/project-merge-branches', 'mp', 'B')
4786 C = self.fake_gerrit.addFakeChange(
4787 'org/project-merge-branches', 'master', 'C')
4788 A.addApproval('code-review', 2)
4789 B.addApproval('code-review', 2)
4790 C.addApproval('code-review', 2)
4791 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4792 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4793 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4794 self.waitUntilSettled()
4795
4796 job_A = None
4797 for job in self.builds:
4798 if 'project-merge' in job.name:
4799 job_A = job
4800 ref_A = self.getParameter(job_A, 'ZUUL_REF')
4801 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
4802 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
4803 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
4804
James E. Blair2a535672017-04-27 12:03:15 -07004805 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
4806 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004807 repo = git.Repo(path)
4808 repo_messages = [c.message.strip()
4809 for c in repo.iter_commits(ref_A)]
4810 repo_messages.reverse()
4811 correct_messages = [
4812 'initial commit', 'add content from fixture', 'A-1']
4813 self.assertEqual(repo_messages, correct_messages)
4814
Paul Belanger174a8272017-03-14 13:20:10 -04004815 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004816 self.waitUntilSettled()
4817
4818 job_B = None
4819 for job in self.builds:
4820 if 'project-merge' in job.name:
4821 job_B = job
4822 ref_B = self.getParameter(job_B, 'ZUUL_REF')
4823 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
4824 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
4825 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
4826
James E. Blair2a535672017-04-27 12:03:15 -07004827 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
4828 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004829 repo = git.Repo(path)
4830 repo_messages = [c.message.strip()
4831 for c in repo.iter_commits(ref_B)]
4832 repo_messages.reverse()
4833 correct_messages = [
4834 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4835 self.assertEqual(repo_messages, correct_messages)
4836
Paul Belanger174a8272017-03-14 13:20:10 -04004837 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004838 self.waitUntilSettled()
4839
4840 job_C = None
4841 for job in self.builds:
4842 if 'project-merge' in job.name:
4843 job_C = job
4844 ref_C = self.getParameter(job_C, 'ZUUL_REF')
4845 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
4846 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
4847 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair2a535672017-04-27 12:03:15 -07004848 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
4849 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004850 repo = git.Repo(path)
4851 repo_messages = [c.message.strip()
4852 for c in repo.iter_commits(ref_C)]
4853
4854 repo_messages.reverse()
4855 correct_messages = [
4856 'initial commit', 'add content from fixture',
4857 'A-1', 'C-1']
4858 # Ensure the right commits are in the history for this ref
4859 self.assertEqual(repo_messages, correct_messages)
4860
Paul Belanger174a8272017-03-14 13:20:10 -04004861 self.executor_server.hold_jobs_in_build = False
4862 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004863 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01004864
4865
James E. Blair9ea0d0b2017-04-20 09:27:15 -07004866class TestSemaphore(ZuulTestCase):
4867 tenant_config_file = 'config/semaphore/main.yaml'
4868
4869 def test_semaphore_one(self):
4870 "Test semaphores with max=1 (mutex)"
4871 tenant = self.sched.abide.tenants.get('tenant-one')
4872
4873 self.executor_server.hold_jobs_in_build = True
4874
4875 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4876 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4877 self.assertFalse('test-semaphore' in
4878 tenant.semaphore_handler.semaphores)
4879
4880 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4881 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4882 self.waitUntilSettled()
4883
4884 self.assertEqual(len(self.builds), 3)
4885 self.assertEqual(self.builds[0].name, 'project-test1')
4886 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
4887 self.assertEqual(self.builds[2].name, 'project-test1')
4888
4889 self.executor_server.release('semaphore-one-test1')
4890 self.waitUntilSettled()
4891
4892 self.assertEqual(len(self.builds), 3)
4893 self.assertEqual(self.builds[0].name, 'project-test1')
4894 self.assertEqual(self.builds[1].name, 'project-test1')
4895 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4896 self.assertTrue('test-semaphore' in
4897 tenant.semaphore_handler.semaphores)
4898
4899 self.executor_server.release('semaphore-one-test2')
4900 self.waitUntilSettled()
4901
4902 self.assertEqual(len(self.builds), 3)
4903 self.assertEqual(self.builds[0].name, 'project-test1')
4904 self.assertEqual(self.builds[1].name, 'project-test1')
4905 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
4906 self.assertTrue('test-semaphore' in
4907 tenant.semaphore_handler.semaphores)
4908
4909 self.executor_server.release('semaphore-one-test1')
4910 self.waitUntilSettled()
4911
4912 self.assertEqual(len(self.builds), 3)
4913 self.assertEqual(self.builds[0].name, 'project-test1')
4914 self.assertEqual(self.builds[1].name, 'project-test1')
4915 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4916 self.assertTrue('test-semaphore' in
4917 tenant.semaphore_handler.semaphores)
4918
4919 self.executor_server.release('semaphore-one-test2')
4920 self.waitUntilSettled()
4921
4922 self.assertEqual(len(self.builds), 2)
4923 self.assertEqual(self.builds[0].name, 'project-test1')
4924 self.assertEqual(self.builds[1].name, 'project-test1')
4925 self.assertFalse('test-semaphore' in
4926 tenant.semaphore_handler.semaphores)
4927
4928 self.executor_server.hold_jobs_in_build = False
4929 self.executor_server.release()
4930
4931 self.waitUntilSettled()
4932 self.assertEqual(len(self.builds), 0)
4933
4934 self.assertEqual(A.reported, 1)
4935 self.assertEqual(B.reported, 1)
4936 self.assertFalse('test-semaphore' in
4937 tenant.semaphore_handler.semaphores)
4938
4939 def test_semaphore_two(self):
4940 "Test semaphores with max>1"
4941 tenant = self.sched.abide.tenants.get('tenant-one')
4942
4943 self.executor_server.hold_jobs_in_build = True
4944 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4945 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4946 self.assertFalse('test-semaphore-two' in
4947 tenant.semaphore_handler.semaphores)
4948
4949 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4950 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4951 self.waitUntilSettled()
4952
4953 self.assertEqual(len(self.builds), 4)
4954 self.assertEqual(self.builds[0].name, 'project-test1')
4955 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
4956 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
4957 self.assertEqual(self.builds[3].name, 'project-test1')
4958 self.assertTrue('test-semaphore-two' in
4959 tenant.semaphore_handler.semaphores)
4960 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4961 'test-semaphore-two', [])), 2)
4962
4963 self.executor_server.release('semaphore-two-test1')
4964 self.waitUntilSettled()
4965
4966 self.assertEqual(len(self.builds), 4)
4967 self.assertEqual(self.builds[0].name, 'project-test1')
4968 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
4969 self.assertEqual(self.builds[2].name, 'project-test1')
4970 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
4971 self.assertTrue('test-semaphore-two' in
4972 tenant.semaphore_handler.semaphores)
4973 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4974 'test-semaphore-two', [])), 2)
4975
4976 self.executor_server.release('semaphore-two-test2')
4977 self.waitUntilSettled()
4978
4979 self.assertEqual(len(self.builds), 4)
4980 self.assertEqual(self.builds[0].name, 'project-test1')
4981 self.assertEqual(self.builds[1].name, 'project-test1')
4982 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
4983 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
4984 self.assertTrue('test-semaphore-two' in
4985 tenant.semaphore_handler.semaphores)
4986 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4987 'test-semaphore-two', [])), 2)
4988
4989 self.executor_server.release('semaphore-two-test1')
4990 self.waitUntilSettled()
4991
4992 self.assertEqual(len(self.builds), 3)
4993 self.assertEqual(self.builds[0].name, 'project-test1')
4994 self.assertEqual(self.builds[1].name, 'project-test1')
4995 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
4996 self.assertTrue('test-semaphore-two' in
4997 tenant.semaphore_handler.semaphores)
4998 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4999 'test-semaphore-two', [])), 1)
5000
5001 self.executor_server.release('semaphore-two-test2')
5002 self.waitUntilSettled()
5003
5004 self.assertEqual(len(self.builds), 2)
5005 self.assertEqual(self.builds[0].name, 'project-test1')
5006 self.assertEqual(self.builds[1].name, 'project-test1')
5007 self.assertFalse('test-semaphore-two' in
5008 tenant.semaphore_handler.semaphores)
5009
5010 self.executor_server.hold_jobs_in_build = False
5011 self.executor_server.release()
5012
5013 self.waitUntilSettled()
5014 self.assertEqual(len(self.builds), 0)
5015
5016 self.assertEqual(A.reported, 1)
5017 self.assertEqual(B.reported, 1)
5018
5019 def test_semaphore_abandon(self):
5020 "Test abandon with job semaphores"
5021 self.executor_server.hold_jobs_in_build = True
5022 tenant = self.sched.abide.tenants.get('tenant-one')
5023 check_pipeline = tenant.layout.pipelines['check']
5024
5025 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5026 self.assertFalse('test-semaphore' in
5027 tenant.semaphore_handler.semaphores)
5028
5029 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5030 self.waitUntilSettled()
5031
5032 self.assertTrue('test-semaphore' in
5033 tenant.semaphore_handler.semaphores)
5034
5035 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5036 self.waitUntilSettled()
5037
5038 # The check pipeline should be empty
5039 items = check_pipeline.getAllItems()
5040 self.assertEqual(len(items), 0)
5041
5042 # The semaphore should be released
5043 self.assertFalse('test-semaphore' in
5044 tenant.semaphore_handler.semaphores)
5045
5046 self.executor_server.hold_jobs_in_build = False
5047 self.executor_server.release()
5048 self.waitUntilSettled()
5049
5050 def test_semaphore_reconfigure(self):
5051 "Test reconfigure with job semaphores"
5052 self.executor_server.hold_jobs_in_build = True
5053 tenant = self.sched.abide.tenants.get('tenant-one')
5054 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5055 self.assertFalse('test-semaphore' in
5056 tenant.semaphore_handler.semaphores)
5057
5058 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5059 self.waitUntilSettled()
5060
5061 self.assertTrue('test-semaphore' in
5062 tenant.semaphore_handler.semaphores)
5063
5064 # reconfigure without layout change
5065 self.sched.reconfigure(self.config)
5066 self.waitUntilSettled()
5067 tenant = self.sched.abide.tenants.get('tenant-one')
5068
5069 # semaphore still must be held
5070 self.assertTrue('test-semaphore' in
5071 tenant.semaphore_handler.semaphores)
5072
5073 self.commitConfigUpdate(
5074 'common-config',
5075 'config/semaphore/zuul-reconfiguration.yaml')
5076 self.sched.reconfigure(self.config)
5077 self.waitUntilSettled()
5078 tenant = self.sched.abide.tenants.get('tenant-one')
5079
5080 self.executor_server.release('project-test1')
5081 self.waitUntilSettled()
5082
5083 # There should be no builds anymore
5084 self.assertEqual(len(self.builds), 0)
5085
5086 # The semaphore should be released
5087 self.assertFalse('test-semaphore' in
5088 tenant.semaphore_handler.semaphores)
5089
5090
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005091class TestSemaphoreMultiTenant(ZuulTestCase):
5092 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5093
5094 def test_semaphore_tenant_isolation(self):
5095 "Test semaphores in multiple tenants"
5096
5097 self.waitUntilSettled()
5098 tenant_one = self.sched.abide.tenants.get('tenant-one')
5099 tenant_two = self.sched.abide.tenants.get('tenant-two')
5100
5101 self.executor_server.hold_jobs_in_build = True
5102 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5103 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5104 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5105 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5106 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5107 self.assertFalse('test-semaphore' in
5108 tenant_one.semaphore_handler.semaphores)
5109 self.assertFalse('test-semaphore' in
5110 tenant_two.semaphore_handler.semaphores)
5111
5112 # add patches to project1 of tenant-one
5113 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5114 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5115 self.waitUntilSettled()
5116
5117 # one build of project1-test1 must run
5118 # semaphore of tenant-one must be acquired once
5119 # semaphore of tenant-two must not be acquired
5120 self.assertEqual(len(self.builds), 1)
5121 self.assertEqual(self.builds[0].name, 'project1-test1')
5122 self.assertTrue('test-semaphore' in
5123 tenant_one.semaphore_handler.semaphores)
5124 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5125 'test-semaphore', [])), 1)
5126 self.assertFalse('test-semaphore' in
5127 tenant_two.semaphore_handler.semaphores)
5128
5129 # add patches to project2 of tenant-two
5130 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5131 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5132 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5133 self.waitUntilSettled()
5134
5135 # one build of project1-test1 must run
5136 # two builds of project2-test1 must run
5137 # semaphore of tenant-one must be acquired once
5138 # semaphore of tenant-two must be acquired twice
5139 self.assertEqual(len(self.builds), 3)
5140 self.assertEqual(self.builds[0].name, 'project1-test1')
5141 self.assertEqual(self.builds[1].name, 'project2-test1')
5142 self.assertEqual(self.builds[2].name, 'project2-test1')
5143 self.assertTrue('test-semaphore' in
5144 tenant_one.semaphore_handler.semaphores)
5145 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5146 'test-semaphore', [])), 1)
5147 self.assertTrue('test-semaphore' in
5148 tenant_two.semaphore_handler.semaphores)
5149 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5150 'test-semaphore', [])), 2)
5151
5152 self.executor_server.release('project1-test1')
5153 self.waitUntilSettled()
5154
5155 # one build of project1-test1 must run
5156 # two builds of project2-test1 must run
5157 # semaphore of tenant-one must be acquired once
5158 # semaphore of tenant-two must be acquired twice
5159 self.assertEqual(len(self.builds), 3)
5160 self.assertEqual(self.builds[0].name, 'project2-test1')
5161 self.assertEqual(self.builds[1].name, 'project2-test1')
5162 self.assertEqual(self.builds[2].name, 'project1-test1')
5163 self.assertTrue('test-semaphore' in
5164 tenant_one.semaphore_handler.semaphores)
5165 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5166 'test-semaphore', [])), 1)
5167 self.assertTrue('test-semaphore' in
5168 tenant_two.semaphore_handler.semaphores)
5169 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5170 'test-semaphore', [])), 2)
5171
5172 self.executor_server.release('project2-test1')
5173 self.waitUntilSettled()
5174
5175 # one build of project1-test1 must run
5176 # one build of project2-test1 must run
5177 # semaphore of tenant-one must be acquired once
5178 # semaphore of tenant-two must be acquired once
5179 self.assertEqual(len(self.builds), 2)
5180 self.assertTrue('test-semaphore' in
5181 tenant_one.semaphore_handler.semaphores)
5182 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5183 'test-semaphore', [])), 1)
5184 self.assertTrue('test-semaphore' in
5185 tenant_two.semaphore_handler.semaphores)
5186 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5187 'test-semaphore', [])), 1)
5188
5189 self.executor_server.hold_jobs_in_build = False
5190 self.executor_server.release()
5191
5192 self.waitUntilSettled()
5193
5194 # no build must run
5195 # semaphore of tenant-one must not be acquired
5196 # semaphore of tenant-two must not be acquired
5197 self.assertEqual(len(self.builds), 0)
5198 self.assertFalse('test-semaphore' in
5199 tenant_one.semaphore_handler.semaphores)
5200 self.assertFalse('test-semaphore' in
5201 tenant_two.semaphore_handler.semaphores)
5202
5203 self.assertEqual(A.reported, 1)
5204 self.assertEqual(B.reported, 1)
5205
5206
5207class TestSemaphoreInRepo(ZuulTestCase):
5208 tenant_config_file = 'config/in-repo/main.yaml'
5209
5210 def test_semaphore_in_repo(self):
5211 "Test semaphores in repo config"
5212
5213 # This tests dynamic semaphore handling in project repos. The semaphore
5214 # max value should not be evaluated dynamically but must be updated
5215 # after the change lands.
5216
5217 self.waitUntilSettled()
5218 tenant = self.sched.abide.tenants.get('tenant-one')
5219
5220 in_repo_conf = textwrap.dedent(
5221 """
5222 - job:
5223 name: project-test2
5224 semaphore: test-semaphore
5225
5226 - project:
5227 name: org/project
5228 tenant-one-gate:
5229 jobs:
5230 - project-test2
5231
5232 # the max value in dynamic layout must be ignored
5233 - semaphore:
5234 name: test-semaphore
5235 max: 2
5236 """)
5237
5238 in_repo_playbook = textwrap.dedent(
5239 """
5240 - hosts: all
5241 tasks: []
5242 """)
5243
5244 file_dict = {'.zuul.yaml': in_repo_conf,
5245 'playbooks/project-test2.yaml': in_repo_playbook}
5246 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5247 files=file_dict)
5248 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5249 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5250 B.setDependsOn(A, 1)
5251 C.setDependsOn(A, 1)
5252
5253 self.executor_server.hold_jobs_in_build = True
5254
5255 A.addApproval('code-review', 2)
5256 B.addApproval('code-review', 2)
5257 C.addApproval('code-review', 2)
5258 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
5259 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
5260 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
5261 self.waitUntilSettled()
5262
5263 # check that the layout in a queue item still has max value of 1
5264 # for test-semaphore
5265 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5266 queue = None
5267 for queue_candidate in pipeline.queues:
5268 if queue_candidate.name == 'org/project':
5269 queue = queue_candidate
5270 break
5271 queue_item = queue.queue[0]
5272 item_dynamic_layout = queue_item.current_build_set.layout
5273 dynamic_test_semaphore = \
5274 item_dynamic_layout.semaphores.get('test-semaphore')
5275 self.assertEqual(dynamic_test_semaphore.max, 1)
5276
5277 # one build must be in queue, one semaphores acquired
5278 self.assertEqual(len(self.builds), 1)
5279 self.assertEqual(self.builds[0].name, 'project-test2')
5280 self.assertTrue('test-semaphore' in
5281 tenant.semaphore_handler.semaphores)
5282 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5283 'test-semaphore', [])), 1)
5284
5285 self.executor_server.release('project-test2')
5286 self.waitUntilSettled()
5287
5288 # change A must be merged
5289 self.assertEqual(A.data['status'], 'MERGED')
5290 self.assertEqual(A.reported, 2)
5291
5292 # send change-merged event as the gerrit mock doesn't send it
5293 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5294 self.waitUntilSettled()
5295
5296 # now that change A was merged, the new semaphore max must be effective
5297 tenant = self.sched.abide.tenants.get('tenant-one')
5298 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5299
5300 # two builds must be in queue, two semaphores acquired
5301 self.assertEqual(len(self.builds), 2)
5302 self.assertEqual(self.builds[0].name, 'project-test2')
5303 self.assertEqual(self.builds[1].name, 'project-test2')
5304 self.assertTrue('test-semaphore' in
5305 tenant.semaphore_handler.semaphores)
5306 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5307 'test-semaphore', [])), 2)
5308
5309 self.executor_server.release('project-test2')
5310 self.waitUntilSettled()
5311
5312 self.assertEqual(len(self.builds), 0)
5313 self.assertFalse('test-semaphore' in
5314 tenant.semaphore_handler.semaphores)
5315
5316 self.executor_server.hold_jobs_in_build = False
5317 self.executor_server.release()
5318
5319 self.waitUntilSettled()
5320 self.assertEqual(len(self.builds), 0)
5321
5322 self.assertEqual(A.reported, 2)
5323 self.assertEqual(B.reported, 2)
5324 self.assertEqual(C.reported, 2)