blob: 09817164b361810f39e3cc6d0aae55b8afb496b8 [file] [log] [blame]
James E. Blairb0fcae42012-07-17 11:12:10 -07001#!/usr/bin/env python
2
3# Copyright 2012 Hewlett-Packard Development Company, L.P.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
James E. Blaira002b032017-04-18 10:35:48 -070017import gc
James E. Blairb0fcae42012-07-17 11:12:10 -070018import json
Tobias Henkel9a0e1942017-03-20 16:16:02 +010019import textwrap
20
Monty Taylorbc758832013-06-17 17:22:42 -040021import os
James E. Blairb0fcae42012-07-17 11:12:10 -070022import re
James E. Blair4886cc12012-07-18 15:39:41 -070023import shutil
Monty Taylorbc758832013-06-17 17:22:42 -040024import time
James E. Blairec056492016-07-22 09:45:56 -070025from unittest import skip
Monty Taylorbc758832013-06-17 17:22:42 -040026
James E. Blair4886cc12012-07-18 15:39:41 -070027import git
Morgan Fainberg293f7f82016-05-30 14:01:22 -070028from six.moves import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040029import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070030
Maru Newby3fe5f852015-01-13 04:22:14 +000031import zuul.change_matcher
James E. Blairb0fcae42012-07-17 11:12:10 -070032import zuul.scheduler
James E. Blairad28e912013-11-27 10:43:22 -080033import zuul.rpcclient
Paul Belanger9bba4902016-11-02 16:07:33 -040034import zuul.model
James E. Blairb0fcae42012-07-17 11:12:10 -070035
Maru Newby3fe5f852015-01-13 04:22:14 +000036from tests.base import (
Maru Newby3fe5f852015-01-13 04:22:14 +000037 ZuulTestCase,
38 repack_repo,
James E. Blair06cc3922017-04-19 10:08:10 -070039 simple_layout,
Maru Newby3fe5f852015-01-13 04:22:14 +000040)
James E. Blairb0fcae42012-07-17 11:12:10 -070041
James E. Blairb0fcae42012-07-17 11:12:10 -070042
Clark Boylanb640e052014-04-03 16:41:46 -070043class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070044 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010045
Paul Belanger174a8272017-03-14 13:20:10 -040046 def test_jobs_executed(self):
47 "Test that jobs are executed and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070048
James E. Blairb0fcae42012-07-17 11:12:10 -070049 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070050 A.addApproval('code-review', 2)
51 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070052 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040053 self.assertEqual(self.getJobFromHistory('project-merge').result,
54 'SUCCESS')
55 self.assertEqual(self.getJobFromHistory('project-test1').result,
56 'SUCCESS')
57 self.assertEqual(self.getJobFromHistory('project-test2').result,
58 'SUCCESS')
59 self.assertEqual(A.data['status'], 'MERGED')
60 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070061 self.assertEqual(self.getJobFromHistory('project-test1').node,
62 'image1')
Adam Gandelmaneace4692017-02-07 15:18:36 -080063 self.assertIsNone(self.getJobFromHistory('project-test2').node)
James E. Blairb0fcae42012-07-17 11:12:10 -070064
James E. Blair552b54f2016-07-22 13:55:32 -070065 # TODOv3(jeblair): we may want to report stats by tenant (also?).
James E. Blair66eeebf2013-07-27 17:44:32 -070066 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
67 self.assertReportedStat('zuul.pipeline.gate.current_changes',
68 value='1|g')
69 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
70 kind='ms')
71 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
72 value='1|c')
73 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
74 self.assertReportedStat('zuul.pipeline.gate.total_changes',
75 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070076 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070077 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -070078 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070079 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070080
James E. Blair5821bd92015-09-16 08:48:15 -070081 for build in self.builds:
82 self.assertEqual(build.parameters['ZUUL_VOTING'], '1')
83
James E. Blair3cb10702013-08-24 08:56:03 -070084 def test_initial_pipeline_gauges(self):
85 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -070086 self.assertReportedStat('zuul.pipeline.gate.current_changes',
87 value='0|g')
88 self.assertReportedStat('zuul.pipeline.check.current_changes',
89 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -070090
James E. Blair34776ee2016-08-25 13:53:54 -070091 def test_job_branch(self):
92 "Test the correct variant of a job runs on a branch"
93 self.create_branch('org/project', 'stable')
94 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
95 A.addApproval('code-review', 2)
96 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
97 self.waitUntilSettled()
98 self.assertEqual(self.getJobFromHistory('project-test1').result,
99 'SUCCESS')
100 self.assertEqual(self.getJobFromHistory('project-test2').result,
101 'SUCCESS')
102 self.assertEqual(A.data['status'], 'MERGED')
103 self.assertEqual(A.reported, 2,
104 "A should report start and success")
105 self.assertIn('gate', A.messages[1],
106 "A should transit gate")
107 self.assertEqual(self.getJobFromHistory('project-test1').node,
108 'image2')
109
James E. Blairb0fcae42012-07-17 11:12:10 -0700110 def test_parallel_changes(self):
111 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700112
Paul Belanger174a8272017-03-14 13:20:10 -0400113 self.executor_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700114 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
115 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
116 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700117 A.addApproval('code-review', 2)
118 B.addApproval('code-review', 2)
119 C.addApproval('code-review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700120
James E. Blair8b5408c2016-08-08 15:37:46 -0700121 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
122 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
123 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700124
125 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400126 self.assertEqual(len(self.builds), 1)
127 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700128 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700129
Paul Belanger174a8272017-03-14 13:20:10 -0400130 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700131 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400132 self.assertEqual(len(self.builds), 3)
133 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700134 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400135 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700136 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400137 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700138 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700139
Paul Belanger174a8272017-03-14 13:20:10 -0400140 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700141 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400142 self.assertEqual(len(self.builds), 5)
143 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700144 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400145 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700146 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700147
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400148 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700149 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400150 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700151 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700152
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400153 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700154 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700155
Paul Belanger174a8272017-03-14 13:20:10 -0400156 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700157 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400158 self.assertEqual(len(self.builds), 6)
159 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700160 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400161 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700162 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700163
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400164 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700165 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400166 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700167 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700168
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400169 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700170 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400171 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700172 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700173
Paul Belanger174a8272017-03-14 13:20:10 -0400174 self.executor_server.hold_jobs_in_build = False
175 self.executor_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700176 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400177 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700178
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400179 self.assertEqual(len(self.history), 9)
180 self.assertEqual(A.data['status'], 'MERGED')
181 self.assertEqual(B.data['status'], 'MERGED')
182 self.assertEqual(C.data['status'], 'MERGED')
183 self.assertEqual(A.reported, 2)
184 self.assertEqual(B.reported, 2)
185 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700186
187 def test_failed_changes(self):
188 "Test that a change behind a failed change is retested"
Paul Belanger174a8272017-03-14 13:20:10 -0400189 self.executor_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700190
James E. Blairb02a3bb2012-07-30 17:49:55 -0700191 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
192 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -0700193 A.addApproval('code-review', 2)
194 B.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700195
Paul Belanger174a8272017-03-14 13:20:10 -0400196 self.executor_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700197
James E. Blair8b5408c2016-08-08 15:37:46 -0700198 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
199 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700200 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700201 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400202
Paul Belanger174a8272017-03-14 13:20:10 -0400203 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400204 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700205 # A/project-merge is complete
206 self.assertBuilds([
207 dict(name='project-test1', changes='1,1'),
208 dict(name='project-test2', changes='1,1'),
209 dict(name='project-merge', changes='1,1 2,1'),
210 ])
James E. Blaire2819012013-06-28 17:17:26 -0400211
Paul Belanger174a8272017-03-14 13:20:10 -0400212 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400213 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700214 # A/project-merge is complete
215 # B/project-merge is complete
216 self.assertBuilds([
217 dict(name='project-test1', changes='1,1'),
218 dict(name='project-test2', changes='1,1'),
219 dict(name='project-test1', changes='1,1 2,1'),
220 dict(name='project-test2', changes='1,1 2,1'),
221 ])
222
223 # Release project-test1 for A which will fail. This will
Paul Belanger174a8272017-03-14 13:20:10 -0400224 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700225 self.builds[0].release()
226 self.waitUntilSettled()
227
228 self.orderedRelease()
229 self.assertHistory([
230 dict(name='project-merge', result='SUCCESS', changes='1,1'),
231 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
232 dict(name='project-test1', result='FAILURE', changes='1,1'),
233 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
234 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
235 dict(name='project-test2', result='SUCCESS', changes='1,1'),
236 dict(name='project-merge', result='SUCCESS', changes='2,1'),
237 dict(name='project-test1', result='SUCCESS', changes='2,1'),
238 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700239 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700240
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400241 self.assertEqual(A.data['status'], 'NEW')
242 self.assertEqual(B.data['status'], 'MERGED')
243 self.assertEqual(A.reported, 2)
244 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700245
246 def test_independent_queues(self):
247 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700248
Paul Belanger174a8272017-03-14 13:20:10 -0400249 self.executor_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900250 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700251 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
252 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700253 A.addApproval('code-review', 2)
254 B.addApproval('code-review', 2)
255 C.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700256
James E. Blair8b5408c2016-08-08 15:37:46 -0700257 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800258 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700259
James E. Blairf59f3cf2017-02-19 14:50:26 -0800260 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
261 self.waitUntilSettled()
262
263 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700264 self.waitUntilSettled()
265
266 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700267 self.assertBuilds([
268 dict(name='project-merge', changes='1,1'),
269 dict(name='project-merge', changes='2,1'),
270 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700271
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700272 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700273 self.builds[0].release()
274 self.waitUntilSettled()
275 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700276 self.waitUntilSettled()
277 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400278 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700279 self.waitUntilSettled()
280
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700281 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700282 self.assertBuilds([
283 dict(name='project-test1', changes='1,1'),
284 dict(name='project-test2', changes='1,1'),
285 dict(name='project-test1', changes='2,1'),
286 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500287 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700288 dict(name='project-test1', changes='2,1 3,1'),
289 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800290 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700291 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700292
James E. Blair5f0d49e2016-08-19 09:36:50 -0700293 self.orderedRelease()
294 self.assertHistory([
295 dict(name='project-merge', result='SUCCESS', changes='1,1'),
296 dict(name='project-merge', result='SUCCESS', changes='2,1'),
297 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
298 dict(name='project-test1', result='SUCCESS', changes='1,1'),
299 dict(name='project-test2', result='SUCCESS', changes='1,1'),
300 dict(name='project-test1', result='SUCCESS', changes='2,1'),
301 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500302 dict(
303 name='project1-project2-integration',
304 result='SUCCESS',
305 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700306 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
307 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800308 dict(name='project1-project2-integration',
309 result='SUCCESS',
310 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700311 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700312
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400313 self.assertEqual(A.data['status'], 'MERGED')
314 self.assertEqual(B.data['status'], 'MERGED')
315 self.assertEqual(C.data['status'], 'MERGED')
316 self.assertEqual(A.reported, 2)
317 self.assertEqual(B.reported, 2)
318 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700319
320 def test_failed_change_at_head(self):
321 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700322
Paul Belanger174a8272017-03-14 13:20:10 -0400323 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700324 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
325 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
326 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700327 A.addApproval('code-review', 2)
328 B.addApproval('code-review', 2)
329 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700330
Paul Belanger174a8272017-03-14 13:20:10 -0400331 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700332
James E. Blair8b5408c2016-08-08 15:37:46 -0700333 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
334 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
335 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700336
337 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700338
James E. Blairb536ecc2016-08-31 10:11:42 -0700339 self.assertBuilds([
340 dict(name='project-merge', changes='1,1'),
341 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700342
Paul Belanger174a8272017-03-14 13:20:10 -0400343 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700344 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400345 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700346 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400347 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700348 self.waitUntilSettled()
349
James E. Blairb536ecc2016-08-31 10:11:42 -0700350 self.assertBuilds([
351 dict(name='project-test1', changes='1,1'),
352 dict(name='project-test2', changes='1,1'),
353 dict(name='project-test1', changes='1,1 2,1'),
354 dict(name='project-test2', changes='1,1 2,1'),
355 dict(name='project-test1', changes='1,1 2,1 3,1'),
356 dict(name='project-test2', changes='1,1 2,1 3,1'),
357 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700358
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400359 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700360 self.waitUntilSettled()
361
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400362 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700363 self.assertBuilds([
364 dict(name='project-test2', changes='1,1'),
365 dict(name='project-merge', changes='2,1'),
366 ])
367 # Unordered history comparison because the aborts can finish
368 # in any order.
369 self.assertHistory([
370 dict(name='project-merge', result='SUCCESS',
371 changes='1,1'),
372 dict(name='project-merge', result='SUCCESS',
373 changes='1,1 2,1'),
374 dict(name='project-merge', result='SUCCESS',
375 changes='1,1 2,1 3,1'),
376 dict(name='project-test1', result='FAILURE',
377 changes='1,1'),
378 dict(name='project-test1', result='ABORTED',
379 changes='1,1 2,1'),
380 dict(name='project-test2', result='ABORTED',
381 changes='1,1 2,1'),
382 dict(name='project-test1', result='ABORTED',
383 changes='1,1 2,1 3,1'),
384 dict(name='project-test2', result='ABORTED',
385 changes='1,1 2,1 3,1'),
386 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700387
Paul Belanger174a8272017-03-14 13:20:10 -0400388 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700389 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400390 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700391 self.waitUntilSettled()
392 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700393
James E. Blairb536ecc2016-08-31 10:11:42 -0700394 self.assertBuilds([])
395 self.assertHistory([
396 dict(name='project-merge', result='SUCCESS',
397 changes='1,1'),
398 dict(name='project-merge', result='SUCCESS',
399 changes='1,1 2,1'),
400 dict(name='project-merge', result='SUCCESS',
401 changes='1,1 2,1 3,1'),
402 dict(name='project-test1', result='FAILURE',
403 changes='1,1'),
404 dict(name='project-test1', result='ABORTED',
405 changes='1,1 2,1'),
406 dict(name='project-test2', result='ABORTED',
407 changes='1,1 2,1'),
408 dict(name='project-test1', result='ABORTED',
409 changes='1,1 2,1 3,1'),
410 dict(name='project-test2', result='ABORTED',
411 changes='1,1 2,1 3,1'),
412 dict(name='project-merge', result='SUCCESS',
413 changes='2,1'),
414 dict(name='project-merge', result='SUCCESS',
415 changes='2,1 3,1'),
416 dict(name='project-test2', result='SUCCESS',
417 changes='1,1'),
418 dict(name='project-test1', result='SUCCESS',
419 changes='2,1'),
420 dict(name='project-test2', result='SUCCESS',
421 changes='2,1'),
422 dict(name='project-test1', result='SUCCESS',
423 changes='2,1 3,1'),
424 dict(name='project-test2', result='SUCCESS',
425 changes='2,1 3,1'),
426 ], ordered=False)
427
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400428 self.assertEqual(A.data['status'], 'NEW')
429 self.assertEqual(B.data['status'], 'MERGED')
430 self.assertEqual(C.data['status'], 'MERGED')
431 self.assertEqual(A.reported, 2)
432 self.assertEqual(B.reported, 2)
433 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700434
James E. Blair0aac4872013-08-23 14:02:38 -0700435 def test_failed_change_in_middle(self):
436 "Test a failed change in the middle of the queue"
437
Paul Belanger174a8272017-03-14 13:20:10 -0400438 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700439 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
440 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
441 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700442 A.addApproval('code-review', 2)
443 B.addApproval('code-review', 2)
444 C.addApproval('code-review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700445
Paul Belanger174a8272017-03-14 13:20:10 -0400446 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700447
James E. Blair8b5408c2016-08-08 15:37:46 -0700448 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
449 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
450 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700451
452 self.waitUntilSettled()
453
Paul Belanger174a8272017-03-14 13:20:10 -0400454 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700455 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400456 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700457 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400458 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700459 self.waitUntilSettled()
460
461 self.assertEqual(len(self.builds), 6)
462 self.assertEqual(self.builds[0].name, 'project-test1')
463 self.assertEqual(self.builds[1].name, 'project-test2')
464 self.assertEqual(self.builds[2].name, 'project-test1')
465 self.assertEqual(self.builds[3].name, 'project-test2')
466 self.assertEqual(self.builds[4].name, 'project-test1')
467 self.assertEqual(self.builds[5].name, 'project-test2')
468
469 self.release(self.builds[2])
470 self.waitUntilSettled()
471
James E. Blair972e3c72013-08-29 12:04:55 -0700472 # project-test1 and project-test2 for A
473 # project-test2 for B
474 # project-merge for C (without B)
475 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700476 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
477
Paul Belanger174a8272017-03-14 13:20:10 -0400478 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700479 self.waitUntilSettled()
480
481 # project-test1 and project-test2 for A
482 # project-test2 for B
483 # project-test1 and project-test2 for C
484 self.assertEqual(len(self.builds), 5)
485
Paul Belanger2e2a0492016-10-30 18:33:37 -0400486 tenant = self.sched.abide.tenants.get('tenant-one')
487 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700488 builds = items[0].current_build_set.getBuilds()
489 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
490 self.assertEqual(self.countJobResults(builds, None), 2)
491 builds = items[1].current_build_set.getBuilds()
492 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
493 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
494 self.assertEqual(self.countJobResults(builds, None), 1)
495 builds = items[2].current_build_set.getBuilds()
496 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700497 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700498
Paul Belanger174a8272017-03-14 13:20:10 -0400499 self.executor_server.hold_jobs_in_build = False
500 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700501 self.waitUntilSettled()
502
503 self.assertEqual(len(self.builds), 0)
504 self.assertEqual(len(self.history), 12)
505 self.assertEqual(A.data['status'], 'MERGED')
506 self.assertEqual(B.data['status'], 'NEW')
507 self.assertEqual(C.data['status'], 'MERGED')
508 self.assertEqual(A.reported, 2)
509 self.assertEqual(B.reported, 2)
510 self.assertEqual(C.reported, 2)
511
James E. Blaird466dc42012-07-31 10:42:56 -0700512 def test_failed_change_at_head_with_queue(self):
513 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700514
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700515 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700516 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
517 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
518 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700519 A.addApproval('code-review', 2)
520 B.addApproval('code-review', 2)
521 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700522
Paul Belanger174a8272017-03-14 13:20:10 -0400523 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700524
James E. Blair8b5408c2016-08-08 15:37:46 -0700525 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
526 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
527 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700528
529 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700530 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400531 self.assertEqual(len(self.builds), 0)
532 self.assertEqual(len(queue), 1)
Paul Belanger174a8272017-03-14 13:20:10 -0400533 self.assertEqual(queue[0].name, 'executor:execute')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800534 job_args = json.loads(queue[0].arguments)
535 self.assertEqual(job_args['job'], 'project-merge')
536 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700537
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700538 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700539 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700540 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700541 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700542 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700543 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700544 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700545
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400546 self.assertEqual(len(self.builds), 0)
547 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800548
549 self.assertEqual(
550 json.loads(queue[0].arguments)['job'], 'project-test1')
551 self.assertEqual(
552 json.loads(queue[1].arguments)['job'], 'project-test2')
553 self.assertEqual(
554 json.loads(queue[2].arguments)['job'], 'project-test1')
555 self.assertEqual(
556 json.loads(queue[3].arguments)['job'], 'project-test2')
557 self.assertEqual(
558 json.loads(queue[4].arguments)['job'], 'project-test1')
559 self.assertEqual(
560 json.loads(queue[5].arguments)['job'], 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700561
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700562 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700563 self.waitUntilSettled()
564
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400565 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700566 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400567 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
568 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700569
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700570 self.gearman_server.hold_jobs_in_queue = False
571 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700572 self.waitUntilSettled()
573
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400574 self.assertEqual(len(self.builds), 0)
575 self.assertEqual(len(self.history), 11)
576 self.assertEqual(A.data['status'], 'NEW')
577 self.assertEqual(B.data['status'], 'MERGED')
578 self.assertEqual(C.data['status'], 'MERGED')
579 self.assertEqual(A.reported, 2)
580 self.assertEqual(B.reported, 2)
581 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700582
James E. Blairec056492016-07-22 09:45:56 -0700583 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700584 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400585 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700586 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -0700587 A.addApproval('code-review', 2)
588 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700589 self.waitUntilSettled()
590 time.sleep(2)
591
592 data = json.loads(self.sched.formatStatusJSON())
593 found_job = None
594 for pipeline in data['pipelines']:
595 if pipeline['name'] != 'gate':
596 continue
597 for queue in pipeline['change_queues']:
598 for head in queue['heads']:
599 for item in head:
600 for job in item['jobs']:
601 if job['name'] == 'project-merge':
602 found_job = job
603 break
604
605 self.assertIsNotNone(found_job)
606 if iteration == 1:
607 self.assertIsNotNone(found_job['estimated_time'])
608 self.assertIsNone(found_job['remaining_time'])
609 else:
610 self.assertIsNotNone(found_job['estimated_time'])
611 self.assertTrue(found_job['estimated_time'] >= 2)
612 self.assertIsNotNone(found_job['remaining_time'])
613
Paul Belanger174a8272017-03-14 13:20:10 -0400614 self.executor_server.hold_jobs_in_build = False
615 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700616 self.waitUntilSettled()
617
James E. Blairec056492016-07-22 09:45:56 -0700618 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700619 def test_time_database(self):
620 "Test the time database"
621
622 self._test_time_database(1)
623 self._test_time_database(2)
624
James E. Blairfef71632013-09-23 11:15:47 -0700625 def test_two_failed_changes_at_head(self):
626 "Test that changes are reparented correctly if 2 fail at head"
627
Paul Belanger174a8272017-03-14 13:20:10 -0400628 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700629 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
630 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
631 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700632 A.addApproval('code-review', 2)
633 B.addApproval('code-review', 2)
634 C.addApproval('code-review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700635
Paul Belanger174a8272017-03-14 13:20:10 -0400636 self.executor_server.failJob('project-test1', A)
637 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700638
James E. Blair8b5408c2016-08-08 15:37:46 -0700639 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
640 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
641 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700642 self.waitUntilSettled()
643
Paul Belanger174a8272017-03-14 13:20:10 -0400644 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700645 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400646 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700647 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400648 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700649 self.waitUntilSettled()
650
651 self.assertEqual(len(self.builds), 6)
652 self.assertEqual(self.builds[0].name, 'project-test1')
653 self.assertEqual(self.builds[1].name, 'project-test2')
654 self.assertEqual(self.builds[2].name, 'project-test1')
655 self.assertEqual(self.builds[3].name, 'project-test2')
656 self.assertEqual(self.builds[4].name, 'project-test1')
657 self.assertEqual(self.builds[5].name, 'project-test2')
658
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400659 self.assertTrue(self.builds[0].hasChanges(A))
660 self.assertTrue(self.builds[2].hasChanges(A))
661 self.assertTrue(self.builds[2].hasChanges(B))
662 self.assertTrue(self.builds[4].hasChanges(A))
663 self.assertTrue(self.builds[4].hasChanges(B))
664 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700665
666 # Fail change B first
667 self.release(self.builds[2])
668 self.waitUntilSettled()
669
670 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400671 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700672 self.waitUntilSettled()
673
674 self.assertEqual(len(self.builds), 5)
675 self.assertEqual(self.builds[0].name, 'project-test1')
676 self.assertEqual(self.builds[1].name, 'project-test2')
677 self.assertEqual(self.builds[2].name, 'project-test2')
678 self.assertEqual(self.builds[3].name, 'project-test1')
679 self.assertEqual(self.builds[4].name, 'project-test2')
680
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400681 self.assertTrue(self.builds[1].hasChanges(A))
682 self.assertTrue(self.builds[2].hasChanges(A))
683 self.assertTrue(self.builds[2].hasChanges(B))
684 self.assertTrue(self.builds[4].hasChanges(A))
685 self.assertFalse(self.builds[4].hasChanges(B))
686 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700687
688 # Finish running all passing jobs for change A
689 self.release(self.builds[1])
690 self.waitUntilSettled()
691 # Fail and report change A
692 self.release(self.builds[0])
693 self.waitUntilSettled()
694
695 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400696 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700697 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400698 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700699 self.waitUntilSettled()
700
701 self.assertEqual(len(self.builds), 4)
702 self.assertEqual(self.builds[0].name, 'project-test1') # B
703 self.assertEqual(self.builds[1].name, 'project-test2') # B
704 self.assertEqual(self.builds[2].name, 'project-test1') # C
705 self.assertEqual(self.builds[3].name, 'project-test2') # C
706
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400707 self.assertFalse(self.builds[1].hasChanges(A))
708 self.assertTrue(self.builds[1].hasChanges(B))
709 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700710
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400711 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700712 # After A failed and B and C restarted, B should be back in
713 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400714 self.assertTrue(self.builds[2].hasChanges(B))
715 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700716
Paul Belanger174a8272017-03-14 13:20:10 -0400717 self.executor_server.hold_jobs_in_build = False
718 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700719 self.waitUntilSettled()
720
721 self.assertEqual(len(self.builds), 0)
722 self.assertEqual(len(self.history), 21)
723 self.assertEqual(A.data['status'], 'NEW')
724 self.assertEqual(B.data['status'], 'NEW')
725 self.assertEqual(C.data['status'], 'MERGED')
726 self.assertEqual(A.reported, 2)
727 self.assertEqual(B.reported, 2)
728 self.assertEqual(C.reported, 2)
729
James E. Blair8c803f82012-07-31 16:25:42 -0700730 def test_patch_order(self):
731 "Test that dependent patches are tested in the right order"
732 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
733 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
734 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700735 A.addApproval('code-review', 2)
736 B.addApproval('code-review', 2)
737 C.addApproval('code-review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700738
739 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
740 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
741 M2.setMerged()
742 M1.setMerged()
743
744 # C -> B -> A -> M1 -> M2
745 # M2 is here to make sure it is never queried. If it is, it
746 # means zuul is walking down the entire history of merged
747 # changes.
748
749 C.setDependsOn(B, 1)
750 B.setDependsOn(A, 1)
751 A.setDependsOn(M1, 1)
752 M1.setDependsOn(M2, 1)
753
James E. Blair8b5408c2016-08-08 15:37:46 -0700754 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700755
756 self.waitUntilSettled()
757
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400758 self.assertEqual(A.data['status'], 'NEW')
759 self.assertEqual(B.data['status'], 'NEW')
760 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700761
James E. Blair8b5408c2016-08-08 15:37:46 -0700762 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
763 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700764
765 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400766 self.assertEqual(M2.queried, 0)
767 self.assertEqual(A.data['status'], 'MERGED')
768 self.assertEqual(B.data['status'], 'MERGED')
769 self.assertEqual(C.data['status'], 'MERGED')
770 self.assertEqual(A.reported, 2)
771 self.assertEqual(B.reported, 2)
772 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700773
James E. Blair063672f2015-01-29 13:09:12 -0800774 def test_needed_changes_enqueue(self):
775 "Test that a needed change is enqueued ahead"
776 # A Given a git tree like this, if we enqueue
777 # / \ change C, we should walk up and down the tree
778 # B G and enqueue changes in the order ABCDEFG.
779 # /|\ This is also the order that you would get if
780 # *C E F you enqueued changes in the order ABCDEFG, so
781 # / the ordering is stable across re-enqueue events.
782 # D
783
784 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
785 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
786 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
787 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
788 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
789 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
790 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
791 B.setDependsOn(A, 1)
792 C.setDependsOn(B, 1)
793 D.setDependsOn(C, 1)
794 E.setDependsOn(B, 1)
795 F.setDependsOn(B, 1)
796 G.setDependsOn(A, 1)
797
James E. Blair8b5408c2016-08-08 15:37:46 -0700798 A.addApproval('code-review', 2)
799 B.addApproval('code-review', 2)
800 C.addApproval('code-review', 2)
801 D.addApproval('code-review', 2)
802 E.addApproval('code-review', 2)
803 F.addApproval('code-review', 2)
804 G.addApproval('code-review', 2)
805 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800806
807 self.waitUntilSettled()
808
809 self.assertEqual(A.data['status'], 'NEW')
810 self.assertEqual(B.data['status'], 'NEW')
811 self.assertEqual(C.data['status'], 'NEW')
812 self.assertEqual(D.data['status'], 'NEW')
813 self.assertEqual(E.data['status'], 'NEW')
814 self.assertEqual(F.data['status'], 'NEW')
815 self.assertEqual(G.data['status'], 'NEW')
816
817 # We're about to add approvals to changes without adding the
818 # triggering events to Zuul, so that we can be sure that it is
819 # enqueing the changes based on dependencies, not because of
820 # triggering events. Since it will have the changes cached
821 # already (without approvals), we need to clear the cache
822 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400823 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100824 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800825
Paul Belanger174a8272017-03-14 13:20:10 -0400826 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -0700827 A.addApproval('approved', 1)
828 B.addApproval('approved', 1)
829 D.addApproval('approved', 1)
830 E.addApproval('approved', 1)
831 F.addApproval('approved', 1)
832 G.addApproval('approved', 1)
833 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800834
835 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400836 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800837 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400838 self.executor_server.hold_jobs_in_build = False
839 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800840 self.waitUntilSettled()
841
842 self.assertEqual(A.data['status'], 'MERGED')
843 self.assertEqual(B.data['status'], 'MERGED')
844 self.assertEqual(C.data['status'], 'MERGED')
845 self.assertEqual(D.data['status'], 'MERGED')
846 self.assertEqual(E.data['status'], 'MERGED')
847 self.assertEqual(F.data['status'], 'MERGED')
848 self.assertEqual(G.data['status'], 'MERGED')
849 self.assertEqual(A.reported, 2)
850 self.assertEqual(B.reported, 2)
851 self.assertEqual(C.reported, 2)
852 self.assertEqual(D.reported, 2)
853 self.assertEqual(E.reported, 2)
854 self.assertEqual(F.reported, 2)
855 self.assertEqual(G.reported, 2)
856 self.assertEqual(self.history[6].changes,
857 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
858
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100859 def test_source_cache(self):
860 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400861 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700862
863 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
864 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
865 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
James E. Blair8b5408c2016-08-08 15:37:46 -0700866 A.addApproval('code-review', 2)
867 B.addApproval('code-review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700868
869 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
870 M1.setMerged()
871
872 B.setDependsOn(A, 1)
873 A.setDependsOn(M1, 1)
874
James E. Blair8b5408c2016-08-08 15:37:46 -0700875 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700876 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
877
878 self.waitUntilSettled()
879
880 for build in self.builds:
881 if build.parameters['ZUUL_PIPELINE'] == 'check':
882 build.release()
883 self.waitUntilSettled()
884 for build in self.builds:
885 if build.parameters['ZUUL_PIPELINE'] == 'check':
886 build.release()
887 self.waitUntilSettled()
888
James E. Blair8b5408c2016-08-08 15:37:46 -0700889 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700890 self.waitUntilSettled()
891
Joshua Hesketh352264b2015-08-11 23:42:08 +1000892 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700893 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000894 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700895
Paul Belanger174a8272017-03-14 13:20:10 -0400896 self.executor_server.hold_jobs_in_build = False
897 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700898 self.waitUntilSettled()
899
900 self.assertEqual(A.data['status'], 'MERGED')
901 self.assertEqual(B.data['status'], 'MERGED')
902 self.assertEqual(A.queried, 2) # Initial and isMerged
903 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
904
James E. Blair8c803f82012-07-31 16:25:42 -0700905 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700906 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700907 # TODO: move to test_gerrit (this is a unit test!)
908 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400909 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -0700910 (trusted, project) = tenant.getProject('org/project')
911 source = project.source
Paul Belanger9bba4902016-11-02 16:07:33 -0400912
913 # TODO(pabelanger): As we add more source / trigger APIs we should make
914 # it easier for users to create events for testing.
915 event = zuul.model.TriggerEvent()
916 event.trigger_name = 'gerrit'
917 event.change_number = '1'
918 event.patch_number = '2'
919
920 a = source.getChange(event)
921 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700922 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700923
James E. Blair8b5408c2016-08-08 15:37:46 -0700924 A.addApproval('code-review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400925 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700926 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700927
James E. Blair8b5408c2016-08-08 15:37:46 -0700928 A.addApproval('approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400929 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700930 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700931
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700932 def test_project_merge_conflict(self):
933 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700934
935 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700936 A = self.fake_gerrit.addFakeChange('org/project',
937 'master', 'A',
938 files={'conflict': 'foo'})
939 B = self.fake_gerrit.addFakeChange('org/project',
940 'master', 'B',
941 files={'conflict': 'bar'})
942 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -0700943 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700944 A.addApproval('code-review', 2)
945 B.addApproval('code-review', 2)
946 C.addApproval('code-review', 2)
947 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
948 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
949 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700950 self.waitUntilSettled()
951
James E. Blair6736beb2013-07-11 15:18:15 -0700952 self.assertEqual(A.reported, 1)
953 self.assertEqual(B.reported, 1)
954 self.assertEqual(C.reported, 1)
955
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700956 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700957 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700958 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700959 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700960 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700961 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700962
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700963 self.gearman_server.hold_jobs_in_queue = False
964 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700965 self.waitUntilSettled()
966
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400967 self.assertEqual(A.data['status'], 'MERGED')
968 self.assertEqual(B.data['status'], 'NEW')
969 self.assertEqual(C.data['status'], 'MERGED')
970 self.assertEqual(A.reported, 2)
971 self.assertEqual(B.reported, 2)
972 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700973
974 self.assertHistory([
975 dict(name='project-merge', result='SUCCESS', changes='1,1'),
976 dict(name='project-test1', result='SUCCESS', changes='1,1'),
977 dict(name='project-test2', result='SUCCESS', changes='1,1'),
978 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
979 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
980 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
981 ], ordered=False)
982
983 def test_delayed_merge_conflict(self):
984 "Test that delayed check merge conflicts are handled properly"
985
986 # Hold jobs in the gearman queue so that we can test whether
987 # the executor returns a merge failure after the scheduler has
988 # successfully merged.
989 self.gearman_server.hold_jobs_in_queue = True
990 A = self.fake_gerrit.addFakeChange('org/project',
991 'master', 'A',
992 files={'conflict': 'foo'})
993 B = self.fake_gerrit.addFakeChange('org/project',
994 'master', 'B',
995 files={'conflict': 'bar'})
996 C = self.fake_gerrit.addFakeChange('org/project',
997 'master', 'C')
998 C.setDependsOn(B, 1)
999
1000 # A enters the gate queue; B and C enter the check queue
1001 A.addApproval('code-review', 2)
1002 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1003 self.waitUntilSettled()
1004 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1005 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1006 self.waitUntilSettled()
1007
1008 self.assertEqual(A.reported, 1)
1009 self.assertEqual(B.reported, 0) # Check does not report start
1010 self.assertEqual(C.reported, 0) # Check does not report start
1011
1012 # A merges while B and C are queued in check
1013 # Release A project-merge
1014 queue = self.gearman_server.getQueue()
1015 self.release(queue[0])
1016 self.waitUntilSettled()
1017
1018 # Release A project-test*
1019 # gate has higher precedence, so A's test jobs are added in
1020 # front of the merge jobs for B and C
1021 queue = self.gearman_server.getQueue()
1022 self.release(queue[0])
1023 self.release(queue[1])
1024 self.waitUntilSettled()
1025
1026 self.assertEqual(A.data['status'], 'MERGED')
1027 self.assertEqual(B.data['status'], 'NEW')
1028 self.assertEqual(C.data['status'], 'NEW')
1029 self.assertEqual(A.reported, 2)
1030 self.assertEqual(B.reported, 0)
1031 self.assertEqual(C.reported, 0)
1032 self.assertHistory([
1033 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1034 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1035 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1036 ], ordered=False)
1037
1038 # B and C report merge conflicts
1039 # Release B project-merge
1040 queue = self.gearman_server.getQueue()
1041 self.release(queue[0])
1042 self.waitUntilSettled()
1043
1044 # Release C
1045 self.gearman_server.hold_jobs_in_queue = False
1046 self.gearman_server.release()
1047 self.waitUntilSettled()
1048
1049 self.assertEqual(A.data['status'], 'MERGED')
1050 self.assertEqual(B.data['status'], 'NEW')
1051 self.assertEqual(C.data['status'], 'NEW')
1052 self.assertEqual(A.reported, 2)
1053 self.assertEqual(B.reported, 1)
1054 self.assertEqual(C.reported, 1)
1055
1056 self.assertHistory([
1057 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1058 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1059 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1060 dict(name='project-merge', result='MERGER_FAILURE', changes='2,1'),
1061 dict(name='project-merge', result='MERGER_FAILURE',
1062 changes='2,1 3,1'),
1063 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001064
James E. Blairdaabed22012-08-15 15:38:57 -07001065 def test_post(self):
1066 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001067
Zhongyue Luo5d556072012-09-21 02:00:47 +09001068 e = {
1069 "type": "ref-updated",
1070 "submitter": {
1071 "name": "User Name",
1072 },
1073 "refUpdate": {
1074 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1075 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1076 "refName": "master",
1077 "project": "org/project",
1078 }
1079 }
James E. Blairdaabed22012-08-15 15:38:57 -07001080 self.fake_gerrit.addEvent(e)
1081 self.waitUntilSettled()
1082
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001083 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001084 self.assertEqual(len(self.history), 1)
1085 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001086
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001087 def test_post_ignore_deletes(self):
1088 "Test that deleting refs does not trigger post jobs"
1089
1090 e = {
1091 "type": "ref-updated",
1092 "submitter": {
1093 "name": "User Name",
1094 },
1095 "refUpdate": {
1096 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1097 "newRev": "0000000000000000000000000000000000000000",
1098 "refName": "master",
1099 "project": "org/project",
1100 }
1101 }
1102 self.fake_gerrit.addEvent(e)
1103 self.waitUntilSettled()
1104
1105 job_names = [x.name for x in self.history]
1106 self.assertEqual(len(self.history), 0)
1107 self.assertNotIn('project-post', job_names)
1108
1109 def test_post_ignore_deletes_negative(self):
1110 "Test that deleting refs does trigger post jobs"
1111
Adam Gandelmanc5e4f1d2016-11-29 14:27:17 -08001112 self.updateConfigLayout('layout-dont-ignore-ref-deletes')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001113 self.sched.reconfigure(self.config)
1114
1115 e = {
1116 "type": "ref-updated",
1117 "submitter": {
1118 "name": "User Name",
1119 },
1120 "refUpdate": {
1121 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1122 "newRev": "0000000000000000000000000000000000000000",
1123 "refName": "master",
1124 "project": "org/project",
1125 }
1126 }
1127 self.fake_gerrit.addEvent(e)
1128 self.waitUntilSettled()
1129
1130 job_names = [x.name for x in self.history]
1131 self.assertEqual(len(self.history), 1)
1132 self.assertIn('project-post', job_names)
1133
James E. Blairec056492016-07-22 09:45:56 -07001134 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001135 def test_build_configuration_branch_interaction(self):
1136 "Test that switching between branches works"
1137 self.test_build_configuration()
1138 self.test_build_configuration_branch()
1139 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001140 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001141 repo = git.Repo(path)
1142 repo.heads.master.commit = repo.commit('init')
1143 self.test_build_configuration()
1144
James E. Blaircaec0c52012-08-22 14:52:22 -07001145 def test_dependent_changes_dequeue(self):
1146 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001147
James E. Blaircaec0c52012-08-22 14:52:22 -07001148 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1149 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1150 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001151 A.addApproval('code-review', 2)
1152 B.addApproval('code-review', 2)
1153 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001154
1155 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1156 M1.setMerged()
1157
1158 # C -> B -> A -> M1
1159
1160 C.setDependsOn(B, 1)
1161 B.setDependsOn(A, 1)
1162 A.setDependsOn(M1, 1)
1163
Paul Belanger174a8272017-03-14 13:20:10 -04001164 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001165
James E. Blair8b5408c2016-08-08 15:37:46 -07001166 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1167 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1168 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001169
1170 self.waitUntilSettled()
1171
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001172 self.assertEqual(A.data['status'], 'NEW')
1173 self.assertEqual(A.reported, 2)
1174 self.assertEqual(B.data['status'], 'NEW')
1175 self.assertEqual(B.reported, 2)
1176 self.assertEqual(C.data['status'], 'NEW')
1177 self.assertEqual(C.reported, 2)
1178 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001179
James E. Blair972e3c72013-08-29 12:04:55 -07001180 def test_failing_dependent_changes(self):
1181 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001182 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001183 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1184 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1185 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1186 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1187 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001188 A.addApproval('code-review', 2)
1189 B.addApproval('code-review', 2)
1190 C.addApproval('code-review', 2)
1191 D.addApproval('code-review', 2)
1192 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001193
1194 # E, D -> C -> B, A
1195
1196 D.setDependsOn(C, 1)
1197 C.setDependsOn(B, 1)
1198
Paul Belanger174a8272017-03-14 13:20:10 -04001199 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001200
James E. Blair8b5408c2016-08-08 15:37:46 -07001201 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1202 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1203 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1204 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1205 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001206
1207 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001208 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001209 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001210 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001211 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001212 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001213 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001214 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001215 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001216 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001217 self.waitUntilSettled()
1218
Paul Belanger174a8272017-03-14 13:20:10 -04001219 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001220 for build in self.builds:
1221 if build.parameters['ZUUL_CHANGE'] != '1':
1222 build.release()
1223 self.waitUntilSettled()
1224
Paul Belanger174a8272017-03-14 13:20:10 -04001225 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001226 self.waitUntilSettled()
1227
1228 self.assertEqual(A.data['status'], 'MERGED')
1229 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001230 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001231 self.assertEqual(B.data['status'], 'NEW')
1232 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001233 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001234 self.assertEqual(C.data['status'], 'NEW')
1235 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001236 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001237 self.assertEqual(D.data['status'], 'NEW')
1238 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001239 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001240 self.assertEqual(E.data['status'], 'MERGED')
1241 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001242 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001243 self.assertEqual(len(self.history), 18)
1244
James E. Blairec590122012-08-22 15:19:31 -07001245 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001246 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001247 # If it's dequeued more than once, we should see extra
1248 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001249
Paul Belanger174a8272017-03-14 13:20:10 -04001250 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001251 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1252 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1253 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001254 A.addApproval('code-review', 2)
1255 B.addApproval('code-review', 2)
1256 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001257
Paul Belanger174a8272017-03-14 13:20:10 -04001258 self.executor_server.failJob('project-test1', A)
1259 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001260
James E. Blair8b5408c2016-08-08 15:37:46 -07001261 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1262 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1263 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001264
1265 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001266
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001267 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001268 self.assertEqual(self.builds[0].name, 'project-merge')
1269 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001270
Paul Belanger174a8272017-03-14 13:20:10 -04001271 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001272 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001273 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001274 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001275 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001276 self.waitUntilSettled()
1277
James E. Blair6df67132017-04-19 13:53:20 -07001278 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001279 self.assertEqual(self.builds[0].name, 'project-test1')
1280 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001281 self.assertEqual(self.builds[2].name, 'project-test1')
1282 self.assertEqual(self.builds[3].name, 'project-test2')
1283 self.assertEqual(self.builds[4].name, 'project-test1')
1284 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001285
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001286 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001287 self.waitUntilSettled()
1288
James E. Blair6df67132017-04-19 13:53:20 -07001289 self.assertEqual(len(self.builds), 2) # test2, merge for B
1290 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001291
Paul Belanger174a8272017-03-14 13:20:10 -04001292 self.executor_server.hold_jobs_in_build = False
1293 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001294 self.waitUntilSettled()
1295
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001296 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001297 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001298
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001299 self.assertEqual(A.data['status'], 'NEW')
1300 self.assertEqual(B.data['status'], 'MERGED')
1301 self.assertEqual(C.data['status'], 'MERGED')
1302 self.assertEqual(A.reported, 2)
1303 self.assertEqual(B.reported, 2)
1304 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001305
James E. Blaircc020532017-04-19 13:04:44 -07001306 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001307 def test_nonvoting_job(self):
1308 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001309
James E. Blair4ec821f2012-08-23 15:28:28 -07001310 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1311 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001312 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001313 self.executor_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001314 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001315
1316 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001317
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001318 self.assertEqual(A.data['status'], 'MERGED')
1319 self.assertEqual(A.reported, 2)
1320 self.assertEqual(
1321 self.getJobFromHistory('nonvoting-project-merge').result,
1322 'SUCCESS')
1323 self.assertEqual(
1324 self.getJobFromHistory('nonvoting-project-test1').result,
1325 'SUCCESS')
1326 self.assertEqual(
1327 self.getJobFromHistory('nonvoting-project-test2').result,
1328 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001329
James E. Blair5821bd92015-09-16 08:48:15 -07001330 for build in self.builds:
1331 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1332
James E. Blaire0487072012-08-29 17:38:31 -07001333 def test_check_queue_success(self):
1334 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001335
James E. Blaire0487072012-08-29 17:38:31 -07001336 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1337 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1338
1339 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001340
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001341 self.assertEqual(A.data['status'], 'NEW')
1342 self.assertEqual(A.reported, 1)
1343 self.assertEqual(self.getJobFromHistory('project-merge').result,
1344 'SUCCESS')
1345 self.assertEqual(self.getJobFromHistory('project-test1').result,
1346 'SUCCESS')
1347 self.assertEqual(self.getJobFromHistory('project-test2').result,
1348 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001349
1350 def test_check_queue_failure(self):
1351 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001352
James E. Blaire0487072012-08-29 17:38:31 -07001353 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001354 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001355 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1356
1357 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001358
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001359 self.assertEqual(A.data['status'], 'NEW')
1360 self.assertEqual(A.reported, 1)
1361 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001362 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001363 self.assertEqual(self.getJobFromHistory('project-test1').result,
1364 'SUCCESS')
1365 self.assertEqual(self.getJobFromHistory('project-test2').result,
1366 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001367
James E. Blair7cb84542017-04-19 13:35:05 -07001368 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001369 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001370 # This particular test does a large amount of merges and needs a little
1371 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001372 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001373 "test that dependent changes behind dequeued changes work"
1374 # This complicated test is a reproduction of a real life bug
1375 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001376
Paul Belanger174a8272017-03-14 13:20:10 -04001377 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001378 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1379 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1380 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1381 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1382 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1383 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1384 D.setDependsOn(C, 1)
1385 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001386 A.addApproval('code-review', 2)
1387 B.addApproval('code-review', 2)
1388 C.addApproval('code-review', 2)
1389 D.addApproval('code-review', 2)
1390 E.addApproval('code-review', 2)
1391 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001392
1393 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001394
1395 # Change object re-use in the gerrit trigger is hidden if
1396 # changes are added in quick succession; waiting makes it more
1397 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001398 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001399 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001400 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001401 self.waitUntilSettled()
1402
Paul Belanger174a8272017-03-14 13:20:10 -04001403 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001404 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001405 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001406 self.waitUntilSettled()
1407
James E. Blair8b5408c2016-08-08 15:37:46 -07001408 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001409 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001410 self.fake_gerrit.addEvent(D.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(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001413 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001414 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001415 self.waitUntilSettled()
1416
Paul Belanger174a8272017-03-14 13:20:10 -04001417 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001418 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001419 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001420 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001421 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001422 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001423 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001424 self.waitUntilSettled()
1425
1426 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001427
1428 # Grab pointers to the jobs we want to release before
1429 # releasing any, because list indexes may change as
1430 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001431 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001432 a.release()
1433 b.release()
1434 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001435 self.waitUntilSettled()
1436
Paul Belanger174a8272017-03-14 13:20:10 -04001437 self.executor_server.hold_jobs_in_build = False
1438 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001439 self.waitUntilSettled()
1440
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001441 self.assertEqual(A.data['status'], 'NEW')
1442 self.assertEqual(B.data['status'], 'MERGED')
1443 self.assertEqual(C.data['status'], 'MERGED')
1444 self.assertEqual(D.data['status'], 'MERGED')
1445 self.assertEqual(E.data['status'], 'MERGED')
1446 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001447
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001448 self.assertEqual(A.reported, 2)
1449 self.assertEqual(B.reported, 2)
1450 self.assertEqual(C.reported, 2)
1451 self.assertEqual(D.reported, 2)
1452 self.assertEqual(E.reported, 2)
1453 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001454
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001455 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1456 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001457
1458 def test_merger_repack(self):
1459 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001460
James E. Blair05fed602012-09-07 12:45:24 -07001461 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001462 A.addApproval('code-review', 2)
1463 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001464 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001465 self.assertEqual(self.getJobFromHistory('project-merge').result,
1466 'SUCCESS')
1467 self.assertEqual(self.getJobFromHistory('project-test1').result,
1468 'SUCCESS')
1469 self.assertEqual(self.getJobFromHistory('project-test2').result,
1470 'SUCCESS')
1471 self.assertEqual(A.data['status'], 'MERGED')
1472 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001473 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001474 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001475
Monty Taylord642d852017-02-23 14:05:42 -05001476 path = os.path.join(self.merger_src_root, "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001477 if os.path.exists(path):
1478 repack_repo(path)
Paul Belanger174a8272017-03-14 13:20:10 -04001479 path = os.path.join(self.executor_src_root, "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001480 if os.path.exists(path):
1481 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001482
1483 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001484 A.addApproval('code-review', 2)
1485 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001486 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001487 self.assertEqual(self.getJobFromHistory('project-merge').result,
1488 'SUCCESS')
1489 self.assertEqual(self.getJobFromHistory('project-test1').result,
1490 'SUCCESS')
1491 self.assertEqual(self.getJobFromHistory('project-test2').result,
1492 'SUCCESS')
1493 self.assertEqual(A.data['status'], 'MERGED')
1494 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001495
James E. Blair4886f282012-11-15 09:27:33 -08001496 def test_merger_repack_large_change(self):
1497 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001498 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001499 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001500 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001501 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001502 url = self.fake_gerrit.getGitUrl(project)
James E. Blair6df67132017-04-19 13:53:20 -07001503 self.merge_server.merger.addProject('org/project', url)
1504 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001505 A.addPatchset(large=True)
James E. Blair6df67132017-04-19 13:53:20 -07001506 path = os.path.join(self.upstream_root, "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001507 repack_repo(path)
James E. Blair6df67132017-04-19 13:53:20 -07001508 path = os.path.join(self.merger_src_root, "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001509 if os.path.exists(path):
1510 repack_repo(path)
James E. Blair6df67132017-04-19 13:53:20 -07001511 path = os.path.join(self.executor_src_root, "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001512 if os.path.exists(path):
1513 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001514
James E. Blair8b5408c2016-08-08 15:37:46 -07001515 A.addApproval('code-review', 2)
1516 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001517 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001518 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001519 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001520 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001521 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001522 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001523 'SUCCESS')
1524 self.assertEqual(A.data['status'], 'MERGED')
1525 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001526
James E. Blair2fa50962013-01-30 21:50:41 -08001527 def test_new_patchset_dequeues_old(self):
1528 "Test that a new patchset causes the old to be dequeued"
1529 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001530 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001531 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1532 M.setMerged()
1533
1534 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1535 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1536 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1537 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001538 A.addApproval('code-review', 2)
1539 B.addApproval('code-review', 2)
1540 C.addApproval('code-review', 2)
1541 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001542
1543 C.setDependsOn(B, 1)
1544 B.setDependsOn(A, 1)
1545 A.setDependsOn(M, 1)
1546
James E. Blair8b5408c2016-08-08 15:37:46 -07001547 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1548 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1549 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1550 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001551 self.waitUntilSettled()
1552
1553 B.addPatchset()
1554 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1555 self.waitUntilSettled()
1556
Paul Belanger174a8272017-03-14 13:20:10 -04001557 self.executor_server.hold_jobs_in_build = False
1558 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001559 self.waitUntilSettled()
1560
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001561 self.assertEqual(A.data['status'], 'MERGED')
1562 self.assertEqual(A.reported, 2)
1563 self.assertEqual(B.data['status'], 'NEW')
1564 self.assertEqual(B.reported, 2)
1565 self.assertEqual(C.data['status'], 'NEW')
1566 self.assertEqual(C.reported, 2)
1567 self.assertEqual(D.data['status'], 'MERGED')
1568 self.assertEqual(D.reported, 2)
1569 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001570
James E. Blairba437362015-02-07 11:41:52 -08001571 def test_new_patchset_check(self):
1572 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001573
Paul Belanger174a8272017-03-14 13:20:10 -04001574 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001575
1576 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001577 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001578 tenant = self.sched.abide.tenants.get('tenant-one')
1579 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001580
1581 # Add two git-dependent changes
1582 B.setDependsOn(A, 1)
1583 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1584 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001585 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1586 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001587
1588 # A live item, and a non-live/live pair
1589 items = check_pipeline.getAllItems()
1590 self.assertEqual(len(items), 3)
1591
1592 self.assertEqual(items[0].change.number, '1')
1593 self.assertEqual(items[0].change.patchset, '1')
1594 self.assertFalse(items[0].live)
1595
1596 self.assertEqual(items[1].change.number, '2')
1597 self.assertEqual(items[1].change.patchset, '1')
1598 self.assertTrue(items[1].live)
1599
1600 self.assertEqual(items[2].change.number, '1')
1601 self.assertEqual(items[2].change.patchset, '1')
1602 self.assertTrue(items[2].live)
1603
1604 # Add a new patchset to A
1605 A.addPatchset()
1606 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1607 self.waitUntilSettled()
1608
1609 # The live copy of A,1 should be gone, but the non-live and B
1610 # should continue, and we should have a new A,2
1611 items = check_pipeline.getAllItems()
1612 self.assertEqual(len(items), 3)
1613
1614 self.assertEqual(items[0].change.number, '1')
1615 self.assertEqual(items[0].change.patchset, '1')
1616 self.assertFalse(items[0].live)
1617
1618 self.assertEqual(items[1].change.number, '2')
1619 self.assertEqual(items[1].change.patchset, '1')
1620 self.assertTrue(items[1].live)
1621
1622 self.assertEqual(items[2].change.number, '1')
1623 self.assertEqual(items[2].change.patchset, '2')
1624 self.assertTrue(items[2].live)
1625
1626 # Add a new patchset to B
1627 B.addPatchset()
1628 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1629 self.waitUntilSettled()
1630
1631 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1632 # but we should have a new B,2 (still based on A,1)
1633 items = check_pipeline.getAllItems()
1634 self.assertEqual(len(items), 3)
1635
1636 self.assertEqual(items[0].change.number, '1')
1637 self.assertEqual(items[0].change.patchset, '2')
1638 self.assertTrue(items[0].live)
1639
1640 self.assertEqual(items[1].change.number, '1')
1641 self.assertEqual(items[1].change.patchset, '1')
1642 self.assertFalse(items[1].live)
1643
1644 self.assertEqual(items[2].change.number, '2')
1645 self.assertEqual(items[2].change.patchset, '2')
1646 self.assertTrue(items[2].live)
1647
1648 self.builds[0].release()
1649 self.waitUntilSettled()
1650 self.builds[0].release()
1651 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001652 self.executor_server.hold_jobs_in_build = False
1653 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001654 self.waitUntilSettled()
1655
1656 self.assertEqual(A.reported, 1)
1657 self.assertEqual(B.reported, 1)
1658 self.assertEqual(self.history[0].result, 'ABORTED')
1659 self.assertEqual(self.history[0].changes, '1,1')
1660 self.assertEqual(self.history[1].result, 'ABORTED')
1661 self.assertEqual(self.history[1].changes, '1,1 2,1')
1662 self.assertEqual(self.history[2].result, 'SUCCESS')
1663 self.assertEqual(self.history[2].changes, '1,2')
1664 self.assertEqual(self.history[3].result, 'SUCCESS')
1665 self.assertEqual(self.history[3].changes, '1,1 2,2')
1666
1667 def test_abandoned_gate(self):
1668 "Test that an abandoned change is dequeued from gate"
1669
Paul Belanger174a8272017-03-14 13:20:10 -04001670 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001671
1672 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001673 A.addApproval('code-review', 2)
1674 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001675 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001676 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1677 self.assertEqual(self.builds[0].name, 'project-merge')
1678
1679 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1680 self.waitUntilSettled()
1681
Paul Belanger174a8272017-03-14 13:20:10 -04001682 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001683 self.waitUntilSettled()
1684
Arie5b4048c2016-10-05 16:12:06 +03001685 self.assertBuilds([])
1686 self.assertHistory([
1687 dict(name='project-merge', result='ABORTED', changes='1,1')],
1688 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001689 self.assertEqual(A.reported, 1,
1690 "Abandoned gate change should report only start")
1691
1692 def test_abandoned_check(self):
1693 "Test that an abandoned change is dequeued from check"
1694
Paul Belanger174a8272017-03-14 13:20:10 -04001695 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001696
1697 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1698 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001699 tenant = self.sched.abide.tenants.get('tenant-one')
1700 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001701
1702 # Add two git-dependent changes
1703 B.setDependsOn(A, 1)
1704 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1705 self.waitUntilSettled()
1706 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1707 self.waitUntilSettled()
1708 # A live item, and a non-live/live pair
1709 items = check_pipeline.getAllItems()
1710 self.assertEqual(len(items), 3)
1711
1712 self.assertEqual(items[0].change.number, '1')
1713 self.assertFalse(items[0].live)
1714
1715 self.assertEqual(items[1].change.number, '2')
1716 self.assertTrue(items[1].live)
1717
1718 self.assertEqual(items[2].change.number, '1')
1719 self.assertTrue(items[2].live)
1720
1721 # Abandon A
1722 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1723 self.waitUntilSettled()
1724
1725 # The live copy of A should be gone, but the non-live and B
1726 # should continue
1727 items = check_pipeline.getAllItems()
1728 self.assertEqual(len(items), 2)
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
Paul Belanger174a8272017-03-14 13:20:10 -04001736 self.executor_server.hold_jobs_in_build = False
1737 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001738 self.waitUntilSettled()
1739
1740 self.assertEqual(len(self.history), 4)
1741 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001742 'Build should have been aborted')
1743 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001744 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001745
Steve Varnau7b78b312015-04-03 14:49:46 -07001746 def test_abandoned_not_timer(self):
1747 "Test that an abandoned change does not cancel timer jobs"
1748
Paul Belanger174a8272017-03-14 13:20:10 -04001749 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001750
1751 # Start timer trigger - also org/project
Clint Byrumead6c562017-02-01 16:34:04 -08001752 self.updateConfigLayout('layout-idle')
Steve Varnau7b78b312015-04-03 14:49:46 -07001753 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001754 # The pipeline triggers every second, so we should have seen
1755 # several by now.
1756 time.sleep(5)
1757 self.waitUntilSettled()
1758 # Stop queuing timer triggered jobs so that the assertions
1759 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001760 # Must be in same repo, so overwrite config with another one
Clint Byrum58264dc2017-02-07 21:21:22 -08001761 self.commitLayoutUpdate('layout-idle', 'layout-no-timer')
Clint Byrumead6c562017-02-01 16:34:04 -08001762
Steve Varnau7b78b312015-04-03 14:49:46 -07001763 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001764 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1765
1766 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1767 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1768 self.waitUntilSettled()
1769 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1770
1771 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1772 self.waitUntilSettled()
1773
1774 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1775
Paul Belanger174a8272017-03-14 13:20:10 -04001776 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001777 self.waitUntilSettled()
1778
Arx Cruzb1b010d2013-10-28 19:49:59 -02001779 def test_zuul_url_return(self):
1780 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001781 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Paul Belanger174a8272017-03-14 13:20:10 -04001782 self.executor_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001783
1784 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001785 A.addApproval('code-review', 2)
1786 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001787 self.waitUntilSettled()
1788
1789 self.assertEqual(len(self.builds), 1)
1790 for build in self.builds:
1791 self.assertTrue('ZUUL_URL' in build.parameters)
1792
Paul Belanger174a8272017-03-14 13:20:10 -04001793 self.executor_server.hold_jobs_in_build = False
1794 self.executor_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001795 self.waitUntilSettled()
1796
James E. Blair2fa50962013-01-30 21:50:41 -08001797 def test_new_patchset_dequeues_old_on_head(self):
1798 "Test that a new patchset causes the old to be dequeued (at head)"
1799 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001800 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001801 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1802 M.setMerged()
1803 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1804 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1805 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1806 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001807 A.addApproval('code-review', 2)
1808 B.addApproval('code-review', 2)
1809 C.addApproval('code-review', 2)
1810 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001811
1812 C.setDependsOn(B, 1)
1813 B.setDependsOn(A, 1)
1814 A.setDependsOn(M, 1)
1815
James E. Blair8b5408c2016-08-08 15:37:46 -07001816 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1817 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1818 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1819 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001820 self.waitUntilSettled()
1821
1822 A.addPatchset()
1823 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1824 self.waitUntilSettled()
1825
Paul Belanger174a8272017-03-14 13:20:10 -04001826 self.executor_server.hold_jobs_in_build = False
1827 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001828 self.waitUntilSettled()
1829
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001830 self.assertEqual(A.data['status'], 'NEW')
1831 self.assertEqual(A.reported, 2)
1832 self.assertEqual(B.data['status'], 'NEW')
1833 self.assertEqual(B.reported, 2)
1834 self.assertEqual(C.data['status'], 'NEW')
1835 self.assertEqual(C.reported, 2)
1836 self.assertEqual(D.data['status'], 'MERGED')
1837 self.assertEqual(D.reported, 2)
1838 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001839
1840 def test_new_patchset_dequeues_old_without_dependents(self):
1841 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001842 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001843 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1844 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1845 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001846 A.addApproval('code-review', 2)
1847 B.addApproval('code-review', 2)
1848 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001849
James E. Blair8b5408c2016-08-08 15:37:46 -07001850 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1851 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1852 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001853 self.waitUntilSettled()
1854
1855 B.addPatchset()
1856 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1857 self.waitUntilSettled()
1858
Paul Belanger174a8272017-03-14 13:20:10 -04001859 self.executor_server.hold_jobs_in_build = False
1860 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001861 self.waitUntilSettled()
1862
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001863 self.assertEqual(A.data['status'], 'MERGED')
1864 self.assertEqual(A.reported, 2)
1865 self.assertEqual(B.data['status'], 'NEW')
1866 self.assertEqual(B.reported, 2)
1867 self.assertEqual(C.data['status'], 'MERGED')
1868 self.assertEqual(C.reported, 2)
1869 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001870
1871 def test_new_patchset_dequeues_old_independent_queue(self):
1872 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001873 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001874 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1875 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1876 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1877 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1878 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1879 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1880 self.waitUntilSettled()
1881
1882 B.addPatchset()
1883 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1884 self.waitUntilSettled()
1885
Paul Belanger174a8272017-03-14 13:20:10 -04001886 self.executor_server.hold_jobs_in_build = False
1887 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001888 self.waitUntilSettled()
1889
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001890 self.assertEqual(A.data['status'], 'NEW')
1891 self.assertEqual(A.reported, 1)
1892 self.assertEqual(B.data['status'], 'NEW')
1893 self.assertEqual(B.reported, 1)
1894 self.assertEqual(C.data['status'], 'NEW')
1895 self.assertEqual(C.reported, 1)
1896 self.assertEqual(len(self.history), 10)
1897 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001898
James E. Blaircc020532017-04-19 13:04:44 -07001899 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07001900 def test_noop_job(self):
1901 "Test that the internal noop job works"
1902 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001903 A.addApproval('code-review', 2)
1904 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001905 self.waitUntilSettled()
1906
1907 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1908 self.assertTrue(self.sched._areAllBuildsComplete())
1909 self.assertEqual(len(self.history), 0)
1910 self.assertEqual(A.data['status'], 'MERGED')
1911 self.assertEqual(A.reported, 2)
1912
James E. Blair06cc3922017-04-19 10:08:10 -07001913 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001914 def test_no_job_project(self):
1915 "Test that reports with no jobs don't get sent"
1916 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1917 'master', 'A')
1918 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1919 self.waitUntilSettled()
1920
1921 # Change wasn't reported to
1922 self.assertEqual(A.reported, False)
1923
1924 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001925 tenant = self.sched.abide.tenants.get('tenant-one')
1926 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001927 items = check_pipeline.getAllItems()
1928 self.assertEqual(len(items), 0)
1929
1930 self.assertEqual(len(self.history), 0)
1931
James E. Blair7d0dedc2013-02-21 17:26:09 -08001932 def test_zuul_refs(self):
1933 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04001934 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001935 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1936 M1.setMerged()
1937 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1938 M2.setMerged()
1939
1940 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1941 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1942 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1943 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001944 A.addApproval('code-review', 2)
1945 B.addApproval('code-review', 2)
1946 C.addApproval('code-review', 2)
1947 D.addApproval('code-review', 2)
1948 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1949 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1950 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1951 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001952
1953 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001954 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001955 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001956 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001957 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001958 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001959 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001960 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001961 self.waitUntilSettled()
1962
James E. Blair7d0dedc2013-02-21 17:26:09 -08001963 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08001964 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001965 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001966 if x.parameters['ZUUL_CHANGE'] == '3':
1967 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001968 a_build = x
1969 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001970 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001971 b_build = x
1972 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001973 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001974 c_build = x
1975 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001976 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001977 d_build = x
1978 if a_build and b_build and c_build and d_build:
1979 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08001980
1981 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001982 self.assertIsNotNone(a_zref)
1983 self.assertIsNotNone(b_zref)
1984 self.assertIsNotNone(c_zref)
1985 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001986
1987 # And they should all be different
1988 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001989 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001990
Clint Byrum3343e3e2016-11-15 16:05:03 -08001991 # should have a, not b, and should not be in project2
1992 self.assertTrue(a_build.hasChanges(A))
1993 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001994
Clint Byrum3343e3e2016-11-15 16:05:03 -08001995 # should have a and b, and should not be in project2
1996 self.assertTrue(b_build.hasChanges(A, B))
1997 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001998
Clint Byrum3343e3e2016-11-15 16:05:03 -08001999 # should have a and b in 1, c in 2
2000 self.assertTrue(c_build.hasChanges(A, B, C))
2001 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002002
Clint Byrum3343e3e2016-11-15 16:05:03 -08002003 # should have a and b in 1, c and d in 2
2004 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002005
Paul Belanger174a8272017-03-14 13:20:10 -04002006 self.executor_server.hold_jobs_in_build = False
2007 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002008 self.waitUntilSettled()
2009
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002010 self.assertEqual(A.data['status'], 'MERGED')
2011 self.assertEqual(A.reported, 2)
2012 self.assertEqual(B.data['status'], 'MERGED')
2013 self.assertEqual(B.reported, 2)
2014 self.assertEqual(C.data['status'], 'MERGED')
2015 self.assertEqual(C.reported, 2)
2016 self.assertEqual(D.data['status'], 'MERGED')
2017 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002018
James E. Blair4a28a882013-08-23 15:17:33 -07002019 def test_rerun_on_error(self):
2020 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002021 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002022 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002023 A.addApproval('code-review', 2)
2024 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002025 self.waitUntilSettled()
2026
James E. Blair412fba82017-01-26 15:00:50 -08002027 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002028 self.executor_server.hold_jobs_in_build = False
2029 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002030 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002031 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002032 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2033
James E. Blair412e5582013-04-22 15:50:12 -07002034 def test_statsd(self):
2035 "Test each of the statsd methods used in the scheduler"
2036 import extras
2037 statsd = extras.try_import('statsd.statsd')
2038 statsd.incr('test-incr')
2039 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002040 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002041 self.assertReportedStat('test-incr', '1|c')
2042 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002043 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002044
James E. Blairdad52252014-02-07 16:59:17 -08002045 def test_stuck_job_cleanup(self):
2046 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002047
2048 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002049 self.gearman_server.hold_jobs_in_queue = True
2050 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002051 A.addApproval('code-review', 2)
2052 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002053 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002054 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002055 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2056
Clint Byrum678e2c32017-03-16 16:27:21 -07002057 self.commitLayoutUpdate('common-config', 'layout-no-jobs')
James E. Blairdad52252014-02-07 16:59:17 -08002058 self.sched.reconfigure(self.config)
2059 self.waitUntilSettled()
2060
James E. Blair18c64442014-03-18 10:14:45 -07002061 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002062 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002063 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002064 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2065 self.assertTrue(self.sched._areAllBuildsComplete())
2066
2067 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002068 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002069 self.assertEqual(self.history[0].result, 'SUCCESS')
2070
James E. Blair879dafb2015-07-17 14:04:49 -07002071 def test_file_head(self):
2072 # This is a regression test for an observed bug. A change
2073 # with a file named "HEAD" in the root directory of the repo
2074 # was processed by a merger. It then was unable to reset the
2075 # repo because of:
2076 # GitCommandError: 'git reset --hard HEAD' returned
2077 # with exit code 128
2078 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2079 # and filename
2080 # Use '--' to separate filenames from revisions'
2081
2082 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002083 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002084 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2085
2086 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2087 self.waitUntilSettled()
2088
2089 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2090 self.waitUntilSettled()
2091
2092 self.assertIn('Build succeeded', A.messages[0])
2093 self.assertIn('Build succeeded', B.messages[0])
2094
James E. Blair70c71582013-03-06 08:50:50 -08002095 def test_file_jobs(self):
2096 "Test that file jobs run only when appropriate"
2097 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002098 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002099 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002100 A.addApproval('code-review', 2)
2101 B.addApproval('code-review', 2)
2102 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2103 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002104 self.waitUntilSettled()
2105
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002106 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002107 if x.name == 'project-testfile']
2108
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002109 self.assertEqual(len(testfile_jobs), 1)
2110 self.assertEqual(testfile_jobs[0].changes, '1,2')
2111 self.assertEqual(A.data['status'], 'MERGED')
2112 self.assertEqual(A.reported, 2)
2113 self.assertEqual(B.data['status'], 'MERGED')
2114 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002115
Clint Byrum3ee39f32016-11-17 23:45:07 -08002116 def _test_irrelevant_files_jobs(self, should_skip):
2117 "Test that jobs with irrelevant-files filter run only when appropriate"
2118 self.updateConfigLayout('layout-irrelevant-files')
Maru Newby3fe5f852015-01-13 04:22:14 +00002119 self.sched.reconfigure(self.config)
Clint Byrum3ee39f32016-11-17 23:45:07 -08002120
2121 if should_skip:
2122 files = {'ignoreme': 'ignored\n'}
2123 else:
2124 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002125
2126 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002127 'master',
2128 'test irrelevant-files',
2129 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002130 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2131 self.waitUntilSettled()
2132
2133 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002134 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002135
2136 if should_skip:
2137 self.assertEqual([], tested_change_ids)
2138 else:
2139 self.assertIn(change.data['number'], tested_change_ids)
2140
Clint Byrum3ee39f32016-11-17 23:45:07 -08002141 def test_irrelevant_files_match_skips_job(self):
2142 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002143
Clint Byrum3ee39f32016-11-17 23:45:07 -08002144 def test_irrelevant_files_no_match_runs_job(self):
2145 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002146
Clint Byrum85493602016-11-18 11:59:47 -08002147 def test_inherited_jobs_keep_matchers(self):
2148 self.updateConfigLayout('layout-inheritance')
2149 self.sched.reconfigure(self.config)
2150
2151 files = {'ignoreme': 'ignored\n'}
2152
2153 change = self.fake_gerrit.addFakeChange('org/project',
2154 'master',
2155 'test irrelevant-files',
2156 files=files)
2157 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2158 self.waitUntilSettled()
2159
2160 run_jobs = set([build.name for build in self.history])
2161
2162 self.assertEqual(set(['project-test-nomatch-starts-empty',
2163 'project-test-nomatch-starts-full']), run_jobs)
2164
James E. Blairec056492016-07-22 09:45:56 -07002165 @skip("Disabled for early v3 development")
James E. Blair3c5e5b52013-04-26 11:17:03 -07002166 def test_test_config(self):
2167 "Test that we can test the config"
James E. Blairf84026c2015-12-08 16:11:46 -08002168 self.sched.testConfig(self.config.get('zuul', 'tenant_config'),
Joshua Hesketh352264b2015-08-11 23:42:08 +10002169 self.connections)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002170
James E. Blairc8a1e052014-02-25 09:29:26 -08002171 def test_queue_names(self):
2172 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002173 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002174 (trusted, project1) = tenant.getProject('org/project1')
2175 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002176 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2177 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2178 self.assertEqual(q1.name, 'integrated')
2179 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002180
James E. Blair64ed6f22013-07-10 14:07:23 -07002181 def test_queue_precedence(self):
2182 "Test that queue precedence works"
2183
2184 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002185 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002186 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2187 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002188 A.addApproval('code-review', 2)
2189 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002190
2191 self.waitUntilSettled()
2192 self.gearman_server.hold_jobs_in_queue = False
2193 self.gearman_server.release()
2194 self.waitUntilSettled()
2195
James E. Blair8de58bd2013-07-18 16:23:33 -07002196 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002197 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002198 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002199 self.waitUntilSettled()
2200
James E. Blair64ed6f22013-07-10 14:07:23 -07002201 self.log.debug(self.history)
2202 self.assertEqual(self.history[0].pipeline, 'gate')
2203 self.assertEqual(self.history[1].pipeline, 'check')
2204 self.assertEqual(self.history[2].pipeline, 'gate')
2205 self.assertEqual(self.history[3].pipeline, 'gate')
2206 self.assertEqual(self.history[4].pipeline, 'check')
2207 self.assertEqual(self.history[5].pipeline, 'check')
2208
Clark Boylana5edbe42014-06-03 16:39:10 -07002209 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002210 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002211 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002212 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002213 A.addApproval('code-review', 2)
2214 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002215 self.waitUntilSettled()
2216
Paul Belanger174a8272017-03-14 13:20:10 -04002217 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002218 self.waitUntilSettled()
2219
James E. Blair1843a552013-07-03 14:19:52 -07002220 port = self.webapp.server.socket.getsockname()[1]
2221
Adam Gandelman77a12c72017-02-03 17:43:43 -08002222 req = urllib.request.Request(
2223 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002224 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002225 headers = f.info()
2226 self.assertIn('Content-Length', headers)
2227 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002228 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2229 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002230 self.assertIn('Access-Control-Allow-Origin', headers)
2231 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002232 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002233 self.assertIn('Expires', headers)
James E. Blair1843a552013-07-03 14:19:52 -07002234 data = f.read()
2235
Paul Belanger174a8272017-03-14 13:20:10 -04002236 self.executor_server.hold_jobs_in_build = False
2237 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002238 self.waitUntilSettled()
2239
2240 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002241 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002242 for p in data['pipelines']:
2243 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002244 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002245 self.assertEqual(q['window'], 20)
2246 else:
2247 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002248 for head in q['heads']:
2249 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002250 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002251 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002252 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002253 status_jobs.append(job)
2254 self.assertEqual('project-merge', status_jobs[0]['name'])
2255 self.assertEqual('https://server/job/project-merge/0/',
2256 status_jobs[0]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002257 self.assertEqual('https://server/job/project-merge/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002258 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002259 self.assertEqual('project-test1', status_jobs[1]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002260 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002261 status_jobs[1]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002262 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002263 status_jobs[1]['report_url'])
2264
2265 self.assertEqual('project-test2', status_jobs[2]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002266 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002267 status_jobs[2]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002268 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002269 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002270
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002271 def test_semaphore_one(self):
2272 "Test semaphores with max=1 (mutex)"
2273 self.updateConfigLayout('layout-semaphore')
James E. Blairaf17a972016-02-03 15:07:18 -08002274 self.sched.reconfigure(self.config)
2275
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002276 self.waitUntilSettled()
2277 tenant = self.sched.abide.tenants.get('openstack')
2278
Paul Belanger174a8272017-03-14 13:20:10 -04002279 self.executor_server.hold_jobs_in_build = True
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002280
James E. Blairaf17a972016-02-03 15:07:18 -08002281 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2282 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002283 self.assertFalse('test-semaphore' in
2284 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002285
2286 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2287 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2288 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002289
James E. Blairaf17a972016-02-03 15:07:18 -08002290 self.assertEqual(len(self.builds), 3)
2291 self.assertEqual(self.builds[0].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002292 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
James E. Blairaf17a972016-02-03 15:07:18 -08002293 self.assertEqual(self.builds[2].name, 'project-test1')
2294
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002295 self.executor_server.release('semaphore-one-test1')
James E. Blairaf17a972016-02-03 15:07:18 -08002296 self.waitUntilSettled()
2297
2298 self.assertEqual(len(self.builds), 3)
2299 self.assertEqual(self.builds[0].name, 'project-test1')
2300 self.assertEqual(self.builds[1].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002301 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
2302 self.assertTrue('test-semaphore' in
2303 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002304
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002305 self.executor_server.release('semaphore-one-test2')
James E. Blairaf17a972016-02-03 15:07:18 -08002306 self.waitUntilSettled()
2307
2308 self.assertEqual(len(self.builds), 3)
2309 self.assertEqual(self.builds[0].name, 'project-test1')
2310 self.assertEqual(self.builds[1].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002311 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
2312 self.assertTrue('test-semaphore' in
2313 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002314
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002315 self.executor_server.release('semaphore-one-test1')
James E. Blairaf17a972016-02-03 15:07:18 -08002316 self.waitUntilSettled()
2317
2318 self.assertEqual(len(self.builds), 3)
2319 self.assertEqual(self.builds[0].name, 'project-test1')
2320 self.assertEqual(self.builds[1].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002321 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
2322 self.assertTrue('test-semaphore' in
2323 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002324
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002325 self.executor_server.release('semaphore-one-test2')
James E. Blairaf17a972016-02-03 15:07:18 -08002326 self.waitUntilSettled()
2327
2328 self.assertEqual(len(self.builds), 2)
2329 self.assertEqual(self.builds[0].name, 'project-test1')
2330 self.assertEqual(self.builds[1].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002331 self.assertFalse('test-semaphore' in
2332 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002333
Paul Belanger174a8272017-03-14 13:20:10 -04002334 self.executor_server.hold_jobs_in_build = False
2335 self.executor_server.release()
James E. Blairaf17a972016-02-03 15:07:18 -08002336
2337 self.waitUntilSettled()
2338 self.assertEqual(len(self.builds), 0)
2339
2340 self.assertEqual(A.reported, 1)
2341 self.assertEqual(B.reported, 1)
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002342 self.assertFalse('test-semaphore' in
2343 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002344
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002345 def test_semaphore_two(self):
2346 "Test semaphores with max>1"
2347 self.updateConfigLayout('layout-semaphore')
Tobias Henkelfb91a492017-02-15 07:29:43 +01002348 self.sched.reconfigure(self.config)
2349
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002350 self.waitUntilSettled()
2351 tenant = self.sched.abide.tenants.get('openstack')
2352
2353 self.executor_server.hold_jobs_in_build = True
2354 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2355 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2356 self.assertFalse('test-semaphore-two' in
2357 tenant.semaphore_handler.semaphores)
2358
2359 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2360 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2361 self.waitUntilSettled()
2362
2363 self.assertEqual(len(self.builds), 4)
2364 self.assertEqual(self.builds[0].name, 'project-test1')
2365 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
2366 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
2367 self.assertEqual(self.builds[3].name, 'project-test1')
2368 self.assertTrue('test-semaphore-two' in
2369 tenant.semaphore_handler.semaphores)
2370 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
2371 'test-semaphore-two', [])), 2)
2372
2373 self.executor_server.release('semaphore-two-test1')
2374 self.waitUntilSettled()
2375
2376 self.assertEqual(len(self.builds), 4)
2377 self.assertEqual(self.builds[0].name, 'project-test1')
2378 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
2379 self.assertEqual(self.builds[2].name, 'project-test1')
2380 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
2381 self.assertTrue('test-semaphore-two' in
2382 tenant.semaphore_handler.semaphores)
2383 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
2384 'test-semaphore-two', [])), 2)
2385
2386 self.executor_server.release('semaphore-two-test2')
2387 self.waitUntilSettled()
2388
2389 self.assertEqual(len(self.builds), 4)
2390 self.assertEqual(self.builds[0].name, 'project-test1')
2391 self.assertEqual(self.builds[1].name, 'project-test1')
2392 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
2393 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
2394 self.assertTrue('test-semaphore-two' in
2395 tenant.semaphore_handler.semaphores)
2396 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
2397 'test-semaphore-two', [])), 2)
2398
2399 self.executor_server.release('semaphore-two-test1')
2400 self.waitUntilSettled()
2401
2402 self.assertEqual(len(self.builds), 3)
2403 self.assertEqual(self.builds[0].name, 'project-test1')
2404 self.assertEqual(self.builds[1].name, 'project-test1')
2405 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
2406 self.assertTrue('test-semaphore-two' in
2407 tenant.semaphore_handler.semaphores)
2408 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
2409 'test-semaphore-two', [])), 1)
2410
2411 self.executor_server.release('semaphore-two-test2')
2412 self.waitUntilSettled()
2413
2414 self.assertEqual(len(self.builds), 2)
2415 self.assertEqual(self.builds[0].name, 'project-test1')
2416 self.assertEqual(self.builds[1].name, 'project-test1')
2417 self.assertFalse('test-semaphore-two' in
2418 tenant.semaphore_handler.semaphores)
2419
2420 self.executor_server.hold_jobs_in_build = False
2421 self.executor_server.release()
2422
2423 self.waitUntilSettled()
2424 self.assertEqual(len(self.builds), 0)
2425
2426 self.assertEqual(A.reported, 1)
2427 self.assertEqual(B.reported, 1)
2428
2429 def test_semaphore_abandon(self):
2430 "Test abandon with job semaphores"
2431 self.updateConfigLayout('layout-semaphore')
2432 self.sched.reconfigure(self.config)
2433
2434 self.waitUntilSettled()
2435 tenant = self.sched.abide.tenants.get('openstack')
2436
Paul Belanger174a8272017-03-14 13:20:10 -04002437 self.executor_server.hold_jobs_in_build = True
Tobias Henkelfb91a492017-02-15 07:29:43 +01002438
2439 tenant = self.sched.abide.tenants.get('openstack')
2440 check_pipeline = tenant.layout.pipelines['check']
2441
2442 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002443 self.assertFalse('test-semaphore' in
2444 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002445
2446 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2447 self.waitUntilSettled()
2448
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002449 self.assertTrue('test-semaphore' in
2450 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002451
2452 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
2453 self.waitUntilSettled()
2454
2455 # The check pipeline should be empty
2456 items = check_pipeline.getAllItems()
2457 self.assertEqual(len(items), 0)
2458
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002459 # The semaphore should be released
2460 self.assertFalse('test-semaphore' in
2461 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002462
Paul Belanger174a8272017-03-14 13:20:10 -04002463 self.executor_server.hold_jobs_in_build = False
2464 self.executor_server.release()
Tobias Henkelfb91a492017-02-15 07:29:43 +01002465 self.waitUntilSettled()
2466
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002467 def test_semaphore_reconfigure(self):
2468 "Test reconfigure with job semaphores"
2469 self.updateConfigLayout('layout-semaphore')
Tobias Henkelfb91a492017-02-15 07:29:43 +01002470 self.sched.reconfigure(self.config)
2471
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002472 self.waitUntilSettled()
2473 tenant = self.sched.abide.tenants.get('openstack')
2474
Paul Belanger174a8272017-03-14 13:20:10 -04002475 self.executor_server.hold_jobs_in_build = True
Tobias Henkelfb91a492017-02-15 07:29:43 +01002476
2477 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002478 self.assertFalse('test-semaphore' in
2479 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002480
2481 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2482 self.waitUntilSettled()
2483
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002484 self.assertTrue('test-semaphore' in
2485 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002486
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002487 # reconfigure without layout change
Tobias Henkelfb91a492017-02-15 07:29:43 +01002488 self.sched.reconfigure(self.config)
2489 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002490 tenant = self.sched.abide.tenants.get('openstack')
2491
2492 # semaphore still must be held
2493 self.assertTrue('test-semaphore' in
2494 tenant.semaphore_handler.semaphores)
2495
2496 self.updateConfigLayout('layout-semaphore-reconfiguration')
2497 self.sched.reconfigure(self.config)
2498 self.waitUntilSettled()
2499 tenant = self.sched.abide.tenants.get('openstack')
Tobias Henkelfb91a492017-02-15 07:29:43 +01002500
Paul Belanger174a8272017-03-14 13:20:10 -04002501 self.executor_server.release('project-test1')
Tobias Henkelfb91a492017-02-15 07:29:43 +01002502 self.waitUntilSettled()
2503
2504 # There should be no builds anymore
2505 self.assertEqual(len(self.builds), 0)
2506
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002507 # The semaphore should be released
2508 self.assertFalse('test-semaphore' in
2509 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002510
James E. Blaircdccd972013-07-01 12:10:22 -07002511 def test_live_reconfiguration(self):
2512 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002513 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002514 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002515 A.addApproval('code-review', 2)
2516 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002517 self.waitUntilSettled()
2518
2519 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002520 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002521
Paul Belanger174a8272017-03-14 13:20:10 -04002522 self.executor_server.hold_jobs_in_build = False
2523 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002524 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002525 self.assertEqual(self.getJobFromHistory('project-merge').result,
2526 'SUCCESS')
2527 self.assertEqual(self.getJobFromHistory('project-test1').result,
2528 'SUCCESS')
2529 self.assertEqual(self.getJobFromHistory('project-test2').result,
2530 'SUCCESS')
2531 self.assertEqual(A.data['status'], 'MERGED')
2532 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002533
James E. Blairec056492016-07-22 09:45:56 -07002534 @skip("Disabled for early v3 development")
James E. Blair6bc782d2015-07-17 16:20:21 -07002535 def test_live_reconfiguration_merge_conflict(self):
2536 # A real-world bug: a change in a gate queue has a merge
2537 # conflict and a job is added to its project while it's
2538 # sitting in the queue. The job gets added to the change and
2539 # enqueued and the change gets stuck.
2540 self.worker.registerFunction('build:project-test3')
Paul Belanger174a8272017-03-14 13:20:10 -04002541 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002542
2543 # This change is fine. It's here to stop the queue long
2544 # enough for the next change to be subject to the
2545 # reconfiguration, as well as to provide a conflict for the
2546 # next change. This change will succeed and merge.
2547 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2548 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002549 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002550
2551 # This change will be in merge conflict. During the
2552 # reconfiguration, we will add a job. We want to make sure
2553 # that doesn't cause it to get stuck.
2554 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2555 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002556 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002557
James E. Blair8b5408c2016-08-08 15:37:46 -07002558 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2559 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002560
2561 self.waitUntilSettled()
2562
2563 # No jobs have run yet
2564 self.assertEqual(A.data['status'], 'NEW')
2565 self.assertEqual(A.reported, 1)
2566 self.assertEqual(B.data['status'], 'NEW')
2567 self.assertEqual(B.reported, 1)
2568 self.assertEqual(len(self.history), 0)
2569
2570 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002571 self.updateConfigLayout(
2572 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002573 self.sched.reconfigure(self.config)
2574 self.waitUntilSettled()
2575
Paul Belanger174a8272017-03-14 13:20:10 -04002576 self.executor_server.hold_jobs_in_build = False
2577 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002578 self.waitUntilSettled()
2579
2580 self.assertEqual(A.data['status'], 'MERGED')
2581 self.assertEqual(A.reported, 2)
2582 self.assertEqual(B.data['status'], 'NEW')
2583 self.assertEqual(B.reported, 2)
2584 self.assertEqual(self.getJobFromHistory('project-merge').result,
2585 'SUCCESS')
2586 self.assertEqual(self.getJobFromHistory('project-test1').result,
2587 'SUCCESS')
2588 self.assertEqual(self.getJobFromHistory('project-test2').result,
2589 'SUCCESS')
2590 self.assertEqual(self.getJobFromHistory('project-test3').result,
2591 'SUCCESS')
2592 self.assertEqual(len(self.history), 4)
2593
James E. Blairec056492016-07-22 09:45:56 -07002594 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002595 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002596 # An extrapolation of test_live_reconfiguration_merge_conflict
2597 # that tests a job added to a job tree with a failed root does
2598 # not run.
2599 self.worker.registerFunction('build:project-test3')
Paul Belanger174a8272017-03-14 13:20:10 -04002600 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002601
2602 # This change is fine. It's here to stop the queue long
2603 # enough for the next change to be subject to the
2604 # reconfiguration. This change will succeed and merge.
2605 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2606 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002607 A.addApproval('code-review', 2)
2608 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002609 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002610 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002611 self.waitUntilSettled()
2612
2613 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002614 self.executor_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002615 B.addApproval('code-review', 2)
2616 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002617 self.waitUntilSettled()
2618
Paul Belanger174a8272017-03-14 13:20:10 -04002619 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002620 self.waitUntilSettled()
2621
2622 # Both -merge jobs have run, but no others.
2623 self.assertEqual(A.data['status'], 'NEW')
2624 self.assertEqual(A.reported, 1)
2625 self.assertEqual(B.data['status'], 'NEW')
2626 self.assertEqual(B.reported, 1)
2627 self.assertEqual(self.history[0].result, 'SUCCESS')
2628 self.assertEqual(self.history[0].name, 'project-merge')
2629 self.assertEqual(self.history[1].result, 'FAILURE')
2630 self.assertEqual(self.history[1].name, 'project-merge')
2631 self.assertEqual(len(self.history), 2)
2632
2633 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002634 self.updateConfigLayout(
2635 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002636 self.sched.reconfigure(self.config)
2637 self.waitUntilSettled()
2638
Paul Belanger174a8272017-03-14 13:20:10 -04002639 self.executor_server.hold_jobs_in_build = False
2640 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002641 self.waitUntilSettled()
2642
2643 self.assertEqual(A.data['status'], 'MERGED')
2644 self.assertEqual(A.reported, 2)
2645 self.assertEqual(B.data['status'], 'NEW')
2646 self.assertEqual(B.reported, 2)
2647 self.assertEqual(self.history[0].result, 'SUCCESS')
2648 self.assertEqual(self.history[0].name, 'project-merge')
2649 self.assertEqual(self.history[1].result, 'FAILURE')
2650 self.assertEqual(self.history[1].name, 'project-merge')
2651 self.assertEqual(self.history[2].result, 'SUCCESS')
2652 self.assertEqual(self.history[3].result, 'SUCCESS')
2653 self.assertEqual(self.history[4].result, 'SUCCESS')
2654 self.assertEqual(len(self.history), 5)
2655
James E. Blairec056492016-07-22 09:45:56 -07002656 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002657 def test_live_reconfiguration_failed_job(self):
2658 # Test that a change with a removed failing job does not
2659 # disrupt reconfiguration. If a change has a failed job and
2660 # that job is removed during a reconfiguration, we observed a
2661 # bug where the code to re-set build statuses would run on
2662 # that build and raise an exception because the job no longer
2663 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002664 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002665
2666 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2667
2668 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002669 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002670
2671 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2672 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002673 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002674 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002675 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002676 self.waitUntilSettled()
2677
2678 self.assertEqual(A.data['status'], 'NEW')
2679 self.assertEqual(A.reported, 0)
2680
2681 self.assertEqual(self.getJobFromHistory('project-merge').result,
2682 'SUCCESS')
2683 self.assertEqual(self.getJobFromHistory('project-test1').result,
2684 'FAILURE')
2685 self.assertEqual(len(self.history), 2)
2686
2687 # Remove the test1 job.
James E. Blairf84026c2015-12-08 16:11:46 -08002688 self.updateConfigLayout(
2689 'tests/fixtures/layout-live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002690 self.sched.reconfigure(self.config)
2691 self.waitUntilSettled()
2692
Paul Belanger174a8272017-03-14 13:20:10 -04002693 self.executor_server.hold_jobs_in_build = False
2694 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002695 self.waitUntilSettled()
2696
2697 self.assertEqual(self.getJobFromHistory('project-test2').result,
2698 'SUCCESS')
2699 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2700 'SUCCESS')
2701 self.assertEqual(len(self.history), 4)
2702
2703 self.assertEqual(A.data['status'], 'NEW')
2704 self.assertEqual(A.reported, 1)
2705 self.assertIn('Build succeeded', A.messages[0])
2706 # Ensure the removed job was not included in the report.
2707 self.assertNotIn('project-test1', A.messages[0])
2708
James E. Blairec056492016-07-22 09:45:56 -07002709 @skip("Disabled for early v3 development")
James E. Blairfe707d12015-08-05 15:18:15 -07002710 def test_live_reconfiguration_shared_queue(self):
2711 # Test that a change with a failing job which was removed from
2712 # this project but otherwise still exists in the system does
2713 # not disrupt reconfiguration.
2714
Paul Belanger174a8272017-03-14 13:20:10 -04002715 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002716
2717 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2718
Paul Belanger174a8272017-03-14 13:20:10 -04002719 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002720
2721 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2722 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002723 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002724 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002725 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002726 self.waitUntilSettled()
2727
2728 self.assertEqual(A.data['status'], 'NEW')
2729 self.assertEqual(A.reported, 0)
2730
2731 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2732 'SUCCESS')
2733 self.assertEqual(self.getJobFromHistory(
2734 'project1-project2-integration').result, 'FAILURE')
2735 self.assertEqual(len(self.history), 2)
2736
2737 # Remove the integration job.
James E. Blairf84026c2015-12-08 16:11:46 -08002738 self.updateConfigLayout(
2739 'tests/fixtures/layout-live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002740 self.sched.reconfigure(self.config)
2741 self.waitUntilSettled()
2742
Paul Belanger174a8272017-03-14 13:20:10 -04002743 self.executor_server.hold_jobs_in_build = False
2744 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002745 self.waitUntilSettled()
2746
2747 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2748 'SUCCESS')
2749 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2750 'SUCCESS')
2751 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2752 'SUCCESS')
2753 self.assertEqual(self.getJobFromHistory(
2754 'project1-project2-integration').result, 'FAILURE')
2755 self.assertEqual(len(self.history), 4)
2756
2757 self.assertEqual(A.data['status'], 'NEW')
2758 self.assertEqual(A.reported, 1)
2759 self.assertIn('Build succeeded', A.messages[0])
2760 # Ensure the removed job was not included in the report.
2761 self.assertNotIn('project1-project2-integration', A.messages[0])
2762
James E. Blairec056492016-07-22 09:45:56 -07002763 @skip("Disabled for early v3 development")
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002764 def test_double_live_reconfiguration_shared_queue(self):
2765 # This was a real-world regression. A change is added to
2766 # gate; a reconfigure happens, a second change which depends
2767 # on the first is added, and a second reconfiguration happens.
2768 # Ensure that both changes merge.
2769
2770 # A failure may indicate incorrect caching or cleaning up of
2771 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002772 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002773
2774 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2775 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2776 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002777 A.addApproval('code-review', 2)
2778 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002779
2780 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002781 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002782 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002783 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002784 self.waitUntilSettled()
2785
2786 # Reconfigure (with only one change in the pipeline).
2787 self.sched.reconfigure(self.config)
2788 self.waitUntilSettled()
2789
2790 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002791 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002792 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002793 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002794 self.waitUntilSettled()
2795
2796 # Reconfigure (with both in the pipeline).
2797 self.sched.reconfigure(self.config)
2798 self.waitUntilSettled()
2799
Paul Belanger174a8272017-03-14 13:20:10 -04002800 self.executor_server.hold_jobs_in_build = False
2801 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002802 self.waitUntilSettled()
2803
2804 self.assertEqual(len(self.history), 8)
2805
2806 self.assertEqual(A.data['status'], 'MERGED')
2807 self.assertEqual(A.reported, 2)
2808 self.assertEqual(B.data['status'], 'MERGED')
2809 self.assertEqual(B.reported, 2)
2810
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002811 def test_live_reconfiguration_del_project(self):
2812 # Test project deletion from layout
2813 # while changes are enqueued
2814
Paul Belanger174a8272017-03-14 13:20:10 -04002815 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002816 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2817 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2818 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2819
2820 # A Depends-On: B
2821 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2822 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002823 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002824
2825 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2826 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2827 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002828 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002829 self.waitUntilSettled()
2830 self.assertEqual(len(self.builds), 5)
2831
2832 # This layout defines only org/project, not org/project1
Clint Byrum0149ada2017-04-04 12:22:01 -07002833 self.commitLayoutUpdate('common-config',
2834 'layout-live-reconfiguration-del-project')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002835 self.sched.reconfigure(self.config)
2836 self.waitUntilSettled()
2837
2838 # Builds for C aborted, builds for A succeed,
2839 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002840 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002841 self.assertEqual(job_c.changes, '3,1')
2842 self.assertEqual(job_c.result, 'ABORTED')
2843
Paul Belanger174a8272017-03-14 13:20:10 -04002844 self.executor_server.hold_jobs_in_build = False
2845 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002846 self.waitUntilSettled()
2847
Clint Byrum0149ada2017-04-04 12:22:01 -07002848 self.assertEqual(
2849 self.getJobFromHistory('project-test1', 'org/project').changes,
2850 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002851
2852 self.assertEqual(A.data['status'], 'NEW')
2853 self.assertEqual(B.data['status'], 'NEW')
2854 self.assertEqual(C.data['status'], 'NEW')
2855 self.assertEqual(A.reported, 1)
2856 self.assertEqual(B.reported, 0)
2857 self.assertEqual(C.reported, 0)
2858
Clint Byrum0149ada2017-04-04 12:22:01 -07002859 tenant = self.sched.abide.tenants.get('tenant-one')
2860 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002861 self.assertIn('Build succeeded', A.messages[0])
2862
James E. Blairec056492016-07-22 09:45:56 -07002863 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002864 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002865 self.updateConfigLayout(
2866 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002867 self.sched.reconfigure(self.config)
2868
2869 self.init_repo("org/new-project")
2870 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2871
James E. Blair8b5408c2016-08-08 15:37:46 -07002872 A.addApproval('code-review', 2)
2873 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002874 self.waitUntilSettled()
2875 self.assertEqual(self.getJobFromHistory('project-merge').result,
2876 'SUCCESS')
2877 self.assertEqual(self.getJobFromHistory('project-test1').result,
2878 'SUCCESS')
2879 self.assertEqual(self.getJobFromHistory('project-test2').result,
2880 'SUCCESS')
2881 self.assertEqual(A.data['status'], 'MERGED')
2882 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002883
Clark Boylan6dbbc482013-10-18 10:57:31 -07002884 def test_repo_deleted(self):
James E. Blair0ffa0102017-03-30 13:11:33 -07002885 self.updateConfigLayout('layout-repo-deleted', ['org/delete-project'])
Clark Boylan6dbbc482013-10-18 10:57:31 -07002886 self.sched.reconfigure(self.config)
2887
2888 self.init_repo("org/delete-project")
2889 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2890
James E. Blair8b5408c2016-08-08 15:37:46 -07002891 A.addApproval('code-review', 2)
2892 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002893 self.waitUntilSettled()
2894 self.assertEqual(self.getJobFromHistory('project-merge').result,
2895 'SUCCESS')
2896 self.assertEqual(self.getJobFromHistory('project-test1').result,
2897 'SUCCESS')
2898 self.assertEqual(self.getJobFromHistory('project-test2').result,
2899 'SUCCESS')
2900 self.assertEqual(A.data['status'], 'MERGED')
2901 self.assertEqual(A.reported, 2)
2902
2903 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002904 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002905 if os.path.exists(os.path.join(self.merger_src_root, p)):
2906 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002907 if os.path.exists(os.path.join(self.executor_src_root, p)):
2908 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002909
2910 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2911
James E. Blair8b5408c2016-08-08 15:37:46 -07002912 B.addApproval('code-review', 2)
2913 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002914 self.waitUntilSettled()
2915 self.assertEqual(self.getJobFromHistory('project-merge').result,
2916 'SUCCESS')
2917 self.assertEqual(self.getJobFromHistory('project-test1').result,
2918 'SUCCESS')
2919 self.assertEqual(self.getJobFromHistory('project-test2').result,
2920 'SUCCESS')
2921 self.assertEqual(B.data['status'], 'MERGED')
2922 self.assertEqual(B.reported, 2)
2923
James E. Blaire68d43f2017-04-19 14:03:30 -07002924 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002925 def test_tags(self):
2926 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002927 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2928 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2929 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2930 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2931 self.waitUntilSettled()
2932
James E. Blaire68d43f2017-04-19 14:03:30 -07002933 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002934
2935 results = {self.getJobFromHistory('merge',
2936 project='org/project1').uuid: 'extratag merge',
2937 self.getJobFromHistory('merge',
2938 project='org/project2').uuid: 'merge'}
James E. Blair456f2fb2016-02-09 09:29:33 -08002939
2940 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002941 self.assertEqual(results.get(build.uuid, ''),
2942 build.parameters['vars']['zuul'].get('tags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002943
James E. Blair63bb0ef2013-07-29 17:14:51 -07002944 def test_timer(self):
2945 "Test that a periodic job is triggered"
Paul Belanger174a8272017-03-14 13:20:10 -04002946 self.executor_server.hold_jobs_in_build = True
Clint Byrum5e729e92017-02-08 06:34:57 -08002947 self.updateConfigLayout('layout-timer')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002948 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002949
Clark Boylan3ee090a2014-04-03 20:55:09 -07002950 # The pipeline triggers every second, so we should have seen
2951 # several by now.
2952 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002953 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002954
2955 self.assertEqual(len(self.builds), 2)
2956
James E. Blair63bb0ef2013-07-29 17:14:51 -07002957 port = self.webapp.server.socket.getsockname()[1]
2958
Clint Byrum5e729e92017-02-08 06:34:57 -08002959 req = urllib.request.Request(
2960 "http://localhost:%s/openstack/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002961 f = urllib.request.urlopen(req)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002962 data = f.read()
2963
Paul Belanger174a8272017-03-14 13:20:10 -04002964 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002965 # Stop queuing timer triggered jobs so that the assertions
2966 # below don't race against more jobs being queued.
Clint Byrum5e729e92017-02-08 06:34:57 -08002967 self.commitLayoutUpdate('layout-timer', 'layout-no-timer')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002968 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04002969 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002970 self.waitUntilSettled()
2971
2972 self.assertEqual(self.getJobFromHistory(
2973 'project-bitrot-stable-old').result, 'SUCCESS')
2974 self.assertEqual(self.getJobFromHistory(
2975 'project-bitrot-stable-older').result, 'SUCCESS')
2976
2977 data = json.loads(data)
2978 status_jobs = set()
2979 for p in data['pipelines']:
2980 for q in p['change_queues']:
2981 for head in q['heads']:
2982 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002983 for job in change['jobs']:
2984 status_jobs.add(job['name'])
2985 self.assertIn('project-bitrot-stable-old', status_jobs)
2986 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002987
James E. Blair4f6033c2014-03-27 15:49:09 -07002988 def test_idle(self):
2989 "Test that frequent periodic jobs work"
Paul Belanger174a8272017-03-14 13:20:10 -04002990 self.executor_server.hold_jobs_in_build = True
Clint Byrum58264dc2017-02-07 21:21:22 -08002991 self.updateConfigLayout('layout-idle')
James E. Blair4f6033c2014-03-27 15:49:09 -07002992
Clark Boylan3ee090a2014-04-03 20:55:09 -07002993 for x in range(1, 3):
2994 # Test that timer triggers periodic jobs even across
2995 # layout config reloads.
2996 # Start timer trigger
Clark Boylan3ee090a2014-04-03 20:55:09 -07002997 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002998 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002999
Clark Boylan3ee090a2014-04-03 20:55:09 -07003000 # The pipeline triggers every second, so we should have seen
3001 # several by now.
3002 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07003003
3004 # Stop queuing timer triggered jobs so that the assertions
3005 # below don't race against more jobs being queued.
Clint Byrum58264dc2017-02-07 21:21:22 -08003006 before = self.commitLayoutUpdate('layout-idle', 'layout-no-timer')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003007 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003008 self.waitUntilSettled()
Clint Byrum58264dc2017-02-07 21:21:22 -08003009 self.assertEqual(len(self.builds), 2,
3010 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04003011 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003012 self.waitUntilSettled()
3013 self.assertEqual(len(self.builds), 0)
3014 self.assertEqual(len(self.history), x * 2)
Clint Byrum58264dc2017-02-07 21:21:22 -08003015 # Revert back to layout-idle
3016 repo = git.Repo(os.path.join(self.test_root,
3017 'upstream',
3018 'layout-idle'))
3019 repo.git.reset('--hard', before)
James E. Blair4f6033c2014-03-27 15:49:09 -07003020
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003021 def test_check_smtp_pool(self):
Paul Belanger01be8ca2016-11-11 12:21:06 -05003022 self.updateConfigLayout('layout-smtp')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003023 self.sched.reconfigure(self.config)
3024
3025 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3026 self.waitUntilSettled()
3027
3028 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3029 self.waitUntilSettled()
3030
James E. Blairff80a2f2013-12-27 13:24:06 -08003031 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003032
3033 # A.messages only holds what FakeGerrit places in it. Thus we
3034 # work on the knowledge of what the first message should be as
3035 # it is only configured to go to SMTP.
3036
3037 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003038 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003039 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003040 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003041 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003042 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003043
3044 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003045 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003046 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003047 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003048 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003049 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003050
James E. Blaire5910202013-12-27 09:50:31 -08003051 def test_timer_smtp(self):
3052 "Test that a periodic job is triggered"
Paul Belanger174a8272017-03-14 13:20:10 -04003053 self.executor_server.hold_jobs_in_build = True
Clint Byrum7b71b2f2017-02-08 07:00:52 -08003054 self.updateConfigLayout('layout-timer-smtp')
James E. Blaire5910202013-12-27 09:50:31 -08003055 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08003056
Clark Boylan3ee090a2014-04-03 20:55:09 -07003057 # The pipeline triggers every second, so we should have seen
3058 # several by now.
3059 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003060 self.waitUntilSettled()
3061
Clark Boylan3ee090a2014-04-03 20:55:09 -07003062 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003063 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003064 self.waitUntilSettled()
3065 self.assertEqual(len(self.history), 2)
3066
James E. Blaire5910202013-12-27 09:50:31 -08003067 self.assertEqual(self.getJobFromHistory(
3068 'project-bitrot-stable-old').result, 'SUCCESS')
3069 self.assertEqual(self.getJobFromHistory(
3070 'project-bitrot-stable-older').result, 'SUCCESS')
3071
James E. Blairff80a2f2013-12-27 13:24:06 -08003072 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003073
3074 # A.messages only holds what FakeGerrit places in it. Thus we
3075 # work on the knowledge of what the first message should be as
3076 # it is only configured to go to SMTP.
3077
3078 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003079 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003080 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003081 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003082 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003083 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003084
Clark Boylan3ee090a2014-04-03 20:55:09 -07003085 # Stop queuing timer triggered jobs and let any that may have
3086 # queued through so that end of test assertions pass.
Clint Byrum7b71b2f2017-02-08 07:00:52 -08003087 self.commitLayoutUpdate('layout-timer-smtp', 'layout-no-timer')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003088 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07003089 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003090 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003091 self.waitUntilSettled()
3092
Joshua Hesketh25695cb2017-03-06 12:50:04 +11003093 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003094 def test_timer_sshkey(self):
3095 "Test that a periodic job can setup SSH key authentication"
3096 self.worker.hold_jobs_in_build = True
3097 self.config.set('zuul', 'layout_config',
3098 'tests/fixtures/layout-timer.yaml')
3099 self.sched.reconfigure(self.config)
3100 self.registerJobs()
3101
3102 # The pipeline triggers every second, so we should have seen
3103 # several by now.
3104 time.sleep(5)
3105 self.waitUntilSettled()
3106
3107 self.assertEqual(len(self.builds), 2)
3108
3109 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3110 self.assertTrue(os.path.isfile(ssh_wrapper))
3111 with open(ssh_wrapper) as f:
3112 ssh_wrapper_content = f.read()
3113 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3114 # In the unit tests Merger runs in the same process,
3115 # so we see its' environment variables
3116 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3117
3118 self.worker.release('.*')
3119 self.waitUntilSettled()
3120 self.assertEqual(len(self.history), 2)
3121
3122 self.assertEqual(self.getJobFromHistory(
3123 'project-bitrot-stable-old').result, 'SUCCESS')
3124 self.assertEqual(self.getJobFromHistory(
3125 'project-bitrot-stable-older').result, 'SUCCESS')
3126
3127 # Stop queuing timer triggered jobs and let any that may have
3128 # queued through so that end of test assertions pass.
3129 self.config.set('zuul', 'layout_config',
3130 'tests/fixtures/layout-no-timer.yaml')
3131 self.sched.reconfigure(self.config)
3132 self.registerJobs()
3133 self.waitUntilSettled()
3134 self.worker.release('.*')
3135 self.waitUntilSettled()
3136
James E. Blair91e34592015-07-31 16:45:59 -07003137 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003138 "Test that the RPC client can enqueue a change"
3139 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003140 A.addApproval('code-review', 2)
3141 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003142
3143 client = zuul.rpcclient.RPCClient('127.0.0.1',
3144 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003145 r = client.enqueue(tenant='tenant-one',
3146 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003147 project='org/project',
3148 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003149 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003150 self.waitUntilSettled()
3151 self.assertEqual(self.getJobFromHistory('project-merge').result,
3152 'SUCCESS')
3153 self.assertEqual(self.getJobFromHistory('project-test1').result,
3154 'SUCCESS')
3155 self.assertEqual(self.getJobFromHistory('project-test2').result,
3156 'SUCCESS')
3157 self.assertEqual(A.data['status'], 'MERGED')
3158 self.assertEqual(A.reported, 2)
3159 self.assertEqual(r, True)
3160
James E. Blair91e34592015-07-31 16:45:59 -07003161 def test_client_enqueue_ref(self):
3162 "Test that the RPC client can enqueue a ref"
3163
3164 client = zuul.rpcclient.RPCClient('127.0.0.1',
3165 self.gearman_server.port)
3166 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003167 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003168 pipeline='post',
3169 project='org/project',
3170 trigger='gerrit',
3171 ref='master',
3172 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
3173 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
3174 self.waitUntilSettled()
3175 job_names = [x.name for x in self.history]
3176 self.assertEqual(len(self.history), 1)
3177 self.assertIn('project-post', job_names)
3178 self.assertEqual(r, True)
3179
James E. Blairad28e912013-11-27 10:43:22 -08003180 def test_client_enqueue_negative(self):
3181 "Test that the RPC client returns errors"
3182 client = zuul.rpcclient.RPCClient('127.0.0.1',
3183 self.gearman_server.port)
3184 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003185 "Invalid tenant"):
3186 r = client.enqueue(tenant='tenant-foo',
3187 pipeline='gate',
3188 project='org/project',
3189 trigger='gerrit',
3190 change='1,1')
3191 client.shutdown()
3192 self.assertEqual(r, False)
3193
3194 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003195 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003196 r = client.enqueue(tenant='tenant-one',
3197 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003198 project='project-does-not-exist',
3199 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003200 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003201 client.shutdown()
3202 self.assertEqual(r, False)
3203
3204 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3205 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003206 r = client.enqueue(tenant='tenant-one',
3207 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003208 project='org/project',
3209 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003210 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003211 client.shutdown()
3212 self.assertEqual(r, False)
3213
3214 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3215 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003216 r = client.enqueue(tenant='tenant-one',
3217 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003218 project='org/project',
3219 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003220 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003221 client.shutdown()
3222 self.assertEqual(r, False)
3223
3224 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3225 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003226 r = client.enqueue(tenant='tenant-one',
3227 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003228 project='org/project',
3229 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003230 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003231 client.shutdown()
3232 self.assertEqual(r, False)
3233
3234 self.waitUntilSettled()
3235 self.assertEqual(len(self.history), 0)
3236 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003237
3238 def test_client_promote(self):
3239 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003240 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003241 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3242 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3243 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003244 A.addApproval('code-review', 2)
3245 B.addApproval('code-review', 2)
3246 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003247
James E. Blair8b5408c2016-08-08 15:37:46 -07003248 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3249 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3250 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003251
3252 self.waitUntilSettled()
3253
Paul Belangerbaca3132016-11-04 12:49:54 -04003254 tenant = self.sched.abide.tenants.get('tenant-one')
3255 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003256 enqueue_times = {}
3257 for item in items:
3258 enqueue_times[str(item.change)] = item.enqueue_time
3259
James E. Blair36658cf2013-12-06 17:53:48 -08003260 client = zuul.rpcclient.RPCClient('127.0.0.1',
3261 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003262 r = client.promote(tenant='tenant-one',
3263 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003264 change_ids=['2,1', '3,1'])
3265
Sean Daguef39b9ca2014-01-10 21:34:35 -05003266 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003267 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003268 for item in items:
3269 self.assertEqual(
3270 enqueue_times[str(item.change)], item.enqueue_time)
3271
James E. Blair78acec92014-02-06 07:11:32 -08003272 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003273 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003274 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003275 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003276 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003277 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003278 self.waitUntilSettled()
3279
3280 self.assertEqual(len(self.builds), 6)
3281 self.assertEqual(self.builds[0].name, 'project-test1')
3282 self.assertEqual(self.builds[1].name, 'project-test2')
3283 self.assertEqual(self.builds[2].name, 'project-test1')
3284 self.assertEqual(self.builds[3].name, 'project-test2')
3285 self.assertEqual(self.builds[4].name, 'project-test1')
3286 self.assertEqual(self.builds[5].name, 'project-test2')
3287
Paul Belangerbaca3132016-11-04 12:49:54 -04003288 self.assertTrue(self.builds[0].hasChanges(B))
3289 self.assertFalse(self.builds[0].hasChanges(A))
3290 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003291
Paul Belangerbaca3132016-11-04 12:49:54 -04003292 self.assertTrue(self.builds[2].hasChanges(B))
3293 self.assertTrue(self.builds[2].hasChanges(C))
3294 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003295
Paul Belangerbaca3132016-11-04 12:49:54 -04003296 self.assertTrue(self.builds[4].hasChanges(B))
3297 self.assertTrue(self.builds[4].hasChanges(C))
3298 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003299
Paul Belanger174a8272017-03-14 13:20:10 -04003300 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003301 self.waitUntilSettled()
3302
3303 self.assertEqual(A.data['status'], 'MERGED')
3304 self.assertEqual(A.reported, 2)
3305 self.assertEqual(B.data['status'], 'MERGED')
3306 self.assertEqual(B.reported, 2)
3307 self.assertEqual(C.data['status'], 'MERGED')
3308 self.assertEqual(C.reported, 2)
3309
3310 client.shutdown()
3311 self.assertEqual(r, True)
3312
3313 def test_client_promote_dependent(self):
3314 "Test that the RPC client can promote a dependent change"
3315 # C (depends on B) -> B -> A ; then promote C to get:
3316 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003317 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003318 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3319 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3320 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3321
3322 C.setDependsOn(B, 1)
3323
James E. Blair8b5408c2016-08-08 15:37:46 -07003324 A.addApproval('code-review', 2)
3325 B.addApproval('code-review', 2)
3326 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003327
James E. Blair8b5408c2016-08-08 15:37:46 -07003328 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3329 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3330 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003331
3332 self.waitUntilSettled()
3333
3334 client = zuul.rpcclient.RPCClient('127.0.0.1',
3335 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003336 r = client.promote(tenant='tenant-one',
3337 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003338 change_ids=['3,1'])
3339
James E. Blair78acec92014-02-06 07:11:32 -08003340 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003341 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003342 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003343 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003344 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003345 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003346 self.waitUntilSettled()
3347
3348 self.assertEqual(len(self.builds), 6)
3349 self.assertEqual(self.builds[0].name, 'project-test1')
3350 self.assertEqual(self.builds[1].name, 'project-test2')
3351 self.assertEqual(self.builds[2].name, 'project-test1')
3352 self.assertEqual(self.builds[3].name, 'project-test2')
3353 self.assertEqual(self.builds[4].name, 'project-test1')
3354 self.assertEqual(self.builds[5].name, 'project-test2')
3355
Paul Belangerbaca3132016-11-04 12:49:54 -04003356 self.assertTrue(self.builds[0].hasChanges(B))
3357 self.assertFalse(self.builds[0].hasChanges(A))
3358 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003359
Paul Belangerbaca3132016-11-04 12:49:54 -04003360 self.assertTrue(self.builds[2].hasChanges(B))
3361 self.assertTrue(self.builds[2].hasChanges(C))
3362 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003363
Paul Belangerbaca3132016-11-04 12:49:54 -04003364 self.assertTrue(self.builds[4].hasChanges(B))
3365 self.assertTrue(self.builds[4].hasChanges(C))
3366 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003367
Paul Belanger174a8272017-03-14 13:20:10 -04003368 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003369 self.waitUntilSettled()
3370
3371 self.assertEqual(A.data['status'], 'MERGED')
3372 self.assertEqual(A.reported, 2)
3373 self.assertEqual(B.data['status'], 'MERGED')
3374 self.assertEqual(B.reported, 2)
3375 self.assertEqual(C.data['status'], 'MERGED')
3376 self.assertEqual(C.reported, 2)
3377
3378 client.shutdown()
3379 self.assertEqual(r, True)
3380
3381 def test_client_promote_negative(self):
3382 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003383 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003384 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003385 A.addApproval('code-review', 2)
3386 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003387 self.waitUntilSettled()
3388
3389 client = zuul.rpcclient.RPCClient('127.0.0.1',
3390 self.gearman_server.port)
3391
3392 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003393 r = client.promote(tenant='tenant-one',
3394 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003395 change_ids=['2,1', '3,1'])
3396 client.shutdown()
3397 self.assertEqual(r, False)
3398
3399 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003400 r = client.promote(tenant='tenant-one',
3401 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003402 change_ids=['4,1'])
3403 client.shutdown()
3404 self.assertEqual(r, False)
3405
Paul Belanger174a8272017-03-14 13:20:10 -04003406 self.executor_server.hold_jobs_in_build = False
3407 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003408 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003409
3410 def test_queue_rate_limiting(self):
3411 "Test that DependentPipelines are rate limited with dep across window"
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003412 self.updateConfigLayout('layout-rate-limit')
Clark Boylan7603a372014-01-21 11:43:20 -08003413 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04003414 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003415 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3416 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3417 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3418
3419 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003420 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003421
James E. Blair8b5408c2016-08-08 15:37:46 -07003422 A.addApproval('code-review', 2)
3423 B.addApproval('code-review', 2)
3424 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003425
James E. Blair8b5408c2016-08-08 15:37:46 -07003426 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3427 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3428 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003429 self.waitUntilSettled()
3430
3431 # Only A and B will have their merge jobs queued because
3432 # window is 2.
3433 self.assertEqual(len(self.builds), 2)
3434 self.assertEqual(self.builds[0].name, 'project-merge')
3435 self.assertEqual(self.builds[1].name, 'project-merge')
3436
Paul Belanger174a8272017-03-14 13:20:10 -04003437 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003438 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003439 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003440 self.waitUntilSettled()
3441
3442 # Only A and B will have their test jobs queued because
3443 # window is 2.
3444 self.assertEqual(len(self.builds), 4)
3445 self.assertEqual(self.builds[0].name, 'project-test1')
3446 self.assertEqual(self.builds[1].name, 'project-test2')
3447 self.assertEqual(self.builds[2].name, 'project-test1')
3448 self.assertEqual(self.builds[3].name, 'project-test2')
3449
Paul Belanger174a8272017-03-14 13:20:10 -04003450 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003451 self.waitUntilSettled()
3452
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003453 tenant = self.sched.abide.tenants.get('openstack')
3454 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003455 # A failed so window is reduced by 1 to 1.
3456 self.assertEqual(queue.window, 1)
3457 self.assertEqual(queue.window_floor, 1)
3458 self.assertEqual(A.data['status'], 'NEW')
3459
3460 # Gate is reset and only B's merge job is queued because
3461 # window shrunk to 1.
3462 self.assertEqual(len(self.builds), 1)
3463 self.assertEqual(self.builds[0].name, 'project-merge')
3464
Paul Belanger174a8272017-03-14 13:20:10 -04003465 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003466 self.waitUntilSettled()
3467
3468 # Only B's test jobs are queued because window is still 1.
3469 self.assertEqual(len(self.builds), 2)
3470 self.assertEqual(self.builds[0].name, 'project-test1')
3471 self.assertEqual(self.builds[1].name, 'project-test2')
3472
Paul Belanger174a8272017-03-14 13:20:10 -04003473 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003474 self.waitUntilSettled()
3475
3476 # B was successfully merged so window is increased to 2.
3477 self.assertEqual(queue.window, 2)
3478 self.assertEqual(queue.window_floor, 1)
3479 self.assertEqual(B.data['status'], 'MERGED')
3480
3481 # Only C is left and its merge job is queued.
3482 self.assertEqual(len(self.builds), 1)
3483 self.assertEqual(self.builds[0].name, 'project-merge')
3484
Paul Belanger174a8272017-03-14 13:20:10 -04003485 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003486 self.waitUntilSettled()
3487
3488 # After successful merge job the test jobs for C are queued.
3489 self.assertEqual(len(self.builds), 2)
3490 self.assertEqual(self.builds[0].name, 'project-test1')
3491 self.assertEqual(self.builds[1].name, 'project-test2')
3492
Paul Belanger174a8272017-03-14 13:20:10 -04003493 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003494 self.waitUntilSettled()
3495
3496 # C successfully merged so window is bumped to 3.
3497 self.assertEqual(queue.window, 3)
3498 self.assertEqual(queue.window_floor, 1)
3499 self.assertEqual(C.data['status'], 'MERGED')
3500
3501 def test_queue_rate_limiting_dependent(self):
3502 "Test that DependentPipelines are rate limited with dep in window"
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003503 self.updateConfigLayout('layout-rate-limit')
Clark Boylan7603a372014-01-21 11:43:20 -08003504 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04003505 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003506 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3507 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3508 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3509
3510 B.setDependsOn(A, 1)
3511
Paul Belanger174a8272017-03-14 13:20:10 -04003512 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003513
James E. Blair8b5408c2016-08-08 15:37:46 -07003514 A.addApproval('code-review', 2)
3515 B.addApproval('code-review', 2)
3516 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003517
James E. Blair8b5408c2016-08-08 15:37:46 -07003518 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3519 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3520 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003521 self.waitUntilSettled()
3522
3523 # Only A and B will have their merge jobs queued because
3524 # window is 2.
3525 self.assertEqual(len(self.builds), 2)
3526 self.assertEqual(self.builds[0].name, 'project-merge')
3527 self.assertEqual(self.builds[1].name, 'project-merge')
3528
James E. Blairf4a5f022017-04-18 14:01:10 -07003529 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003530
3531 # Only A and B will have their test jobs queued because
3532 # window is 2.
3533 self.assertEqual(len(self.builds), 4)
3534 self.assertEqual(self.builds[0].name, 'project-test1')
3535 self.assertEqual(self.builds[1].name, 'project-test2')
3536 self.assertEqual(self.builds[2].name, 'project-test1')
3537 self.assertEqual(self.builds[3].name, 'project-test2')
3538
Paul Belanger174a8272017-03-14 13:20:10 -04003539 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003540 self.waitUntilSettled()
3541
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003542 tenant = self.sched.abide.tenants.get('openstack')
3543 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003544 # A failed so window is reduced by 1 to 1.
3545 self.assertEqual(queue.window, 1)
3546 self.assertEqual(queue.window_floor, 1)
3547 self.assertEqual(A.data['status'], 'NEW')
3548 self.assertEqual(B.data['status'], 'NEW')
3549
3550 # Gate is reset and only C's merge job is queued because
3551 # window shrunk to 1 and A and B were dequeued.
3552 self.assertEqual(len(self.builds), 1)
3553 self.assertEqual(self.builds[0].name, 'project-merge')
3554
James E. Blairf4a5f022017-04-18 14:01:10 -07003555 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003556
3557 # Only C's test jobs are queued because window is still 1.
3558 self.assertEqual(len(self.builds), 2)
3559 self.assertEqual(self.builds[0].name, 'project-test1')
3560 self.assertEqual(self.builds[1].name, 'project-test2')
3561
Paul Belanger174a8272017-03-14 13:20:10 -04003562 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003563 self.waitUntilSettled()
3564
3565 # C was successfully merged so window is increased to 2.
3566 self.assertEqual(queue.window, 2)
3567 self.assertEqual(queue.window_floor, 1)
3568 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003569
James E. Blairec056492016-07-22 09:45:56 -07003570 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003571 def test_worker_update_metadata(self):
3572 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003573 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003574
3575 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003576 A.addApproval('code-review', 2)
3577 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003578 self.waitUntilSettled()
3579
Paul Belanger174a8272017-03-14 13:20:10 -04003580 self.assertEqual(len(self.executor.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003581
3582 self.log.debug('Current builds:')
Paul Belanger174a8272017-03-14 13:20:10 -04003583 self.log.debug(self.executor.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003584
3585 start = time.time()
3586 while True:
3587 if time.time() - start > 10:
3588 raise Exception("Timeout waiting for gearman server to report "
3589 + "back to the client")
Paul Belanger174a8272017-03-14 13:20:10 -04003590 build = self.executor.builds.values()[0]
Joshua Heskethba8776a2014-01-12 14:35:40 +08003591 if build.worker.name == "My Worker":
3592 break
3593 else:
3594 time.sleep(0)
3595
3596 self.log.debug(build)
3597 self.assertEqual("My Worker", build.worker.name)
3598 self.assertEqual("localhost", build.worker.hostname)
3599 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3600 self.assertEqual("zuul.example.org", build.worker.fqdn)
3601 self.assertEqual("FakeBuilder", build.worker.program)
3602 self.assertEqual("v1.1", build.worker.version)
3603 self.assertEqual({'something': 'else'}, build.worker.extra)
3604
Paul Belanger174a8272017-03-14 13:20:10 -04003605 self.executor_server.hold_jobs_in_build = False
3606 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003607 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003608
3609 def test_footer_message(self):
3610 "Test a pipeline's footer message is correctly added to the report."
Adam Gandelman82258fd2017-02-07 16:51:58 -08003611 self.updateConfigLayout('layout-footer-message')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003612 self.sched.reconfigure(self.config)
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003613
3614 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003615 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003616 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003617 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003618 self.waitUntilSettled()
3619
3620 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003621 B.addApproval('code-review', 2)
3622 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003623 self.waitUntilSettled()
3624
3625 self.assertEqual(2, len(self.smtp_messages))
3626
Adam Gandelman82258fd2017-02-07 16:51:58 -08003627 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003628Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003629http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003630
Adam Gandelman82258fd2017-02-07 16:51:58 -08003631 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003632For CI problems and help debugging, contact ci@example.org"""
3633
Adam Gandelman82258fd2017-02-07 16:51:58 -08003634 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3635 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3636 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3637 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003638
James E. Blairec056492016-07-22 09:45:56 -07003639 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003640 def test_merge_failure_reporters(self):
3641 """Check that the config is set up correctly"""
3642
James E. Blairf84026c2015-12-08 16:11:46 -08003643 self.updateConfigLayout(
3644 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003645 self.sched.reconfigure(self.config)
3646 self.registerJobs()
3647
3648 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003649 "Merge Failed.\n\nThis change or one of its cross-repo "
3650 "dependencies was unable to be automatically merged with the "
3651 "current state of its repository. Please rebase the change and "
3652 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003653 self.sched.layout.pipelines['check'].merge_failure_message)
3654 self.assertEqual(
3655 "The merge failed! For more information...",
3656 self.sched.layout.pipelines['gate'].merge_failure_message)
3657
3658 self.assertEqual(
3659 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3660 self.assertEqual(
3661 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3662
3663 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003664 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3665 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003666
3667 self.assertTrue(
3668 (
3669 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003670 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003671 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003672 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003673 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003674 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003675 ) or (
3676 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003677 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003678 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003679 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003680 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003681 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003682 )
3683 )
3684
James E. Blairec056492016-07-22 09:45:56 -07003685 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003686 def test_merge_failure_reports(self):
3687 """Check that when a change fails to merge the correct message is sent
3688 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003689 self.updateConfigLayout(
3690 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003691 self.sched.reconfigure(self.config)
3692 self.registerJobs()
3693
3694 # Check a test failure isn't reported to SMTP
3695 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003696 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003697 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003698 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003699 self.waitUntilSettled()
3700
3701 self.assertEqual(3, len(self.history)) # 3 jobs
3702 self.assertEqual(0, len(self.smtp_messages))
3703
3704 # Check a merge failure is reported to SMTP
3705 # B should be merged, but C will conflict with B
3706 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3707 B.addPatchset(['conflict'])
3708 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3709 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003710 B.addApproval('code-review', 2)
3711 C.addApproval('code-review', 2)
3712 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3713 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003714 self.waitUntilSettled()
3715
3716 self.assertEqual(6, len(self.history)) # A and B jobs
3717 self.assertEqual(1, len(self.smtp_messages))
3718 self.assertEqual('The merge failed! For more information...',
3719 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003720
James E. Blairec056492016-07-22 09:45:56 -07003721 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003722 def test_default_merge_failure_reports(self):
3723 """Check that the default merge failure reports are correct."""
3724
3725 # A should report success, B should report merge failure.
3726 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3727 A.addPatchset(['conflict'])
3728 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3729 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003730 A.addApproval('code-review', 2)
3731 B.addApproval('code-review', 2)
3732 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3733 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003734 self.waitUntilSettled()
3735
3736 self.assertEqual(3, len(self.history)) # A jobs
3737 self.assertEqual(A.reported, 2)
3738 self.assertEqual(B.reported, 2)
3739 self.assertEqual(A.data['status'], 'MERGED')
3740 self.assertEqual(B.data['status'], 'NEW')
3741 self.assertIn('Build succeeded', A.messages[1])
3742 self.assertIn('Merge Failed', B.messages[1])
3743 self.assertIn('automatically merged', B.messages[1])
3744 self.assertNotIn('logs.example.com', B.messages[1])
3745 self.assertNotIn('SKIPPED', B.messages[1])
3746
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003747 def test_client_get_running_jobs(self):
3748 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003749 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003750 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003751 A.addApproval('code-review', 2)
3752 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003753 self.waitUntilSettled()
3754
3755 client = zuul.rpcclient.RPCClient('127.0.0.1',
3756 self.gearman_server.port)
3757
3758 # Wait for gearman server to send the initial workData back to zuul
3759 start = time.time()
3760 while True:
3761 if time.time() - start > 10:
3762 raise Exception("Timeout waiting for gearman server to report "
3763 + "back to the client")
Paul Belanger174a8272017-03-14 13:20:10 -04003764 build = self.executor_client.builds.values()[0]
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003765 if build.worker.name == "My Worker":
3766 break
3767 else:
3768 time.sleep(0)
3769
3770 running_items = client.get_running_jobs()
3771
3772 self.assertEqual(1, len(running_items))
3773 running_item = running_items[0]
3774 self.assertEqual([], running_item['failing_reasons'])
3775 self.assertEqual([], running_item['items_behind'])
3776 self.assertEqual('https://hostname/1', running_item['url'])
3777 self.assertEqual(None, running_item['item_ahead'])
3778 self.assertEqual('org/project', running_item['project'])
3779 self.assertEqual(None, running_item['remaining_time'])
3780 self.assertEqual(True, running_item['active'])
3781 self.assertEqual('1,1', running_item['id'])
3782
3783 self.assertEqual(3, len(running_item['jobs']))
3784 for job in running_item['jobs']:
3785 if job['name'] == 'project-merge':
3786 self.assertEqual('project-merge', job['name'])
3787 self.assertEqual('gate', job['pipeline'])
3788 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003789 self.assertEqual('https://server/job/project-merge/0/',
3790 job['url'])
3791 self.assertEqual(7, len(job['worker']))
3792 self.assertEqual(False, job['canceled'])
3793 self.assertEqual(True, job['voting'])
3794 self.assertEqual(None, job['result'])
3795 self.assertEqual('gate', job['pipeline'])
3796 break
3797
Paul Belanger174a8272017-03-14 13:20:10 -04003798 self.executor_server.hold_jobs_in_build = False
3799 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003800 self.waitUntilSettled()
3801
3802 running_items = client.get_running_jobs()
3803 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003804
James E. Blairb70e55a2017-04-19 12:57:02 -07003805 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003806 def test_nonvoting_pipeline(self):
3807 "Test that a nonvoting pipeline (experimental) can still report"
3808
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003809 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3810 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003811 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3812 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003813
3814 self.assertEqual(self.getJobFromHistory('project-merge').result,
3815 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003816 self.assertEqual(
3817 self.getJobFromHistory('experimental-project-test').result,
3818 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003819 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003820
3821 def test_crd_gate(self):
3822 "Test cross-repo dependencies"
3823 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3824 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003825 A.addApproval('code-review', 2)
3826 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003827
3828 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3829 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3830 AM2.setMerged()
3831 AM1.setMerged()
3832
3833 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3834 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3835 BM2.setMerged()
3836 BM1.setMerged()
3837
3838 # A -> AM1 -> AM2
3839 # B -> BM1 -> BM2
3840 # A Depends-On: B
3841 # M2 is here to make sure it is never queried. If it is, it
3842 # means zuul is walking down the entire history of merged
3843 # changes.
3844
3845 B.setDependsOn(BM1, 1)
3846 BM1.setDependsOn(BM2, 1)
3847
3848 A.setDependsOn(AM1, 1)
3849 AM1.setDependsOn(AM2, 1)
3850
3851 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3852 A.subject, B.data['id'])
3853
James E. Blair8b5408c2016-08-08 15:37:46 -07003854 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003855 self.waitUntilSettled()
3856
3857 self.assertEqual(A.data['status'], 'NEW')
3858 self.assertEqual(B.data['status'], 'NEW')
3859
Paul Belanger5dccbe72016-11-14 11:17:37 -05003860 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003861 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003862
Paul Belanger174a8272017-03-14 13:20:10 -04003863 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003864 B.addApproval('approved', 1)
3865 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003866 self.waitUntilSettled()
3867
Paul Belanger174a8272017-03-14 13:20:10 -04003868 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003869 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003870 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003871 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003872 self.executor_server.hold_jobs_in_build = False
3873 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003874 self.waitUntilSettled()
3875
3876 self.assertEqual(AM2.queried, 0)
3877 self.assertEqual(BM2.queried, 0)
3878 self.assertEqual(A.data['status'], 'MERGED')
3879 self.assertEqual(B.data['status'], 'MERGED')
3880 self.assertEqual(A.reported, 2)
3881 self.assertEqual(B.reported, 2)
3882
Paul Belanger5dccbe72016-11-14 11:17:37 -05003883 changes = self.getJobFromHistory(
3884 'project-merge', 'org/project1').changes
3885 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003886
3887 def test_crd_branch(self):
3888 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003889
3890 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003891 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3892 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3893 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3894 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003895 A.addApproval('code-review', 2)
3896 B.addApproval('code-review', 2)
3897 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003898
3899 # A Depends-On: B+C
3900 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3901 A.subject, B.data['id'])
3902
Paul Belanger174a8272017-03-14 13:20:10 -04003903 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003904 B.addApproval('approved', 1)
3905 C.addApproval('approved', 1)
3906 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003907 self.waitUntilSettled()
3908
Paul Belanger174a8272017-03-14 13:20:10 -04003909 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003910 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003911 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003912 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003913 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003914 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003915 self.executor_server.hold_jobs_in_build = False
3916 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003917 self.waitUntilSettled()
3918
3919 self.assertEqual(A.data['status'], 'MERGED')
3920 self.assertEqual(B.data['status'], 'MERGED')
3921 self.assertEqual(C.data['status'], 'MERGED')
3922 self.assertEqual(A.reported, 2)
3923 self.assertEqual(B.reported, 2)
3924 self.assertEqual(C.reported, 2)
3925
Paul Belanger6379db12016-11-14 13:57:54 -05003926 changes = self.getJobFromHistory(
3927 'project-merge', 'org/project1').changes
3928 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003929
3930 def test_crd_multiline(self):
3931 "Test multiple depends-on lines in commit"
3932 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3933 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3934 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003935 A.addApproval('code-review', 2)
3936 B.addApproval('code-review', 2)
3937 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003938
3939 # A Depends-On: B+C
3940 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3941 A.subject, B.data['id'], C.data['id'])
3942
Paul Belanger174a8272017-03-14 13:20:10 -04003943 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003944 B.addApproval('approved', 1)
3945 C.addApproval('approved', 1)
3946 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003947 self.waitUntilSettled()
3948
Paul Belanger174a8272017-03-14 13:20:10 -04003949 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003950 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003951 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003952 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003953 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003954 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003955 self.executor_server.hold_jobs_in_build = False
3956 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003957 self.waitUntilSettled()
3958
3959 self.assertEqual(A.data['status'], 'MERGED')
3960 self.assertEqual(B.data['status'], 'MERGED')
3961 self.assertEqual(C.data['status'], 'MERGED')
3962 self.assertEqual(A.reported, 2)
3963 self.assertEqual(B.reported, 2)
3964 self.assertEqual(C.reported, 2)
3965
Paul Belanger5dccbe72016-11-14 11:17:37 -05003966 changes = self.getJobFromHistory(
3967 'project-merge', 'org/project1').changes
3968 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003969
3970 def test_crd_unshared_gate(self):
3971 "Test cross-repo dependencies in unshared gate queues"
3972 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3973 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003974 A.addApproval('code-review', 2)
3975 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003976
3977 # A Depends-On: B
3978 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3979 A.subject, B.data['id'])
3980
3981 # A and B do not share a queue, make sure that A is unable to
3982 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003983 B.addApproval('approved', 1)
3984 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003985 self.waitUntilSettled()
3986
3987 self.assertEqual(A.data['status'], 'NEW')
3988 self.assertEqual(B.data['status'], 'NEW')
3989 self.assertEqual(A.reported, 0)
3990 self.assertEqual(B.reported, 0)
3991 self.assertEqual(len(self.history), 0)
3992
3993 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003994 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003995 self.waitUntilSettled()
3996
3997 self.assertEqual(B.data['status'], 'MERGED')
3998 self.assertEqual(B.reported, 2)
3999
4000 # Now that B is merged, A should be able to be enqueued and
4001 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07004002 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004003 self.waitUntilSettled()
4004
4005 self.assertEqual(A.data['status'], 'MERGED')
4006 self.assertEqual(A.reported, 2)
4007
James E. Blair96698e22015-04-02 07:48:21 -07004008 def test_crd_gate_reverse(self):
4009 "Test reverse cross-repo dependencies"
4010 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4011 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004012 A.addApproval('code-review', 2)
4013 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07004014
4015 # A Depends-On: B
4016
4017 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4018 A.subject, B.data['id'])
4019
James E. Blair8b5408c2016-08-08 15:37:46 -07004020 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004021 self.waitUntilSettled()
4022
4023 self.assertEqual(A.data['status'], 'NEW')
4024 self.assertEqual(B.data['status'], 'NEW')
4025
Paul Belanger174a8272017-03-14 13:20:10 -04004026 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07004027 A.addApproval('approved', 1)
4028 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004029 self.waitUntilSettled()
4030
Paul Belanger174a8272017-03-14 13:20:10 -04004031 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004032 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004033 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004034 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004035 self.executor_server.hold_jobs_in_build = False
4036 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07004037 self.waitUntilSettled()
4038
4039 self.assertEqual(A.data['status'], 'MERGED')
4040 self.assertEqual(B.data['status'], 'MERGED')
4041 self.assertEqual(A.reported, 2)
4042 self.assertEqual(B.reported, 2)
4043
Paul Belanger5dccbe72016-11-14 11:17:37 -05004044 changes = self.getJobFromHistory(
4045 'project-merge', 'org/project1').changes
4046 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07004047
James E. Blair5ee24252014-12-30 10:12:29 -08004048 def test_crd_cycle(self):
4049 "Test cross-repo dependency cycles"
4050 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4051 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004052 A.addApproval('code-review', 2)
4053 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004054
4055 # A -> B -> A (via commit-depends)
4056
4057 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4058 A.subject, B.data['id'])
4059 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4060 B.subject, A.data['id'])
4061
James E. Blair8b5408c2016-08-08 15:37:46 -07004062 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004063 self.waitUntilSettled()
4064
4065 self.assertEqual(A.reported, 0)
4066 self.assertEqual(B.reported, 0)
4067 self.assertEqual(A.data['status'], 'NEW')
4068 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004069
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004070 def test_crd_gate_unknown(self):
4071 "Test unknown projects in dependent pipeline"
4072 self.init_repo("org/unknown")
4073 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4074 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004075 A.addApproval('code-review', 2)
4076 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004077
4078 # A Depends-On: B
4079 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4080 A.subject, B.data['id'])
4081
James E. Blair8b5408c2016-08-08 15:37:46 -07004082 B.addApproval('approved', 1)
4083 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004084 self.waitUntilSettled()
4085
4086 # Unknown projects cannot share a queue with any other
4087 # since they don't have common jobs with any other (they have no jobs).
4088 # Changes which depend on unknown project changes
4089 # should not be processed in dependent pipeline
4090 self.assertEqual(A.data['status'], 'NEW')
4091 self.assertEqual(B.data['status'], 'NEW')
4092 self.assertEqual(A.reported, 0)
4093 self.assertEqual(B.reported, 0)
4094 self.assertEqual(len(self.history), 0)
4095
4096 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07004097 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004098 B.setMerged()
4099 self.waitUntilSettled()
4100 self.assertEqual(len(self.history), 0)
4101
4102 # Now that B is merged, A should be able to be enqueued and
4103 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07004104 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004105 self.waitUntilSettled()
4106
4107 self.assertEqual(A.data['status'], 'MERGED')
4108 self.assertEqual(A.reported, 2)
4109 self.assertEqual(B.data['status'], 'MERGED')
4110 self.assertEqual(B.reported, 0)
4111
James E. Blairbfb8e042014-12-30 17:01:44 -08004112 def test_crd_check(self):
4113 "Test cross-repo dependencies in independent pipelines"
4114
Paul Belanger174a8272017-03-14 13:20:10 -04004115 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004116 self.gearman_server.hold_jobs_in_queue = True
4117 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4118 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4119
4120 # A Depends-On: B
4121 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4122 A.subject, B.data['id'])
4123
4124 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4125 self.waitUntilSettled()
4126
4127 queue = self.gearman_server.getQueue()
4128 ref = self.getParameter(queue[-1], 'ZUUL_REF')
4129 self.gearman_server.hold_jobs_in_queue = False
4130 self.gearman_server.release()
4131 self.waitUntilSettled()
4132
Paul Belanger174a8272017-03-14 13:20:10 -04004133 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05004134 self.waitUntilSettled()
4135
Monty Taylord642d852017-02-23 14:05:42 -05004136 path = os.path.join(self.builds[0].jobdir.src_root, "org/project1")
James E. Blairbfb8e042014-12-30 17:01:44 -08004137 repo = git.Repo(path)
4138 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4139 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004140 correct_messages = [
4141 'initial commit', 'add content from fixture', 'A-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004142 self.assertEqual(repo_messages, correct_messages)
4143
Monty Taylord642d852017-02-23 14:05:42 -05004144 path = os.path.join(self.builds[0].jobdir.src_root, "org/project2")
James E. Blairbfb8e042014-12-30 17:01:44 -08004145 repo = git.Repo(path)
4146 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4147 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004148 correct_messages = [
4149 'initial commit', 'add content from fixture', 'B-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004150 self.assertEqual(repo_messages, correct_messages)
4151
Paul Belanger174a8272017-03-14 13:20:10 -04004152 self.executor_server.hold_jobs_in_build = False
4153 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05004154 self.waitUntilSettled()
4155
James E. Blairbfb8e042014-12-30 17:01:44 -08004156 self.assertEqual(A.data['status'], 'NEW')
4157 self.assertEqual(B.data['status'], 'NEW')
4158 self.assertEqual(A.reported, 1)
4159 self.assertEqual(B.reported, 0)
4160
4161 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004162 tenant = self.sched.abide.tenants.get('tenant-one')
4163 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004164
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004165 def test_crd_check_git_depends(self):
4166 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004167 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004168 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4169 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4170
4171 # Add two git-dependent changes and make sure they both report
4172 # success.
4173 B.setDependsOn(A, 1)
4174 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4175 self.waitUntilSettled()
4176 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4177 self.waitUntilSettled()
4178
James E. Blairb8c16472015-05-05 14:55:26 -07004179 self.orderedRelease()
4180 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004181 self.waitUntilSettled()
4182
4183 self.assertEqual(A.data['status'], 'NEW')
4184 self.assertEqual(B.data['status'], 'NEW')
4185 self.assertEqual(A.reported, 1)
4186 self.assertEqual(B.reported, 1)
4187
4188 self.assertEqual(self.history[0].changes, '1,1')
4189 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004190 tenant = self.sched.abide.tenants.get('tenant-one')
4191 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004192
4193 self.assertIn('Build succeeded', A.messages[0])
4194 self.assertIn('Build succeeded', B.messages[0])
4195
4196 def test_crd_check_duplicate(self):
4197 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004198 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004199 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4200 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004201 tenant = self.sched.abide.tenants.get('tenant-one')
4202 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004203
4204 # Add two git-dependent changes...
4205 B.setDependsOn(A, 1)
4206 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4207 self.waitUntilSettled()
4208 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4209
4210 # ...make sure the live one is not duplicated...
4211 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4212 self.waitUntilSettled()
4213 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4214
4215 # ...but the non-live one is able to be.
4216 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4217 self.waitUntilSettled()
4218 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4219
Clark Boylandd849822015-03-02 12:38:14 -08004220 # Release jobs in order to avoid races with change A jobs
4221 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004222 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004223 self.executor_server.hold_jobs_in_build = False
4224 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004225 self.waitUntilSettled()
4226
4227 self.assertEqual(A.data['status'], 'NEW')
4228 self.assertEqual(B.data['status'], 'NEW')
4229 self.assertEqual(A.reported, 1)
4230 self.assertEqual(B.reported, 1)
4231
4232 self.assertEqual(self.history[0].changes, '1,1 2,1')
4233 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004234 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004235
4236 self.assertIn('Build succeeded', A.messages[0])
4237 self.assertIn('Build succeeded', B.messages[0])
4238
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004239 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004240 "Test cross-repo dependencies re-enqueued in independent pipelines"
4241
4242 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004243 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4244 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004245
4246 # A Depends-On: B
4247 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4248 A.subject, B.data['id'])
4249
4250 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4251 self.waitUntilSettled()
4252
4253 self.sched.reconfigure(self.config)
4254
4255 # Make sure the items still share a change queue, and the
4256 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004257 tenant = self.sched.abide.tenants.get('tenant-one')
4258 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4259 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004260 first_item = queue.queue[0]
4261 for item in queue.queue:
4262 self.assertEqual(item.queue, first_item.queue)
4263 self.assertFalse(first_item.live)
4264 self.assertTrue(queue.queue[1].live)
4265
4266 self.gearman_server.hold_jobs_in_queue = False
4267 self.gearman_server.release()
4268 self.waitUntilSettled()
4269
4270 self.assertEqual(A.data['status'], 'NEW')
4271 self.assertEqual(B.data['status'], 'NEW')
4272 self.assertEqual(A.reported, 1)
4273 self.assertEqual(B.reported, 0)
4274
4275 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004276 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004277
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004278 def test_crd_check_reconfiguration(self):
4279 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4280
4281 def test_crd_undefined_project(self):
4282 """Test that undefined projects in dependencies are handled for
4283 independent pipelines"""
4284 # It's a hack for fake gerrit,
4285 # as it implies repo creation upon the creation of any change
4286 self.init_repo("org/unknown")
4287 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4288
James E. Blair17dd6772015-02-09 14:45:18 -08004289 def test_crd_check_ignore_dependencies(self):
4290 "Test cross-repo dependencies can be ignored"
Clint Byrum12282ca2017-04-03 11:04:02 -07004291 self.updateConfigLayout('layout-ignore-dependencies')
James E. Blair17dd6772015-02-09 14:45:18 -08004292 self.sched.reconfigure(self.config)
James E. Blair17dd6772015-02-09 14:45:18 -08004293
4294 self.gearman_server.hold_jobs_in_queue = True
4295 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4296 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4297 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4298
4299 # A Depends-On: B
4300 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4301 A.subject, B.data['id'])
4302 # C git-depends on B
4303 C.setDependsOn(B, 1)
4304 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4305 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4306 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4307 self.waitUntilSettled()
4308
4309 # Make sure none of the items share a change queue, and all
4310 # are live.
Clint Byrum12282ca2017-04-03 11:04:02 -07004311 tenant = self.sched.abide.tenants.get('openstack')
4312 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004313 self.assertEqual(len(check_pipeline.queues), 3)
4314 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4315 for item in check_pipeline.getAllItems():
4316 self.assertTrue(item.live)
4317
4318 self.gearman_server.hold_jobs_in_queue = False
4319 self.gearman_server.release()
4320 self.waitUntilSettled()
4321
4322 self.assertEqual(A.data['status'], 'NEW')
4323 self.assertEqual(B.data['status'], 'NEW')
4324 self.assertEqual(C.data['status'], 'NEW')
4325 self.assertEqual(A.reported, 1)
4326 self.assertEqual(B.reported, 1)
4327 self.assertEqual(C.reported, 1)
4328
4329 # Each job should have tested exactly one change
4330 for job in self.history:
4331 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004332
James E. Blair7cb84542017-04-19 13:35:05 -07004333 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004334 def test_crd_check_transitive(self):
4335 "Test transitive cross-repo dependencies"
4336 # Specifically, if A -> B -> C, and C gets a new patchset and
4337 # A gets a new patchset, ensure the test of A,2 includes B,1
4338 # and C,2 (not C,1 which would indicate stale data in the
4339 # cache for B).
4340 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4341 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4342 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4343
4344 # A Depends-On: B
4345 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4346 A.subject, B.data['id'])
4347
4348 # B Depends-On: C
4349 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4350 B.subject, C.data['id'])
4351
4352 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4353 self.waitUntilSettled()
4354 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4355
4356 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4357 self.waitUntilSettled()
4358 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4359
4360 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4361 self.waitUntilSettled()
4362 self.assertEqual(self.history[-1].changes, '3,1')
4363
4364 C.addPatchset()
4365 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4366 self.waitUntilSettled()
4367 self.assertEqual(self.history[-1].changes, '3,2')
4368
4369 A.addPatchset()
4370 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4371 self.waitUntilSettled()
4372 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004373
James E. Blaira8b90b32016-08-24 15:18:50 -07004374 def test_crd_check_unknown(self):
4375 "Test unknown projects in independent pipeline"
4376 self.init_repo("org/unknown")
4377 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4378 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4379 # A Depends-On: B
4380 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4381 A.subject, B.data['id'])
4382
4383 # Make sure zuul has seen an event on B.
4384 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4385 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4386 self.waitUntilSettled()
4387
4388 self.assertEqual(A.data['status'], 'NEW')
4389 self.assertEqual(A.reported, 1)
4390 self.assertEqual(B.data['status'], 'NEW')
4391 self.assertEqual(B.reported, 0)
4392
James E. Blair92464a22016-04-05 10:21:26 -07004393 def test_crd_cycle_join(self):
4394 "Test an updated change creates a cycle"
4395 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4396
4397 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4398 self.waitUntilSettled()
4399
4400 # Create B->A
4401 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4402 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4403 B.subject, A.data['id'])
4404 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4405 self.waitUntilSettled()
4406
4407 # Update A to add A->B (a cycle).
4408 A.addPatchset()
4409 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4410 A.subject, B.data['id'])
4411 # Normally we would submit the patchset-created event for
4412 # processing here, however, we have no way of noting whether
4413 # the dependency cycle detection correctly raised an
4414 # exception, so instead, we reach into the source driver and
4415 # call the method that would ultimately be called by the event
4416 # processing.
4417
Paul Belanger0e155e22016-11-14 14:12:23 -05004418 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07004419 (trusted, project) = tenant.getProject('org/project')
4420 source = project.source
Paul Belanger0e155e22016-11-14 14:12:23 -05004421
4422 # TODO(pabelanger): As we add more source / trigger APIs we should make
4423 # it easier for users to create events for testing.
4424 event = zuul.model.TriggerEvent()
4425 event.trigger_name = 'gerrit'
4426 event.change_number = '1'
4427 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004428 with testtools.ExpectedException(
4429 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004430 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004431 self.log.debug("Got expected dependency cycle exception")
4432
4433 # Now if we update B to remove the depends-on, everything
4434 # should be okay. B; A->B
4435
4436 B.addPatchset()
4437 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004438
4439 source.getChange(event, True)
4440 event.change_number = '2'
4441 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004442
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004443 def test_disable_at(self):
4444 "Test a pipeline will only report to the disabled trigger when failing"
4445
Paul Belanger7dc76e82016-11-11 16:51:08 -05004446 self.updateConfigLayout('layout-disabled-at')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004447 self.sched.reconfigure(self.config)
4448
Paul Belanger7dc76e82016-11-11 16:51:08 -05004449 tenant = self.sched.abide.tenants.get('openstack')
4450 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004451 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004452 0, tenant.layout.pipelines['check']._consecutive_failures)
4453 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004454
4455 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4456 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4457 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4458 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4459 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4460 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4461 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4462 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4463 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4464 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4465 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4466
Paul Belanger174a8272017-03-14 13:20:10 -04004467 self.executor_server.failJob('project-test1', A)
4468 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004469 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004470 self.executor_server.failJob('project-test1', D)
4471 self.executor_server.failJob('project-test1', E)
4472 self.executor_server.failJob('project-test1', F)
4473 self.executor_server.failJob('project-test1', G)
4474 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004475 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004476 self.executor_server.failJob('project-test1', J)
4477 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004478
4479 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4480 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4481 self.waitUntilSettled()
4482
4483 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004484 2, tenant.layout.pipelines['check']._consecutive_failures)
4485 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004486
4487 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4488 self.waitUntilSettled()
4489
4490 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004491 0, tenant.layout.pipelines['check']._consecutive_failures)
4492 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004493
4494 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4495 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4496 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4497 self.waitUntilSettled()
4498
4499 # We should be disabled now
4500 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004501 3, tenant.layout.pipelines['check']._consecutive_failures)
4502 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004503
4504 # We need to wait between each of these patches to make sure the
4505 # smtp messages come back in an expected order
4506 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4507 self.waitUntilSettled()
4508 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4509 self.waitUntilSettled()
4510 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4511 self.waitUntilSettled()
4512
4513 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4514 # leaving a message on each change
4515 self.assertEqual(1, len(A.messages))
4516 self.assertIn('Build failed.', A.messages[0])
4517 self.assertEqual(1, len(B.messages))
4518 self.assertIn('Build failed.', B.messages[0])
4519 self.assertEqual(1, len(C.messages))
4520 self.assertIn('Build succeeded.', C.messages[0])
4521 self.assertEqual(1, len(D.messages))
4522 self.assertIn('Build failed.', D.messages[0])
4523 self.assertEqual(1, len(E.messages))
4524 self.assertIn('Build failed.', E.messages[0])
4525 self.assertEqual(1, len(F.messages))
4526 self.assertIn('Build failed.', F.messages[0])
4527
4528 # The last 3 (GHI) would have only reported via smtp.
4529 self.assertEqual(3, len(self.smtp_messages))
4530 self.assertEqual(0, len(G.messages))
4531 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004532 self.assertIn(
4533 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004534 self.assertEqual(0, len(H.messages))
4535 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004536 self.assertIn(
4537 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004538 self.assertEqual(0, len(I.messages))
4539 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004540 self.assertIn(
4541 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004542
4543 # Now reload the configuration (simulate a HUP) to check the pipeline
4544 # comes out of disabled
4545 self.sched.reconfigure(self.config)
4546
Paul Belanger7dc76e82016-11-11 16:51:08 -05004547 tenant = self.sched.abide.tenants.get('openstack')
4548
4549 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004550 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004551 0, tenant.layout.pipelines['check']._consecutive_failures)
4552 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004553
4554 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4555 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4556 self.waitUntilSettled()
4557
4558 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004559 2, tenant.layout.pipelines['check']._consecutive_failures)
4560 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004561
4562 # J and K went back to gerrit
4563 self.assertEqual(1, len(J.messages))
4564 self.assertIn('Build failed.', J.messages[0])
4565 self.assertEqual(1, len(K.messages))
4566 self.assertIn('Build failed.', K.messages[0])
4567 # No more messages reported via smtp
4568 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004569
James E. Blaircc020532017-04-19 13:04:44 -07004570 @simple_layout('layouts/one-job-project.yaml')
4571 def test_one_job_project(self):
4572 "Test that queueing works with one job"
4573 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4574 'master', 'A')
4575 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4576 'master', 'B')
4577 A.addApproval('code-review', 2)
4578 B.addApproval('code-review', 2)
4579 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4580 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4581 self.waitUntilSettled()
4582
4583 self.assertEqual(A.data['status'], 'MERGED')
4584 self.assertEqual(A.reported, 2)
4585 self.assertEqual(B.data['status'], 'MERGED')
4586 self.assertEqual(B.reported, 2)
4587
Paul Belanger71d98172016-11-08 10:56:31 -05004588 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004589 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004590
Paul Belanger174a8272017-03-14 13:20:10 -04004591 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004592 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4593 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4594 self.waitUntilSettled()
4595
Paul Belanger174a8272017-03-14 13:20:10 -04004596 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004597 self.waitUntilSettled()
4598
4599 self.assertEqual(len(self.builds), 2)
4600 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004601 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004602 self.waitUntilSettled()
4603
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004604 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004605 self.assertEqual(len(self.builds), 1,
4606 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004607 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004608 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004609 self.waitUntilSettled()
4610
Paul Belanger174a8272017-03-14 13:20:10 -04004611 self.executor_server.hold_jobs_in_build = False
4612 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004613 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004614 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004615 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4616 self.assertEqual(A.reported, 1)
4617 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004618
James E. Blair15be0e12017-01-03 13:45:20 -08004619 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004620 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004621
4622 self.fake_nodepool.paused = True
4623 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4624 A.addApproval('code-review', 2)
4625 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4626 self.waitUntilSettled()
4627
4628 self.zk.client.stop()
4629 self.zk.client.start()
4630 self.fake_nodepool.paused = False
4631 self.waitUntilSettled()
4632
4633 self.assertEqual(A.data['status'], 'MERGED')
4634 self.assertEqual(A.reported, 2)
4635
James E. Blair6ab79e02017-01-06 10:10:17 -08004636 def test_nodepool_failure(self):
4637 "Test that jobs are reported after a nodepool failure"
4638
4639 self.fake_nodepool.paused = True
4640 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4641 A.addApproval('code-review', 2)
4642 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4643 self.waitUntilSettled()
4644
4645 req = self.fake_nodepool.getNodeRequests()[0]
4646 self.fake_nodepool.addFailRequest(req)
4647
4648 self.fake_nodepool.paused = False
4649 self.waitUntilSettled()
4650
4651 self.assertEqual(A.data['status'], 'NEW')
4652 self.assertEqual(A.reported, 2)
4653 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4654 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4655 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4656
Arieb6f068c2016-10-09 13:11:06 +03004657
James E. Blaira002b032017-04-18 10:35:48 -07004658class TestExecutor(ZuulTestCase):
4659 tenant_config_file = 'config/single-tenant/main.yaml'
4660
4661 def assertFinalState(self):
4662 # In this test, we expect to shut down in a non-final state,
4663 # so skip these checks.
4664 pass
4665
4666 def assertCleanShutdown(self):
4667 self.log.debug("Assert clean shutdown")
4668
4669 # After shutdown, make sure no jobs are running
4670 self.assertEqual({}, self.executor_server.job_workers)
4671
4672 # Make sure that git.Repo objects have been garbage collected.
4673 repos = []
4674 gc.collect()
4675 for obj in gc.get_objects():
4676 if isinstance(obj, git.Repo):
4677 self.log.debug("Leaked git repo object: %s" % repr(obj))
4678 repos.append(obj)
4679 self.assertEqual(len(repos), 0)
4680
4681 def test_executor_shutdown(self):
4682 "Test that the executor can shut down with jobs running"
4683
4684 self.executor_server.hold_jobs_in_build = True
4685 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4686 A.addApproval('code-review', 2)
4687 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4688 self.waitUntilSettled()
4689
4690
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004691class TestDependencyGraph(ZuulTestCase):
4692 tenant_config_file = 'config/dependency-graph/main.yaml'
4693
4694 def test_dependeny_graph_dispatch_jobs_once(self):
4695 "Test a job in a dependency graph is queued only once"
4696 # Job dependencies, starting with A
4697 # A
4698 # / \
4699 # B C
4700 # / \ / \
4701 # D F E
4702 # |
4703 # G
4704
4705 self.executor_server.hold_jobs_in_build = True
4706 change = self.fake_gerrit.addFakeChange(
4707 'org/project', 'master', 'change')
4708 change.addApproval('code-review', 2)
4709 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4710
4711 self.waitUntilSettled()
4712 self.assertEqual([b.name for b in self.builds], ['A'])
4713
4714 self.executor_server.release('A')
4715 self.waitUntilSettled()
4716 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4717
4718 self.executor_server.release('B')
4719 self.waitUntilSettled()
4720 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4721
4722 self.executor_server.release('D')
4723 self.waitUntilSettled()
4724 self.assertEqual([b.name for b in self.builds], ['C'])
4725
4726 self.executor_server.release('C')
4727 self.waitUntilSettled()
4728 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4729
4730 self.executor_server.release('F')
4731 self.waitUntilSettled()
4732 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4733
4734 self.executor_server.release('G')
4735 self.waitUntilSettled()
4736 self.assertEqual([b.name for b in self.builds], ['E'])
4737
4738 self.executor_server.release('E')
4739 self.waitUntilSettled()
4740 self.assertEqual(len(self.builds), 0)
4741
4742 self.executor_server.hold_jobs_in_build = False
4743 self.executor_server.release()
4744 self.waitUntilSettled()
4745
4746 self.assertEqual(len(self.builds), 0)
4747 self.assertEqual(len(self.history), 7)
4748
4749 self.assertEqual(change.data['status'], 'MERGED')
4750 self.assertEqual(change.reported, 2)
4751
4752 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4753 "Test that a job waits till all dependencies are successful"
4754 # Job dependencies, starting with A
4755 # A
4756 # / \
4757 # B C*
4758 # / \ / \
4759 # D F E
4760 # |
4761 # G
4762
4763 self.executor_server.hold_jobs_in_build = True
4764 change = self.fake_gerrit.addFakeChange(
4765 'org/project', 'master', 'change')
4766 change.addApproval('code-review', 2)
4767
4768 self.executor_server.failJob('C', change)
4769
4770 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4771
4772 self.waitUntilSettled()
4773 self.assertEqual([b.name for b in self.builds], ['A'])
4774
4775 self.executor_server.release('A')
4776 self.waitUntilSettled()
4777 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4778
4779 self.executor_server.release('B')
4780 self.waitUntilSettled()
4781 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4782
4783 self.executor_server.release('D')
4784 self.waitUntilSettled()
4785 self.assertEqual([b.name for b in self.builds], ['C'])
4786
4787 self.executor_server.release('C')
4788 self.waitUntilSettled()
4789 self.assertEqual(len(self.builds), 0)
4790
4791 self.executor_server.hold_jobs_in_build = False
4792 self.executor_server.release()
4793 self.waitUntilSettled()
4794
4795 self.assertEqual(len(self.builds), 0)
4796 self.assertEqual(len(self.history), 4)
4797
4798 self.assertEqual(change.data['status'], 'NEW')
4799 self.assertEqual(change.reported, 2)
4800
4801
Arieb6f068c2016-10-09 13:11:06 +03004802class TestDuplicatePipeline(ZuulTestCase):
4803 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4804
4805 def test_duplicate_pipelines(self):
4806 "Test that a change matching multiple pipelines works"
4807
4808 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4809 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4810 self.waitUntilSettled()
4811
4812 self.assertHistory([
4813 dict(name='project-test1', result='SUCCESS', changes='1,1',
4814 pipeline='dup1'),
4815 dict(name='project-test1', result='SUCCESS', changes='1,1',
4816 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004817 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004818
4819 self.assertEqual(len(A.messages), 2)
4820
Paul Belangera46a3742016-11-09 14:23:03 -05004821 if 'dup1' in A.messages[0]:
4822 self.assertIn('dup1', A.messages[0])
4823 self.assertNotIn('dup2', A.messages[0])
4824 self.assertIn('project-test1', A.messages[0])
4825 self.assertIn('dup2', A.messages[1])
4826 self.assertNotIn('dup1', A.messages[1])
4827 self.assertIn('project-test1', A.messages[1])
4828 else:
4829 self.assertIn('dup1', A.messages[1])
4830 self.assertNotIn('dup2', A.messages[1])
4831 self.assertIn('project-test1', A.messages[1])
4832 self.assertIn('dup2', A.messages[0])
4833 self.assertNotIn('dup1', A.messages[0])
4834 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004835
4836
Paul Belanger86085b32016-11-03 12:48:57 -04004837class TestSchedulerTemplatedProject(ZuulTestCase):
4838 tenant_config_file = 'config/templated-project/main.yaml'
4839
Paul Belanger174a8272017-03-14 13:20:10 -04004840 def test_job_from_templates_executed(self):
4841 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004842
4843 A = self.fake_gerrit.addFakeChange(
4844 'org/templated-project', 'master', 'A')
4845 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4846 self.waitUntilSettled()
4847
4848 self.assertEqual(self.getJobFromHistory('project-test1').result,
4849 'SUCCESS')
4850 self.assertEqual(self.getJobFromHistory('project-test2').result,
4851 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004852
4853 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004854 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004855
4856 A = self.fake_gerrit.addFakeChange(
4857 'org/layered-project', 'master', 'A')
4858 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4859 self.waitUntilSettled()
4860
4861 self.assertEqual(self.getJobFromHistory('project-test1').result,
4862 'SUCCESS')
4863 self.assertEqual(self.getJobFromHistory('project-test2').result,
4864 'SUCCESS')
4865 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4866 ).result, 'SUCCESS')
4867 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4868 ).result, 'SUCCESS')
4869 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4870 ).result, 'SUCCESS')
4871 self.assertEqual(self.getJobFromHistory('project-test6').result,
4872 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004873
4874
4875class TestSchedulerSuccessURL(ZuulTestCase):
4876 tenant_config_file = 'config/success-url/main.yaml'
4877
4878 def test_success_url(self):
4879 "Ensure bad build params are ignored"
4880 self.sched.reconfigure(self.config)
4881 self.init_repo('org/docs')
4882
4883 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4884 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4885 self.waitUntilSettled()
4886
4887 # Both builds ran: docs-draft-test + docs-draft-test2
4888 self.assertEqual(len(self.history), 2)
4889
4890 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004891 for build in self.history:
4892 if build.name == 'docs-draft-test':
4893 uuid = build.uuid[:7]
4894 break
Adam Gandelman94a60062016-11-21 16:43:14 -08004895
4896 # Two msgs: 'Starting...' + results
4897 self.assertEqual(len(self.smtp_messages), 2)
4898 body = self.smtp_messages[1]['body'].splitlines()
4899 self.assertEqual('Build succeeded.', body[0])
4900
4901 self.assertIn(
4902 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4903 'docs-draft-test/%s/publish-docs/' % uuid,
4904 body[2])
4905
Paul Belanger174a8272017-03-14 13:20:10 -04004906 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004907 self.assertIn(
4908 '- docs-draft-test2 https://server/job',
4909 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004910
4911
Adam Gandelman4da00f62016-12-09 15:47:33 -08004912class TestSchedulerMerges(ZuulTestCase):
4913 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004914
4915 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004916 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004917 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004918 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004919 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4920 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4921 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4922 A.addApproval('code-review', 2)
4923 B.addApproval('code-review', 2)
4924 C.addApproval('code-review', 2)
4925 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4926 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4927 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4928 self.waitUntilSettled()
4929
4930 build = self.builds[-1]
4931 ref = self.getParameter(build, 'ZUUL_REF')
4932
Monty Taylord642d852017-02-23 14:05:42 -05004933 path = os.path.join(build.jobdir.src_root, project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004934 repo = git.Repo(path)
4935 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4936 repo_messages.reverse()
4937
Paul Belanger174a8272017-03-14 13:20:10 -04004938 self.executor_server.hold_jobs_in_build = False
4939 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004940 self.waitUntilSettled()
4941
4942 return repo_messages
4943
4944 def _test_merge(self, mode):
4945 us_path = os.path.join(
4946 self.upstream_root, 'org/project-%s' % mode)
4947 expected_messages = [
4948 'initial commit',
4949 'add content from fixture',
4950 # the intermediate commits order is nondeterministic
4951 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4952 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4953 ]
4954 result = self._test_project_merge_mode(mode)
4955 self.assertEqual(result[:2], expected_messages[:2])
4956 self.assertEqual(result[-2:], expected_messages[-2:])
4957
4958 def test_project_merge_mode_merge(self):
4959 self._test_merge('merge')
4960
4961 def test_project_merge_mode_merge_resolve(self):
4962 self._test_merge('merge-resolve')
4963
4964 def test_project_merge_mode_cherrypick(self):
4965 expected_messages = [
4966 'initial commit',
4967 'add content from fixture',
4968 'A-1',
4969 'B-1',
4970 'C-1']
4971 result = self._test_project_merge_mode('cherry-pick')
4972 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004973
4974 def test_merge_branch(self):
4975 "Test that the right commits are on alternate branches"
4976 self.create_branch('org/project-merge-branches', 'mp')
4977
Paul Belanger174a8272017-03-14 13:20:10 -04004978 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004979 A = self.fake_gerrit.addFakeChange(
4980 'org/project-merge-branches', 'mp', 'A')
4981 B = self.fake_gerrit.addFakeChange(
4982 'org/project-merge-branches', 'mp', 'B')
4983 C = self.fake_gerrit.addFakeChange(
4984 'org/project-merge-branches', 'mp', 'C')
4985 A.addApproval('code-review', 2)
4986 B.addApproval('code-review', 2)
4987 C.addApproval('code-review', 2)
4988 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4989 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4990 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4991 self.waitUntilSettled()
4992
Paul Belanger174a8272017-03-14 13:20:10 -04004993 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004994 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004995 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004996 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004997 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004998 self.waitUntilSettled()
4999
5000 build = self.builds[-1]
5001 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
5002 ref = self.getParameter(build, 'ZUUL_REF')
5003 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05005004 build.jobdir.src_root, 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005005 repo = git.Repo(path)
5006
5007 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
5008 repo_messages.reverse()
5009 correct_messages = [
5010 'initial commit',
5011 'add content from fixture',
5012 'mp commit',
5013 'A-1', 'B-1', 'C-1']
5014 self.assertEqual(repo_messages, correct_messages)
5015
Paul Belanger174a8272017-03-14 13:20:10 -04005016 self.executor_server.hold_jobs_in_build = False
5017 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005018 self.waitUntilSettled()
5019
5020 def test_merge_multi_branch(self):
5021 "Test that dependent changes on multiple branches are merged"
5022 self.create_branch('org/project-merge-branches', 'mp')
5023
Paul Belanger174a8272017-03-14 13:20:10 -04005024 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08005025 A = self.fake_gerrit.addFakeChange(
5026 'org/project-merge-branches', 'master', 'A')
5027 B = self.fake_gerrit.addFakeChange(
5028 'org/project-merge-branches', 'mp', 'B')
5029 C = self.fake_gerrit.addFakeChange(
5030 'org/project-merge-branches', 'master', 'C')
5031 A.addApproval('code-review', 2)
5032 B.addApproval('code-review', 2)
5033 C.addApproval('code-review', 2)
5034 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
5035 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
5036 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
5037 self.waitUntilSettled()
5038
5039 job_A = None
5040 for job in self.builds:
5041 if 'project-merge' in job.name:
5042 job_A = job
5043 ref_A = self.getParameter(job_A, 'ZUUL_REF')
5044 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
5045 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
5046 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
5047
5048 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05005049 job_A.jobdir.src_root, "org/project-merge-branches")
Adam Gandelman4da00f62016-12-09 15:47:33 -08005050 repo = git.Repo(path)
5051 repo_messages = [c.message.strip()
5052 for c in repo.iter_commits(ref_A)]
5053 repo_messages.reverse()
5054 correct_messages = [
5055 'initial commit', 'add content from fixture', 'A-1']
5056 self.assertEqual(repo_messages, correct_messages)
5057
Paul Belanger174a8272017-03-14 13:20:10 -04005058 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005059 self.waitUntilSettled()
5060
5061 job_B = None
5062 for job in self.builds:
5063 if 'project-merge' in job.name:
5064 job_B = job
5065 ref_B = self.getParameter(job_B, 'ZUUL_REF')
5066 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
5067 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
5068 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
5069
5070 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05005071 job_B.jobdir.src_root, "org/project-merge-branches")
Adam Gandelman4da00f62016-12-09 15:47:33 -08005072 repo = git.Repo(path)
5073 repo_messages = [c.message.strip()
5074 for c in repo.iter_commits(ref_B)]
5075 repo_messages.reverse()
5076 correct_messages = [
5077 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
5078 self.assertEqual(repo_messages, correct_messages)
5079
Paul Belanger174a8272017-03-14 13:20:10 -04005080 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005081 self.waitUntilSettled()
5082
5083 job_C = None
5084 for job in self.builds:
5085 if 'project-merge' in job.name:
5086 job_C = job
5087 ref_C = self.getParameter(job_C, 'ZUUL_REF')
5088 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
5089 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
5090 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
5091 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05005092 job_C.jobdir.src_root, "org/project-merge-branches")
Adam Gandelman4da00f62016-12-09 15:47:33 -08005093 repo = git.Repo(path)
5094 repo_messages = [c.message.strip()
5095 for c in repo.iter_commits(ref_C)]
5096
5097 repo_messages.reverse()
5098 correct_messages = [
5099 'initial commit', 'add content from fixture',
5100 'A-1', 'C-1']
5101 # Ensure the right commits are in the history for this ref
5102 self.assertEqual(repo_messages, correct_messages)
5103
Paul Belanger174a8272017-03-14 13:20:10 -04005104 self.executor_server.hold_jobs_in_build = False
5105 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005106 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005107
5108
5109class TestSemaphoreMultiTenant(ZuulTestCase):
5110 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5111
5112 def test_semaphore_tenant_isolation(self):
5113 "Test semaphores in multiple tenants"
5114
5115 self.waitUntilSettled()
5116 tenant_one = self.sched.abide.tenants.get('tenant-one')
5117 tenant_two = self.sched.abide.tenants.get('tenant-two')
5118
5119 self.executor_server.hold_jobs_in_build = True
5120 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5121 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5122 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5123 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5124 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5125 self.assertFalse('test-semaphore' in
5126 tenant_one.semaphore_handler.semaphores)
5127 self.assertFalse('test-semaphore' in
5128 tenant_two.semaphore_handler.semaphores)
5129
5130 # add patches to project1 of tenant-one
5131 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5132 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5133 self.waitUntilSettled()
5134
5135 # one build of project1-test1 must run
5136 # semaphore of tenant-one must be acquired once
5137 # semaphore of tenant-two must not be acquired
5138 self.assertEqual(len(self.builds), 1)
5139 self.assertEqual(self.builds[0].name, 'project1-test1')
5140 self.assertTrue('test-semaphore' in
5141 tenant_one.semaphore_handler.semaphores)
5142 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5143 'test-semaphore', [])), 1)
5144 self.assertFalse('test-semaphore' in
5145 tenant_two.semaphore_handler.semaphores)
5146
5147 # add patches to project2 of tenant-two
5148 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5149 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5150 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5151 self.waitUntilSettled()
5152
5153 # one build of project1-test1 must run
5154 # two builds of project2-test1 must run
5155 # semaphore of tenant-one must be acquired once
5156 # semaphore of tenant-two must be acquired twice
5157 self.assertEqual(len(self.builds), 3)
5158 self.assertEqual(self.builds[0].name, 'project1-test1')
5159 self.assertEqual(self.builds[1].name, 'project2-test1')
5160 self.assertEqual(self.builds[2].name, 'project2-test1')
5161 self.assertTrue('test-semaphore' in
5162 tenant_one.semaphore_handler.semaphores)
5163 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5164 'test-semaphore', [])), 1)
5165 self.assertTrue('test-semaphore' in
5166 tenant_two.semaphore_handler.semaphores)
5167 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5168 'test-semaphore', [])), 2)
5169
5170 self.executor_server.release('project1-test1')
5171 self.waitUntilSettled()
5172
5173 # one build of project1-test1 must run
5174 # two builds of project2-test1 must run
5175 # semaphore of tenant-one must be acquired once
5176 # semaphore of tenant-two must be acquired twice
5177 self.assertEqual(len(self.builds), 3)
5178 self.assertEqual(self.builds[0].name, 'project2-test1')
5179 self.assertEqual(self.builds[1].name, 'project2-test1')
5180 self.assertEqual(self.builds[2].name, 'project1-test1')
5181 self.assertTrue('test-semaphore' in
5182 tenant_one.semaphore_handler.semaphores)
5183 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5184 'test-semaphore', [])), 1)
5185 self.assertTrue('test-semaphore' in
5186 tenant_two.semaphore_handler.semaphores)
5187 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5188 'test-semaphore', [])), 2)
5189
5190 self.executor_server.release('project2-test1')
5191 self.waitUntilSettled()
5192
5193 # one build of project1-test1 must run
5194 # one build of project2-test1 must run
5195 # semaphore of tenant-one must be acquired once
5196 # semaphore of tenant-two must be acquired once
5197 self.assertEqual(len(self.builds), 2)
5198 self.assertTrue('test-semaphore' in
5199 tenant_one.semaphore_handler.semaphores)
5200 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5201 'test-semaphore', [])), 1)
5202 self.assertTrue('test-semaphore' in
5203 tenant_two.semaphore_handler.semaphores)
5204 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5205 'test-semaphore', [])), 1)
5206
5207 self.executor_server.hold_jobs_in_build = False
5208 self.executor_server.release()
5209
5210 self.waitUntilSettled()
5211
5212 # no build must run
5213 # semaphore of tenant-one must not be acquired
5214 # semaphore of tenant-two must not be acquired
5215 self.assertEqual(len(self.builds), 0)
5216 self.assertFalse('test-semaphore' in
5217 tenant_one.semaphore_handler.semaphores)
5218 self.assertFalse('test-semaphore' in
5219 tenant_two.semaphore_handler.semaphores)
5220
5221 self.assertEqual(A.reported, 1)
5222 self.assertEqual(B.reported, 1)
5223
5224
5225class TestSemaphoreInRepo(ZuulTestCase):
5226 tenant_config_file = 'config/in-repo/main.yaml'
5227
5228 def test_semaphore_in_repo(self):
5229 "Test semaphores in repo config"
5230
5231 # This tests dynamic semaphore handling in project repos. The semaphore
5232 # max value should not be evaluated dynamically but must be updated
5233 # after the change lands.
5234
5235 self.waitUntilSettled()
5236 tenant = self.sched.abide.tenants.get('tenant-one')
5237
5238 in_repo_conf = textwrap.dedent(
5239 """
5240 - job:
5241 name: project-test2
5242 semaphore: test-semaphore
5243
5244 - project:
5245 name: org/project
5246 tenant-one-gate:
5247 jobs:
5248 - project-test2
5249
5250 # the max value in dynamic layout must be ignored
5251 - semaphore:
5252 name: test-semaphore
5253 max: 2
5254 """)
5255
5256 in_repo_playbook = textwrap.dedent(
5257 """
5258 - hosts: all
5259 tasks: []
5260 """)
5261
5262 file_dict = {'.zuul.yaml': in_repo_conf,
5263 'playbooks/project-test2.yaml': in_repo_playbook}
5264 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5265 files=file_dict)
5266 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5267 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5268 B.setDependsOn(A, 1)
5269 C.setDependsOn(A, 1)
5270
5271 self.executor_server.hold_jobs_in_build = True
5272
5273 A.addApproval('code-review', 2)
5274 B.addApproval('code-review', 2)
5275 C.addApproval('code-review', 2)
5276 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
5277 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
5278 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
5279 self.waitUntilSettled()
5280
5281 # check that the layout in a queue item still has max value of 1
5282 # for test-semaphore
5283 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5284 queue = None
5285 for queue_candidate in pipeline.queues:
5286 if queue_candidate.name == 'org/project':
5287 queue = queue_candidate
5288 break
5289 queue_item = queue.queue[0]
5290 item_dynamic_layout = queue_item.current_build_set.layout
5291 dynamic_test_semaphore = \
5292 item_dynamic_layout.semaphores.get('test-semaphore')
5293 self.assertEqual(dynamic_test_semaphore.max, 1)
5294
5295 # one build must be in queue, one semaphores acquired
5296 self.assertEqual(len(self.builds), 1)
5297 self.assertEqual(self.builds[0].name, 'project-test2')
5298 self.assertTrue('test-semaphore' in
5299 tenant.semaphore_handler.semaphores)
5300 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5301 'test-semaphore', [])), 1)
5302
5303 self.executor_server.release('project-test2')
5304 self.waitUntilSettled()
5305
5306 # change A must be merged
5307 self.assertEqual(A.data['status'], 'MERGED')
5308 self.assertEqual(A.reported, 2)
5309
5310 # send change-merged event as the gerrit mock doesn't send it
5311 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5312 self.waitUntilSettled()
5313
5314 # now that change A was merged, the new semaphore max must be effective
5315 tenant = self.sched.abide.tenants.get('tenant-one')
5316 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5317
5318 # two builds must be in queue, two semaphores acquired
5319 self.assertEqual(len(self.builds), 2)
5320 self.assertEqual(self.builds[0].name, 'project-test2')
5321 self.assertEqual(self.builds[1].name, 'project-test2')
5322 self.assertTrue('test-semaphore' in
5323 tenant.semaphore_handler.semaphores)
5324 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5325 'test-semaphore', [])), 2)
5326
5327 self.executor_server.release('project-test2')
5328 self.waitUntilSettled()
5329
5330 self.assertEqual(len(self.builds), 0)
5331 self.assertFalse('test-semaphore' in
5332 tenant.semaphore_handler.semaphores)
5333
5334 self.executor_server.hold_jobs_in_build = False
5335 self.executor_server.release()
5336
5337 self.waitUntilSettled()
5338 self.assertEqual(len(self.builds), 0)
5339
5340 self.assertEqual(A.reported, 2)
5341 self.assertEqual(B.reported, 2)
5342 self.assertEqual(C.reported, 2)