blob: 93367b918ab8cefdf9f9c7142a224d51cc520744 [file] [log] [blame]
James E. Blairb0fcae42012-07-17 11:12:10 -07001#!/usr/bin/env python
2
3# Copyright 2012 Hewlett-Packard Development Company, L.P.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
James E. Blaira002b032017-04-18 10:35:48 -070017import gc
James E. Blairb0fcae42012-07-17 11:12:10 -070018import json
Tobias Henkel9a0e1942017-03-20 16:16:02 +010019import textwrap
20
Monty Taylorbc758832013-06-17 17:22:42 -040021import os
James E. Blairb0fcae42012-07-17 11:12:10 -070022import re
James E. Blair4886cc12012-07-18 15:39:41 -070023import shutil
Monty Taylorbc758832013-06-17 17:22:42 -040024import time
Clint Byrum03454a52017-05-26 17:14:02 -070025from unittest import skip
Monty Taylorbc758832013-06-17 17:22:42 -040026
James E. Blair4886cc12012-07-18 15:39:41 -070027import git
Monty Taylorbc758832013-06-17 17:22:42 -040028import testtools
Monty Taylorb934c1a2017-06-16 19:31:47 -050029import urllib
James E. Blairb0fcae42012-07-17 11:12:10 -070030
Maru Newby3fe5f852015-01-13 04:22:14 +000031import zuul.change_matcher
Clint Byrumb9de8be2017-05-27 13:49:19 -070032from zuul.driver.gerrit import gerritreporter
James E. Blairb0fcae42012-07-17 11:12:10 -070033import zuul.scheduler
James E. Blairad28e912013-11-27 10:43:22 -080034import zuul.rpcclient
Paul Belanger9bba4902016-11-02 16:07:33 -040035import zuul.model
James E. Blairb0fcae42012-07-17 11:12:10 -070036
Maru Newby3fe5f852015-01-13 04:22:14 +000037from tests.base import (
Paul Belanger0a21f0a2017-06-13 13:14:42 -040038 SSLZuulTestCase,
Maru Newby3fe5f852015-01-13 04:22:14 +000039 ZuulTestCase,
40 repack_repo,
James E. Blair06cc3922017-04-19 10:08:10 -070041 simple_layout,
Maru Newby3fe5f852015-01-13 04:22:14 +000042)
James E. Blairb0fcae42012-07-17 11:12:10 -070043
James E. Blairb0fcae42012-07-17 11:12:10 -070044
Paul Belanger0a21f0a2017-06-13 13:14:42 -040045class TestSchedulerSSL(SSLZuulTestCase):
46 tenant_config_file = 'config/single-tenant/main.yaml'
47
48 def test_jobs_executed(self):
49 "Test that jobs are executed and a change is merged"
50
51 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020052 A.addApproval('Code-Review', 2)
53 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Paul Belanger0a21f0a2017-06-13 13:14:42 -040054 self.waitUntilSettled()
55 self.assertEqual(self.getJobFromHistory('project-merge').result,
56 'SUCCESS')
57 self.assertEqual(self.getJobFromHistory('project-test1').result,
58 'SUCCESS')
59 self.assertEqual(self.getJobFromHistory('project-test2').result,
60 'SUCCESS')
61 self.assertEqual(A.data['status'], 'MERGED')
62 self.assertEqual(A.reported, 2)
63 self.assertEqual(self.getJobFromHistory('project-test1').node,
64 'label1')
65 self.assertIsNone(self.getJobFromHistory('project-test2').node)
66
67
Clark Boylanb640e052014-04-03 16:41:46 -070068class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070069 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010070
Paul Belanger174a8272017-03-14 13:20:10 -040071 def test_jobs_executed(self):
72 "Test that jobs are executed and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070073
James E. Blairb0fcae42012-07-17 11:12:10 -070074 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020075 A.addApproval('Code-Review', 2)
76 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070077 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040078 self.assertEqual(self.getJobFromHistory('project-merge').result,
79 'SUCCESS')
80 self.assertEqual(self.getJobFromHistory('project-test1').result,
81 'SUCCESS')
82 self.assertEqual(self.getJobFromHistory('project-test2').result,
83 'SUCCESS')
84 self.assertEqual(A.data['status'], 'MERGED')
85 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070086 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -070087 'label1')
Adam Gandelmaneace4692017-02-07 15:18:36 -080088 self.assertIsNone(self.getJobFromHistory('project-test2').node)
James E. Blairb0fcae42012-07-17 11:12:10 -070089
James E. Blair552b54f2016-07-22 13:55:32 -070090 # TODOv3(jeblair): we may want to report stats by tenant (also?).
Monty Taylorf1385042017-05-27 12:25:05 -050091 # Per-driver
92 self.assertReportedStat('zuul.event.gerrit.comment-added', value='1|c')
93 # Per-driver per-connection
94 self.assertReportedStat('zuul.event.gerrit.gerrit.comment-added',
95 value='1|c')
James E. Blair66eeebf2013-07-27 17:44:32 -070096 self.assertReportedStat('zuul.pipeline.gate.current_changes',
97 value='1|g')
98 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
99 kind='ms')
100 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
101 value='1|c')
102 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
103 self.assertReportedStat('zuul.pipeline.gate.total_changes',
104 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -0700105 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -0700106 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -0700107 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -0700108 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -0700109
James E. Blair2ef29e92017-07-21 15:25:05 -0700110 for build in self.history:
111 self.assertTrue(build.parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -0700112
James E. Blair3cb10702013-08-24 08:56:03 -0700113 def test_initial_pipeline_gauges(self):
114 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -0700115 self.assertReportedStat('zuul.pipeline.gate.current_changes',
116 value='0|g')
117 self.assertReportedStat('zuul.pipeline.check.current_changes',
118 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -0700119
James E. Blair34776ee2016-08-25 13:53:54 -0700120 def test_job_branch(self):
121 "Test the correct variant of a job runs on a branch"
122 self.create_branch('org/project', 'stable')
123 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200124 A.addApproval('Code-Review', 2)
125 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair34776ee2016-08-25 13:53:54 -0700126 self.waitUntilSettled()
127 self.assertEqual(self.getJobFromHistory('project-test1').result,
128 'SUCCESS')
129 self.assertEqual(self.getJobFromHistory('project-test2').result,
130 'SUCCESS')
131 self.assertEqual(A.data['status'], 'MERGED')
132 self.assertEqual(A.reported, 2,
133 "A should report start and success")
134 self.assertIn('gate', A.messages[1],
135 "A should transit gate")
136 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -0700137 'label2')
James E. Blair34776ee2016-08-25 13:53:54 -0700138
James E. Blairb0fcae42012-07-17 11:12:10 -0700139 def test_parallel_changes(self):
140 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700141
Paul Belanger174a8272017-03-14 13:20:10 -0400142 self.executor_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700143 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
144 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
145 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200146 A.addApproval('Code-Review', 2)
147 B.addApproval('Code-Review', 2)
148 C.addApproval('Code-Review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700149
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200150 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
151 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
152 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700153
154 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400155 self.assertEqual(len(self.builds), 1)
156 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700157 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700158
Paul Belanger174a8272017-03-14 13:20:10 -0400159 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700160 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400161 self.assertEqual(len(self.builds), 3)
162 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700163 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400164 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700165 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400166 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700167 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700168
Paul Belanger174a8272017-03-14 13:20:10 -0400169 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700170 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400171 self.assertEqual(len(self.builds), 5)
172 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700173 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400174 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700175 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700176
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400177 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700178 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400179 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700180 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700181
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400182 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700183 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700184
Paul Belanger174a8272017-03-14 13:20:10 -0400185 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700186 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400187 self.assertEqual(len(self.builds), 6)
188 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700189 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400190 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700191 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700192
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400193 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700194 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400195 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700196 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700197
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400198 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700199 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400200 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700201 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700202
Paul Belanger174a8272017-03-14 13:20:10 -0400203 self.executor_server.hold_jobs_in_build = False
204 self.executor_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700205 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400206 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700207
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400208 self.assertEqual(len(self.history), 9)
209 self.assertEqual(A.data['status'], 'MERGED')
210 self.assertEqual(B.data['status'], 'MERGED')
211 self.assertEqual(C.data['status'], 'MERGED')
212 self.assertEqual(A.reported, 2)
213 self.assertEqual(B.reported, 2)
214 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700215
216 def test_failed_changes(self):
217 "Test that a change behind a failed change is retested"
Paul Belanger174a8272017-03-14 13:20:10 -0400218 self.executor_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700219
James E. Blairb02a3bb2012-07-30 17:49:55 -0700220 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
221 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200222 A.addApproval('Code-Review', 2)
223 B.addApproval('Code-Review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700224
Paul Belanger174a8272017-03-14 13:20:10 -0400225 self.executor_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700226
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200227 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
228 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700229 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700230 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400231
Paul Belanger174a8272017-03-14 13:20:10 -0400232 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400233 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700234 # A/project-merge is complete
235 self.assertBuilds([
236 dict(name='project-test1', changes='1,1'),
237 dict(name='project-test2', changes='1,1'),
238 dict(name='project-merge', changes='1,1 2,1'),
239 ])
James E. Blaire2819012013-06-28 17:17:26 -0400240
Paul Belanger174a8272017-03-14 13:20:10 -0400241 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400242 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700243 # A/project-merge is complete
244 # B/project-merge is complete
245 self.assertBuilds([
246 dict(name='project-test1', changes='1,1'),
247 dict(name='project-test2', changes='1,1'),
248 dict(name='project-test1', changes='1,1 2,1'),
249 dict(name='project-test2', changes='1,1 2,1'),
250 ])
251
252 # Release project-test1 for A which will fail. This will
Paul Belanger174a8272017-03-14 13:20:10 -0400253 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700254 self.builds[0].release()
255 self.waitUntilSettled()
256
257 self.orderedRelease()
258 self.assertHistory([
259 dict(name='project-merge', result='SUCCESS', changes='1,1'),
260 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
261 dict(name='project-test1', result='FAILURE', changes='1,1'),
262 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
263 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
264 dict(name='project-test2', result='SUCCESS', changes='1,1'),
265 dict(name='project-merge', result='SUCCESS', changes='2,1'),
266 dict(name='project-test1', result='SUCCESS', changes='2,1'),
267 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700268 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700269
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400270 self.assertEqual(A.data['status'], 'NEW')
271 self.assertEqual(B.data['status'], 'MERGED')
272 self.assertEqual(A.reported, 2)
273 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700274
275 def test_independent_queues(self):
276 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700277
Paul Belanger174a8272017-03-14 13:20:10 -0400278 self.executor_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900279 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700280 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
281 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200282 A.addApproval('Code-Review', 2)
283 B.addApproval('Code-Review', 2)
284 C.addApproval('Code-Review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700285
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200286 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800287 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700288
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200289 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800290 self.waitUntilSettled()
291
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200292 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700293 self.waitUntilSettled()
294
295 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700296 self.assertBuilds([
297 dict(name='project-merge', changes='1,1'),
298 dict(name='project-merge', changes='2,1'),
299 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700300
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700301 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700302 self.builds[0].release()
303 self.waitUntilSettled()
304 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700305 self.waitUntilSettled()
306 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400307 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700308 self.waitUntilSettled()
309
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700310 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700311 self.assertBuilds([
312 dict(name='project-test1', changes='1,1'),
313 dict(name='project-test2', changes='1,1'),
314 dict(name='project-test1', changes='2,1'),
315 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500316 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700317 dict(name='project-test1', changes='2,1 3,1'),
318 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800319 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700320 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700321
James E. Blair5f0d49e2016-08-19 09:36:50 -0700322 self.orderedRelease()
323 self.assertHistory([
324 dict(name='project-merge', result='SUCCESS', changes='1,1'),
325 dict(name='project-merge', result='SUCCESS', changes='2,1'),
326 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
327 dict(name='project-test1', result='SUCCESS', changes='1,1'),
328 dict(name='project-test2', result='SUCCESS', changes='1,1'),
329 dict(name='project-test1', result='SUCCESS', changes='2,1'),
330 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500331 dict(
332 name='project1-project2-integration',
333 result='SUCCESS',
334 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700335 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
336 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800337 dict(name='project1-project2-integration',
338 result='SUCCESS',
339 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700340 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700341
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400342 self.assertEqual(A.data['status'], 'MERGED')
343 self.assertEqual(B.data['status'], 'MERGED')
344 self.assertEqual(C.data['status'], 'MERGED')
345 self.assertEqual(A.reported, 2)
346 self.assertEqual(B.reported, 2)
347 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700348
349 def test_failed_change_at_head(self):
350 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700351
Paul Belanger174a8272017-03-14 13:20:10 -0400352 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700353 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
354 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
355 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200356 A.addApproval('Code-Review', 2)
357 B.addApproval('Code-Review', 2)
358 C.addApproval('Code-Review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700359
Paul Belanger174a8272017-03-14 13:20:10 -0400360 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700361
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200362 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
363 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
364 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700365
366 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700367
James E. Blairb536ecc2016-08-31 10:11:42 -0700368 self.assertBuilds([
369 dict(name='project-merge', changes='1,1'),
370 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700371
Paul Belanger174a8272017-03-14 13:20:10 -0400372 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700373 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400374 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700375 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400376 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700377 self.waitUntilSettled()
378
James E. Blairb536ecc2016-08-31 10:11:42 -0700379 self.assertBuilds([
380 dict(name='project-test1', changes='1,1'),
381 dict(name='project-test2', changes='1,1'),
382 dict(name='project-test1', changes='1,1 2,1'),
383 dict(name='project-test2', changes='1,1 2,1'),
384 dict(name='project-test1', changes='1,1 2,1 3,1'),
385 dict(name='project-test2', changes='1,1 2,1 3,1'),
386 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700387
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400388 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700389 self.waitUntilSettled()
390
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400391 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700392 self.assertBuilds([
393 dict(name='project-test2', changes='1,1'),
394 dict(name='project-merge', changes='2,1'),
395 ])
396 # Unordered history comparison because the aborts can finish
397 # in any order.
398 self.assertHistory([
399 dict(name='project-merge', result='SUCCESS',
400 changes='1,1'),
401 dict(name='project-merge', result='SUCCESS',
402 changes='1,1 2,1'),
403 dict(name='project-merge', result='SUCCESS',
404 changes='1,1 2,1 3,1'),
405 dict(name='project-test1', result='FAILURE',
406 changes='1,1'),
407 dict(name='project-test1', result='ABORTED',
408 changes='1,1 2,1'),
409 dict(name='project-test2', result='ABORTED',
410 changes='1,1 2,1'),
411 dict(name='project-test1', result='ABORTED',
412 changes='1,1 2,1 3,1'),
413 dict(name='project-test2', result='ABORTED',
414 changes='1,1 2,1 3,1'),
415 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700416
Paul Belanger174a8272017-03-14 13:20:10 -0400417 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700418 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400419 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700420 self.waitUntilSettled()
421 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700422
James E. Blairb536ecc2016-08-31 10:11:42 -0700423 self.assertBuilds([])
424 self.assertHistory([
425 dict(name='project-merge', result='SUCCESS',
426 changes='1,1'),
427 dict(name='project-merge', result='SUCCESS',
428 changes='1,1 2,1'),
429 dict(name='project-merge', result='SUCCESS',
430 changes='1,1 2,1 3,1'),
431 dict(name='project-test1', result='FAILURE',
432 changes='1,1'),
433 dict(name='project-test1', result='ABORTED',
434 changes='1,1 2,1'),
435 dict(name='project-test2', result='ABORTED',
436 changes='1,1 2,1'),
437 dict(name='project-test1', result='ABORTED',
438 changes='1,1 2,1 3,1'),
439 dict(name='project-test2', result='ABORTED',
440 changes='1,1 2,1 3,1'),
441 dict(name='project-merge', result='SUCCESS',
442 changes='2,1'),
443 dict(name='project-merge', result='SUCCESS',
444 changes='2,1 3,1'),
445 dict(name='project-test2', result='SUCCESS',
446 changes='1,1'),
447 dict(name='project-test1', result='SUCCESS',
448 changes='2,1'),
449 dict(name='project-test2', result='SUCCESS',
450 changes='2,1'),
451 dict(name='project-test1', result='SUCCESS',
452 changes='2,1 3,1'),
453 dict(name='project-test2', result='SUCCESS',
454 changes='2,1 3,1'),
455 ], ordered=False)
456
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400457 self.assertEqual(A.data['status'], 'NEW')
458 self.assertEqual(B.data['status'], 'MERGED')
459 self.assertEqual(C.data['status'], 'MERGED')
460 self.assertEqual(A.reported, 2)
461 self.assertEqual(B.reported, 2)
462 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700463
James E. Blair0aac4872013-08-23 14:02:38 -0700464 def test_failed_change_in_middle(self):
465 "Test a failed change in the middle of the queue"
466
Paul Belanger174a8272017-03-14 13:20:10 -0400467 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700468 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
469 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
470 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200471 A.addApproval('Code-Review', 2)
472 B.addApproval('Code-Review', 2)
473 C.addApproval('Code-Review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700474
Paul Belanger174a8272017-03-14 13:20:10 -0400475 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700476
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200477 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
478 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
479 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700480
481 self.waitUntilSettled()
482
Paul Belanger174a8272017-03-14 13:20:10 -0400483 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700484 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400485 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700486 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400487 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700488 self.waitUntilSettled()
489
490 self.assertEqual(len(self.builds), 6)
491 self.assertEqual(self.builds[0].name, 'project-test1')
492 self.assertEqual(self.builds[1].name, 'project-test2')
493 self.assertEqual(self.builds[2].name, 'project-test1')
494 self.assertEqual(self.builds[3].name, 'project-test2')
495 self.assertEqual(self.builds[4].name, 'project-test1')
496 self.assertEqual(self.builds[5].name, 'project-test2')
497
498 self.release(self.builds[2])
499 self.waitUntilSettled()
500
James E. Blair972e3c72013-08-29 12:04:55 -0700501 # project-test1 and project-test2 for A
502 # project-test2 for B
503 # project-merge for C (without B)
504 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700505 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
506
Paul Belanger174a8272017-03-14 13:20:10 -0400507 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700508 self.waitUntilSettled()
509
510 # project-test1 and project-test2 for A
511 # project-test2 for B
512 # project-test1 and project-test2 for C
513 self.assertEqual(len(self.builds), 5)
514
Paul Belanger2e2a0492016-10-30 18:33:37 -0400515 tenant = self.sched.abide.tenants.get('tenant-one')
516 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700517 builds = items[0].current_build_set.getBuilds()
518 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
519 self.assertEqual(self.countJobResults(builds, None), 2)
520 builds = items[1].current_build_set.getBuilds()
521 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
522 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
523 self.assertEqual(self.countJobResults(builds, None), 1)
524 builds = items[2].current_build_set.getBuilds()
525 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700526 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700527
Paul Belanger174a8272017-03-14 13:20:10 -0400528 self.executor_server.hold_jobs_in_build = False
529 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700530 self.waitUntilSettled()
531
532 self.assertEqual(len(self.builds), 0)
533 self.assertEqual(len(self.history), 12)
534 self.assertEqual(A.data['status'], 'MERGED')
535 self.assertEqual(B.data['status'], 'NEW')
536 self.assertEqual(C.data['status'], 'MERGED')
537 self.assertEqual(A.reported, 2)
538 self.assertEqual(B.reported, 2)
539 self.assertEqual(C.reported, 2)
540
James E. Blaird466dc42012-07-31 10:42:56 -0700541 def test_failed_change_at_head_with_queue(self):
542 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700543
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700544 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700545 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
546 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
547 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200548 A.addApproval('Code-Review', 2)
549 B.addApproval('Code-Review', 2)
550 C.addApproval('Code-Review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700551
Paul Belanger174a8272017-03-14 13:20:10 -0400552 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700553
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200554 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
555 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
556 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700557
558 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700559 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400560 self.assertEqual(len(self.builds), 0)
561 self.assertEqual(len(queue), 1)
Clint Byrumf322fe22017-05-10 20:53:12 -0700562 self.assertEqual(queue[0].name, b'executor:execute')
563 job_args = json.loads(queue[0].arguments.decode('utf8'))
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800564 self.assertEqual(job_args['job'], 'project-merge')
565 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700566
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700567 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700568 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700569 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700570 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700571 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700572 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700573 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700574
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400575 self.assertEqual(len(self.builds), 0)
576 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800577
578 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700579 json.loads(queue[0].arguments.decode('utf8'))['job'],
580 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800581 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700582 json.loads(queue[1].arguments.decode('utf8'))['job'],
583 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800584 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700585 json.loads(queue[2].arguments.decode('utf8'))['job'],
586 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800587 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700588 json.loads(queue[3].arguments.decode('utf8'))['job'],
589 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800590 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700591 json.loads(queue[4].arguments.decode('utf8'))['job'],
592 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800593 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700594 json.loads(queue[5].arguments.decode('utf8'))['job'],
595 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700596
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700597 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700598 self.waitUntilSettled()
599
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400600 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700601 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400602 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
603 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700604
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700605 self.gearman_server.hold_jobs_in_queue = False
606 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700607 self.waitUntilSettled()
608
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400609 self.assertEqual(len(self.builds), 0)
610 self.assertEqual(len(self.history), 11)
611 self.assertEqual(A.data['status'], 'NEW')
612 self.assertEqual(B.data['status'], 'MERGED')
613 self.assertEqual(C.data['status'], 'MERGED')
614 self.assertEqual(A.reported, 2)
615 self.assertEqual(B.reported, 2)
616 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700617
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, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400620 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700621 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200622 A.addApproval('Code-Review', 2)
623 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700624 self.waitUntilSettled()
625 time.sleep(2)
626
627 data = json.loads(self.sched.formatStatusJSON())
628 found_job = None
629 for pipeline in data['pipelines']:
630 if pipeline['name'] != 'gate':
631 continue
632 for queue in pipeline['change_queues']:
633 for head in queue['heads']:
634 for item in head:
635 for job in item['jobs']:
636 if job['name'] == 'project-merge':
637 found_job = job
638 break
639
640 self.assertIsNotNone(found_job)
641 if iteration == 1:
642 self.assertIsNotNone(found_job['estimated_time'])
643 self.assertIsNone(found_job['remaining_time'])
644 else:
645 self.assertIsNotNone(found_job['estimated_time'])
646 self.assertTrue(found_job['estimated_time'] >= 2)
647 self.assertIsNotNone(found_job['remaining_time'])
648
Paul Belanger174a8272017-03-14 13:20:10 -0400649 self.executor_server.hold_jobs_in_build = False
650 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700651 self.waitUntilSettled()
652
James E. Blairec056492016-07-22 09:45:56 -0700653 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700654 def test_time_database(self):
655 "Test the time database"
656
657 self._test_time_database(1)
658 self._test_time_database(2)
659
James E. Blairfef71632013-09-23 11:15:47 -0700660 def test_two_failed_changes_at_head(self):
661 "Test that changes are reparented correctly if 2 fail at head"
662
Paul Belanger174a8272017-03-14 13:20:10 -0400663 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700664 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
665 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
666 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200667 A.addApproval('Code-Review', 2)
668 B.addApproval('Code-Review', 2)
669 C.addApproval('Code-Review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700670
Paul Belanger174a8272017-03-14 13:20:10 -0400671 self.executor_server.failJob('project-test1', A)
672 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700673
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200674 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
675 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
676 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700677 self.waitUntilSettled()
678
Paul Belanger174a8272017-03-14 13:20:10 -0400679 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700680 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400681 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700682 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400683 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700684 self.waitUntilSettled()
685
686 self.assertEqual(len(self.builds), 6)
687 self.assertEqual(self.builds[0].name, 'project-test1')
688 self.assertEqual(self.builds[1].name, 'project-test2')
689 self.assertEqual(self.builds[2].name, 'project-test1')
690 self.assertEqual(self.builds[3].name, 'project-test2')
691 self.assertEqual(self.builds[4].name, 'project-test1')
692 self.assertEqual(self.builds[5].name, 'project-test2')
693
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400694 self.assertTrue(self.builds[0].hasChanges(A))
695 self.assertTrue(self.builds[2].hasChanges(A))
696 self.assertTrue(self.builds[2].hasChanges(B))
697 self.assertTrue(self.builds[4].hasChanges(A))
698 self.assertTrue(self.builds[4].hasChanges(B))
699 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700700
701 # Fail change B first
702 self.release(self.builds[2])
703 self.waitUntilSettled()
704
705 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400706 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700707 self.waitUntilSettled()
708
709 self.assertEqual(len(self.builds), 5)
710 self.assertEqual(self.builds[0].name, 'project-test1')
711 self.assertEqual(self.builds[1].name, 'project-test2')
712 self.assertEqual(self.builds[2].name, 'project-test2')
713 self.assertEqual(self.builds[3].name, 'project-test1')
714 self.assertEqual(self.builds[4].name, 'project-test2')
715
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400716 self.assertTrue(self.builds[1].hasChanges(A))
717 self.assertTrue(self.builds[2].hasChanges(A))
718 self.assertTrue(self.builds[2].hasChanges(B))
719 self.assertTrue(self.builds[4].hasChanges(A))
720 self.assertFalse(self.builds[4].hasChanges(B))
721 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700722
723 # Finish running all passing jobs for change A
724 self.release(self.builds[1])
725 self.waitUntilSettled()
726 # Fail and report change A
727 self.release(self.builds[0])
728 self.waitUntilSettled()
729
730 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400731 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700732 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400733 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700734 self.waitUntilSettled()
735
736 self.assertEqual(len(self.builds), 4)
737 self.assertEqual(self.builds[0].name, 'project-test1') # B
738 self.assertEqual(self.builds[1].name, 'project-test2') # B
739 self.assertEqual(self.builds[2].name, 'project-test1') # C
740 self.assertEqual(self.builds[3].name, 'project-test2') # C
741
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400742 self.assertFalse(self.builds[1].hasChanges(A))
743 self.assertTrue(self.builds[1].hasChanges(B))
744 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700745
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400746 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700747 # After A failed and B and C restarted, B should be back in
748 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400749 self.assertTrue(self.builds[2].hasChanges(B))
750 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700751
Paul Belanger174a8272017-03-14 13:20:10 -0400752 self.executor_server.hold_jobs_in_build = False
753 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700754 self.waitUntilSettled()
755
756 self.assertEqual(len(self.builds), 0)
757 self.assertEqual(len(self.history), 21)
758 self.assertEqual(A.data['status'], 'NEW')
759 self.assertEqual(B.data['status'], 'NEW')
760 self.assertEqual(C.data['status'], 'MERGED')
761 self.assertEqual(A.reported, 2)
762 self.assertEqual(B.reported, 2)
763 self.assertEqual(C.reported, 2)
764
James E. Blair8c803f82012-07-31 16:25:42 -0700765 def test_patch_order(self):
766 "Test that dependent patches are tested in the right order"
767 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
768 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
769 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200770 A.addApproval('Code-Review', 2)
771 B.addApproval('Code-Review', 2)
772 C.addApproval('Code-Review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700773
774 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
775 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
776 M2.setMerged()
777 M1.setMerged()
778
779 # C -> B -> A -> M1 -> M2
780 # M2 is here to make sure it is never queried. If it is, it
781 # means zuul is walking down the entire history of merged
782 # changes.
783
784 C.setDependsOn(B, 1)
785 B.setDependsOn(A, 1)
786 A.setDependsOn(M1, 1)
787 M1.setDependsOn(M2, 1)
788
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200789 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700790
791 self.waitUntilSettled()
792
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400793 self.assertEqual(A.data['status'], 'NEW')
794 self.assertEqual(B.data['status'], 'NEW')
795 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700796
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200797 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
798 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700799
800 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400801 self.assertEqual(M2.queried, 0)
802 self.assertEqual(A.data['status'], 'MERGED')
803 self.assertEqual(B.data['status'], 'MERGED')
804 self.assertEqual(C.data['status'], 'MERGED')
805 self.assertEqual(A.reported, 2)
806 self.assertEqual(B.reported, 2)
807 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700808
James E. Blair063672f2015-01-29 13:09:12 -0800809 def test_needed_changes_enqueue(self):
810 "Test that a needed change is enqueued ahead"
811 # A Given a git tree like this, if we enqueue
812 # / \ change C, we should walk up and down the tree
813 # B G and enqueue changes in the order ABCDEFG.
814 # /|\ This is also the order that you would get if
815 # *C E F you enqueued changes in the order ABCDEFG, so
816 # / the ordering is stable across re-enqueue events.
817 # D
818
819 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
820 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
821 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
822 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
823 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
824 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
825 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
826 B.setDependsOn(A, 1)
827 C.setDependsOn(B, 1)
828 D.setDependsOn(C, 1)
829 E.setDependsOn(B, 1)
830 F.setDependsOn(B, 1)
831 G.setDependsOn(A, 1)
832
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200833 A.addApproval('Code-Review', 2)
834 B.addApproval('Code-Review', 2)
835 C.addApproval('Code-Review', 2)
836 D.addApproval('Code-Review', 2)
837 E.addApproval('Code-Review', 2)
838 F.addApproval('Code-Review', 2)
839 G.addApproval('Code-Review', 2)
840 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800841
842 self.waitUntilSettled()
843
844 self.assertEqual(A.data['status'], 'NEW')
845 self.assertEqual(B.data['status'], 'NEW')
846 self.assertEqual(C.data['status'], 'NEW')
847 self.assertEqual(D.data['status'], 'NEW')
848 self.assertEqual(E.data['status'], 'NEW')
849 self.assertEqual(F.data['status'], 'NEW')
850 self.assertEqual(G.data['status'], 'NEW')
851
852 # We're about to add approvals to changes without adding the
853 # triggering events to Zuul, so that we can be sure that it is
854 # enqueing the changes based on dependencies, not because of
855 # triggering events. Since it will have the changes cached
856 # already (without approvals), we need to clear the cache
857 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400858 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100859 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800860
Paul Belanger174a8272017-03-14 13:20:10 -0400861 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200862 A.addApproval('Approved', 1)
863 B.addApproval('Approved', 1)
864 D.addApproval('Approved', 1)
865 E.addApproval('Approved', 1)
866 F.addApproval('Approved', 1)
867 G.addApproval('Approved', 1)
868 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800869
870 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400871 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800872 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400873 self.executor_server.hold_jobs_in_build = False
874 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800875 self.waitUntilSettled()
876
877 self.assertEqual(A.data['status'], 'MERGED')
878 self.assertEqual(B.data['status'], 'MERGED')
879 self.assertEqual(C.data['status'], 'MERGED')
880 self.assertEqual(D.data['status'], 'MERGED')
881 self.assertEqual(E.data['status'], 'MERGED')
882 self.assertEqual(F.data['status'], 'MERGED')
883 self.assertEqual(G.data['status'], 'MERGED')
884 self.assertEqual(A.reported, 2)
885 self.assertEqual(B.reported, 2)
886 self.assertEqual(C.reported, 2)
887 self.assertEqual(D.reported, 2)
888 self.assertEqual(E.reported, 2)
889 self.assertEqual(F.reported, 2)
890 self.assertEqual(G.reported, 2)
891 self.assertEqual(self.history[6].changes,
892 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
893
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100894 def test_source_cache(self):
895 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400896 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700897
898 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
899 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
900 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200901 A.addApproval('Code-Review', 2)
902 B.addApproval('Code-Review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700903
904 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
905 M1.setMerged()
906
907 B.setDependsOn(A, 1)
908 A.setDependsOn(M1, 1)
909
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200910 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700911 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
912
913 self.waitUntilSettled()
914
915 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700916 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700917 build.release()
918 self.waitUntilSettled()
919 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700920 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700921 build.release()
922 self.waitUntilSettled()
923
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200924 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700925 self.waitUntilSettled()
926
Joshua Hesketh352264b2015-08-11 23:42:08 +1000927 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700928 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000929 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700930
Paul Belanger174a8272017-03-14 13:20:10 -0400931 self.executor_server.hold_jobs_in_build = False
932 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700933 self.waitUntilSettled()
934
935 self.assertEqual(A.data['status'], 'MERGED')
936 self.assertEqual(B.data['status'], 'MERGED')
937 self.assertEqual(A.queried, 2) # Initial and isMerged
938 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
939
James E. Blair8c803f82012-07-31 16:25:42 -0700940 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700941 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700942 # TODO: move to test_gerrit (this is a unit test!)
943 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400944 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -0700945 (trusted, project) = tenant.getProject('org/project')
946 source = project.source
Paul Belanger9bba4902016-11-02 16:07:33 -0400947
948 # TODO(pabelanger): As we add more source / trigger APIs we should make
949 # it easier for users to create events for testing.
950 event = zuul.model.TriggerEvent()
951 event.trigger_name = 'gerrit'
952 event.change_number = '1'
953 event.patch_number = '2'
954
955 a = source.getChange(event)
956 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700957 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700958
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200959 A.addApproval('Code-Review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400960 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700961 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700962
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200963 A.addApproval('Approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400964 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700965 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700966
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700967 def test_project_merge_conflict(self):
968 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700969
970 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700971 A = self.fake_gerrit.addFakeChange('org/project',
972 'master', 'A',
973 files={'conflict': 'foo'})
974 B = self.fake_gerrit.addFakeChange('org/project',
975 'master', 'B',
976 files={'conflict': 'bar'})
977 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -0700978 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200979 A.addApproval('Code-Review', 2)
980 B.addApproval('Code-Review', 2)
981 C.addApproval('Code-Review', 2)
982 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
983 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
984 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700985 self.waitUntilSettled()
986
James E. Blair6736beb2013-07-11 15:18:15 -0700987 self.assertEqual(A.reported, 1)
James E. Blair6736beb2013-07-11 15:18:15 -0700988 self.assertEqual(C.reported, 1)
989
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700990 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700991 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700992 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700993 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700994 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700995 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700996
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700997 self.gearman_server.hold_jobs_in_queue = False
998 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700999 self.waitUntilSettled()
1000
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001001 self.assertEqual(A.data['status'], 'MERGED')
1002 self.assertEqual(B.data['status'], 'NEW')
1003 self.assertEqual(C.data['status'], 'MERGED')
1004 self.assertEqual(A.reported, 2)
Tobias Henkel9842bd72017-05-16 13:40:03 +02001005 self.assertIn('Merge Failed', B.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001006 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001007
1008 self.assertHistory([
1009 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1010 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1011 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1012 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
1013 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
1014 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
1015 ], ordered=False)
1016
1017 def test_delayed_merge_conflict(self):
1018 "Test that delayed check merge conflicts are handled properly"
1019
1020 # Hold jobs in the gearman queue so that we can test whether
James E. Blair1960d682017-04-28 15:44:14 -07001021 # the executor sucesfully merges a change based on an old
1022 # repo state (frozen by the scheduler) which would otherwise
1023 # conflict.
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001024 self.gearman_server.hold_jobs_in_queue = True
1025 A = self.fake_gerrit.addFakeChange('org/project',
1026 'master', 'A',
1027 files={'conflict': 'foo'})
1028 B = self.fake_gerrit.addFakeChange('org/project',
1029 'master', 'B',
1030 files={'conflict': 'bar'})
1031 C = self.fake_gerrit.addFakeChange('org/project',
1032 'master', 'C')
1033 C.setDependsOn(B, 1)
1034
1035 # A enters the gate queue; B and C enter the check queue
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001036 A.addApproval('Code-Review', 2)
1037 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001038 self.waitUntilSettled()
1039 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
James E. Blair8d144dc2017-05-05 10:13:45 -07001040 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001041 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1042 self.waitUntilSettled()
1043
1044 self.assertEqual(A.reported, 1)
1045 self.assertEqual(B.reported, 0) # Check does not report start
1046 self.assertEqual(C.reported, 0) # Check does not report start
1047
1048 # A merges while B and C are queued in check
1049 # Release A project-merge
1050 queue = self.gearman_server.getQueue()
1051 self.release(queue[0])
1052 self.waitUntilSettled()
1053
1054 # Release A project-test*
1055 # gate has higher precedence, so A's test jobs are added in
1056 # front of the merge jobs for B and C
1057 queue = self.gearman_server.getQueue()
1058 self.release(queue[0])
1059 self.release(queue[1])
1060 self.waitUntilSettled()
1061
1062 self.assertEqual(A.data['status'], 'MERGED')
1063 self.assertEqual(B.data['status'], 'NEW')
1064 self.assertEqual(C.data['status'], 'NEW')
1065 self.assertEqual(A.reported, 2)
1066 self.assertEqual(B.reported, 0)
1067 self.assertEqual(C.reported, 0)
1068 self.assertHistory([
1069 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1070 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1071 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1072 ], ordered=False)
1073
1074 # B and C report merge conflicts
1075 # Release B project-merge
1076 queue = self.gearman_server.getQueue()
1077 self.release(queue[0])
1078 self.waitUntilSettled()
1079
1080 # Release C
1081 self.gearman_server.hold_jobs_in_queue = False
1082 self.gearman_server.release()
1083 self.waitUntilSettled()
1084
1085 self.assertEqual(A.data['status'], 'MERGED')
1086 self.assertEqual(B.data['status'], 'NEW')
1087 self.assertEqual(C.data['status'], 'NEW')
1088 self.assertEqual(A.reported, 2)
1089 self.assertEqual(B.reported, 1)
1090 self.assertEqual(C.reported, 1)
1091
1092 self.assertHistory([
1093 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1094 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1095 dict(name='project-test2', result='SUCCESS', changes='1,1'),
James E. Blair1960d682017-04-28 15:44:14 -07001096 dict(name='project-merge', result='SUCCESS', changes='2,1'),
1097 dict(name='project-test1', result='SUCCESS', changes='2,1'),
1098 dict(name='project-test2', result='SUCCESS', changes='2,1'),
1099 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
1100 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
1101 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001102 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001103
James E. Blairdaabed22012-08-15 15:38:57 -07001104 def test_post(self):
1105 "Test that post jobs run"
James E. Blair289f5932017-07-27 15:02:29 -07001106 p = "review.example.com/org/project"
1107 upstream = self.getUpstreamRepos([p])
1108 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1109 A.setMerged()
1110 A_commit = str(upstream[p].commit('master'))
1111 self.log.debug("A commit: %s" % A_commit)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001112
Zhongyue Luo5d556072012-09-21 02:00:47 +09001113 e = {
1114 "type": "ref-updated",
1115 "submitter": {
1116 "name": "User Name",
1117 },
1118 "refUpdate": {
1119 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
James E. Blair289f5932017-07-27 15:02:29 -07001120 "newRev": A_commit,
Zhongyue Luo5d556072012-09-21 02:00:47 +09001121 "refName": "master",
1122 "project": "org/project",
1123 }
1124 }
James E. Blairdaabed22012-08-15 15:38:57 -07001125 self.fake_gerrit.addEvent(e)
1126 self.waitUntilSettled()
1127
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001128 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001129 self.assertEqual(len(self.history), 1)
1130 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001131
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001132 def test_post_ignore_deletes(self):
1133 "Test that deleting refs does not trigger post jobs"
1134
1135 e = {
1136 "type": "ref-updated",
1137 "submitter": {
1138 "name": "User Name",
1139 },
1140 "refUpdate": {
1141 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1142 "newRev": "0000000000000000000000000000000000000000",
1143 "refName": "master",
1144 "project": "org/project",
1145 }
1146 }
1147 self.fake_gerrit.addEvent(e)
1148 self.waitUntilSettled()
1149
1150 job_names = [x.name for x in self.history]
1151 self.assertEqual(len(self.history), 0)
1152 self.assertNotIn('project-post', job_names)
1153
James E. Blaircc324192017-04-19 14:36:08 -07001154 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001155 def test_post_ignore_deletes_negative(self):
1156 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001157 e = {
1158 "type": "ref-updated",
1159 "submitter": {
1160 "name": "User Name",
1161 },
1162 "refUpdate": {
1163 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1164 "newRev": "0000000000000000000000000000000000000000",
James E. Blair289f5932017-07-27 15:02:29 -07001165 "refName": "testbranch",
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001166 "project": "org/project",
1167 }
1168 }
1169 self.fake_gerrit.addEvent(e)
1170 self.waitUntilSettled()
1171
1172 job_names = [x.name for x in self.history]
1173 self.assertEqual(len(self.history), 1)
1174 self.assertIn('project-post', job_names)
1175
James E. Blairec056492016-07-22 09:45:56 -07001176 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001177 def test_build_configuration_branch_interaction(self):
1178 "Test that switching between branches works"
1179 self.test_build_configuration()
1180 self.test_build_configuration_branch()
1181 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001182 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001183 repo = git.Repo(path)
1184 repo.heads.master.commit = repo.commit('init')
1185 self.test_build_configuration()
1186
James E. Blairdb93b302017-07-19 15:33:11 -07001187 def test_dependent_changes_rebase(self):
1188 # Test that no errors occur when we walk a dependency tree
1189 # with an unused leaf node due to a rebase.
1190 # Start by constructing: C -> B -> A
1191 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1192 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1193 B.setDependsOn(A, 1)
1194
1195 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1196 C.setDependsOn(B, 1)
1197
1198 # Then rebase to form: D -> C -> A
1199 C.addPatchset() # C,2
1200 C.setDependsOn(A, 1)
1201
1202 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1203 D.setDependsOn(C, 2)
1204
1205 # Walk the entire tree
1206 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1207 self.waitUntilSettled()
1208 self.assertEqual(len(self.history), 3)
1209
1210 # Verify that walking just part of the tree still works
1211 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
1212 self.waitUntilSettled()
1213 self.assertEqual(len(self.history), 6)
1214
James E. Blaircaec0c52012-08-22 14:52:22 -07001215 def test_dependent_changes_dequeue(self):
1216 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001217
James E. Blaircaec0c52012-08-22 14:52:22 -07001218 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1219 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1220 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001221 A.addApproval('Code-Review', 2)
1222 B.addApproval('Code-Review', 2)
1223 C.addApproval('Code-Review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001224
1225 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1226 M1.setMerged()
1227
1228 # C -> B -> A -> M1
1229
1230 C.setDependsOn(B, 1)
1231 B.setDependsOn(A, 1)
1232 A.setDependsOn(M1, 1)
1233
Paul Belanger174a8272017-03-14 13:20:10 -04001234 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001235
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001236 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1237 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1238 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001239
1240 self.waitUntilSettled()
1241
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001242 self.assertEqual(A.data['status'], 'NEW')
1243 self.assertEqual(A.reported, 2)
1244 self.assertEqual(B.data['status'], 'NEW')
1245 self.assertEqual(B.reported, 2)
1246 self.assertEqual(C.data['status'], 'NEW')
James E. Blair0d3e83b2017-06-05 13:51:57 -07001247 self.assertIn('This change depends on a change that failed to merge.',
1248 C.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001249 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001250
James E. Blair972e3c72013-08-29 12:04:55 -07001251 def test_failing_dependent_changes(self):
1252 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001253 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001254 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1255 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1256 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1257 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1258 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001259 A.addApproval('Code-Review', 2)
1260 B.addApproval('Code-Review', 2)
1261 C.addApproval('Code-Review', 2)
1262 D.addApproval('Code-Review', 2)
1263 E.addApproval('Code-Review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001264
1265 # E, D -> C -> B, A
1266
1267 D.setDependsOn(C, 1)
1268 C.setDependsOn(B, 1)
1269
Paul Belanger174a8272017-03-14 13:20:10 -04001270 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001271
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001272 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1273 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
1274 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1275 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1276 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001277
1278 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001279 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001280 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001281 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001282 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001283 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001284 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001285 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001286 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001287 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001288 self.waitUntilSettled()
1289
Paul Belanger174a8272017-03-14 13:20:10 -04001290 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001291 for build in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07001292 if build.parameters['zuul']['change'] != '1':
James E. Blair972e3c72013-08-29 12:04:55 -07001293 build.release()
1294 self.waitUntilSettled()
1295
Paul Belanger174a8272017-03-14 13:20:10 -04001296 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001297 self.waitUntilSettled()
1298
1299 self.assertEqual(A.data['status'], 'MERGED')
1300 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001301 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001302 self.assertEqual(B.data['status'], 'NEW')
1303 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001304 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001305 self.assertEqual(C.data['status'], 'NEW')
1306 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001307 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001308 self.assertEqual(D.data['status'], 'NEW')
1309 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001310 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001311 self.assertEqual(E.data['status'], 'MERGED')
1312 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001313 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001314 self.assertEqual(len(self.history), 18)
1315
James E. Blairec590122012-08-22 15:19:31 -07001316 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001317 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001318 # If it's dequeued more than once, we should see extra
1319 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001320
Paul Belanger174a8272017-03-14 13:20:10 -04001321 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001322 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1323 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1324 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001325 A.addApproval('Code-Review', 2)
1326 B.addApproval('Code-Review', 2)
1327 C.addApproval('Code-Review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001328
Paul Belanger174a8272017-03-14 13:20:10 -04001329 self.executor_server.failJob('project-test1', A)
1330 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001331
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001332 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1333 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1334 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001335
1336 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001337
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001338 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001339 self.assertEqual(self.builds[0].name, 'project-merge')
1340 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001341
Paul Belanger174a8272017-03-14 13:20:10 -04001342 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001343 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001344 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001345 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001346 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001347 self.waitUntilSettled()
1348
James E. Blair6df67132017-04-19 13:53:20 -07001349 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001350 self.assertEqual(self.builds[0].name, 'project-test1')
1351 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001352 self.assertEqual(self.builds[2].name, 'project-test1')
1353 self.assertEqual(self.builds[3].name, 'project-test2')
1354 self.assertEqual(self.builds[4].name, 'project-test1')
1355 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001356
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001357 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001358 self.waitUntilSettled()
1359
James E. Blair6df67132017-04-19 13:53:20 -07001360 self.assertEqual(len(self.builds), 2) # test2, merge for B
1361 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001362
Paul Belanger174a8272017-03-14 13:20:10 -04001363 self.executor_server.hold_jobs_in_build = False
1364 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001365 self.waitUntilSettled()
1366
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001367 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001368 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001369
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001370 self.assertEqual(A.data['status'], 'NEW')
1371 self.assertEqual(B.data['status'], 'MERGED')
1372 self.assertEqual(C.data['status'], 'MERGED')
1373 self.assertEqual(A.reported, 2)
1374 self.assertEqual(B.reported, 2)
1375 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001376
James E. Blaircc020532017-04-19 13:04:44 -07001377 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001378 def test_nonvoting_job(self):
1379 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001380
James E. Blair4ec821f2012-08-23 15:28:28 -07001381 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1382 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001383 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001384 self.executor_server.failJob('nonvoting-project-test2', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001385 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001386
1387 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001388
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001389 self.assertEqual(A.data['status'], 'MERGED')
1390 self.assertEqual(A.reported, 2)
1391 self.assertEqual(
1392 self.getJobFromHistory('nonvoting-project-merge').result,
1393 'SUCCESS')
1394 self.assertEqual(
1395 self.getJobFromHistory('nonvoting-project-test1').result,
1396 'SUCCESS')
1397 self.assertEqual(
1398 self.getJobFromHistory('nonvoting-project-test2').result,
1399 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001400
James E. Blair2ef29e92017-07-21 15:25:05 -07001401 self.assertTrue(self.getJobFromHistory('nonvoting-project-merge').
1402 parameters['zuul']['voting'])
1403 self.assertTrue(self.getJobFromHistory('nonvoting-project-test1').
1404 parameters['zuul']['voting'])
1405 self.assertFalse(self.getJobFromHistory('nonvoting-project-test2').
1406 parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -07001407
James E. Blaire0487072012-08-29 17:38:31 -07001408 def test_check_queue_success(self):
1409 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001410
James E. Blaire0487072012-08-29 17:38:31 -07001411 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1412 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1413
1414 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001415
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001416 self.assertEqual(A.data['status'], 'NEW')
1417 self.assertEqual(A.reported, 1)
1418 self.assertEqual(self.getJobFromHistory('project-merge').result,
1419 'SUCCESS')
1420 self.assertEqual(self.getJobFromHistory('project-test1').result,
1421 'SUCCESS')
1422 self.assertEqual(self.getJobFromHistory('project-test2').result,
1423 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001424
1425 def test_check_queue_failure(self):
1426 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001427
James E. Blaire0487072012-08-29 17:38:31 -07001428 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001429 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001430 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1431
1432 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001433
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001434 self.assertEqual(A.data['status'], 'NEW')
1435 self.assertEqual(A.reported, 1)
1436 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001437 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001438 self.assertEqual(self.getJobFromHistory('project-test1').result,
1439 'SUCCESS')
1440 self.assertEqual(self.getJobFromHistory('project-test2').result,
1441 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001442
David Shrewsburyffab07a2017-07-24 12:45:07 -04001443 @simple_layout('layouts/autohold.yaml')
1444 def test_autohold(self):
1445 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1446
1447 client = zuul.rpcclient.RPCClient('127.0.0.1',
1448 self.gearman_server.port)
1449 self.addCleanup(client.shutdown)
David Shrewsbury36b2adf2017-07-31 15:40:13 -04001450 r = client.autohold('tenant-one', 'org/project', 'project-test2',
1451 "reason text", 1)
David Shrewsburyffab07a2017-07-24 12:45:07 -04001452 self.assertTrue(r)
1453
1454 self.executor_server.failJob('project-test2', A)
1455 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1456
1457 self.waitUntilSettled()
1458
1459 self.assertEqual(A.data['status'], 'NEW')
1460 self.assertEqual(A.reported, 1)
1461 self.assertEqual(self.getJobFromHistory('project-test2').result,
1462 'FAILURE')
1463
1464 # Check nodepool for a held node
1465 held_node = None
1466 for node in self.fake_nodepool.getNodes():
1467 if node['state'] == zuul.model.STATE_HOLD:
1468 held_node = node
1469 break
1470 self.assertIsNotNone(held_node)
1471
1472 # Validate node has recorded the failed job
1473 self.assertEqual(
1474 held_node['hold_job'],
1475 " ".join(['tenant-one',
1476 'review.example.com/org/project',
1477 'project-test2'])
1478 )
David Shrewsburyf9af9df2017-08-01 15:19:26 -04001479 self.assertEqual(held_node['comment'], "reason text")
David Shrewsburyffab07a2017-07-24 12:45:07 -04001480
1481 # Another failed change should not hold any more nodes
1482 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1483 self.executor_server.failJob('project-test2', B)
1484 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1485 self.waitUntilSettled()
1486 self.assertEqual(B.data['status'], 'NEW')
1487 self.assertEqual(B.reported, 1)
1488 self.assertEqual(self.getJobFromHistory('project-test2').result,
1489 'FAILURE')
1490
1491 held_nodes = 0
1492 for node in self.fake_nodepool.getNodes():
1493 if node['state'] == zuul.model.STATE_HOLD:
1494 held_nodes += 1
1495 self.assertEqual(held_nodes, 1)
1496
James E. Blair7cb84542017-04-19 13:35:05 -07001497 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001498 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001499 # This particular test does a large amount of merges and needs a little
1500 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001501 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001502 "test that dependent changes behind dequeued changes work"
1503 # This complicated test is a reproduction of a real life bug
1504 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001505
Paul Belanger174a8272017-03-14 13:20:10 -04001506 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001507 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1508 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1509 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1510 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1511 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1512 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1513 D.setDependsOn(C, 1)
1514 E.setDependsOn(D, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001515 A.addApproval('Code-Review', 2)
1516 B.addApproval('Code-Review', 2)
1517 C.addApproval('Code-Review', 2)
1518 D.addApproval('Code-Review', 2)
1519 E.addApproval('Code-Review', 2)
1520 F.addApproval('Code-Review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001521
1522 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001523
1524 # Change object re-use in the gerrit trigger is hidden if
1525 # changes are added in quick succession; waiting makes it more
1526 # like real life.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001527 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001528 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001529 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001530 self.waitUntilSettled()
1531
Paul Belanger174a8272017-03-14 13:20:10 -04001532 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001533 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001534 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001535 self.waitUntilSettled()
1536
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001537 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001538 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001539 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001540 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001541 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001542 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001543 self.fake_gerrit.addEvent(F.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001544 self.waitUntilSettled()
1545
Paul Belanger174a8272017-03-14 13:20:10 -04001546 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001547 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001548 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001549 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001550 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001551 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001552 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001553 self.waitUntilSettled()
1554
1555 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001556
1557 # Grab pointers to the jobs we want to release before
1558 # releasing any, because list indexes may change as
1559 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001560 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001561 a.release()
1562 b.release()
1563 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001564 self.waitUntilSettled()
1565
Paul Belanger174a8272017-03-14 13:20:10 -04001566 self.executor_server.hold_jobs_in_build = False
1567 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001568 self.waitUntilSettled()
1569
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001570 self.assertEqual(A.data['status'], 'NEW')
1571 self.assertEqual(B.data['status'], 'MERGED')
1572 self.assertEqual(C.data['status'], 'MERGED')
1573 self.assertEqual(D.data['status'], 'MERGED')
1574 self.assertEqual(E.data['status'], 'MERGED')
1575 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001576
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001577 self.assertEqual(A.reported, 2)
1578 self.assertEqual(B.reported, 2)
1579 self.assertEqual(C.reported, 2)
1580 self.assertEqual(D.reported, 2)
1581 self.assertEqual(E.reported, 2)
1582 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001583
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001584 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1585 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001586
1587 def test_merger_repack(self):
1588 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001589
James E. Blair05fed602012-09-07 12:45:24 -07001590 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001591 A.addApproval('Code-Review', 2)
1592 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001593 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001594 self.assertEqual(self.getJobFromHistory('project-merge').result,
1595 'SUCCESS')
1596 self.assertEqual(self.getJobFromHistory('project-test1').result,
1597 'SUCCESS')
1598 self.assertEqual(self.getJobFromHistory('project-test2').result,
1599 'SUCCESS')
1600 self.assertEqual(A.data['status'], 'MERGED')
1601 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001602 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001603 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001604
James E. Blair2a535672017-04-27 12:03:15 -07001605 path = os.path.join(self.merger_src_root, "review.example.com",
1606 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001607 if os.path.exists(path):
1608 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001609 path = os.path.join(self.executor_src_root, "review.example.com",
1610 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001611 if os.path.exists(path):
1612 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001613
1614 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001615 A.addApproval('Code-Review', 2)
1616 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001617 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001618 self.assertEqual(self.getJobFromHistory('project-merge').result,
1619 'SUCCESS')
1620 self.assertEqual(self.getJobFromHistory('project-test1').result,
1621 'SUCCESS')
1622 self.assertEqual(self.getJobFromHistory('project-test2').result,
1623 'SUCCESS')
1624 self.assertEqual(A.data['status'], 'MERGED')
1625 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001626
James E. Blair4886f282012-11-15 09:27:33 -08001627 def test_merger_repack_large_change(self):
1628 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001629 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001630 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001631 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001632 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001633 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001634 self.executor_server.merger._addProject('review.example.com',
James E. Blair197e8202017-06-09 12:54:28 -07001635 'org/project', url, None)
James E. Blair6df67132017-04-19 13:53:20 -07001636 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001637 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001638 # TODOv3(jeblair): add hostname to upstream root
1639 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001640 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001641 path = os.path.join(self.merger_src_root, 'review.example.com',
1642 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001643 if os.path.exists(path):
1644 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001645 path = os.path.join(self.executor_src_root, 'review.example.com',
1646 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001647 if os.path.exists(path):
1648 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001649
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001650 A.addApproval('Code-Review', 2)
1651 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001652 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001653 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001654 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001655 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001656 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001657 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001658 'SUCCESS')
1659 self.assertEqual(A.data['status'], 'MERGED')
1660 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001661
James E. Blair2fa50962013-01-30 21:50:41 -08001662 def test_new_patchset_dequeues_old(self):
1663 "Test that a new patchset causes the old to be dequeued"
1664 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001665 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001666 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1667 M.setMerged()
1668
1669 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1670 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1671 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1672 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001673 A.addApproval('Code-Review', 2)
1674 B.addApproval('Code-Review', 2)
1675 C.addApproval('Code-Review', 2)
1676 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001677
1678 C.setDependsOn(B, 1)
1679 B.setDependsOn(A, 1)
1680 A.setDependsOn(M, 1)
1681
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001682 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1683 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1684 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1685 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001686 self.waitUntilSettled()
1687
1688 B.addPatchset()
1689 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1690 self.waitUntilSettled()
1691
Paul Belanger174a8272017-03-14 13:20:10 -04001692 self.executor_server.hold_jobs_in_build = False
1693 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001694 self.waitUntilSettled()
1695
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001696 self.assertEqual(A.data['status'], 'MERGED')
1697 self.assertEqual(A.reported, 2)
1698 self.assertEqual(B.data['status'], 'NEW')
1699 self.assertEqual(B.reported, 2)
1700 self.assertEqual(C.data['status'], 'NEW')
1701 self.assertEqual(C.reported, 2)
1702 self.assertEqual(D.data['status'], 'MERGED')
1703 self.assertEqual(D.reported, 2)
1704 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001705
James E. Blairba437362015-02-07 11:41:52 -08001706 def test_new_patchset_check(self):
1707 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001708
Paul Belanger174a8272017-03-14 13:20:10 -04001709 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001710
1711 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001712 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001713 tenant = self.sched.abide.tenants.get('tenant-one')
1714 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001715
1716 # Add two git-dependent changes
1717 B.setDependsOn(A, 1)
1718 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1719 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001720 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1721 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001722
1723 # A live item, and a non-live/live pair
1724 items = check_pipeline.getAllItems()
1725 self.assertEqual(len(items), 3)
1726
1727 self.assertEqual(items[0].change.number, '1')
1728 self.assertEqual(items[0].change.patchset, '1')
1729 self.assertFalse(items[0].live)
1730
1731 self.assertEqual(items[1].change.number, '2')
1732 self.assertEqual(items[1].change.patchset, '1')
1733 self.assertTrue(items[1].live)
1734
1735 self.assertEqual(items[2].change.number, '1')
1736 self.assertEqual(items[2].change.patchset, '1')
1737 self.assertTrue(items[2].live)
1738
1739 # Add a new patchset to A
1740 A.addPatchset()
1741 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1742 self.waitUntilSettled()
1743
1744 # The live copy of A,1 should be gone, but the non-live and B
1745 # should continue, and we should have a new A,2
1746 items = check_pipeline.getAllItems()
1747 self.assertEqual(len(items), 3)
1748
1749 self.assertEqual(items[0].change.number, '1')
1750 self.assertEqual(items[0].change.patchset, '1')
1751 self.assertFalse(items[0].live)
1752
1753 self.assertEqual(items[1].change.number, '2')
1754 self.assertEqual(items[1].change.patchset, '1')
1755 self.assertTrue(items[1].live)
1756
1757 self.assertEqual(items[2].change.number, '1')
1758 self.assertEqual(items[2].change.patchset, '2')
1759 self.assertTrue(items[2].live)
1760
1761 # Add a new patchset to B
1762 B.addPatchset()
1763 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1764 self.waitUntilSettled()
1765
1766 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1767 # but we should have a new B,2 (still based on A,1)
1768 items = check_pipeline.getAllItems()
1769 self.assertEqual(len(items), 3)
1770
1771 self.assertEqual(items[0].change.number, '1')
1772 self.assertEqual(items[0].change.patchset, '2')
1773 self.assertTrue(items[0].live)
1774
1775 self.assertEqual(items[1].change.number, '1')
1776 self.assertEqual(items[1].change.patchset, '1')
1777 self.assertFalse(items[1].live)
1778
1779 self.assertEqual(items[2].change.number, '2')
1780 self.assertEqual(items[2].change.patchset, '2')
1781 self.assertTrue(items[2].live)
1782
1783 self.builds[0].release()
1784 self.waitUntilSettled()
1785 self.builds[0].release()
1786 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001787 self.executor_server.hold_jobs_in_build = False
1788 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001789 self.waitUntilSettled()
1790
1791 self.assertEqual(A.reported, 1)
1792 self.assertEqual(B.reported, 1)
1793 self.assertEqual(self.history[0].result, 'ABORTED')
1794 self.assertEqual(self.history[0].changes, '1,1')
1795 self.assertEqual(self.history[1].result, 'ABORTED')
1796 self.assertEqual(self.history[1].changes, '1,1 2,1')
1797 self.assertEqual(self.history[2].result, 'SUCCESS')
1798 self.assertEqual(self.history[2].changes, '1,2')
1799 self.assertEqual(self.history[3].result, 'SUCCESS')
1800 self.assertEqual(self.history[3].changes, '1,1 2,2')
1801
1802 def test_abandoned_gate(self):
1803 "Test that an abandoned change is dequeued from gate"
1804
Paul Belanger174a8272017-03-14 13:20:10 -04001805 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001806
1807 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001808 A.addApproval('Code-Review', 2)
1809 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001810 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001811 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1812 self.assertEqual(self.builds[0].name, 'project-merge')
1813
1814 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1815 self.waitUntilSettled()
1816
Paul Belanger174a8272017-03-14 13:20:10 -04001817 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001818 self.waitUntilSettled()
1819
Arie5b4048c2016-10-05 16:12:06 +03001820 self.assertBuilds([])
1821 self.assertHistory([
1822 dict(name='project-merge', result='ABORTED', changes='1,1')],
1823 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001824 self.assertEqual(A.reported, 1,
1825 "Abandoned gate change should report only start")
1826
1827 def test_abandoned_check(self):
1828 "Test that an abandoned change is dequeued from check"
1829
Paul Belanger174a8272017-03-14 13:20:10 -04001830 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001831
1832 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1833 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001834 tenant = self.sched.abide.tenants.get('tenant-one')
1835 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001836
1837 # Add two git-dependent changes
1838 B.setDependsOn(A, 1)
1839 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1840 self.waitUntilSettled()
1841 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1842 self.waitUntilSettled()
1843 # A live item, and a non-live/live pair
1844 items = check_pipeline.getAllItems()
1845 self.assertEqual(len(items), 3)
1846
1847 self.assertEqual(items[0].change.number, '1')
1848 self.assertFalse(items[0].live)
1849
1850 self.assertEqual(items[1].change.number, '2')
1851 self.assertTrue(items[1].live)
1852
1853 self.assertEqual(items[2].change.number, '1')
1854 self.assertTrue(items[2].live)
1855
1856 # Abandon A
1857 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1858 self.waitUntilSettled()
1859
1860 # The live copy of A should be gone, but the non-live and B
1861 # should continue
1862 items = check_pipeline.getAllItems()
1863 self.assertEqual(len(items), 2)
1864
1865 self.assertEqual(items[0].change.number, '1')
1866 self.assertFalse(items[0].live)
1867
1868 self.assertEqual(items[1].change.number, '2')
1869 self.assertTrue(items[1].live)
1870
Paul Belanger174a8272017-03-14 13:20:10 -04001871 self.executor_server.hold_jobs_in_build = False
1872 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001873 self.waitUntilSettled()
1874
1875 self.assertEqual(len(self.history), 4)
1876 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001877 'Build should have been aborted')
1878 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001879 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001880
Steve Varnau7b78b312015-04-03 14:49:46 -07001881 def test_abandoned_not_timer(self):
1882 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001883 # This test can not use simple_layout because it must start
1884 # with a configuration which does not include a
1885 # timer-triggered job so that we have an opportunity to set
1886 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001887 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001888 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001889 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001890 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001891 # The pipeline triggers every second, so we should have seen
1892 # several by now.
1893 time.sleep(5)
1894 self.waitUntilSettled()
1895 # Stop queuing timer triggered jobs so that the assertions
1896 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001897 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001898 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001899 self.sched.reconfigure(self.config)
James E. Blair0b137b42017-07-27 08:51:25 -07001900 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07001901 # If APScheduler is in mid-event when we remove the job, we
1902 # can end up with one more event firing, so give it an extra
1903 # second to settle.
1904 time.sleep(1)
1905 self.waitUntilSettled()
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001906
James E. Blair21037782017-07-19 11:56:55 -07001907 self.assertEqual(len(self.builds), 1, "One timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001908
1909 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1910 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1911 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07001912 self.assertEqual(len(self.builds), 2, "One change plus one timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001913
1914 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1915 self.waitUntilSettled()
1916
James E. Blair21037782017-07-19 11:56:55 -07001917 self.assertEqual(len(self.builds), 1, "One timer job remains")
Steve Varnau7b78b312015-04-03 14:49:46 -07001918
Paul Belanger174a8272017-03-14 13:20:10 -04001919 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001920 self.waitUntilSettled()
1921
James E. Blair2fa50962013-01-30 21:50:41 -08001922 def test_new_patchset_dequeues_old_on_head(self):
1923 "Test that a new patchset causes the old to be dequeued (at head)"
1924 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001925 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001926 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1927 M.setMerged()
1928 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1929 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1930 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1931 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001932 A.addApproval('Code-Review', 2)
1933 B.addApproval('Code-Review', 2)
1934 C.addApproval('Code-Review', 2)
1935 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001936
1937 C.setDependsOn(B, 1)
1938 B.setDependsOn(A, 1)
1939 A.setDependsOn(M, 1)
1940
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001941 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1942 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1943 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1944 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001945 self.waitUntilSettled()
1946
1947 A.addPatchset()
1948 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1949 self.waitUntilSettled()
1950
Paul Belanger174a8272017-03-14 13:20:10 -04001951 self.executor_server.hold_jobs_in_build = False
1952 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001953 self.waitUntilSettled()
1954
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001955 self.assertEqual(A.data['status'], 'NEW')
1956 self.assertEqual(A.reported, 2)
1957 self.assertEqual(B.data['status'], 'NEW')
1958 self.assertEqual(B.reported, 2)
1959 self.assertEqual(C.data['status'], 'NEW')
1960 self.assertEqual(C.reported, 2)
1961 self.assertEqual(D.data['status'], 'MERGED')
1962 self.assertEqual(D.reported, 2)
1963 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001964
1965 def test_new_patchset_dequeues_old_without_dependents(self):
1966 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001967 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001968 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1969 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1970 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001971 A.addApproval('Code-Review', 2)
1972 B.addApproval('Code-Review', 2)
1973 C.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001974
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001975 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1976 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1977 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001978 self.waitUntilSettled()
1979
1980 B.addPatchset()
1981 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1982 self.waitUntilSettled()
1983
Paul Belanger174a8272017-03-14 13:20:10 -04001984 self.executor_server.hold_jobs_in_build = False
1985 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001986 self.waitUntilSettled()
1987
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001988 self.assertEqual(A.data['status'], 'MERGED')
1989 self.assertEqual(A.reported, 2)
1990 self.assertEqual(B.data['status'], 'NEW')
1991 self.assertEqual(B.reported, 2)
1992 self.assertEqual(C.data['status'], 'MERGED')
1993 self.assertEqual(C.reported, 2)
1994 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001995
1996 def test_new_patchset_dequeues_old_independent_queue(self):
1997 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001998 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001999 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2000 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2001 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2002 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2003 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2004 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2005 self.waitUntilSettled()
2006
2007 B.addPatchset()
2008 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2009 self.waitUntilSettled()
2010
Paul Belanger174a8272017-03-14 13:20:10 -04002011 self.executor_server.hold_jobs_in_build = False
2012 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002013 self.waitUntilSettled()
2014
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002015 self.assertEqual(A.data['status'], 'NEW')
2016 self.assertEqual(A.reported, 1)
2017 self.assertEqual(B.data['status'], 'NEW')
2018 self.assertEqual(B.reported, 1)
2019 self.assertEqual(C.data['status'], 'NEW')
2020 self.assertEqual(C.reported, 1)
2021 self.assertEqual(len(self.history), 10)
2022 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002023
James E. Blaircc020532017-04-19 13:04:44 -07002024 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07002025 def test_noop_job(self):
2026 "Test that the internal noop job works"
2027 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002028 A.addApproval('Code-Review', 2)
2029 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07002030 self.waitUntilSettled()
2031
2032 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2033 self.assertTrue(self.sched._areAllBuildsComplete())
2034 self.assertEqual(len(self.history), 0)
2035 self.assertEqual(A.data['status'], 'MERGED')
2036 self.assertEqual(A.reported, 2)
2037
James E. Blair06cc3922017-04-19 10:08:10 -07002038 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002039 def test_no_job_project(self):
2040 "Test that reports with no jobs don't get sent"
2041 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
2042 'master', 'A')
2043 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2044 self.waitUntilSettled()
2045
2046 # Change wasn't reported to
2047 self.assertEqual(A.reported, False)
2048
2049 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08002050 tenant = self.sched.abide.tenants.get('tenant-one')
2051 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002052 items = check_pipeline.getAllItems()
2053 self.assertEqual(len(items), 0)
2054
2055 self.assertEqual(len(self.history), 0)
2056
James E. Blair7d0dedc2013-02-21 17:26:09 -08002057 def test_zuul_refs(self):
2058 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04002059 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002060 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2061 M1.setMerged()
2062 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2063 M2.setMerged()
2064
2065 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2066 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2067 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2068 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002069 A.addApproval('Code-Review', 2)
2070 B.addApproval('Code-Review', 2)
2071 C.addApproval('Code-Review', 2)
2072 D.addApproval('Code-Review', 2)
2073 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2074 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2075 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2076 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002077
2078 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002079 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002080 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002081 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002082 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002083 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002084 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002085 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002086 self.waitUntilSettled()
2087
Clint Byrum3343e3e2016-11-15 16:05:03 -08002088 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002089 for x in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07002090 if x.parameters['zuul']['change'] == '3':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002091 a_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002092 elif x.parameters['zuul']['change'] == '4':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002093 b_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002094 elif x.parameters['zuul']['change'] == '5':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002095 c_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002096 elif x.parameters['zuul']['change'] == '6':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002097 d_build = x
2098 if a_build and b_build and c_build and d_build:
2099 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002100
Clint Byrum3343e3e2016-11-15 16:05:03 -08002101 # should have a, not b, and should not be in project2
2102 self.assertTrue(a_build.hasChanges(A))
2103 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002104
Clint Byrum3343e3e2016-11-15 16:05:03 -08002105 # should have a and b, and should not be in project2
2106 self.assertTrue(b_build.hasChanges(A, B))
2107 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002108
Clint Byrum3343e3e2016-11-15 16:05:03 -08002109 # should have a and b in 1, c in 2
2110 self.assertTrue(c_build.hasChanges(A, B, C))
2111 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002112
Clint Byrum3343e3e2016-11-15 16:05:03 -08002113 # should have a and b in 1, c and d in 2
2114 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002115
Paul Belanger174a8272017-03-14 13:20:10 -04002116 self.executor_server.hold_jobs_in_build = False
2117 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002118 self.waitUntilSettled()
2119
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002120 self.assertEqual(A.data['status'], 'MERGED')
2121 self.assertEqual(A.reported, 2)
2122 self.assertEqual(B.data['status'], 'MERGED')
2123 self.assertEqual(B.reported, 2)
2124 self.assertEqual(C.data['status'], 'MERGED')
2125 self.assertEqual(C.reported, 2)
2126 self.assertEqual(D.data['status'], 'MERGED')
2127 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002128
James E. Blair4a28a882013-08-23 15:17:33 -07002129 def test_rerun_on_error(self):
2130 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002131 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002132 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002133 A.addApproval('Code-Review', 2)
2134 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002135 self.waitUntilSettled()
2136
James E. Blair412fba82017-01-26 15:00:50 -08002137 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002138 self.executor_server.hold_jobs_in_build = False
2139 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002140 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002141 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002142 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2143
James E. Blair412e5582013-04-22 15:50:12 -07002144 def test_statsd(self):
2145 "Test each of the statsd methods used in the scheduler"
2146 import extras
2147 statsd = extras.try_import('statsd.statsd')
2148 statsd.incr('test-incr')
2149 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002150 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002151 self.assertReportedStat('test-incr', '1|c')
2152 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002153 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002154
James E. Blairdad52252014-02-07 16:59:17 -08002155 def test_stuck_job_cleanup(self):
2156 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002157
2158 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002159 self.gearman_server.hold_jobs_in_queue = True
2160 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002161 A.addApproval('Code-Review', 2)
2162 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002163 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002164 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002165 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2166
James E. Blair31cb3e82017-04-20 13:08:56 -07002167 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002168 self.sched.reconfigure(self.config)
2169 self.waitUntilSettled()
2170
James E. Blair18c64442014-03-18 10:14:45 -07002171 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002172 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002173 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002174 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2175 self.assertTrue(self.sched._areAllBuildsComplete())
2176
2177 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002178 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002179 self.assertEqual(self.history[0].result, 'SUCCESS')
2180
James E. Blair879dafb2015-07-17 14:04:49 -07002181 def test_file_head(self):
2182 # This is a regression test for an observed bug. A change
2183 # with a file named "HEAD" in the root directory of the repo
2184 # was processed by a merger. It then was unable to reset the
2185 # repo because of:
2186 # GitCommandError: 'git reset --hard HEAD' returned
2187 # with exit code 128
2188 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2189 # and filename
2190 # Use '--' to separate filenames from revisions'
2191
2192 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002193 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002194 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2195
2196 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2197 self.waitUntilSettled()
2198
2199 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2200 self.waitUntilSettled()
2201
2202 self.assertIn('Build succeeded', A.messages[0])
2203 self.assertIn('Build succeeded', B.messages[0])
2204
James E. Blair70c71582013-03-06 08:50:50 -08002205 def test_file_jobs(self):
2206 "Test that file jobs run only when appropriate"
2207 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002208 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002209 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002210 A.addApproval('Code-Review', 2)
2211 B.addApproval('Code-Review', 2)
2212 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2213 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002214 self.waitUntilSettled()
2215
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002216 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002217 if x.name == 'project-testfile']
2218
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002219 self.assertEqual(len(testfile_jobs), 1)
2220 self.assertEqual(testfile_jobs[0].changes, '1,2')
2221 self.assertEqual(A.data['status'], 'MERGED')
2222 self.assertEqual(A.reported, 2)
2223 self.assertEqual(B.data['status'], 'MERGED')
2224 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002225
Clint Byrum3ee39f32016-11-17 23:45:07 -08002226 def _test_irrelevant_files_jobs(self, should_skip):
2227 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002228 if should_skip:
2229 files = {'ignoreme': 'ignored\n'}
2230 else:
2231 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002232
2233 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002234 'master',
2235 'test irrelevant-files',
2236 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002237 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2238 self.waitUntilSettled()
2239
2240 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002241 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002242
2243 if should_skip:
2244 self.assertEqual([], tested_change_ids)
2245 else:
2246 self.assertIn(change.data['number'], tested_change_ids)
2247
James E. Blaircc324192017-04-19 14:36:08 -07002248 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002249 def test_irrelevant_files_match_skips_job(self):
2250 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002251
James E. Blaircc324192017-04-19 14:36:08 -07002252 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002253 def test_irrelevant_files_no_match_runs_job(self):
2254 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002255
James E. Blaircc324192017-04-19 14:36:08 -07002256 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002257 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002258 files = {'ignoreme': 'ignored\n'}
2259
2260 change = self.fake_gerrit.addFakeChange('org/project',
2261 'master',
2262 'test irrelevant-files',
2263 files=files)
2264 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2265 self.waitUntilSettled()
2266
2267 run_jobs = set([build.name for build in self.history])
2268
2269 self.assertEqual(set(['project-test-nomatch-starts-empty',
2270 'project-test-nomatch-starts-full']), run_jobs)
2271
James E. Blairc8a1e052014-02-25 09:29:26 -08002272 def test_queue_names(self):
2273 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002274 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002275 (trusted, project1) = tenant.getProject('org/project1')
2276 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002277 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2278 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2279 self.assertEqual(q1.name, 'integrated')
2280 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002281
James E. Blair64ed6f22013-07-10 14:07:23 -07002282 def test_queue_precedence(self):
2283 "Test that queue precedence works"
2284
2285 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002286 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002287 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2288 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002289 A.addApproval('Code-Review', 2)
2290 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002291
2292 self.waitUntilSettled()
2293 self.gearman_server.hold_jobs_in_queue = False
2294 self.gearman_server.release()
2295 self.waitUntilSettled()
2296
James E. Blair8de58bd2013-07-18 16:23:33 -07002297 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002298 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002299 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002300 self.waitUntilSettled()
2301
James E. Blair64ed6f22013-07-10 14:07:23 -07002302 self.log.debug(self.history)
2303 self.assertEqual(self.history[0].pipeline, 'gate')
2304 self.assertEqual(self.history[1].pipeline, 'check')
2305 self.assertEqual(self.history[2].pipeline, 'gate')
2306 self.assertEqual(self.history[3].pipeline, 'gate')
2307 self.assertEqual(self.history[4].pipeline, 'check')
2308 self.assertEqual(self.history[5].pipeline, 'check')
2309
Clark Boylana5edbe42014-06-03 16:39:10 -07002310 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002311 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002312 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002313 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002314 A.addApproval('Code-Review', 2)
2315 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002316 self.waitUntilSettled()
2317
Paul Belanger174a8272017-03-14 13:20:10 -04002318 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002319 self.waitUntilSettled()
2320
James E. Blair1843a552013-07-03 14:19:52 -07002321 port = self.webapp.server.socket.getsockname()[1]
2322
Adam Gandelman77a12c72017-02-03 17:43:43 -08002323 req = urllib.request.Request(
2324 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002325 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002326 headers = f.info()
2327 self.assertIn('Content-Length', headers)
2328 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002329 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2330 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002331 self.assertIn('Access-Control-Allow-Origin', headers)
2332 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002333 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002334 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002335 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002336
Paul Belanger174a8272017-03-14 13:20:10 -04002337 self.executor_server.hold_jobs_in_build = False
2338 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002339 self.waitUntilSettled()
2340
2341 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002342 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002343 for p in data['pipelines']:
2344 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002345 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002346 self.assertEqual(q['window'], 20)
2347 else:
2348 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002349 for head in q['heads']:
2350 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002351 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002352 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002353 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002354 status_jobs.append(job)
2355 self.assertEqual('project-merge', status_jobs[0]['name'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002356 # TODO(mordred) pull uuids from self.builds
Monty Taylor51139a02016-05-24 11:28:10 -05002357 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002358 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2359 uuid=status_jobs[0]['uuid']),
2360 status_jobs[0]['url'])
2361 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002362 'finger://{hostname}/{uuid}'.format(
2363 hostname=self.executor_server.hostname,
2364 uuid=status_jobs[0]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002365 status_jobs[0]['finger_url'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002366 # TOOD(mordred) configure a success-url on the base job
Monty Taylor51139a02016-05-24 11:28:10 -05002367 self.assertEqual(
2368 'finger://{hostname}/{uuid}'.format(
2369 hostname=self.executor_server.hostname,
2370 uuid=status_jobs[0]['uuid']),
2371 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002372 self.assertEqual('project-test1', status_jobs[1]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002373 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002374 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2375 uuid=status_jobs[1]['uuid']),
2376 status_jobs[1]['url'])
2377 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002378 'finger://{hostname}/{uuid}'.format(
2379 hostname=self.executor_server.hostname,
2380 uuid=status_jobs[1]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002381 status_jobs[1]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002382 self.assertEqual(
2383 'finger://{hostname}/{uuid}'.format(
2384 hostname=self.executor_server.hostname,
2385 uuid=status_jobs[1]['uuid']),
2386 status_jobs[1]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002387
2388 self.assertEqual('project-test2', status_jobs[2]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002389 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002390 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2391 uuid=status_jobs[2]['uuid']),
2392 status_jobs[2]['url'])
2393 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002394 'finger://{hostname}/{uuid}'.format(
2395 hostname=self.executor_server.hostname,
2396 uuid=status_jobs[2]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002397 status_jobs[2]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002398 self.assertEqual(
2399 'finger://{hostname}/{uuid}'.format(
2400 hostname=self.executor_server.hostname,
2401 uuid=status_jobs[2]['uuid']),
2402 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002403
Tobias Henkel65639f82017-07-10 10:25:42 +02002404 # check job dependencies
2405 self.assertIsNotNone(status_jobs[0]['dependencies'])
2406 self.assertIsNotNone(status_jobs[1]['dependencies'])
2407 self.assertIsNotNone(status_jobs[2]['dependencies'])
2408 self.assertEqual(len(status_jobs[0]['dependencies']), 0)
2409 self.assertEqual(len(status_jobs[1]['dependencies']), 1)
2410 self.assertEqual(len(status_jobs[2]['dependencies']), 1)
2411 self.assertIn('project-merge', status_jobs[1]['dependencies'])
2412 self.assertIn('project-merge', status_jobs[2]['dependencies'])
2413
James E. Blaircdccd972013-07-01 12:10:22 -07002414 def test_live_reconfiguration(self):
2415 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002416 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002417 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002418 A.addApproval('Code-Review', 2)
2419 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002420 self.waitUntilSettled()
2421
2422 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002423 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002424
Paul Belanger174a8272017-03-14 13:20:10 -04002425 self.executor_server.hold_jobs_in_build = False
2426 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002427 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002428 self.assertEqual(self.getJobFromHistory('project-merge').result,
2429 'SUCCESS')
2430 self.assertEqual(self.getJobFromHistory('project-test1').result,
2431 'SUCCESS')
2432 self.assertEqual(self.getJobFromHistory('project-test2').result,
2433 'SUCCESS')
2434 self.assertEqual(A.data['status'], 'MERGED')
2435 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002436
James E. Blair6bc782d2015-07-17 16:20:21 -07002437 def test_live_reconfiguration_merge_conflict(self):
2438 # A real-world bug: a change in a gate queue has a merge
2439 # conflict and a job is added to its project while it's
2440 # sitting in the queue. The job gets added to the change and
2441 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002442 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002443
2444 # This change is fine. It's here to stop the queue long
2445 # enough for the next change to be subject to the
2446 # reconfiguration, as well as to provide a conflict for the
2447 # next change. This change will succeed and merge.
2448 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002449 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002450 A.addApproval('Code-Review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002451
2452 # This change will be in merge conflict. During the
2453 # reconfiguration, we will add a job. We want to make sure
2454 # that doesn't cause it to get stuck.
2455 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002456 B.addPatchset({'conflict': 'B'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002457 B.addApproval('Code-Review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002458
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002459 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2460 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002461
2462 self.waitUntilSettled()
2463
2464 # No jobs have run yet
2465 self.assertEqual(A.data['status'], 'NEW')
2466 self.assertEqual(A.reported, 1)
2467 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002468 self.assertEqual(len(self.history), 0)
2469
2470 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002471 self.commitConfigUpdate('common-config',
2472 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002473 self.sched.reconfigure(self.config)
2474 self.waitUntilSettled()
2475
Paul Belanger174a8272017-03-14 13:20:10 -04002476 self.executor_server.hold_jobs_in_build = False
2477 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002478 self.waitUntilSettled()
2479
2480 self.assertEqual(A.data['status'], 'MERGED')
2481 self.assertEqual(A.reported, 2)
2482 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002483 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002484 self.assertEqual(self.getJobFromHistory('project-merge').result,
2485 'SUCCESS')
2486 self.assertEqual(self.getJobFromHistory('project-test1').result,
2487 'SUCCESS')
2488 self.assertEqual(self.getJobFromHistory('project-test2').result,
2489 'SUCCESS')
2490 self.assertEqual(self.getJobFromHistory('project-test3').result,
2491 'SUCCESS')
2492 self.assertEqual(len(self.history), 4)
2493
James E. Blair400e8fd2015-07-30 17:44:45 -07002494 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002495 # An extrapolation of test_live_reconfiguration_merge_conflict
2496 # that tests a job added to a job tree with a failed root does
2497 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002498 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002499
2500 # This change is fine. It's here to stop the queue long
2501 # enough for the next change to be subject to the
2502 # reconfiguration. This change will succeed and merge.
2503 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002504 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002505 A.addApproval('Code-Review', 2)
2506 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002507 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002508 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002509 self.waitUntilSettled()
2510
2511 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002512 self.executor_server.failJob('project-merge', B)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002513 B.addApproval('Code-Review', 2)
2514 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002515 self.waitUntilSettled()
2516
Paul Belanger174a8272017-03-14 13:20:10 -04002517 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002518 self.waitUntilSettled()
2519
2520 # Both -merge jobs have run, but no others.
2521 self.assertEqual(A.data['status'], 'NEW')
2522 self.assertEqual(A.reported, 1)
2523 self.assertEqual(B.data['status'], 'NEW')
2524 self.assertEqual(B.reported, 1)
2525 self.assertEqual(self.history[0].result, 'SUCCESS')
2526 self.assertEqual(self.history[0].name, 'project-merge')
2527 self.assertEqual(self.history[1].result, 'FAILURE')
2528 self.assertEqual(self.history[1].name, 'project-merge')
2529 self.assertEqual(len(self.history), 2)
2530
2531 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002532 self.commitConfigUpdate('common-config',
2533 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002534 self.sched.reconfigure(self.config)
2535 self.waitUntilSettled()
2536
Paul Belanger174a8272017-03-14 13:20:10 -04002537 self.executor_server.hold_jobs_in_build = False
2538 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002539 self.waitUntilSettled()
2540
2541 self.assertEqual(A.data['status'], 'MERGED')
2542 self.assertEqual(A.reported, 2)
2543 self.assertEqual(B.data['status'], 'NEW')
2544 self.assertEqual(B.reported, 2)
2545 self.assertEqual(self.history[0].result, 'SUCCESS')
2546 self.assertEqual(self.history[0].name, 'project-merge')
2547 self.assertEqual(self.history[1].result, 'FAILURE')
2548 self.assertEqual(self.history[1].name, 'project-merge')
2549 self.assertEqual(self.history[2].result, 'SUCCESS')
2550 self.assertEqual(self.history[3].result, 'SUCCESS')
2551 self.assertEqual(self.history[4].result, 'SUCCESS')
2552 self.assertEqual(len(self.history), 5)
2553
James E. Blair400e8fd2015-07-30 17:44:45 -07002554 def test_live_reconfiguration_failed_job(self):
2555 # Test that a change with a removed failing job does not
2556 # disrupt reconfiguration. If a change has a failed job and
2557 # that job is removed during a reconfiguration, we observed a
2558 # bug where the code to re-set build statuses would run on
2559 # that build and raise an exception because the job no longer
2560 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002561 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002562
2563 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2564
2565 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002566 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002567
2568 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2569 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002570 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002571 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002572 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002573 self.waitUntilSettled()
2574
2575 self.assertEqual(A.data['status'], 'NEW')
2576 self.assertEqual(A.reported, 0)
2577
2578 self.assertEqual(self.getJobFromHistory('project-merge').result,
2579 'SUCCESS')
2580 self.assertEqual(self.getJobFromHistory('project-test1').result,
2581 'FAILURE')
2582 self.assertEqual(len(self.history), 2)
2583
2584 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002585 self.commitConfigUpdate('common-config',
2586 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002587 self.sched.reconfigure(self.config)
2588 self.waitUntilSettled()
2589
Paul Belanger174a8272017-03-14 13:20:10 -04002590 self.executor_server.hold_jobs_in_build = False
2591 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002592 self.waitUntilSettled()
2593
2594 self.assertEqual(self.getJobFromHistory('project-test2').result,
2595 'SUCCESS')
2596 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2597 'SUCCESS')
2598 self.assertEqual(len(self.history), 4)
2599
2600 self.assertEqual(A.data['status'], 'NEW')
2601 self.assertEqual(A.reported, 1)
2602 self.assertIn('Build succeeded', A.messages[0])
2603 # Ensure the removed job was not included in the report.
2604 self.assertNotIn('project-test1', A.messages[0])
2605
James E. Blairfe707d12015-08-05 15:18:15 -07002606 def test_live_reconfiguration_shared_queue(self):
2607 # Test that a change with a failing job which was removed from
2608 # this project but otherwise still exists in the system does
2609 # not disrupt reconfiguration.
2610
Paul Belanger174a8272017-03-14 13:20:10 -04002611 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002612
2613 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2614
Paul Belanger174a8272017-03-14 13:20:10 -04002615 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002616
2617 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2618 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002619 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002620 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002621 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002622 self.waitUntilSettled()
2623
2624 self.assertEqual(A.data['status'], 'NEW')
2625 self.assertEqual(A.reported, 0)
2626
Clint Byrum69663d42017-05-01 14:51:04 -07002627 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002628 'SUCCESS')
2629 self.assertEqual(self.getJobFromHistory(
2630 'project1-project2-integration').result, 'FAILURE')
2631 self.assertEqual(len(self.history), 2)
2632
2633 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002634 self.commitConfigUpdate(
2635 'common-config',
2636 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002637 self.sched.reconfigure(self.config)
2638 self.waitUntilSettled()
2639
Paul Belanger174a8272017-03-14 13:20:10 -04002640 self.executor_server.hold_jobs_in_build = False
2641 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002642 self.waitUntilSettled()
2643
Clint Byrum69663d42017-05-01 14:51:04 -07002644 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002645 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002646 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002647 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002648 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002649 'SUCCESS')
2650 self.assertEqual(self.getJobFromHistory(
2651 'project1-project2-integration').result, 'FAILURE')
2652 self.assertEqual(len(self.history), 4)
2653
2654 self.assertEqual(A.data['status'], 'NEW')
2655 self.assertEqual(A.reported, 1)
2656 self.assertIn('Build succeeded', A.messages[0])
2657 # Ensure the removed job was not included in the report.
2658 self.assertNotIn('project1-project2-integration', A.messages[0])
2659
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002660 def test_double_live_reconfiguration_shared_queue(self):
2661 # This was a real-world regression. A change is added to
2662 # gate; a reconfigure happens, a second change which depends
2663 # on the first is added, and a second reconfiguration happens.
2664 # Ensure that both changes merge.
2665
2666 # A failure may indicate incorrect caching or cleaning up of
2667 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002668 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002669
2670 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2671 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2672 B.setDependsOn(A, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002673 A.addApproval('Code-Review', 2)
2674 B.addApproval('Code-Review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002675
2676 # Add the parent change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002677 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002678 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002679 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002680 self.waitUntilSettled()
2681
2682 # Reconfigure (with only one change in the pipeline).
2683 self.sched.reconfigure(self.config)
2684 self.waitUntilSettled()
2685
2686 # Add the child change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002687 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002688 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002689 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002690 self.waitUntilSettled()
2691
2692 # Reconfigure (with both in the pipeline).
2693 self.sched.reconfigure(self.config)
2694 self.waitUntilSettled()
2695
Paul Belanger174a8272017-03-14 13:20:10 -04002696 self.executor_server.hold_jobs_in_build = False
2697 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002698 self.waitUntilSettled()
2699
2700 self.assertEqual(len(self.history), 8)
2701
2702 self.assertEqual(A.data['status'], 'MERGED')
2703 self.assertEqual(A.reported, 2)
2704 self.assertEqual(B.data['status'], 'MERGED')
2705 self.assertEqual(B.reported, 2)
2706
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002707 def test_live_reconfiguration_del_project(self):
2708 # Test project deletion from layout
2709 # while changes are enqueued
2710
Paul Belanger174a8272017-03-14 13:20:10 -04002711 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002712 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2713 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2714 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2715
2716 # A Depends-On: B
2717 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2718 A.subject, B.data['id'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002719 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002720
2721 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2722 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2723 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002724 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002725 self.waitUntilSettled()
2726 self.assertEqual(len(self.builds), 5)
2727
2728 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002729 self.commitConfigUpdate(
2730 'common-config',
2731 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002732 self.sched.reconfigure(self.config)
2733 self.waitUntilSettled()
2734
2735 # Builds for C aborted, builds for A succeed,
2736 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002737 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002738 self.assertEqual(job_c.changes, '3,1')
2739 self.assertEqual(job_c.result, 'ABORTED')
2740
Paul Belanger174a8272017-03-14 13:20:10 -04002741 self.executor_server.hold_jobs_in_build = False
2742 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002743 self.waitUntilSettled()
2744
Clint Byrum0149ada2017-04-04 12:22:01 -07002745 self.assertEqual(
2746 self.getJobFromHistory('project-test1', 'org/project').changes,
2747 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002748
2749 self.assertEqual(A.data['status'], 'NEW')
2750 self.assertEqual(B.data['status'], 'NEW')
2751 self.assertEqual(C.data['status'], 'NEW')
2752 self.assertEqual(A.reported, 1)
2753 self.assertEqual(B.reported, 0)
2754 self.assertEqual(C.reported, 0)
2755
Clint Byrum0149ada2017-04-04 12:22:01 -07002756 tenant = self.sched.abide.tenants.get('tenant-one')
2757 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002758 self.assertIn('Build succeeded', A.messages[0])
2759
James E. Blairec056492016-07-22 09:45:56 -07002760 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002761 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002762 self.updateConfigLayout(
2763 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002764 self.sched.reconfigure(self.config)
2765
2766 self.init_repo("org/new-project")
2767 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2768
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002769 A.addApproval('Code-Review', 2)
2770 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002771 self.waitUntilSettled()
2772 self.assertEqual(self.getJobFromHistory('project-merge').result,
2773 'SUCCESS')
2774 self.assertEqual(self.getJobFromHistory('project-test1').result,
2775 'SUCCESS')
2776 self.assertEqual(self.getJobFromHistory('project-test2').result,
2777 'SUCCESS')
2778 self.assertEqual(A.data['status'], 'MERGED')
2779 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002780
James E. Blair9ea70072017-04-19 16:05:30 -07002781 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002782 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002783 self.init_repo("org/delete-project")
2784 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2785
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002786 A.addApproval('Code-Review', 2)
2787 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002788 self.waitUntilSettled()
2789 self.assertEqual(self.getJobFromHistory('project-merge').result,
2790 'SUCCESS')
2791 self.assertEqual(self.getJobFromHistory('project-test1').result,
2792 'SUCCESS')
2793 self.assertEqual(self.getJobFromHistory('project-test2').result,
2794 'SUCCESS')
2795 self.assertEqual(A.data['status'], 'MERGED')
2796 self.assertEqual(A.reported, 2)
2797
2798 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002799 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002800 if os.path.exists(os.path.join(self.merger_src_root, p)):
2801 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002802 if os.path.exists(os.path.join(self.executor_src_root, p)):
2803 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002804
2805 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2806
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002807 B.addApproval('Code-Review', 2)
2808 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002809 self.waitUntilSettled()
2810 self.assertEqual(self.getJobFromHistory('project-merge').result,
2811 'SUCCESS')
2812 self.assertEqual(self.getJobFromHistory('project-test1').result,
2813 'SUCCESS')
2814 self.assertEqual(self.getJobFromHistory('project-test2').result,
2815 'SUCCESS')
2816 self.assertEqual(B.data['status'], 'MERGED')
2817 self.assertEqual(B.reported, 2)
2818
James E. Blaire68d43f2017-04-19 14:03:30 -07002819 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002820 def test_tags(self):
2821 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002822 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2823 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2824 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2825 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2826 self.waitUntilSettled()
2827
James E. Blaire68d43f2017-04-19 14:03:30 -07002828 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002829
2830 results = {self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002831 project='org/project1').uuid: ['extratag', 'merge'],
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002832 self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002833 project='org/project2').uuid: ['merge']}
James E. Blair456f2fb2016-02-09 09:29:33 -08002834
2835 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002836 self.assertEqual(results.get(build.uuid, ''),
James E. Blair9d46f092017-07-20 16:06:20 -07002837 build.parameters['zuul'].get('jobtags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002838
James E. Blair63bb0ef2013-07-29 17:14:51 -07002839 def test_timer(self):
2840 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002841 # This test can not use simple_layout because it must start
2842 # with a configuration which does not include a
2843 # timer-triggered job so that we have an opportunity to set
2844 # the hold flag before the first job.
James E. Blair21037782017-07-19 11:56:55 -07002845 self.create_branch('org/project', 'stable')
Paul Belanger174a8272017-03-14 13:20:10 -04002846 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002847 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002848 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002849
Clark Boylan3ee090a2014-04-03 20:55:09 -07002850 # The pipeline triggers every second, so we should have seen
2851 # several by now.
2852 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002853 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002854
2855 self.assertEqual(len(self.builds), 2)
2856
James E. Blair63bb0ef2013-07-29 17:14:51 -07002857 port = self.webapp.server.socket.getsockname()[1]
2858
Clint Byrum5e729e92017-02-08 06:34:57 -08002859 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002860 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002861 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002862 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002863
Paul Belanger174a8272017-03-14 13:20:10 -04002864 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002865 # Stop queuing timer triggered jobs so that the assertions
2866 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002867 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002868 self.sched.reconfigure(self.config)
James E. Blair78ae4782017-08-02 14:19:10 -07002869 self.waitUntilSettled()
2870 # If APScheduler is in mid-event when we remove the job, we
2871 # can end up with one more event firing, so give it an extra
2872 # second to settle.
2873 time.sleep(1)
2874 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002875 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002876 self.waitUntilSettled()
2877
James E. Blair21037782017-07-19 11:56:55 -07002878 self.assertHistory([
2879 dict(name='project-bitrot', result='SUCCESS',
2880 ref='refs/heads/master'),
2881 dict(name='project-bitrot', result='SUCCESS',
2882 ref='refs/heads/stable'),
2883 ], ordered=False)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002884
2885 data = json.loads(data)
2886 status_jobs = set()
2887 for p in data['pipelines']:
2888 for q in p['change_queues']:
2889 for head in q['heads']:
2890 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002891 for job in change['jobs']:
2892 status_jobs.add(job['name'])
James E. Blair21037782017-07-19 11:56:55 -07002893 self.assertIn('project-bitrot', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002894
James E. Blair4f6033c2014-03-27 15:49:09 -07002895 def test_idle(self):
2896 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002897 # This test can not use simple_layout because it must start
2898 # with a configuration which does not include a
2899 # timer-triggered job so that we have an opportunity to set
2900 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002901 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002902
Clark Boylan3ee090a2014-04-03 20:55:09 -07002903 for x in range(1, 3):
2904 # Test that timer triggers periodic jobs even across
2905 # layout config reloads.
2906 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002907 self.commitConfigUpdate('common-config',
2908 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002909 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002910 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002911
Clark Boylan3ee090a2014-04-03 20:55:09 -07002912 # The pipeline triggers every second, so we should have seen
2913 # several by now.
2914 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002915
2916 # Stop queuing timer triggered jobs so that the assertions
2917 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002918 self.commitConfigUpdate('common-config',
2919 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002920 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002921 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07002922 # If APScheduler is in mid-event when we remove the job,
2923 # we can end up with one more event firing, so give it an
2924 # extra second to settle.
2925 time.sleep(1)
2926 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07002927 self.assertEqual(len(self.builds), 1,
Clint Byrum58264dc2017-02-07 21:21:22 -08002928 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002929 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002930 self.waitUntilSettled()
2931 self.assertEqual(len(self.builds), 0)
James E. Blair21037782017-07-19 11:56:55 -07002932 self.assertEqual(len(self.history), x)
James E. Blair4f6033c2014-03-27 15:49:09 -07002933
James E. Blaircc324192017-04-19 14:36:08 -07002934 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002935 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002936 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2937 self.waitUntilSettled()
2938
2939 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2940 self.waitUntilSettled()
2941
James E. Blairff80a2f2013-12-27 13:24:06 -08002942 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002943
2944 # A.messages only holds what FakeGerrit places in it. Thus we
2945 # work on the knowledge of what the first message should be as
2946 # it is only configured to go to SMTP.
2947
2948 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002949 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002950 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002951 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002952 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002953 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002954
2955 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002956 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002957 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002958 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002959 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002960 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002961
James E. Blaire5910202013-12-27 09:50:31 -08002962 def test_timer_smtp(self):
2963 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002964 # This test can not use simple_layout because it must start
2965 # with a configuration which does not include a
2966 # timer-triggered job so that we have an opportunity to set
2967 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002968 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002969 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002970 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002971
Clark Boylan3ee090a2014-04-03 20:55:09 -07002972 # The pipeline triggers every second, so we should have seen
2973 # several by now.
2974 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002975 self.waitUntilSettled()
2976
Clark Boylan3ee090a2014-04-03 20:55:09 -07002977 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04002978 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002979 self.waitUntilSettled()
2980 self.assertEqual(len(self.history), 2)
2981
James E. Blaire5910202013-12-27 09:50:31 -08002982 self.assertEqual(self.getJobFromHistory(
2983 'project-bitrot-stable-old').result, 'SUCCESS')
2984 self.assertEqual(self.getJobFromHistory(
2985 'project-bitrot-stable-older').result, 'SUCCESS')
2986
James E. Blairff80a2f2013-12-27 13:24:06 -08002987 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002988
2989 # A.messages only holds what FakeGerrit places in it. Thus we
2990 # work on the knowledge of what the first message should be as
2991 # it is only configured to go to SMTP.
2992
2993 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002994 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002995 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002996 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002997 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002998 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002999
Clark Boylan3ee090a2014-04-03 20:55:09 -07003000 # Stop queuing timer triggered jobs and let any that may have
3001 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003002 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003003 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07003004 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003005 # If APScheduler is in mid-event when we remove the job, we
3006 # can end up with one more event firing, so give it an extra
3007 # second to settle.
3008 time.sleep(1)
3009 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003010 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003011 self.waitUntilSettled()
3012
Joshua Hesketh25695cb2017-03-06 12:50:04 +11003013 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003014 def test_timer_sshkey(self):
3015 "Test that a periodic job can setup SSH key authentication"
3016 self.worker.hold_jobs_in_build = True
3017 self.config.set('zuul', 'layout_config',
3018 'tests/fixtures/layout-timer.yaml')
3019 self.sched.reconfigure(self.config)
3020 self.registerJobs()
3021
3022 # The pipeline triggers every second, so we should have seen
3023 # several by now.
3024 time.sleep(5)
3025 self.waitUntilSettled()
3026
3027 self.assertEqual(len(self.builds), 2)
3028
3029 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3030 self.assertTrue(os.path.isfile(ssh_wrapper))
3031 with open(ssh_wrapper) as f:
3032 ssh_wrapper_content = f.read()
3033 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3034 # In the unit tests Merger runs in the same process,
3035 # so we see its' environment variables
3036 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3037
3038 self.worker.release('.*')
3039 self.waitUntilSettled()
3040 self.assertEqual(len(self.history), 2)
3041
3042 self.assertEqual(self.getJobFromHistory(
3043 'project-bitrot-stable-old').result, 'SUCCESS')
3044 self.assertEqual(self.getJobFromHistory(
3045 'project-bitrot-stable-older').result, 'SUCCESS')
3046
3047 # Stop queuing timer triggered jobs and let any that may have
3048 # queued through so that end of test assertions pass.
3049 self.config.set('zuul', 'layout_config',
3050 'tests/fixtures/layout-no-timer.yaml')
3051 self.sched.reconfigure(self.config)
3052 self.registerJobs()
3053 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003054 # If APScheduler is in mid-event when we remove the job, we
3055 # can end up with one more event firing, so give it an extra
3056 # second to settle.
3057 time.sleep(1)
3058 self.waitUntilSettled()
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003059 self.worker.release('.*')
3060 self.waitUntilSettled()
3061
James E. Blair91e34592015-07-31 16:45:59 -07003062 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003063 "Test that the RPC client can enqueue a change"
3064 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003065 A.addApproval('Code-Review', 2)
3066 A.addApproval('Approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003067
3068 client = zuul.rpcclient.RPCClient('127.0.0.1',
3069 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003070 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003071 r = client.enqueue(tenant='tenant-one',
3072 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003073 project='org/project',
3074 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003075 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003076 self.waitUntilSettled()
3077 self.assertEqual(self.getJobFromHistory('project-merge').result,
3078 'SUCCESS')
3079 self.assertEqual(self.getJobFromHistory('project-test1').result,
3080 'SUCCESS')
3081 self.assertEqual(self.getJobFromHistory('project-test2').result,
3082 'SUCCESS')
3083 self.assertEqual(A.data['status'], 'MERGED')
3084 self.assertEqual(A.reported, 2)
3085 self.assertEqual(r, True)
3086
James E. Blair91e34592015-07-31 16:45:59 -07003087 def test_client_enqueue_ref(self):
3088 "Test that the RPC client can enqueue a ref"
James E. Blair289f5932017-07-27 15:02:29 -07003089 p = "review.example.com/org/project"
3090 upstream = self.getUpstreamRepos([p])
3091 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3092 A.setMerged()
3093 A_commit = str(upstream[p].commit('master'))
3094 self.log.debug("A commit: %s" % A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003095
3096 client = zuul.rpcclient.RPCClient('127.0.0.1',
3097 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003098 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07003099 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003100 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003101 pipeline='post',
3102 project='org/project',
3103 trigger='gerrit',
3104 ref='master',
3105 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
James E. Blair289f5932017-07-27 15:02:29 -07003106 newrev=A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003107 self.waitUntilSettled()
3108 job_names = [x.name for x in self.history]
3109 self.assertEqual(len(self.history), 1)
3110 self.assertIn('project-post', job_names)
3111 self.assertEqual(r, True)
3112
James E. Blairad28e912013-11-27 10:43:22 -08003113 def test_client_enqueue_negative(self):
3114 "Test that the RPC client returns errors"
3115 client = zuul.rpcclient.RPCClient('127.0.0.1',
3116 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003117 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003118 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003119 "Invalid tenant"):
3120 r = client.enqueue(tenant='tenant-foo',
3121 pipeline='gate',
3122 project='org/project',
3123 trigger='gerrit',
3124 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003125 self.assertEqual(r, False)
3126
3127 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003128 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003129 r = client.enqueue(tenant='tenant-one',
3130 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003131 project='project-does-not-exist',
3132 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003133 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003134 self.assertEqual(r, False)
3135
3136 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3137 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003138 r = client.enqueue(tenant='tenant-one',
3139 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003140 project='org/project',
3141 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003142 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003143 self.assertEqual(r, False)
3144
3145 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3146 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003147 r = client.enqueue(tenant='tenant-one',
3148 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003149 project='org/project',
3150 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003151 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003152 self.assertEqual(r, False)
3153
3154 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3155 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003156 r = client.enqueue(tenant='tenant-one',
3157 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003158 project='org/project',
3159 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003160 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003161 self.assertEqual(r, False)
3162
3163 self.waitUntilSettled()
3164 self.assertEqual(len(self.history), 0)
3165 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003166
3167 def test_client_promote(self):
3168 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003169 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003170 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3171 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3172 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003173 A.addApproval('Code-Review', 2)
3174 B.addApproval('Code-Review', 2)
3175 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003176
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003177 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3178 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3179 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003180
3181 self.waitUntilSettled()
3182
Paul Belangerbaca3132016-11-04 12:49:54 -04003183 tenant = self.sched.abide.tenants.get('tenant-one')
3184 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003185 enqueue_times = {}
3186 for item in items:
3187 enqueue_times[str(item.change)] = item.enqueue_time
3188
James E. Blair36658cf2013-12-06 17:53:48 -08003189 client = zuul.rpcclient.RPCClient('127.0.0.1',
3190 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003191 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003192 r = client.promote(tenant='tenant-one',
3193 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003194 change_ids=['2,1', '3,1'])
3195
Sean Daguef39b9ca2014-01-10 21:34:35 -05003196 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003197 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003198 for item in items:
3199 self.assertEqual(
3200 enqueue_times[str(item.change)], item.enqueue_time)
3201
James E. Blair78acec92014-02-06 07:11:32 -08003202 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003203 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003204 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003205 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003206 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003207 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003208 self.waitUntilSettled()
3209
3210 self.assertEqual(len(self.builds), 6)
3211 self.assertEqual(self.builds[0].name, 'project-test1')
3212 self.assertEqual(self.builds[1].name, 'project-test2')
3213 self.assertEqual(self.builds[2].name, 'project-test1')
3214 self.assertEqual(self.builds[3].name, 'project-test2')
3215 self.assertEqual(self.builds[4].name, 'project-test1')
3216 self.assertEqual(self.builds[5].name, 'project-test2')
3217
Paul Belangerbaca3132016-11-04 12:49:54 -04003218 self.assertTrue(self.builds[0].hasChanges(B))
3219 self.assertFalse(self.builds[0].hasChanges(A))
3220 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003221
Paul Belangerbaca3132016-11-04 12:49:54 -04003222 self.assertTrue(self.builds[2].hasChanges(B))
3223 self.assertTrue(self.builds[2].hasChanges(C))
3224 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003225
Paul Belangerbaca3132016-11-04 12:49:54 -04003226 self.assertTrue(self.builds[4].hasChanges(B))
3227 self.assertTrue(self.builds[4].hasChanges(C))
3228 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003229
Paul Belanger174a8272017-03-14 13:20:10 -04003230 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003231 self.waitUntilSettled()
3232
3233 self.assertEqual(A.data['status'], 'MERGED')
3234 self.assertEqual(A.reported, 2)
3235 self.assertEqual(B.data['status'], 'MERGED')
3236 self.assertEqual(B.reported, 2)
3237 self.assertEqual(C.data['status'], 'MERGED')
3238 self.assertEqual(C.reported, 2)
3239
James E. Blair36658cf2013-12-06 17:53:48 -08003240 self.assertEqual(r, True)
3241
3242 def test_client_promote_dependent(self):
3243 "Test that the RPC client can promote a dependent change"
3244 # C (depends on B) -> B -> A ; then promote C to get:
3245 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003246 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003247 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3248 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3249 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3250
3251 C.setDependsOn(B, 1)
3252
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003253 A.addApproval('Code-Review', 2)
3254 B.addApproval('Code-Review', 2)
3255 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003256
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003257 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3258 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3259 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003260
3261 self.waitUntilSettled()
3262
3263 client = zuul.rpcclient.RPCClient('127.0.0.1',
3264 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003265 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003266 r = client.promote(tenant='tenant-one',
3267 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003268 change_ids=['3,1'])
3269
James E. Blair78acec92014-02-06 07:11:32 -08003270 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003271 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -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()
3277
3278 self.assertEqual(len(self.builds), 6)
3279 self.assertEqual(self.builds[0].name, 'project-test1')
3280 self.assertEqual(self.builds[1].name, 'project-test2')
3281 self.assertEqual(self.builds[2].name, 'project-test1')
3282 self.assertEqual(self.builds[3].name, 'project-test2')
3283 self.assertEqual(self.builds[4].name, 'project-test1')
3284 self.assertEqual(self.builds[5].name, 'project-test2')
3285
Paul Belangerbaca3132016-11-04 12:49:54 -04003286 self.assertTrue(self.builds[0].hasChanges(B))
3287 self.assertFalse(self.builds[0].hasChanges(A))
3288 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003289
Paul Belangerbaca3132016-11-04 12:49:54 -04003290 self.assertTrue(self.builds[2].hasChanges(B))
3291 self.assertTrue(self.builds[2].hasChanges(C))
3292 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003293
Paul Belangerbaca3132016-11-04 12:49:54 -04003294 self.assertTrue(self.builds[4].hasChanges(B))
3295 self.assertTrue(self.builds[4].hasChanges(C))
3296 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003297
Paul Belanger174a8272017-03-14 13:20:10 -04003298 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003299 self.waitUntilSettled()
3300
3301 self.assertEqual(A.data['status'], 'MERGED')
3302 self.assertEqual(A.reported, 2)
3303 self.assertEqual(B.data['status'], 'MERGED')
3304 self.assertEqual(B.reported, 2)
3305 self.assertEqual(C.data['status'], 'MERGED')
3306 self.assertEqual(C.reported, 2)
3307
James E. Blair36658cf2013-12-06 17:53:48 -08003308 self.assertEqual(r, True)
3309
3310 def test_client_promote_negative(self):
3311 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003312 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003313 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003314 A.addApproval('Code-Review', 2)
3315 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003316 self.waitUntilSettled()
3317
3318 client = zuul.rpcclient.RPCClient('127.0.0.1',
3319 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003320 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003321
3322 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003323 r = client.promote(tenant='tenant-one',
3324 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003325 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003326 self.assertEqual(r, False)
3327
3328 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003329 r = client.promote(tenant='tenant-one',
3330 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003331 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003332 self.assertEqual(r, False)
3333
Paul Belanger174a8272017-03-14 13:20:10 -04003334 self.executor_server.hold_jobs_in_build = False
3335 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003336 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003337
James E. Blaircc324192017-04-19 14:36:08 -07003338 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003339 def test_queue_rate_limiting(self):
3340 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003341 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003342 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3343 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3344 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3345
3346 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003347 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003348
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003349 A.addApproval('Code-Review', 2)
3350 B.addApproval('Code-Review', 2)
3351 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003352
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003353 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3354 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3355 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003356 self.waitUntilSettled()
3357
3358 # Only A and B will have their merge jobs queued because
3359 # window is 2.
3360 self.assertEqual(len(self.builds), 2)
3361 self.assertEqual(self.builds[0].name, 'project-merge')
3362 self.assertEqual(self.builds[1].name, 'project-merge')
3363
James E. Blair732ee862017-04-21 07:49:26 -07003364 # Release the merge jobs one at a time.
3365 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003366 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003367 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003368 self.waitUntilSettled()
3369
3370 # Only A and B will have their test jobs queued because
3371 # window is 2.
3372 self.assertEqual(len(self.builds), 4)
3373 self.assertEqual(self.builds[0].name, 'project-test1')
3374 self.assertEqual(self.builds[1].name, 'project-test2')
3375 self.assertEqual(self.builds[2].name, 'project-test1')
3376 self.assertEqual(self.builds[3].name, 'project-test2')
3377
Paul Belanger174a8272017-03-14 13:20:10 -04003378 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003379 self.waitUntilSettled()
3380
James E. Blaircc324192017-04-19 14:36:08 -07003381 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003382 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003383 # A failed so window is reduced by 1 to 1.
3384 self.assertEqual(queue.window, 1)
3385 self.assertEqual(queue.window_floor, 1)
3386 self.assertEqual(A.data['status'], 'NEW')
3387
3388 # Gate is reset and only B's merge job is queued because
3389 # window shrunk to 1.
3390 self.assertEqual(len(self.builds), 1)
3391 self.assertEqual(self.builds[0].name, 'project-merge')
3392
Paul Belanger174a8272017-03-14 13:20:10 -04003393 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003394 self.waitUntilSettled()
3395
3396 # Only B's test jobs are queued because window is still 1.
3397 self.assertEqual(len(self.builds), 2)
3398 self.assertEqual(self.builds[0].name, 'project-test1')
3399 self.assertEqual(self.builds[1].name, 'project-test2')
3400
Paul Belanger174a8272017-03-14 13:20:10 -04003401 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003402 self.waitUntilSettled()
3403
3404 # B was successfully merged so window is increased to 2.
3405 self.assertEqual(queue.window, 2)
3406 self.assertEqual(queue.window_floor, 1)
3407 self.assertEqual(B.data['status'], 'MERGED')
3408
3409 # Only C is left and its merge job is queued.
3410 self.assertEqual(len(self.builds), 1)
3411 self.assertEqual(self.builds[0].name, 'project-merge')
3412
Paul Belanger174a8272017-03-14 13:20:10 -04003413 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003414 self.waitUntilSettled()
3415
3416 # After successful merge job the test jobs for C are queued.
3417 self.assertEqual(len(self.builds), 2)
3418 self.assertEqual(self.builds[0].name, 'project-test1')
3419 self.assertEqual(self.builds[1].name, 'project-test2')
3420
Paul Belanger174a8272017-03-14 13:20:10 -04003421 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003422 self.waitUntilSettled()
3423
3424 # C successfully merged so window is bumped to 3.
3425 self.assertEqual(queue.window, 3)
3426 self.assertEqual(queue.window_floor, 1)
3427 self.assertEqual(C.data['status'], 'MERGED')
3428
James E. Blaircc324192017-04-19 14:36:08 -07003429 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003430 def test_queue_rate_limiting_dependent(self):
3431 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003432 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003433 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3434 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3435 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3436
3437 B.setDependsOn(A, 1)
3438
Paul Belanger174a8272017-03-14 13:20:10 -04003439 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003440
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003441 A.addApproval('Code-Review', 2)
3442 B.addApproval('Code-Review', 2)
3443 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003444
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003445 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3446 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3447 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003448 self.waitUntilSettled()
3449
3450 # Only A and B will have their merge jobs queued because
3451 # window is 2.
3452 self.assertEqual(len(self.builds), 2)
3453 self.assertEqual(self.builds[0].name, 'project-merge')
3454 self.assertEqual(self.builds[1].name, 'project-merge')
3455
James E. Blairf4a5f022017-04-18 14:01:10 -07003456 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003457
3458 # Only A and B will have their test jobs queued because
3459 # window is 2.
3460 self.assertEqual(len(self.builds), 4)
3461 self.assertEqual(self.builds[0].name, 'project-test1')
3462 self.assertEqual(self.builds[1].name, 'project-test2')
3463 self.assertEqual(self.builds[2].name, 'project-test1')
3464 self.assertEqual(self.builds[3].name, 'project-test2')
3465
Paul Belanger174a8272017-03-14 13:20:10 -04003466 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003467 self.waitUntilSettled()
3468
James E. Blaircc324192017-04-19 14:36:08 -07003469 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003470 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003471 # A failed so window is reduced by 1 to 1.
3472 self.assertEqual(queue.window, 1)
3473 self.assertEqual(queue.window_floor, 1)
3474 self.assertEqual(A.data['status'], 'NEW')
3475 self.assertEqual(B.data['status'], 'NEW')
3476
3477 # Gate is reset and only C's merge job is queued because
3478 # window shrunk to 1 and A and B were dequeued.
3479 self.assertEqual(len(self.builds), 1)
3480 self.assertEqual(self.builds[0].name, 'project-merge')
3481
James E. Blairf4a5f022017-04-18 14:01:10 -07003482 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003483
3484 # Only C's test jobs are queued because window is still 1.
3485 self.assertEqual(len(self.builds), 2)
James E. Blairdf25ddc2017-07-08 07:57:09 -07003486 builds = self.getSortedBuilds()
3487 self.assertEqual(builds[0].name, 'project-test1')
3488 self.assertEqual(builds[1].name, 'project-test2')
Clark Boylan7603a372014-01-21 11:43:20 -08003489
Paul Belanger174a8272017-03-14 13:20:10 -04003490 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003491 self.waitUntilSettled()
3492
3493 # C was successfully merged so window is increased to 2.
3494 self.assertEqual(queue.window, 2)
3495 self.assertEqual(queue.window_floor, 1)
3496 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003497
3498 def test_worker_update_metadata(self):
3499 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003500 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003501
3502 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003503 A.addApproval('Code-Review', 2)
3504 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003505 self.waitUntilSettled()
3506
Clint Byrumd4de79a2017-05-27 11:39:13 -07003507 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003508
3509 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07003510 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003511
3512 start = time.time()
3513 while True:
3514 if time.time() - start > 10:
3515 raise Exception("Timeout waiting for gearman server to report "
3516 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07003517 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003518 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08003519 break
3520 else:
3521 time.sleep(0)
3522
3523 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05003524 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003525
Paul Belanger174a8272017-03-14 13:20:10 -04003526 self.executor_server.hold_jobs_in_build = False
3527 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003528 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003529
James E. Blaircc324192017-04-19 14:36:08 -07003530 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003531 def test_footer_message(self):
3532 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003533 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003534 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003535 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003536 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003537 self.waitUntilSettled()
3538
3539 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003540 B.addApproval('Code-Review', 2)
3541 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003542 self.waitUntilSettled()
3543
3544 self.assertEqual(2, len(self.smtp_messages))
3545
Adam Gandelman82258fd2017-02-07 16:51:58 -08003546 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003547Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003548http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003549
Adam Gandelman82258fd2017-02-07 16:51:58 -08003550 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003551For CI problems and help debugging, contact ci@example.org"""
3552
Adam Gandelman82258fd2017-02-07 16:51:58 -08003553 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3554 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3555 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3556 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003557
Tobias Henkel9842bd72017-05-16 13:40:03 +02003558 @simple_layout('layouts/unmanaged-project.yaml')
3559 def test_unmanaged_project_start_message(self):
3560 "Test start reporting is not done for unmanaged projects."
3561 self.init_repo("org/project", tag='init')
3562 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3563 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3564 self.waitUntilSettled()
3565
3566 self.assertEqual(0, len(A.messages))
3567
Clint Byrumb9de8be2017-05-27 13:49:19 -07003568 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003569 def test_merge_failure_reporters(self):
3570 """Check that the config is set up correctly"""
3571
Clint Byrumb9de8be2017-05-27 13:49:19 -07003572 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11003573 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003574 "Merge Failed.\n\nThis change or one of its cross-repo "
3575 "dependencies was unable to be automatically merged with the "
3576 "current state of its repository. Please rebase the change and "
3577 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003578 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003579 self.assertEqual(
3580 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003581 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003582
3583 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003584 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11003585 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003586 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11003587
3588 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003589 tenant.layout.pipelines['check'].merge_failure_actions[0],
3590 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003591
3592 self.assertTrue(
3593 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003594 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003595 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003596 zuul.driver.smtp.smtpreporter.SMTPReporter) and
3597 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003598 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003599 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003600 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003601 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003602 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003603 gerritreporter.GerritReporter) and
3604 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003605 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003606 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003607 )
3608 )
3609
James E. Blairec056492016-07-22 09:45:56 -07003610 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003611 def test_merge_failure_reports(self):
3612 """Check that when a change fails to merge the correct message is sent
3613 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003614 self.updateConfigLayout(
3615 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003616 self.sched.reconfigure(self.config)
3617 self.registerJobs()
3618
3619 # Check a test failure isn't reported to SMTP
3620 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003621 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003622 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003623 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003624 self.waitUntilSettled()
3625
3626 self.assertEqual(3, len(self.history)) # 3 jobs
3627 self.assertEqual(0, len(self.smtp_messages))
3628
3629 # Check a merge failure is reported to SMTP
3630 # B should be merged, but C will conflict with B
3631 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3632 B.addPatchset(['conflict'])
3633 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3634 C.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003635 B.addApproval('Code-Review', 2)
3636 C.addApproval('Code-Review', 2)
3637 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3638 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003639 self.waitUntilSettled()
3640
3641 self.assertEqual(6, len(self.history)) # A and B jobs
3642 self.assertEqual(1, len(self.smtp_messages))
3643 self.assertEqual('The merge failed! For more information...',
3644 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003645
James E. Blairec056492016-07-22 09:45:56 -07003646 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003647 def test_default_merge_failure_reports(self):
3648 """Check that the default merge failure reports are correct."""
3649
3650 # A should report success, B should report merge failure.
3651 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3652 A.addPatchset(['conflict'])
3653 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3654 B.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003655 A.addApproval('Code-Review', 2)
3656 B.addApproval('Code-Review', 2)
3657 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3658 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003659 self.waitUntilSettled()
3660
3661 self.assertEqual(3, len(self.history)) # A jobs
3662 self.assertEqual(A.reported, 2)
3663 self.assertEqual(B.reported, 2)
3664 self.assertEqual(A.data['status'], 'MERGED')
3665 self.assertEqual(B.data['status'], 'NEW')
3666 self.assertIn('Build succeeded', A.messages[1])
3667 self.assertIn('Merge Failed', B.messages[1])
3668 self.assertIn('automatically merged', B.messages[1])
3669 self.assertNotIn('logs.example.com', B.messages[1])
3670 self.assertNotIn('SKIPPED', B.messages[1])
3671
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003672 def test_client_get_running_jobs(self):
3673 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003674 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003675 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003676 A.addApproval('Code-Review', 2)
3677 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003678 self.waitUntilSettled()
3679
3680 client = zuul.rpcclient.RPCClient('127.0.0.1',
3681 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003682 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003683
3684 # Wait for gearman server to send the initial workData back to zuul
3685 start = time.time()
3686 while True:
3687 if time.time() - start > 10:
3688 raise Exception("Timeout waiting for gearman server to report "
3689 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003690 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003691 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003692 break
3693 else:
3694 time.sleep(0)
3695
3696 running_items = client.get_running_jobs()
3697
3698 self.assertEqual(1, len(running_items))
3699 running_item = running_items[0]
3700 self.assertEqual([], running_item['failing_reasons'])
3701 self.assertEqual([], running_item['items_behind'])
3702 self.assertEqual('https://hostname/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003703 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003704 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003705 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003706 self.assertEqual(True, running_item['active'])
3707 self.assertEqual('1,1', running_item['id'])
3708
3709 self.assertEqual(3, len(running_item['jobs']))
3710 for job in running_item['jobs']:
3711 if job['name'] == 'project-merge':
3712 self.assertEqual('project-merge', job['name'])
3713 self.assertEqual('gate', job['pipeline'])
3714 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05003715 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003716 'static/stream.html?uuid={uuid}&logfile=console.log'
3717 .format(uuid=job['uuid']), job['url'])
3718 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05003719 'finger://{hostname}/{uuid}'.format(
3720 hostname=self.executor_server.hostname,
3721 uuid=job['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003722 job['finger_url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07003723 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003724 self.assertEqual(False, job['canceled'])
3725 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003726 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003727 self.assertEqual('gate', job['pipeline'])
3728 break
3729
Paul Belanger174a8272017-03-14 13:20:10 -04003730 self.executor_server.hold_jobs_in_build = False
3731 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003732 self.waitUntilSettled()
3733
3734 running_items = client.get_running_jobs()
3735 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003736
James E. Blairb70e55a2017-04-19 12:57:02 -07003737 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003738 def test_nonvoting_pipeline(self):
3739 "Test that a nonvoting pipeline (experimental) can still report"
3740
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003741 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3742 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003743 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3744 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003745
3746 self.assertEqual(self.getJobFromHistory('project-merge').result,
3747 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003748 self.assertEqual(
3749 self.getJobFromHistory('experimental-project-test').result,
3750 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003751 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003752
3753 def test_crd_gate(self):
3754 "Test cross-repo dependencies"
3755 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3756 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003757 A.addApproval('Code-Review', 2)
3758 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003759
3760 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3761 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3762 AM2.setMerged()
3763 AM1.setMerged()
3764
3765 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3766 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3767 BM2.setMerged()
3768 BM1.setMerged()
3769
3770 # A -> AM1 -> AM2
3771 # B -> BM1 -> BM2
3772 # A Depends-On: B
3773 # M2 is here to make sure it is never queried. If it is, it
3774 # means zuul is walking down the entire history of merged
3775 # changes.
3776
3777 B.setDependsOn(BM1, 1)
3778 BM1.setDependsOn(BM2, 1)
3779
3780 A.setDependsOn(AM1, 1)
3781 AM1.setDependsOn(AM2, 1)
3782
3783 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3784 A.subject, B.data['id'])
3785
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003786 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003787 self.waitUntilSettled()
3788
3789 self.assertEqual(A.data['status'], 'NEW')
3790 self.assertEqual(B.data['status'], 'NEW')
3791
Paul Belanger5dccbe72016-11-14 11:17:37 -05003792 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003793 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003794
Paul Belanger174a8272017-03-14 13:20:10 -04003795 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003796 B.addApproval('Approved', 1)
3797 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003798 self.waitUntilSettled()
3799
Paul Belanger174a8272017-03-14 13:20:10 -04003800 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003801 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003802 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003803 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003804 self.executor_server.hold_jobs_in_build = False
3805 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003806 self.waitUntilSettled()
3807
3808 self.assertEqual(AM2.queried, 0)
3809 self.assertEqual(BM2.queried, 0)
3810 self.assertEqual(A.data['status'], 'MERGED')
3811 self.assertEqual(B.data['status'], 'MERGED')
3812 self.assertEqual(A.reported, 2)
3813 self.assertEqual(B.reported, 2)
3814
Paul Belanger5dccbe72016-11-14 11:17:37 -05003815 changes = self.getJobFromHistory(
3816 'project-merge', 'org/project1').changes
3817 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003818
3819 def test_crd_branch(self):
3820 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003821
3822 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003823 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3824 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3825 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3826 C.data['id'] = B.data['id']
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003827 A.addApproval('Code-Review', 2)
3828 B.addApproval('Code-Review', 2)
3829 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003830
3831 # A Depends-On: B+C
3832 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3833 A.subject, B.data['id'])
3834
Paul Belanger174a8272017-03-14 13:20:10 -04003835 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003836 B.addApproval('Approved', 1)
3837 C.addApproval('Approved', 1)
3838 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003839 self.waitUntilSettled()
3840
Paul Belanger174a8272017-03-14 13:20:10 -04003841 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003842 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003843 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003844 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003845 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003846 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003847 self.executor_server.hold_jobs_in_build = False
3848 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003849 self.waitUntilSettled()
3850
3851 self.assertEqual(A.data['status'], 'MERGED')
3852 self.assertEqual(B.data['status'], 'MERGED')
3853 self.assertEqual(C.data['status'], 'MERGED')
3854 self.assertEqual(A.reported, 2)
3855 self.assertEqual(B.reported, 2)
3856 self.assertEqual(C.reported, 2)
3857
Paul Belanger6379db12016-11-14 13:57:54 -05003858 changes = self.getJobFromHistory(
3859 'project-merge', 'org/project1').changes
3860 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003861
3862 def test_crd_multiline(self):
3863 "Test multiple depends-on lines in commit"
3864 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3865 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3866 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003867 A.addApproval('Code-Review', 2)
3868 B.addApproval('Code-Review', 2)
3869 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003870
3871 # A Depends-On: B+C
3872 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3873 A.subject, B.data['id'], C.data['id'])
3874
Paul Belanger174a8272017-03-14 13:20:10 -04003875 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003876 B.addApproval('Approved', 1)
3877 C.addApproval('Approved', 1)
3878 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003879 self.waitUntilSettled()
3880
Paul Belanger174a8272017-03-14 13:20:10 -04003881 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003882 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003883 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003884 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003885 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003886 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003887 self.executor_server.hold_jobs_in_build = False
3888 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003889 self.waitUntilSettled()
3890
3891 self.assertEqual(A.data['status'], 'MERGED')
3892 self.assertEqual(B.data['status'], 'MERGED')
3893 self.assertEqual(C.data['status'], 'MERGED')
3894 self.assertEqual(A.reported, 2)
3895 self.assertEqual(B.reported, 2)
3896 self.assertEqual(C.reported, 2)
3897
Paul Belanger5dccbe72016-11-14 11:17:37 -05003898 changes = self.getJobFromHistory(
3899 'project-merge', 'org/project1').changes
3900 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003901
3902 def test_crd_unshared_gate(self):
3903 "Test cross-repo dependencies in unshared gate queues"
3904 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3905 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003906 A.addApproval('Code-Review', 2)
3907 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003908
3909 # A Depends-On: B
3910 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3911 A.subject, B.data['id'])
3912
3913 # A and B do not share a queue, make sure that A is unable to
3914 # enqueue B (and therefore, A is unable to be enqueued).
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003915 B.addApproval('Approved', 1)
3916 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003917 self.waitUntilSettled()
3918
3919 self.assertEqual(A.data['status'], 'NEW')
3920 self.assertEqual(B.data['status'], 'NEW')
3921 self.assertEqual(A.reported, 0)
3922 self.assertEqual(B.reported, 0)
3923 self.assertEqual(len(self.history), 0)
3924
3925 # Enqueue and merge B alone.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003926 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003927 self.waitUntilSettled()
3928
3929 self.assertEqual(B.data['status'], 'MERGED')
3930 self.assertEqual(B.reported, 2)
3931
3932 # Now that B is merged, A should be able to be enqueued and
3933 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003934 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003935 self.waitUntilSettled()
3936
3937 self.assertEqual(A.data['status'], 'MERGED')
3938 self.assertEqual(A.reported, 2)
3939
James E. Blair96698e22015-04-02 07:48:21 -07003940 def test_crd_gate_reverse(self):
3941 "Test reverse cross-repo dependencies"
3942 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3943 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003944 A.addApproval('Code-Review', 2)
3945 B.addApproval('Code-Review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003946
3947 # A Depends-On: B
3948
3949 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3950 A.subject, B.data['id'])
3951
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003952 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003953 self.waitUntilSettled()
3954
3955 self.assertEqual(A.data['status'], 'NEW')
3956 self.assertEqual(B.data['status'], 'NEW')
3957
Paul Belanger174a8272017-03-14 13:20:10 -04003958 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003959 A.addApproval('Approved', 1)
3960 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003961 self.waitUntilSettled()
3962
Paul Belanger174a8272017-03-14 13:20:10 -04003963 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003964 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003965 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003966 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003967 self.executor_server.hold_jobs_in_build = False
3968 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003969 self.waitUntilSettled()
3970
3971 self.assertEqual(A.data['status'], 'MERGED')
3972 self.assertEqual(B.data['status'], 'MERGED')
3973 self.assertEqual(A.reported, 2)
3974 self.assertEqual(B.reported, 2)
3975
Paul Belanger5dccbe72016-11-14 11:17:37 -05003976 changes = self.getJobFromHistory(
3977 'project-merge', 'org/project1').changes
3978 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003979
James E. Blair5ee24252014-12-30 10:12:29 -08003980 def test_crd_cycle(self):
3981 "Test cross-repo dependency cycles"
3982 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3983 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003984 A.addApproval('Code-Review', 2)
3985 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003986
3987 # A -> B -> A (via commit-depends)
3988
3989 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3990 A.subject, B.data['id'])
3991 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3992 B.subject, A.data['id'])
3993
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003994 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003995 self.waitUntilSettled()
3996
3997 self.assertEqual(A.reported, 0)
3998 self.assertEqual(B.reported, 0)
3999 self.assertEqual(A.data['status'], 'NEW')
4000 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004001
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004002 def test_crd_gate_unknown(self):
4003 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004004 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004005 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4006 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004007 A.addApproval('Code-Review', 2)
4008 B.addApproval('Code-Review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004009
4010 # A Depends-On: B
4011 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4012 A.subject, B.data['id'])
4013
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004014 B.addApproval('Approved', 1)
4015 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004016 self.waitUntilSettled()
4017
4018 # Unknown projects cannot share a queue with any other
4019 # since they don't have common jobs with any other (they have no jobs).
4020 # Changes which depend on unknown project changes
4021 # should not be processed in dependent pipeline
4022 self.assertEqual(A.data['status'], 'NEW')
4023 self.assertEqual(B.data['status'], 'NEW')
4024 self.assertEqual(A.reported, 0)
4025 self.assertEqual(B.reported, 0)
4026 self.assertEqual(len(self.history), 0)
4027
James E. Blair7720b402017-06-07 08:51:49 -07004028 # Simulate change B being gated outside this layout Set the
4029 # change merged before submitting the event so that when the
4030 # event triggers a gerrit query to update the change, we get
4031 # the information that it was merged.
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004032 B.setMerged()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004033 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004034 self.waitUntilSettled()
4035 self.assertEqual(len(self.history), 0)
4036
4037 # Now that B is merged, A should be able to be enqueued and
4038 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004039 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004040 self.waitUntilSettled()
4041
4042 self.assertEqual(A.data['status'], 'MERGED')
4043 self.assertEqual(A.reported, 2)
4044 self.assertEqual(B.data['status'], 'MERGED')
4045 self.assertEqual(B.reported, 0)
4046
James E. Blairbfb8e042014-12-30 17:01:44 -08004047 def test_crd_check(self):
4048 "Test cross-repo dependencies in independent pipelines"
4049
Paul Belanger174a8272017-03-14 13:20:10 -04004050 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004051 self.gearman_server.hold_jobs_in_queue = True
4052 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4053 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4054
4055 # A Depends-On: B
4056 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4057 A.subject, B.data['id'])
4058
4059 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4060 self.waitUntilSettled()
4061
James E. Blairbfb8e042014-12-30 17:01:44 -08004062 self.gearman_server.hold_jobs_in_queue = False
4063 self.gearman_server.release()
4064 self.waitUntilSettled()
4065
Paul Belanger174a8272017-03-14 13:20:10 -04004066 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05004067 self.waitUntilSettled()
4068
James E. Blair2a535672017-04-27 12:03:15 -07004069 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08004070
Paul Belanger174a8272017-03-14 13:20:10 -04004071 self.executor_server.hold_jobs_in_build = False
4072 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05004073 self.waitUntilSettled()
4074
James E. Blairbfb8e042014-12-30 17:01:44 -08004075 self.assertEqual(A.data['status'], 'NEW')
4076 self.assertEqual(B.data['status'], 'NEW')
4077 self.assertEqual(A.reported, 1)
4078 self.assertEqual(B.reported, 0)
4079
4080 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004081 tenant = self.sched.abide.tenants.get('tenant-one')
4082 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004083
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004084 def test_crd_check_git_depends(self):
4085 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004086 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004087 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4088 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4089
4090 # Add two git-dependent changes and make sure they both report
4091 # success.
4092 B.setDependsOn(A, 1)
4093 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4094 self.waitUntilSettled()
4095 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4096 self.waitUntilSettled()
4097
James E. Blairb8c16472015-05-05 14:55:26 -07004098 self.orderedRelease()
4099 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004100 self.waitUntilSettled()
4101
4102 self.assertEqual(A.data['status'], 'NEW')
4103 self.assertEqual(B.data['status'], 'NEW')
4104 self.assertEqual(A.reported, 1)
4105 self.assertEqual(B.reported, 1)
4106
4107 self.assertEqual(self.history[0].changes, '1,1')
4108 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004109 tenant = self.sched.abide.tenants.get('tenant-one')
4110 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004111
4112 self.assertIn('Build succeeded', A.messages[0])
4113 self.assertIn('Build succeeded', B.messages[0])
4114
4115 def test_crd_check_duplicate(self):
4116 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004117 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004118 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4119 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004120 tenant = self.sched.abide.tenants.get('tenant-one')
4121 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004122
4123 # Add two git-dependent changes...
4124 B.setDependsOn(A, 1)
4125 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4126 self.waitUntilSettled()
4127 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4128
4129 # ...make sure the live one is not duplicated...
4130 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4131 self.waitUntilSettled()
4132 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4133
4134 # ...but the non-live one is able to be.
4135 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4136 self.waitUntilSettled()
4137 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4138
Clark Boylandd849822015-03-02 12:38:14 -08004139 # Release jobs in order to avoid races with change A jobs
4140 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004141 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004142 self.executor_server.hold_jobs_in_build = False
4143 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004144 self.waitUntilSettled()
4145
4146 self.assertEqual(A.data['status'], 'NEW')
4147 self.assertEqual(B.data['status'], 'NEW')
4148 self.assertEqual(A.reported, 1)
4149 self.assertEqual(B.reported, 1)
4150
4151 self.assertEqual(self.history[0].changes, '1,1 2,1')
4152 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004153 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004154
4155 self.assertIn('Build succeeded', A.messages[0])
4156 self.assertIn('Build succeeded', B.messages[0])
4157
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004158 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004159 "Test cross-repo dependencies re-enqueued in independent pipelines"
4160
4161 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004162 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4163 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004164
4165 # A Depends-On: B
4166 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4167 A.subject, B.data['id'])
4168
4169 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4170 self.waitUntilSettled()
4171
4172 self.sched.reconfigure(self.config)
4173
4174 # Make sure the items still share a change queue, and the
4175 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004176 tenant = self.sched.abide.tenants.get('tenant-one')
4177 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4178 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004179 first_item = queue.queue[0]
4180 for item in queue.queue:
4181 self.assertEqual(item.queue, first_item.queue)
4182 self.assertFalse(first_item.live)
4183 self.assertTrue(queue.queue[1].live)
4184
4185 self.gearman_server.hold_jobs_in_queue = False
4186 self.gearman_server.release()
4187 self.waitUntilSettled()
4188
4189 self.assertEqual(A.data['status'], 'NEW')
4190 self.assertEqual(B.data['status'], 'NEW')
4191 self.assertEqual(A.reported, 1)
4192 self.assertEqual(B.reported, 0)
4193
4194 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004195 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004196
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004197 def test_crd_check_reconfiguration(self):
4198 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4199
4200 def test_crd_undefined_project(self):
4201 """Test that undefined projects in dependencies are handled for
4202 independent pipelines"""
4203 # It's a hack for fake gerrit,
4204 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004205 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004206 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4207
Jesse Keatingea68ab42017-04-19 15:03:30 -07004208 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004209 def test_crd_check_ignore_dependencies(self):
4210 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004211
4212 self.gearman_server.hold_jobs_in_queue = True
4213 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4214 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4215 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4216
4217 # A Depends-On: B
4218 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4219 A.subject, B.data['id'])
4220 # C git-depends on B
4221 C.setDependsOn(B, 1)
4222 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4223 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4224 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4225 self.waitUntilSettled()
4226
4227 # Make sure none of the items share a change queue, and all
4228 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004229 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004230 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004231 self.assertEqual(len(check_pipeline.queues), 3)
4232 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4233 for item in check_pipeline.getAllItems():
4234 self.assertTrue(item.live)
4235
4236 self.gearman_server.hold_jobs_in_queue = False
4237 self.gearman_server.release()
4238 self.waitUntilSettled()
4239
4240 self.assertEqual(A.data['status'], 'NEW')
4241 self.assertEqual(B.data['status'], 'NEW')
4242 self.assertEqual(C.data['status'], 'NEW')
4243 self.assertEqual(A.reported, 1)
4244 self.assertEqual(B.reported, 1)
4245 self.assertEqual(C.reported, 1)
4246
4247 # Each job should have tested exactly one change
4248 for job in self.history:
4249 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004250
James E. Blair7cb84542017-04-19 13:35:05 -07004251 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004252 def test_crd_check_transitive(self):
4253 "Test transitive cross-repo dependencies"
4254 # Specifically, if A -> B -> C, and C gets a new patchset and
4255 # A gets a new patchset, ensure the test of A,2 includes B,1
4256 # and C,2 (not C,1 which would indicate stale data in the
4257 # cache for B).
4258 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4259 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4260 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4261
4262 # A Depends-On: B
4263 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4264 A.subject, B.data['id'])
4265
4266 # B Depends-On: C
4267 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4268 B.subject, C.data['id'])
4269
4270 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4271 self.waitUntilSettled()
4272 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4273
4274 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4275 self.waitUntilSettled()
4276 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4277
4278 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4279 self.waitUntilSettled()
4280 self.assertEqual(self.history[-1].changes, '3,1')
4281
4282 C.addPatchset()
4283 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4284 self.waitUntilSettled()
4285 self.assertEqual(self.history[-1].changes, '3,2')
4286
4287 A.addPatchset()
4288 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4289 self.waitUntilSettled()
4290 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004291
James E. Blaira8b90b32016-08-24 15:18:50 -07004292 def test_crd_check_unknown(self):
4293 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004294 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004295 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4296 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4297 # A Depends-On: B
4298 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4299 A.subject, B.data['id'])
4300
4301 # Make sure zuul has seen an event on B.
4302 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4303 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4304 self.waitUntilSettled()
4305
4306 self.assertEqual(A.data['status'], 'NEW')
4307 self.assertEqual(A.reported, 1)
4308 self.assertEqual(B.data['status'], 'NEW')
4309 self.assertEqual(B.reported, 0)
4310
James E. Blair92464a22016-04-05 10:21:26 -07004311 def test_crd_cycle_join(self):
4312 "Test an updated change creates a cycle"
4313 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4314
4315 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4316 self.waitUntilSettled()
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004317 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004318
4319 # Create B->A
4320 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4321 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4322 B.subject, A.data['id'])
4323 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4324 self.waitUntilSettled()
4325
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004326 # Dep is there so zuul should have reported on B
4327 self.assertEqual(B.reported, 1)
4328
James E. Blair92464a22016-04-05 10:21:26 -07004329 # Update A to add A->B (a cycle).
4330 A.addPatchset()
4331 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4332 A.subject, B.data['id'])
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004333 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4334 self.waitUntilSettled()
James E. Blair92464a22016-04-05 10:21:26 -07004335
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004336 # Dependency cycle injected so zuul should not have reported again on A
4337 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004338
4339 # Now if we update B to remove the depends-on, everything
4340 # should be okay. B; A->B
4341
4342 B.addPatchset()
4343 B.data['commitMessage'] = '%s\n' % (B.subject,)
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004344 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4345 self.waitUntilSettled()
Paul Belanger0e155e22016-11-14 14:12:23 -05004346
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004347 # Cycle was removed so now zuul should have reported again on A
4348 self.assertEqual(A.reported, 2)
4349
4350 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
4351 self.waitUntilSettled()
4352 self.assertEqual(B.reported, 2)
James E. Blair92464a22016-04-05 10:21:26 -07004353
Jesse Keatingcc67b122017-04-19 14:54:57 -07004354 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004355 def test_disable_at(self):
4356 "Test a pipeline will only report to the disabled trigger when failing"
4357
Jesse Keatingcc67b122017-04-19 14:54:57 -07004358 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004359 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004360 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004361 0, tenant.layout.pipelines['check']._consecutive_failures)
4362 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004363
4364 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4365 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4366 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4367 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4368 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4369 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4370 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4371 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4372 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4373 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4374 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4375
Paul Belanger174a8272017-03-14 13:20:10 -04004376 self.executor_server.failJob('project-test1', A)
4377 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004378 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004379 self.executor_server.failJob('project-test1', D)
4380 self.executor_server.failJob('project-test1', E)
4381 self.executor_server.failJob('project-test1', F)
4382 self.executor_server.failJob('project-test1', G)
4383 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004384 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004385 self.executor_server.failJob('project-test1', J)
4386 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004387
4388 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4389 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4390 self.waitUntilSettled()
4391
4392 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004393 2, tenant.layout.pipelines['check']._consecutive_failures)
4394 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004395
4396 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4397 self.waitUntilSettled()
4398
4399 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004400 0, tenant.layout.pipelines['check']._consecutive_failures)
4401 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004402
4403 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4404 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4405 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4406 self.waitUntilSettled()
4407
4408 # We should be disabled now
4409 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004410 3, tenant.layout.pipelines['check']._consecutive_failures)
4411 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004412
4413 # We need to wait between each of these patches to make sure the
4414 # smtp messages come back in an expected order
4415 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4416 self.waitUntilSettled()
4417 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4418 self.waitUntilSettled()
4419 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4420 self.waitUntilSettled()
4421
4422 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4423 # leaving a message on each change
4424 self.assertEqual(1, len(A.messages))
4425 self.assertIn('Build failed.', A.messages[0])
4426 self.assertEqual(1, len(B.messages))
4427 self.assertIn('Build failed.', B.messages[0])
4428 self.assertEqual(1, len(C.messages))
4429 self.assertIn('Build succeeded.', C.messages[0])
4430 self.assertEqual(1, len(D.messages))
4431 self.assertIn('Build failed.', D.messages[0])
4432 self.assertEqual(1, len(E.messages))
4433 self.assertIn('Build failed.', E.messages[0])
4434 self.assertEqual(1, len(F.messages))
4435 self.assertIn('Build failed.', F.messages[0])
4436
4437 # The last 3 (GHI) would have only reported via smtp.
4438 self.assertEqual(3, len(self.smtp_messages))
4439 self.assertEqual(0, len(G.messages))
4440 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004441 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004442 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004443 self.assertEqual(0, len(H.messages))
4444 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004445 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004446 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004447 self.assertEqual(0, len(I.messages))
4448 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004449 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004450 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004451
4452 # Now reload the configuration (simulate a HUP) to check the pipeline
4453 # comes out of disabled
4454 self.sched.reconfigure(self.config)
4455
Jesse Keatingcc67b122017-04-19 14:54:57 -07004456 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004457
4458 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004459 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004460 0, tenant.layout.pipelines['check']._consecutive_failures)
4461 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004462
4463 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4464 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4465 self.waitUntilSettled()
4466
4467 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004468 2, tenant.layout.pipelines['check']._consecutive_failures)
4469 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004470
4471 # J and K went back to gerrit
4472 self.assertEqual(1, len(J.messages))
4473 self.assertIn('Build failed.', J.messages[0])
4474 self.assertEqual(1, len(K.messages))
4475 self.assertIn('Build failed.', K.messages[0])
4476 # No more messages reported via smtp
4477 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004478
James E. Blaircc020532017-04-19 13:04:44 -07004479 @simple_layout('layouts/one-job-project.yaml')
4480 def test_one_job_project(self):
4481 "Test that queueing works with one job"
4482 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4483 'master', 'A')
4484 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4485 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004486 A.addApproval('Code-Review', 2)
4487 B.addApproval('Code-Review', 2)
4488 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4489 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blaircc020532017-04-19 13:04:44 -07004490 self.waitUntilSettled()
4491
4492 self.assertEqual(A.data['status'], 'MERGED')
4493 self.assertEqual(A.reported, 2)
4494 self.assertEqual(B.data['status'], 'MERGED')
4495 self.assertEqual(B.reported, 2)
4496
Paul Belanger71d98172016-11-08 10:56:31 -05004497 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004498 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004499
Paul Belanger174a8272017-03-14 13:20:10 -04004500 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004501 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4502 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4503 self.waitUntilSettled()
4504
Paul Belanger174a8272017-03-14 13:20:10 -04004505 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004506 self.waitUntilSettled()
4507
4508 self.assertEqual(len(self.builds), 2)
4509 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004510 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004511 self.waitUntilSettled()
4512
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004513 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004514 self.assertEqual(len(self.builds), 1,
4515 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004516 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004517 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004518 self.waitUntilSettled()
4519
Paul Belanger174a8272017-03-14 13:20:10 -04004520 self.executor_server.hold_jobs_in_build = False
4521 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004522 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004523 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004524 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4525 self.assertEqual(A.reported, 1)
4526 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004527
James E. Blair15be0e12017-01-03 13:45:20 -08004528 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004529 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004530
4531 self.fake_nodepool.paused = True
4532 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004533 A.addApproval('Code-Review', 2)
4534 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair15be0e12017-01-03 13:45:20 -08004535 self.waitUntilSettled()
4536
4537 self.zk.client.stop()
4538 self.zk.client.start()
4539 self.fake_nodepool.paused = False
4540 self.waitUntilSettled()
4541
4542 self.assertEqual(A.data['status'], 'MERGED')
4543 self.assertEqual(A.reported, 2)
4544
James E. Blair6ab79e02017-01-06 10:10:17 -08004545 def test_nodepool_failure(self):
4546 "Test that jobs are reported after a nodepool failure"
4547
4548 self.fake_nodepool.paused = True
4549 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004550 A.addApproval('Code-Review', 2)
4551 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6ab79e02017-01-06 10:10:17 -08004552 self.waitUntilSettled()
4553
4554 req = self.fake_nodepool.getNodeRequests()[0]
4555 self.fake_nodepool.addFailRequest(req)
4556
4557 self.fake_nodepool.paused = False
4558 self.waitUntilSettled()
4559
4560 self.assertEqual(A.data['status'], 'NEW')
4561 self.assertEqual(A.reported, 2)
4562 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4563 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4564 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4565
Arieb6f068c2016-10-09 13:11:06 +03004566
James E. Blaira002b032017-04-18 10:35:48 -07004567class TestExecutor(ZuulTestCase):
4568 tenant_config_file = 'config/single-tenant/main.yaml'
4569
4570 def assertFinalState(self):
4571 # In this test, we expect to shut down in a non-final state,
4572 # so skip these checks.
4573 pass
4574
4575 def assertCleanShutdown(self):
4576 self.log.debug("Assert clean shutdown")
4577
4578 # After shutdown, make sure no jobs are running
4579 self.assertEqual({}, self.executor_server.job_workers)
4580
4581 # Make sure that git.Repo objects have been garbage collected.
4582 repos = []
4583 gc.collect()
4584 for obj in gc.get_objects():
4585 if isinstance(obj, git.Repo):
4586 self.log.debug("Leaked git repo object: %s" % repr(obj))
4587 repos.append(obj)
4588 self.assertEqual(len(repos), 0)
4589
4590 def test_executor_shutdown(self):
4591 "Test that the executor can shut down with jobs running"
4592
4593 self.executor_server.hold_jobs_in_build = True
4594 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004595 A.addApproval('Code-Review', 2)
4596 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaira002b032017-04-18 10:35:48 -07004597 self.waitUntilSettled()
4598
4599
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004600class TestDependencyGraph(ZuulTestCase):
4601 tenant_config_file = 'config/dependency-graph/main.yaml'
4602
4603 def test_dependeny_graph_dispatch_jobs_once(self):
4604 "Test a job in a dependency graph is queued only once"
4605 # Job dependencies, starting with A
4606 # A
4607 # / \
4608 # B C
4609 # / \ / \
4610 # D F E
4611 # |
4612 # G
4613
4614 self.executor_server.hold_jobs_in_build = True
4615 change = self.fake_gerrit.addFakeChange(
4616 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004617 change.addApproval('Code-Review', 2)
4618 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004619
4620 self.waitUntilSettled()
4621 self.assertEqual([b.name for b in self.builds], ['A'])
4622
4623 self.executor_server.release('A')
4624 self.waitUntilSettled()
4625 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4626
4627 self.executor_server.release('B')
4628 self.waitUntilSettled()
4629 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4630
4631 self.executor_server.release('D')
4632 self.waitUntilSettled()
4633 self.assertEqual([b.name for b in self.builds], ['C'])
4634
4635 self.executor_server.release('C')
4636 self.waitUntilSettled()
4637 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4638
4639 self.executor_server.release('F')
4640 self.waitUntilSettled()
4641 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4642
4643 self.executor_server.release('G')
4644 self.waitUntilSettled()
4645 self.assertEqual([b.name for b in self.builds], ['E'])
4646
4647 self.executor_server.release('E')
4648 self.waitUntilSettled()
4649 self.assertEqual(len(self.builds), 0)
4650
4651 self.executor_server.hold_jobs_in_build = False
4652 self.executor_server.release()
4653 self.waitUntilSettled()
4654
4655 self.assertEqual(len(self.builds), 0)
4656 self.assertEqual(len(self.history), 7)
4657
4658 self.assertEqual(change.data['status'], 'MERGED')
4659 self.assertEqual(change.reported, 2)
4660
4661 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4662 "Test that a job waits till all dependencies are successful"
4663 # Job dependencies, starting with A
4664 # A
4665 # / \
4666 # B C*
4667 # / \ / \
4668 # D F E
4669 # |
4670 # G
4671
4672 self.executor_server.hold_jobs_in_build = True
4673 change = self.fake_gerrit.addFakeChange(
4674 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004675 change.addApproval('Code-Review', 2)
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004676
4677 self.executor_server.failJob('C', change)
4678
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004679 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004680
4681 self.waitUntilSettled()
4682 self.assertEqual([b.name for b in self.builds], ['A'])
4683
4684 self.executor_server.release('A')
4685 self.waitUntilSettled()
4686 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4687
4688 self.executor_server.release('B')
4689 self.waitUntilSettled()
4690 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4691
4692 self.executor_server.release('D')
4693 self.waitUntilSettled()
4694 self.assertEqual([b.name for b in self.builds], ['C'])
4695
4696 self.executor_server.release('C')
4697 self.waitUntilSettled()
4698 self.assertEqual(len(self.builds), 0)
4699
4700 self.executor_server.hold_jobs_in_build = False
4701 self.executor_server.release()
4702 self.waitUntilSettled()
4703
4704 self.assertEqual(len(self.builds), 0)
4705 self.assertEqual(len(self.history), 4)
4706
4707 self.assertEqual(change.data['status'], 'NEW')
4708 self.assertEqual(change.reported, 2)
4709
4710
Arieb6f068c2016-10-09 13:11:06 +03004711class TestDuplicatePipeline(ZuulTestCase):
4712 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4713
4714 def test_duplicate_pipelines(self):
4715 "Test that a change matching multiple pipelines works"
4716
4717 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4718 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4719 self.waitUntilSettled()
4720
4721 self.assertHistory([
4722 dict(name='project-test1', result='SUCCESS', changes='1,1',
4723 pipeline='dup1'),
4724 dict(name='project-test1', result='SUCCESS', changes='1,1',
4725 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004726 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004727
4728 self.assertEqual(len(A.messages), 2)
4729
Paul Belangera46a3742016-11-09 14:23:03 -05004730 if 'dup1' in A.messages[0]:
4731 self.assertIn('dup1', A.messages[0])
4732 self.assertNotIn('dup2', A.messages[0])
4733 self.assertIn('project-test1', A.messages[0])
4734 self.assertIn('dup2', A.messages[1])
4735 self.assertNotIn('dup1', A.messages[1])
4736 self.assertIn('project-test1', A.messages[1])
4737 else:
4738 self.assertIn('dup1', A.messages[1])
4739 self.assertNotIn('dup2', A.messages[1])
4740 self.assertIn('project-test1', A.messages[1])
4741 self.assertIn('dup2', A.messages[0])
4742 self.assertNotIn('dup1', A.messages[0])
4743 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004744
4745
Paul Belanger86085b32016-11-03 12:48:57 -04004746class TestSchedulerTemplatedProject(ZuulTestCase):
4747 tenant_config_file = 'config/templated-project/main.yaml'
4748
Paul Belanger174a8272017-03-14 13:20:10 -04004749 def test_job_from_templates_executed(self):
4750 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004751
4752 A = self.fake_gerrit.addFakeChange(
4753 'org/templated-project', 'master', 'A')
4754 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4755 self.waitUntilSettled()
4756
4757 self.assertEqual(self.getJobFromHistory('project-test1').result,
4758 'SUCCESS')
4759 self.assertEqual(self.getJobFromHistory('project-test2').result,
4760 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004761
4762 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004763 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004764
4765 A = self.fake_gerrit.addFakeChange(
4766 'org/layered-project', 'master', 'A')
4767 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4768 self.waitUntilSettled()
4769
4770 self.assertEqual(self.getJobFromHistory('project-test1').result,
4771 'SUCCESS')
4772 self.assertEqual(self.getJobFromHistory('project-test2').result,
4773 'SUCCESS')
4774 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4775 ).result, 'SUCCESS')
4776 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4777 ).result, 'SUCCESS')
4778 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4779 ).result, 'SUCCESS')
4780 self.assertEqual(self.getJobFromHistory('project-test6').result,
4781 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004782
4783
4784class TestSchedulerSuccessURL(ZuulTestCase):
4785 tenant_config_file = 'config/success-url/main.yaml'
4786
4787 def test_success_url(self):
4788 "Ensure bad build params are ignored"
4789 self.sched.reconfigure(self.config)
4790 self.init_repo('org/docs')
4791
4792 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4793 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4794 self.waitUntilSettled()
4795
4796 # Both builds ran: docs-draft-test + docs-draft-test2
4797 self.assertEqual(len(self.history), 2)
4798
4799 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004800 for build in self.history:
4801 if build.name == 'docs-draft-test':
4802 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06004803 elif build.name == 'docs-draft-test2':
4804 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08004805
4806 # Two msgs: 'Starting...' + results
4807 self.assertEqual(len(self.smtp_messages), 2)
4808 body = self.smtp_messages[1]['body'].splitlines()
4809 self.assertEqual('Build succeeded.', body[0])
4810
4811 self.assertIn(
4812 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4813 'docs-draft-test/%s/publish-docs/' % uuid,
4814 body[2])
4815
Paul Belanger174a8272017-03-14 13:20:10 -04004816 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004817 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05004818 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
4819 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06004820 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08004821 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004822
4823
Adam Gandelman4da00f62016-12-09 15:47:33 -08004824class TestSchedulerMerges(ZuulTestCase):
4825 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004826
4827 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004828 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004829 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004830 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004831 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4832 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4833 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004834 A.addApproval('Code-Review', 2)
4835 B.addApproval('Code-Review', 2)
4836 C.addApproval('Code-Review', 2)
4837 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4838 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4839 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman8bd57102016-12-02 12:58:42 -08004840 self.waitUntilSettled()
4841
4842 build = self.builds[-1]
James E. Blair2a535672017-04-27 12:03:15 -07004843 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4844 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004845 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004846 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman8bd57102016-12-02 12:58:42 -08004847 repo_messages.reverse()
4848
Paul Belanger174a8272017-03-14 13:20:10 -04004849 self.executor_server.hold_jobs_in_build = False
4850 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004851 self.waitUntilSettled()
4852
4853 return repo_messages
4854
4855 def _test_merge(self, mode):
4856 us_path = os.path.join(
4857 self.upstream_root, 'org/project-%s' % mode)
4858 expected_messages = [
4859 'initial commit',
4860 'add content from fixture',
4861 # the intermediate commits order is nondeterministic
4862 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4863 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4864 ]
4865 result = self._test_project_merge_mode(mode)
4866 self.assertEqual(result[:2], expected_messages[:2])
4867 self.assertEqual(result[-2:], expected_messages[-2:])
4868
4869 def test_project_merge_mode_merge(self):
4870 self._test_merge('merge')
4871
4872 def test_project_merge_mode_merge_resolve(self):
4873 self._test_merge('merge-resolve')
4874
4875 def test_project_merge_mode_cherrypick(self):
4876 expected_messages = [
4877 'initial commit',
4878 'add content from fixture',
4879 'A-1',
4880 'B-1',
4881 'C-1']
4882 result = self._test_project_merge_mode('cherry-pick')
4883 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004884
4885 def test_merge_branch(self):
4886 "Test that the right commits are on alternate branches"
4887 self.create_branch('org/project-merge-branches', 'mp')
4888
Paul Belanger174a8272017-03-14 13:20:10 -04004889 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004890 A = self.fake_gerrit.addFakeChange(
4891 'org/project-merge-branches', 'mp', 'A')
4892 B = self.fake_gerrit.addFakeChange(
4893 'org/project-merge-branches', 'mp', 'B')
4894 C = self.fake_gerrit.addFakeChange(
4895 'org/project-merge-branches', 'mp', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004896 A.addApproval('Code-Review', 2)
4897 B.addApproval('Code-Review', 2)
4898 C.addApproval('Code-Review', 2)
4899 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4900 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4901 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08004902 self.waitUntilSettled()
4903
Paul Belanger174a8272017-03-14 13:20:10 -04004904 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004905 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004906 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004907 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004908 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004909 self.waitUntilSettled()
4910
4911 build = self.builds[-1]
James E. Blair3b222492017-07-21 15:17:37 -07004912 self.assertEqual(build.parameters['zuul']['branch'], 'mp')
James E. Blair2a535672017-04-27 12:03:15 -07004913 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4914 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004915 repo = git.Repo(path)
4916
James E. Blair247cab72017-07-20 16:52:36 -07004917 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004918 repo_messages.reverse()
4919 correct_messages = [
4920 'initial commit',
4921 'add content from fixture',
4922 'mp commit',
4923 'A-1', 'B-1', 'C-1']
4924 self.assertEqual(repo_messages, correct_messages)
4925
Paul Belanger174a8272017-03-14 13:20:10 -04004926 self.executor_server.hold_jobs_in_build = False
4927 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004928 self.waitUntilSettled()
4929
4930 def test_merge_multi_branch(self):
4931 "Test that dependent changes on multiple branches are merged"
4932 self.create_branch('org/project-merge-branches', 'mp')
4933
Paul Belanger174a8272017-03-14 13:20:10 -04004934 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004935 A = self.fake_gerrit.addFakeChange(
4936 'org/project-merge-branches', 'master', 'A')
4937 B = self.fake_gerrit.addFakeChange(
4938 'org/project-merge-branches', 'mp', 'B')
4939 C = self.fake_gerrit.addFakeChange(
4940 'org/project-merge-branches', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004941 A.addApproval('Code-Review', 2)
4942 B.addApproval('Code-Review', 2)
4943 C.addApproval('Code-Review', 2)
4944 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4945 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4946 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08004947 self.waitUntilSettled()
4948
4949 job_A = None
4950 for job in self.builds:
4951 if 'project-merge' in job.name:
4952 job_A = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08004953
James E. Blair2a535672017-04-27 12:03:15 -07004954 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
4955 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004956 repo = git.Repo(path)
4957 repo_messages = [c.message.strip()
James E. Blair247cab72017-07-20 16:52:36 -07004958 for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004959 repo_messages.reverse()
4960 correct_messages = [
4961 'initial commit', 'add content from fixture', 'A-1']
4962 self.assertEqual(repo_messages, correct_messages)
4963
Paul Belanger174a8272017-03-14 13:20:10 -04004964 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004965 self.waitUntilSettled()
4966
4967 job_B = None
4968 for job in self.builds:
4969 if 'project-merge' in job.name:
4970 job_B = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08004971
James E. Blair2a535672017-04-27 12:03:15 -07004972 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
4973 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004974 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004975 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004976 repo_messages.reverse()
4977 correct_messages = [
4978 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4979 self.assertEqual(repo_messages, correct_messages)
4980
Paul Belanger174a8272017-03-14 13:20:10 -04004981 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004982 self.waitUntilSettled()
4983
4984 job_C = None
4985 for job in self.builds:
4986 if 'project-merge' in job.name:
4987 job_C = job
James E. Blairb1345832017-07-21 14:51:35 -07004988
James E. Blair2a535672017-04-27 12:03:15 -07004989 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
4990 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004991 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004992 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004993
4994 repo_messages.reverse()
4995 correct_messages = [
4996 'initial commit', 'add content from fixture',
4997 'A-1', 'C-1']
4998 # Ensure the right commits are in the history for this ref
4999 self.assertEqual(repo_messages, correct_messages)
5000
Paul Belanger174a8272017-03-14 13:20:10 -04005001 self.executor_server.hold_jobs_in_build = False
5002 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005003 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005004
5005
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005006class TestSemaphore(ZuulTestCase):
5007 tenant_config_file = 'config/semaphore/main.yaml'
5008
5009 def test_semaphore_one(self):
5010 "Test semaphores with max=1 (mutex)"
5011 tenant = self.sched.abide.tenants.get('tenant-one')
5012
5013 self.executor_server.hold_jobs_in_build = True
5014
5015 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5016 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5017 self.assertFalse('test-semaphore' in
5018 tenant.semaphore_handler.semaphores)
5019
5020 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5021 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5022 self.waitUntilSettled()
5023
5024 self.assertEqual(len(self.builds), 3)
5025 self.assertEqual(self.builds[0].name, 'project-test1')
5026 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
5027 self.assertEqual(self.builds[2].name, 'project-test1')
5028
5029 self.executor_server.release('semaphore-one-test1')
5030 self.waitUntilSettled()
5031
5032 self.assertEqual(len(self.builds), 3)
5033 self.assertEqual(self.builds[0].name, 'project-test1')
5034 self.assertEqual(self.builds[1].name, 'project-test1')
5035 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5036 self.assertTrue('test-semaphore' in
5037 tenant.semaphore_handler.semaphores)
5038
5039 self.executor_server.release('semaphore-one-test2')
5040 self.waitUntilSettled()
5041
5042 self.assertEqual(len(self.builds), 3)
5043 self.assertEqual(self.builds[0].name, 'project-test1')
5044 self.assertEqual(self.builds[1].name, 'project-test1')
5045 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
5046 self.assertTrue('test-semaphore' in
5047 tenant.semaphore_handler.semaphores)
5048
5049 self.executor_server.release('semaphore-one-test1')
5050 self.waitUntilSettled()
5051
5052 self.assertEqual(len(self.builds), 3)
5053 self.assertEqual(self.builds[0].name, 'project-test1')
5054 self.assertEqual(self.builds[1].name, 'project-test1')
5055 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5056 self.assertTrue('test-semaphore' in
5057 tenant.semaphore_handler.semaphores)
5058
5059 self.executor_server.release('semaphore-one-test2')
5060 self.waitUntilSettled()
5061
5062 self.assertEqual(len(self.builds), 2)
5063 self.assertEqual(self.builds[0].name, 'project-test1')
5064 self.assertEqual(self.builds[1].name, 'project-test1')
5065 self.assertFalse('test-semaphore' in
5066 tenant.semaphore_handler.semaphores)
5067
5068 self.executor_server.hold_jobs_in_build = False
5069 self.executor_server.release()
5070
5071 self.waitUntilSettled()
5072 self.assertEqual(len(self.builds), 0)
5073
5074 self.assertEqual(A.reported, 1)
5075 self.assertEqual(B.reported, 1)
5076 self.assertFalse('test-semaphore' in
5077 tenant.semaphore_handler.semaphores)
5078
5079 def test_semaphore_two(self):
5080 "Test semaphores with max>1"
5081 tenant = self.sched.abide.tenants.get('tenant-one')
5082
5083 self.executor_server.hold_jobs_in_build = True
5084 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5085 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5086 self.assertFalse('test-semaphore-two' in
5087 tenant.semaphore_handler.semaphores)
5088
5089 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5090 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5091 self.waitUntilSettled()
5092
5093 self.assertEqual(len(self.builds), 4)
5094 self.assertEqual(self.builds[0].name, 'project-test1')
5095 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
5096 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5097 self.assertEqual(self.builds[3].name, 'project-test1')
5098 self.assertTrue('test-semaphore-two' in
5099 tenant.semaphore_handler.semaphores)
5100 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5101 'test-semaphore-two', [])), 2)
5102
5103 self.executor_server.release('semaphore-two-test1')
5104 self.waitUntilSettled()
5105
5106 self.assertEqual(len(self.builds), 4)
5107 self.assertEqual(self.builds[0].name, 'project-test1')
5108 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
5109 self.assertEqual(self.builds[2].name, 'project-test1')
5110 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
5111 self.assertTrue('test-semaphore-two' in
5112 tenant.semaphore_handler.semaphores)
5113 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5114 'test-semaphore-two', [])), 2)
5115
5116 self.executor_server.release('semaphore-two-test2')
5117 self.waitUntilSettled()
5118
5119 self.assertEqual(len(self.builds), 4)
5120 self.assertEqual(self.builds[0].name, 'project-test1')
5121 self.assertEqual(self.builds[1].name, 'project-test1')
5122 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5123 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
5124 self.assertTrue('test-semaphore-two' in
5125 tenant.semaphore_handler.semaphores)
5126 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5127 'test-semaphore-two', [])), 2)
5128
5129 self.executor_server.release('semaphore-two-test1')
5130 self.waitUntilSettled()
5131
5132 self.assertEqual(len(self.builds), 3)
5133 self.assertEqual(self.builds[0].name, 'project-test1')
5134 self.assertEqual(self.builds[1].name, 'project-test1')
5135 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5136 self.assertTrue('test-semaphore-two' in
5137 tenant.semaphore_handler.semaphores)
5138 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5139 'test-semaphore-two', [])), 1)
5140
5141 self.executor_server.release('semaphore-two-test2')
5142 self.waitUntilSettled()
5143
5144 self.assertEqual(len(self.builds), 2)
5145 self.assertEqual(self.builds[0].name, 'project-test1')
5146 self.assertEqual(self.builds[1].name, 'project-test1')
5147 self.assertFalse('test-semaphore-two' in
5148 tenant.semaphore_handler.semaphores)
5149
5150 self.executor_server.hold_jobs_in_build = False
5151 self.executor_server.release()
5152
5153 self.waitUntilSettled()
5154 self.assertEqual(len(self.builds), 0)
5155
5156 self.assertEqual(A.reported, 1)
5157 self.assertEqual(B.reported, 1)
5158
5159 def test_semaphore_abandon(self):
5160 "Test abandon with job semaphores"
5161 self.executor_server.hold_jobs_in_build = True
5162 tenant = self.sched.abide.tenants.get('tenant-one')
5163 check_pipeline = tenant.layout.pipelines['check']
5164
5165 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5166 self.assertFalse('test-semaphore' in
5167 tenant.semaphore_handler.semaphores)
5168
5169 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5170 self.waitUntilSettled()
5171
5172 self.assertTrue('test-semaphore' in
5173 tenant.semaphore_handler.semaphores)
5174
5175 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5176 self.waitUntilSettled()
5177
5178 # The check pipeline should be empty
5179 items = check_pipeline.getAllItems()
5180 self.assertEqual(len(items), 0)
5181
5182 # The semaphore should be released
5183 self.assertFalse('test-semaphore' in
5184 tenant.semaphore_handler.semaphores)
5185
5186 self.executor_server.hold_jobs_in_build = False
5187 self.executor_server.release()
5188 self.waitUntilSettled()
5189
Tobias Henkel0f714002017-06-30 23:30:52 +02005190 def test_semaphore_new_patchset(self):
5191 "Test new patchset with job semaphores"
5192 self.executor_server.hold_jobs_in_build = True
5193 tenant = self.sched.abide.tenants.get('tenant-one')
5194 check_pipeline = tenant.layout.pipelines['check']
5195
5196 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5197 self.assertFalse('test-semaphore' in
5198 tenant.semaphore_handler.semaphores)
5199
5200 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5201 self.waitUntilSettled()
5202
5203 self.assertTrue('test-semaphore' in
5204 tenant.semaphore_handler.semaphores)
5205 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5206 self.assertEqual(len(semaphore), 1)
5207
5208 A.addPatchset()
5209 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
5210 self.waitUntilSettled()
5211
5212 self.assertTrue('test-semaphore' in
5213 tenant.semaphore_handler.semaphores)
5214 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5215 self.assertEqual(len(semaphore), 1)
5216
5217 items = check_pipeline.getAllItems()
5218 self.assertEqual(items[0].change.number, '1')
5219 self.assertEqual(items[0].change.patchset, '2')
5220 self.assertTrue(items[0].live)
5221
5222 self.executor_server.hold_jobs_in_build = False
5223 self.executor_server.release()
5224 self.waitUntilSettled()
5225
5226 # The semaphore should be released
5227 self.assertFalse('test-semaphore' in
5228 tenant.semaphore_handler.semaphores)
5229
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005230 def test_semaphore_reconfigure(self):
5231 "Test reconfigure with job semaphores"
5232 self.executor_server.hold_jobs_in_build = True
5233 tenant = self.sched.abide.tenants.get('tenant-one')
5234 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5235 self.assertFalse('test-semaphore' in
5236 tenant.semaphore_handler.semaphores)
5237
5238 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5239 self.waitUntilSettled()
5240
5241 self.assertTrue('test-semaphore' in
5242 tenant.semaphore_handler.semaphores)
5243
5244 # reconfigure without layout change
5245 self.sched.reconfigure(self.config)
5246 self.waitUntilSettled()
5247 tenant = self.sched.abide.tenants.get('tenant-one')
5248
5249 # semaphore still must be held
5250 self.assertTrue('test-semaphore' in
5251 tenant.semaphore_handler.semaphores)
5252
5253 self.commitConfigUpdate(
5254 'common-config',
5255 'config/semaphore/zuul-reconfiguration.yaml')
5256 self.sched.reconfigure(self.config)
5257 self.waitUntilSettled()
5258 tenant = self.sched.abide.tenants.get('tenant-one')
5259
5260 self.executor_server.release('project-test1')
5261 self.waitUntilSettled()
5262
5263 # There should be no builds anymore
5264 self.assertEqual(len(self.builds), 0)
5265
5266 # The semaphore should be released
5267 self.assertFalse('test-semaphore' in
5268 tenant.semaphore_handler.semaphores)
5269
5270
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005271class TestSemaphoreMultiTenant(ZuulTestCase):
5272 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5273
5274 def test_semaphore_tenant_isolation(self):
5275 "Test semaphores in multiple tenants"
5276
5277 self.waitUntilSettled()
5278 tenant_one = self.sched.abide.tenants.get('tenant-one')
5279 tenant_two = self.sched.abide.tenants.get('tenant-two')
5280
5281 self.executor_server.hold_jobs_in_build = True
5282 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5283 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5284 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5285 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5286 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5287 self.assertFalse('test-semaphore' in
5288 tenant_one.semaphore_handler.semaphores)
5289 self.assertFalse('test-semaphore' in
5290 tenant_two.semaphore_handler.semaphores)
5291
5292 # add patches to project1 of tenant-one
5293 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5294 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5295 self.waitUntilSettled()
5296
5297 # one build of project1-test1 must run
5298 # semaphore of tenant-one must be acquired once
5299 # semaphore of tenant-two must not be acquired
5300 self.assertEqual(len(self.builds), 1)
5301 self.assertEqual(self.builds[0].name, 'project1-test1')
5302 self.assertTrue('test-semaphore' in
5303 tenant_one.semaphore_handler.semaphores)
5304 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5305 'test-semaphore', [])), 1)
5306 self.assertFalse('test-semaphore' in
5307 tenant_two.semaphore_handler.semaphores)
5308
5309 # add patches to project2 of tenant-two
5310 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5311 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5312 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5313 self.waitUntilSettled()
5314
5315 # one build of project1-test1 must run
5316 # two builds of project2-test1 must run
5317 # semaphore of tenant-one must be acquired once
5318 # semaphore of tenant-two must be acquired twice
5319 self.assertEqual(len(self.builds), 3)
5320 self.assertEqual(self.builds[0].name, 'project1-test1')
5321 self.assertEqual(self.builds[1].name, 'project2-test1')
5322 self.assertEqual(self.builds[2].name, 'project2-test1')
5323 self.assertTrue('test-semaphore' in
5324 tenant_one.semaphore_handler.semaphores)
5325 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5326 'test-semaphore', [])), 1)
5327 self.assertTrue('test-semaphore' in
5328 tenant_two.semaphore_handler.semaphores)
5329 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5330 'test-semaphore', [])), 2)
5331
5332 self.executor_server.release('project1-test1')
5333 self.waitUntilSettled()
5334
5335 # one build of project1-test1 must run
5336 # two builds of project2-test1 must run
5337 # semaphore of tenant-one must be acquired once
5338 # semaphore of tenant-two must be acquired twice
5339 self.assertEqual(len(self.builds), 3)
5340 self.assertEqual(self.builds[0].name, 'project2-test1')
5341 self.assertEqual(self.builds[1].name, 'project2-test1')
5342 self.assertEqual(self.builds[2].name, 'project1-test1')
5343 self.assertTrue('test-semaphore' in
5344 tenant_one.semaphore_handler.semaphores)
5345 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5346 'test-semaphore', [])), 1)
5347 self.assertTrue('test-semaphore' in
5348 tenant_two.semaphore_handler.semaphores)
5349 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5350 'test-semaphore', [])), 2)
5351
5352 self.executor_server.release('project2-test1')
5353 self.waitUntilSettled()
5354
5355 # one build of project1-test1 must run
5356 # one build of project2-test1 must run
5357 # semaphore of tenant-one must be acquired once
5358 # semaphore of tenant-two must be acquired once
5359 self.assertEqual(len(self.builds), 2)
5360 self.assertTrue('test-semaphore' in
5361 tenant_one.semaphore_handler.semaphores)
5362 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5363 'test-semaphore', [])), 1)
5364 self.assertTrue('test-semaphore' in
5365 tenant_two.semaphore_handler.semaphores)
5366 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5367 'test-semaphore', [])), 1)
5368
5369 self.executor_server.hold_jobs_in_build = False
5370 self.executor_server.release()
5371
5372 self.waitUntilSettled()
5373
5374 # no build must run
5375 # semaphore of tenant-one must not be acquired
5376 # semaphore of tenant-two must not be acquired
5377 self.assertEqual(len(self.builds), 0)
5378 self.assertFalse('test-semaphore' in
5379 tenant_one.semaphore_handler.semaphores)
5380 self.assertFalse('test-semaphore' in
5381 tenant_two.semaphore_handler.semaphores)
5382
5383 self.assertEqual(A.reported, 1)
5384 self.assertEqual(B.reported, 1)
5385
5386
5387class TestSemaphoreInRepo(ZuulTestCase):
Tobias Henkelabf973e2017-07-28 10:07:34 +02005388 config_file = 'zuul-connections-gerrit-and-github.conf'
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005389 tenant_config_file = 'config/in-repo/main.yaml'
5390
5391 def test_semaphore_in_repo(self):
5392 "Test semaphores in repo config"
5393
5394 # This tests dynamic semaphore handling in project repos. The semaphore
5395 # max value should not be evaluated dynamically but must be updated
5396 # after the change lands.
5397
5398 self.waitUntilSettled()
5399 tenant = self.sched.abide.tenants.get('tenant-one')
5400
5401 in_repo_conf = textwrap.dedent(
5402 """
5403 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +02005404 name: project-test1
5405
5406 - job:
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005407 name: project-test2
5408 semaphore: test-semaphore
5409
5410 - project:
5411 name: org/project
5412 tenant-one-gate:
5413 jobs:
5414 - project-test2
5415
5416 # the max value in dynamic layout must be ignored
5417 - semaphore:
5418 name: test-semaphore
5419 max: 2
5420 """)
5421
5422 in_repo_playbook = textwrap.dedent(
5423 """
5424 - hosts: all
5425 tasks: []
5426 """)
5427
5428 file_dict = {'.zuul.yaml': in_repo_conf,
5429 'playbooks/project-test2.yaml': in_repo_playbook}
5430 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5431 files=file_dict)
5432 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5433 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5434 B.setDependsOn(A, 1)
5435 C.setDependsOn(A, 1)
5436
5437 self.executor_server.hold_jobs_in_build = True
5438
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005439 A.addApproval('Code-Review', 2)
5440 B.addApproval('Code-Review', 2)
5441 C.addApproval('Code-Review', 2)
5442 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5443 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5444 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005445 self.waitUntilSettled()
5446
5447 # check that the layout in a queue item still has max value of 1
5448 # for test-semaphore
5449 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5450 queue = None
5451 for queue_candidate in pipeline.queues:
5452 if queue_candidate.name == 'org/project':
5453 queue = queue_candidate
5454 break
5455 queue_item = queue.queue[0]
5456 item_dynamic_layout = queue_item.current_build_set.layout
5457 dynamic_test_semaphore = \
5458 item_dynamic_layout.semaphores.get('test-semaphore')
5459 self.assertEqual(dynamic_test_semaphore.max, 1)
5460
5461 # one build must be in queue, one semaphores acquired
5462 self.assertEqual(len(self.builds), 1)
5463 self.assertEqual(self.builds[0].name, 'project-test2')
5464 self.assertTrue('test-semaphore' in
5465 tenant.semaphore_handler.semaphores)
5466 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5467 'test-semaphore', [])), 1)
5468
5469 self.executor_server.release('project-test2')
5470 self.waitUntilSettled()
5471
5472 # change A must be merged
5473 self.assertEqual(A.data['status'], 'MERGED')
5474 self.assertEqual(A.reported, 2)
5475
5476 # send change-merged event as the gerrit mock doesn't send it
5477 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5478 self.waitUntilSettled()
5479
5480 # now that change A was merged, the new semaphore max must be effective
5481 tenant = self.sched.abide.tenants.get('tenant-one')
5482 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5483
5484 # two builds must be in queue, two semaphores acquired
5485 self.assertEqual(len(self.builds), 2)
5486 self.assertEqual(self.builds[0].name, 'project-test2')
5487 self.assertEqual(self.builds[1].name, 'project-test2')
5488 self.assertTrue('test-semaphore' in
5489 tenant.semaphore_handler.semaphores)
5490 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5491 'test-semaphore', [])), 2)
5492
5493 self.executor_server.release('project-test2')
5494 self.waitUntilSettled()
5495
5496 self.assertEqual(len(self.builds), 0)
5497 self.assertFalse('test-semaphore' in
5498 tenant.semaphore_handler.semaphores)
5499
5500 self.executor_server.hold_jobs_in_build = False
5501 self.executor_server.release()
5502
5503 self.waitUntilSettled()
5504 self.assertEqual(len(self.builds), 0)
5505
5506 self.assertEqual(A.reported, 2)
5507 self.assertEqual(B.reported, 2)
5508 self.assertEqual(C.reported, 2)