blob: c8ebeeaa1daee1fc2c7b127419584de0757bc97e [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. Blair1f4c2bb2013-04-26 08:40:46 -07001106
Zhongyue Luo5d556072012-09-21 02:00:47 +09001107 e = {
1108 "type": "ref-updated",
1109 "submitter": {
1110 "name": "User Name",
1111 },
1112 "refUpdate": {
1113 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1114 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1115 "refName": "master",
1116 "project": "org/project",
1117 }
1118 }
James E. Blairdaabed22012-08-15 15:38:57 -07001119 self.fake_gerrit.addEvent(e)
1120 self.waitUntilSettled()
1121
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001122 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001123 self.assertEqual(len(self.history), 1)
1124 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001125
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001126 def test_post_ignore_deletes(self):
1127 "Test that deleting refs does not trigger post jobs"
1128
1129 e = {
1130 "type": "ref-updated",
1131 "submitter": {
1132 "name": "User Name",
1133 },
1134 "refUpdate": {
1135 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1136 "newRev": "0000000000000000000000000000000000000000",
1137 "refName": "master",
1138 "project": "org/project",
1139 }
1140 }
1141 self.fake_gerrit.addEvent(e)
1142 self.waitUntilSettled()
1143
1144 job_names = [x.name for x in self.history]
1145 self.assertEqual(len(self.history), 0)
1146 self.assertNotIn('project-post', job_names)
1147
James E. Blaircc324192017-04-19 14:36:08 -07001148 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001149 def test_post_ignore_deletes_negative(self):
1150 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001151 e = {
1152 "type": "ref-updated",
1153 "submitter": {
1154 "name": "User Name",
1155 },
1156 "refUpdate": {
1157 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1158 "newRev": "0000000000000000000000000000000000000000",
1159 "refName": "master",
1160 "project": "org/project",
1161 }
1162 }
1163 self.fake_gerrit.addEvent(e)
1164 self.waitUntilSettled()
1165
1166 job_names = [x.name for x in self.history]
1167 self.assertEqual(len(self.history), 1)
1168 self.assertIn('project-post', job_names)
1169
James E. Blairec056492016-07-22 09:45:56 -07001170 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001171 def test_build_configuration_branch_interaction(self):
1172 "Test that switching between branches works"
1173 self.test_build_configuration()
1174 self.test_build_configuration_branch()
1175 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001176 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001177 repo = git.Repo(path)
1178 repo.heads.master.commit = repo.commit('init')
1179 self.test_build_configuration()
1180
James E. Blairdb93b302017-07-19 15:33:11 -07001181 def test_dependent_changes_rebase(self):
1182 # Test that no errors occur when we walk a dependency tree
1183 # with an unused leaf node due to a rebase.
1184 # Start by constructing: C -> B -> A
1185 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1186 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1187 B.setDependsOn(A, 1)
1188
1189 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1190 C.setDependsOn(B, 1)
1191
1192 # Then rebase to form: D -> C -> A
1193 C.addPatchset() # C,2
1194 C.setDependsOn(A, 1)
1195
1196 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1197 D.setDependsOn(C, 2)
1198
1199 # Walk the entire tree
1200 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1201 self.waitUntilSettled()
1202 self.assertEqual(len(self.history), 3)
1203
1204 # Verify that walking just part of the tree still works
1205 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
1206 self.waitUntilSettled()
1207 self.assertEqual(len(self.history), 6)
1208
James E. Blaircaec0c52012-08-22 14:52:22 -07001209 def test_dependent_changes_dequeue(self):
1210 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001211
James E. Blaircaec0c52012-08-22 14:52:22 -07001212 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1213 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1214 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001215 A.addApproval('Code-Review', 2)
1216 B.addApproval('Code-Review', 2)
1217 C.addApproval('Code-Review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001218
1219 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1220 M1.setMerged()
1221
1222 # C -> B -> A -> M1
1223
1224 C.setDependsOn(B, 1)
1225 B.setDependsOn(A, 1)
1226 A.setDependsOn(M1, 1)
1227
Paul Belanger174a8272017-03-14 13:20:10 -04001228 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001229
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001230 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1231 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1232 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001233
1234 self.waitUntilSettled()
1235
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001236 self.assertEqual(A.data['status'], 'NEW')
1237 self.assertEqual(A.reported, 2)
1238 self.assertEqual(B.data['status'], 'NEW')
1239 self.assertEqual(B.reported, 2)
1240 self.assertEqual(C.data['status'], 'NEW')
James E. Blair0d3e83b2017-06-05 13:51:57 -07001241 self.assertIn('This change depends on a change that failed to merge.',
1242 C.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001243 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001244
James E. Blair972e3c72013-08-29 12:04:55 -07001245 def test_failing_dependent_changes(self):
1246 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001247 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001248 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1249 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1250 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1251 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1252 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001253 A.addApproval('Code-Review', 2)
1254 B.addApproval('Code-Review', 2)
1255 C.addApproval('Code-Review', 2)
1256 D.addApproval('Code-Review', 2)
1257 E.addApproval('Code-Review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001258
1259 # E, D -> C -> B, A
1260
1261 D.setDependsOn(C, 1)
1262 C.setDependsOn(B, 1)
1263
Paul Belanger174a8272017-03-14 13:20:10 -04001264 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001265
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001266 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1267 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
1268 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1269 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1270 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001271
1272 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001273 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001274 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001275 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001276 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001277 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001278 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()
1283
Paul Belanger174a8272017-03-14 13:20:10 -04001284 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001285 for build in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07001286 if build.parameters['zuul']['change'] != '1':
James E. Blair972e3c72013-08-29 12:04:55 -07001287 build.release()
1288 self.waitUntilSettled()
1289
Paul Belanger174a8272017-03-14 13:20:10 -04001290 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001291 self.waitUntilSettled()
1292
1293 self.assertEqual(A.data['status'], 'MERGED')
1294 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001295 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001296 self.assertEqual(B.data['status'], 'NEW')
1297 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001298 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001299 self.assertEqual(C.data['status'], 'NEW')
1300 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001301 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001302 self.assertEqual(D.data['status'], 'NEW')
1303 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001304 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001305 self.assertEqual(E.data['status'], 'MERGED')
1306 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001307 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001308 self.assertEqual(len(self.history), 18)
1309
James E. Blairec590122012-08-22 15:19:31 -07001310 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001311 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001312 # If it's dequeued more than once, we should see extra
1313 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001314
Paul Belanger174a8272017-03-14 13:20:10 -04001315 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001316 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1317 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1318 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001319 A.addApproval('Code-Review', 2)
1320 B.addApproval('Code-Review', 2)
1321 C.addApproval('Code-Review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001322
Paul Belanger174a8272017-03-14 13:20:10 -04001323 self.executor_server.failJob('project-test1', A)
1324 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001325
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001326 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1327 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1328 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001329
1330 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001331
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001332 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001333 self.assertEqual(self.builds[0].name, 'project-merge')
1334 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001335
Paul Belanger174a8272017-03-14 13:20:10 -04001336 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001337 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001338 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001339 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001340 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001341 self.waitUntilSettled()
1342
James E. Blair6df67132017-04-19 13:53:20 -07001343 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001344 self.assertEqual(self.builds[0].name, 'project-test1')
1345 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001346 self.assertEqual(self.builds[2].name, 'project-test1')
1347 self.assertEqual(self.builds[3].name, 'project-test2')
1348 self.assertEqual(self.builds[4].name, 'project-test1')
1349 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001350
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001351 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001352 self.waitUntilSettled()
1353
James E. Blair6df67132017-04-19 13:53:20 -07001354 self.assertEqual(len(self.builds), 2) # test2, merge for B
1355 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001356
Paul Belanger174a8272017-03-14 13:20:10 -04001357 self.executor_server.hold_jobs_in_build = False
1358 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001359 self.waitUntilSettled()
1360
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001361 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001362 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001363
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001364 self.assertEqual(A.data['status'], 'NEW')
1365 self.assertEqual(B.data['status'], 'MERGED')
1366 self.assertEqual(C.data['status'], 'MERGED')
1367 self.assertEqual(A.reported, 2)
1368 self.assertEqual(B.reported, 2)
1369 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001370
James E. Blaircc020532017-04-19 13:04:44 -07001371 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001372 def test_nonvoting_job(self):
1373 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001374
James E. Blair4ec821f2012-08-23 15:28:28 -07001375 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1376 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001377 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001378 self.executor_server.failJob('nonvoting-project-test2', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001379 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001380
1381 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001382
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001383 self.assertEqual(A.data['status'], 'MERGED')
1384 self.assertEqual(A.reported, 2)
1385 self.assertEqual(
1386 self.getJobFromHistory('nonvoting-project-merge').result,
1387 'SUCCESS')
1388 self.assertEqual(
1389 self.getJobFromHistory('nonvoting-project-test1').result,
1390 'SUCCESS')
1391 self.assertEqual(
1392 self.getJobFromHistory('nonvoting-project-test2').result,
1393 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001394
James E. Blair2ef29e92017-07-21 15:25:05 -07001395 self.assertTrue(self.getJobFromHistory('nonvoting-project-merge').
1396 parameters['zuul']['voting'])
1397 self.assertTrue(self.getJobFromHistory('nonvoting-project-test1').
1398 parameters['zuul']['voting'])
1399 self.assertFalse(self.getJobFromHistory('nonvoting-project-test2').
1400 parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -07001401
James E. Blaire0487072012-08-29 17:38:31 -07001402 def test_check_queue_success(self):
1403 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001404
James E. Blaire0487072012-08-29 17:38:31 -07001405 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1406 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1407
1408 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001409
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001410 self.assertEqual(A.data['status'], 'NEW')
1411 self.assertEqual(A.reported, 1)
1412 self.assertEqual(self.getJobFromHistory('project-merge').result,
1413 'SUCCESS')
1414 self.assertEqual(self.getJobFromHistory('project-test1').result,
1415 'SUCCESS')
1416 self.assertEqual(self.getJobFromHistory('project-test2').result,
1417 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001418
1419 def test_check_queue_failure(self):
1420 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001421
James E. Blaire0487072012-08-29 17:38:31 -07001422 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001423 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001424 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1425
1426 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001427
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001428 self.assertEqual(A.data['status'], 'NEW')
1429 self.assertEqual(A.reported, 1)
1430 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001431 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001432 self.assertEqual(self.getJobFromHistory('project-test1').result,
1433 'SUCCESS')
1434 self.assertEqual(self.getJobFromHistory('project-test2').result,
1435 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001436
David Shrewsburyffab07a2017-07-24 12:45:07 -04001437 @simple_layout('layouts/autohold.yaml')
1438 def test_autohold(self):
1439 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1440
1441 client = zuul.rpcclient.RPCClient('127.0.0.1',
1442 self.gearman_server.port)
1443 self.addCleanup(client.shutdown)
David Shrewsbury36b2adf2017-07-31 15:40:13 -04001444 r = client.autohold('tenant-one', 'org/project', 'project-test2',
1445 "reason text", 1)
David Shrewsburyffab07a2017-07-24 12:45:07 -04001446 self.assertTrue(r)
1447
1448 self.executor_server.failJob('project-test2', A)
1449 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1450
1451 self.waitUntilSettled()
1452
1453 self.assertEqual(A.data['status'], 'NEW')
1454 self.assertEqual(A.reported, 1)
1455 self.assertEqual(self.getJobFromHistory('project-test2').result,
1456 'FAILURE')
1457
1458 # Check nodepool for a held node
1459 held_node = None
1460 for node in self.fake_nodepool.getNodes():
1461 if node['state'] == zuul.model.STATE_HOLD:
1462 held_node = node
1463 break
1464 self.assertIsNotNone(held_node)
1465
1466 # Validate node has recorded the failed job
1467 self.assertEqual(
1468 held_node['hold_job'],
1469 " ".join(['tenant-one',
1470 'review.example.com/org/project',
1471 'project-test2'])
1472 )
David Shrewsbury36b2adf2017-07-31 15:40:13 -04001473 self.assertEqual(held_node['hold_reason'], "reason text")
David Shrewsburyffab07a2017-07-24 12:45:07 -04001474
1475 # Another failed change should not hold any more nodes
1476 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1477 self.executor_server.failJob('project-test2', B)
1478 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1479 self.waitUntilSettled()
1480 self.assertEqual(B.data['status'], 'NEW')
1481 self.assertEqual(B.reported, 1)
1482 self.assertEqual(self.getJobFromHistory('project-test2').result,
1483 'FAILURE')
1484
1485 held_nodes = 0
1486 for node in self.fake_nodepool.getNodes():
1487 if node['state'] == zuul.model.STATE_HOLD:
1488 held_nodes += 1
1489 self.assertEqual(held_nodes, 1)
1490
James E. Blair7cb84542017-04-19 13:35:05 -07001491 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001492 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001493 # This particular test does a large amount of merges and needs a little
1494 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001495 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001496 "test that dependent changes behind dequeued changes work"
1497 # This complicated test is a reproduction of a real life bug
1498 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001499
Paul Belanger174a8272017-03-14 13:20:10 -04001500 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001501 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1502 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1503 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1504 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1505 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1506 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1507 D.setDependsOn(C, 1)
1508 E.setDependsOn(D, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001509 A.addApproval('Code-Review', 2)
1510 B.addApproval('Code-Review', 2)
1511 C.addApproval('Code-Review', 2)
1512 D.addApproval('Code-Review', 2)
1513 E.addApproval('Code-Review', 2)
1514 F.addApproval('Code-Review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001515
1516 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001517
1518 # Change object re-use in the gerrit trigger is hidden if
1519 # changes are added in quick succession; waiting makes it more
1520 # like real life.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001521 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001522 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001523 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001524 self.waitUntilSettled()
1525
Paul Belanger174a8272017-03-14 13:20:10 -04001526 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001527 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001528 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001529 self.waitUntilSettled()
1530
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001531 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001532 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001533 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001534 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001535 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001536 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001537 self.fake_gerrit.addEvent(F.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001538 self.waitUntilSettled()
1539
Paul Belanger174a8272017-03-14 13:20:10 -04001540 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001541 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001542 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001543 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001544 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001545 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001546 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001547 self.waitUntilSettled()
1548
1549 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001550
1551 # Grab pointers to the jobs we want to release before
1552 # releasing any, because list indexes may change as
1553 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001554 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001555 a.release()
1556 b.release()
1557 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001558 self.waitUntilSettled()
1559
Paul Belanger174a8272017-03-14 13:20:10 -04001560 self.executor_server.hold_jobs_in_build = False
1561 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001562 self.waitUntilSettled()
1563
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001564 self.assertEqual(A.data['status'], 'NEW')
1565 self.assertEqual(B.data['status'], 'MERGED')
1566 self.assertEqual(C.data['status'], 'MERGED')
1567 self.assertEqual(D.data['status'], 'MERGED')
1568 self.assertEqual(E.data['status'], 'MERGED')
1569 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001570
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001571 self.assertEqual(A.reported, 2)
1572 self.assertEqual(B.reported, 2)
1573 self.assertEqual(C.reported, 2)
1574 self.assertEqual(D.reported, 2)
1575 self.assertEqual(E.reported, 2)
1576 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001577
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001578 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1579 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001580
1581 def test_merger_repack(self):
1582 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001583
James E. Blair05fed602012-09-07 12:45:24 -07001584 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001585 A.addApproval('Code-Review', 2)
1586 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001587 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001588 self.assertEqual(self.getJobFromHistory('project-merge').result,
1589 'SUCCESS')
1590 self.assertEqual(self.getJobFromHistory('project-test1').result,
1591 'SUCCESS')
1592 self.assertEqual(self.getJobFromHistory('project-test2').result,
1593 'SUCCESS')
1594 self.assertEqual(A.data['status'], 'MERGED')
1595 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001596 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001597 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001598
James E. Blair2a535672017-04-27 12:03:15 -07001599 path = os.path.join(self.merger_src_root, "review.example.com",
1600 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001601 if os.path.exists(path):
1602 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001603 path = os.path.join(self.executor_src_root, "review.example.com",
1604 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001605 if os.path.exists(path):
1606 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001607
1608 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001609 A.addApproval('Code-Review', 2)
1610 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001611 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001612 self.assertEqual(self.getJobFromHistory('project-merge').result,
1613 'SUCCESS')
1614 self.assertEqual(self.getJobFromHistory('project-test1').result,
1615 'SUCCESS')
1616 self.assertEqual(self.getJobFromHistory('project-test2').result,
1617 'SUCCESS')
1618 self.assertEqual(A.data['status'], 'MERGED')
1619 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001620
James E. Blair4886f282012-11-15 09:27:33 -08001621 def test_merger_repack_large_change(self):
1622 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001623 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001624 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001625 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001626 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001627 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001628 self.executor_server.merger._addProject('review.example.com',
James E. Blair197e8202017-06-09 12:54:28 -07001629 'org/project', url, None)
James E. Blair6df67132017-04-19 13:53:20 -07001630 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001631 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001632 # TODOv3(jeblair): add hostname to upstream root
1633 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001634 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001635 path = os.path.join(self.merger_src_root, 'review.example.com',
1636 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001637 if os.path.exists(path):
1638 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001639 path = os.path.join(self.executor_src_root, 'review.example.com',
1640 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001641 if os.path.exists(path):
1642 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001643
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001644 A.addApproval('Code-Review', 2)
1645 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001646 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001647 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001648 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001649 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001650 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001651 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001652 'SUCCESS')
1653 self.assertEqual(A.data['status'], 'MERGED')
1654 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001655
James E. Blair2fa50962013-01-30 21:50:41 -08001656 def test_new_patchset_dequeues_old(self):
1657 "Test that a new patchset causes the old to be dequeued"
1658 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001659 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001660 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1661 M.setMerged()
1662
1663 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1664 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1665 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1666 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001667 A.addApproval('Code-Review', 2)
1668 B.addApproval('Code-Review', 2)
1669 C.addApproval('Code-Review', 2)
1670 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001671
1672 C.setDependsOn(B, 1)
1673 B.setDependsOn(A, 1)
1674 A.setDependsOn(M, 1)
1675
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001676 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1677 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1678 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1679 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001680 self.waitUntilSettled()
1681
1682 B.addPatchset()
1683 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1684 self.waitUntilSettled()
1685
Paul Belanger174a8272017-03-14 13:20:10 -04001686 self.executor_server.hold_jobs_in_build = False
1687 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001688 self.waitUntilSettled()
1689
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001690 self.assertEqual(A.data['status'], 'MERGED')
1691 self.assertEqual(A.reported, 2)
1692 self.assertEqual(B.data['status'], 'NEW')
1693 self.assertEqual(B.reported, 2)
1694 self.assertEqual(C.data['status'], 'NEW')
1695 self.assertEqual(C.reported, 2)
1696 self.assertEqual(D.data['status'], 'MERGED')
1697 self.assertEqual(D.reported, 2)
1698 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001699
James E. Blairba437362015-02-07 11:41:52 -08001700 def test_new_patchset_check(self):
1701 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001702
Paul Belanger174a8272017-03-14 13:20:10 -04001703 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001704
1705 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001706 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001707 tenant = self.sched.abide.tenants.get('tenant-one')
1708 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001709
1710 # Add two git-dependent changes
1711 B.setDependsOn(A, 1)
1712 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1713 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001714 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1715 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001716
1717 # A live item, and a non-live/live pair
1718 items = check_pipeline.getAllItems()
1719 self.assertEqual(len(items), 3)
1720
1721 self.assertEqual(items[0].change.number, '1')
1722 self.assertEqual(items[0].change.patchset, '1')
1723 self.assertFalse(items[0].live)
1724
1725 self.assertEqual(items[1].change.number, '2')
1726 self.assertEqual(items[1].change.patchset, '1')
1727 self.assertTrue(items[1].live)
1728
1729 self.assertEqual(items[2].change.number, '1')
1730 self.assertEqual(items[2].change.patchset, '1')
1731 self.assertTrue(items[2].live)
1732
1733 # Add a new patchset to A
1734 A.addPatchset()
1735 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1736 self.waitUntilSettled()
1737
1738 # The live copy of A,1 should be gone, but the non-live and B
1739 # should continue, and we should have a new A,2
1740 items = check_pipeline.getAllItems()
1741 self.assertEqual(len(items), 3)
1742
1743 self.assertEqual(items[0].change.number, '1')
1744 self.assertEqual(items[0].change.patchset, '1')
1745 self.assertFalse(items[0].live)
1746
1747 self.assertEqual(items[1].change.number, '2')
1748 self.assertEqual(items[1].change.patchset, '1')
1749 self.assertTrue(items[1].live)
1750
1751 self.assertEqual(items[2].change.number, '1')
1752 self.assertEqual(items[2].change.patchset, '2')
1753 self.assertTrue(items[2].live)
1754
1755 # Add a new patchset to B
1756 B.addPatchset()
1757 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1758 self.waitUntilSettled()
1759
1760 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1761 # but we should have a new B,2 (still based on A,1)
1762 items = check_pipeline.getAllItems()
1763 self.assertEqual(len(items), 3)
1764
1765 self.assertEqual(items[0].change.number, '1')
1766 self.assertEqual(items[0].change.patchset, '2')
1767 self.assertTrue(items[0].live)
1768
1769 self.assertEqual(items[1].change.number, '1')
1770 self.assertEqual(items[1].change.patchset, '1')
1771 self.assertFalse(items[1].live)
1772
1773 self.assertEqual(items[2].change.number, '2')
1774 self.assertEqual(items[2].change.patchset, '2')
1775 self.assertTrue(items[2].live)
1776
1777 self.builds[0].release()
1778 self.waitUntilSettled()
1779 self.builds[0].release()
1780 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001781 self.executor_server.hold_jobs_in_build = False
1782 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001783 self.waitUntilSettled()
1784
1785 self.assertEqual(A.reported, 1)
1786 self.assertEqual(B.reported, 1)
1787 self.assertEqual(self.history[0].result, 'ABORTED')
1788 self.assertEqual(self.history[0].changes, '1,1')
1789 self.assertEqual(self.history[1].result, 'ABORTED')
1790 self.assertEqual(self.history[1].changes, '1,1 2,1')
1791 self.assertEqual(self.history[2].result, 'SUCCESS')
1792 self.assertEqual(self.history[2].changes, '1,2')
1793 self.assertEqual(self.history[3].result, 'SUCCESS')
1794 self.assertEqual(self.history[3].changes, '1,1 2,2')
1795
1796 def test_abandoned_gate(self):
1797 "Test that an abandoned change is dequeued from gate"
1798
Paul Belanger174a8272017-03-14 13:20:10 -04001799 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001800
1801 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001802 A.addApproval('Code-Review', 2)
1803 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001804 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001805 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1806 self.assertEqual(self.builds[0].name, 'project-merge')
1807
1808 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1809 self.waitUntilSettled()
1810
Paul Belanger174a8272017-03-14 13:20:10 -04001811 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001812 self.waitUntilSettled()
1813
Arie5b4048c2016-10-05 16:12:06 +03001814 self.assertBuilds([])
1815 self.assertHistory([
1816 dict(name='project-merge', result='ABORTED', changes='1,1')],
1817 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001818 self.assertEqual(A.reported, 1,
1819 "Abandoned gate change should report only start")
1820
1821 def test_abandoned_check(self):
1822 "Test that an abandoned change is dequeued from check"
1823
Paul Belanger174a8272017-03-14 13:20:10 -04001824 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001825
1826 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1827 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001828 tenant = self.sched.abide.tenants.get('tenant-one')
1829 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001830
1831 # Add two git-dependent changes
1832 B.setDependsOn(A, 1)
1833 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1834 self.waitUntilSettled()
1835 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1836 self.waitUntilSettled()
1837 # A live item, and a non-live/live pair
1838 items = check_pipeline.getAllItems()
1839 self.assertEqual(len(items), 3)
1840
1841 self.assertEqual(items[0].change.number, '1')
1842 self.assertFalse(items[0].live)
1843
1844 self.assertEqual(items[1].change.number, '2')
1845 self.assertTrue(items[1].live)
1846
1847 self.assertEqual(items[2].change.number, '1')
1848 self.assertTrue(items[2].live)
1849
1850 # Abandon A
1851 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1852 self.waitUntilSettled()
1853
1854 # The live copy of A should be gone, but the non-live and B
1855 # should continue
1856 items = check_pipeline.getAllItems()
1857 self.assertEqual(len(items), 2)
1858
1859 self.assertEqual(items[0].change.number, '1')
1860 self.assertFalse(items[0].live)
1861
1862 self.assertEqual(items[1].change.number, '2')
1863 self.assertTrue(items[1].live)
1864
Paul Belanger174a8272017-03-14 13:20:10 -04001865 self.executor_server.hold_jobs_in_build = False
1866 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001867 self.waitUntilSettled()
1868
1869 self.assertEqual(len(self.history), 4)
1870 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001871 'Build should have been aborted')
1872 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001873 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001874
Steve Varnau7b78b312015-04-03 14:49:46 -07001875 def test_abandoned_not_timer(self):
1876 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001877 # This test can not use simple_layout because it must start
1878 # with a configuration which does not include a
1879 # timer-triggered job so that we have an opportunity to set
1880 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001881 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001882 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001883 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001884 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001885 # The pipeline triggers every second, so we should have seen
1886 # several by now.
1887 time.sleep(5)
1888 self.waitUntilSettled()
1889 # Stop queuing timer triggered jobs so that the assertions
1890 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001891 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001892 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001893 self.sched.reconfigure(self.config)
James E. Blair0b137b42017-07-27 08:51:25 -07001894 self.waitUntilSettled()
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001895
James E. Blair21037782017-07-19 11:56:55 -07001896 self.assertEqual(len(self.builds), 1, "One timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001897
1898 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1899 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1900 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07001901 self.assertEqual(len(self.builds), 2, "One change plus one timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001902
1903 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1904 self.waitUntilSettled()
1905
James E. Blair21037782017-07-19 11:56:55 -07001906 self.assertEqual(len(self.builds), 1, "One timer job remains")
Steve Varnau7b78b312015-04-03 14:49:46 -07001907
Paul Belanger174a8272017-03-14 13:20:10 -04001908 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001909 self.waitUntilSettled()
1910
James E. Blair2fa50962013-01-30 21:50:41 -08001911 def test_new_patchset_dequeues_old_on_head(self):
1912 "Test that a new patchset causes the old to be dequeued (at head)"
1913 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001914 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001915 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1916 M.setMerged()
1917 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1918 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1919 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1920 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001921 A.addApproval('Code-Review', 2)
1922 B.addApproval('Code-Review', 2)
1923 C.addApproval('Code-Review', 2)
1924 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001925
1926 C.setDependsOn(B, 1)
1927 B.setDependsOn(A, 1)
1928 A.setDependsOn(M, 1)
1929
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001930 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1931 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1932 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1933 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001934 self.waitUntilSettled()
1935
1936 A.addPatchset()
1937 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1938 self.waitUntilSettled()
1939
Paul Belanger174a8272017-03-14 13:20:10 -04001940 self.executor_server.hold_jobs_in_build = False
1941 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001942 self.waitUntilSettled()
1943
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001944 self.assertEqual(A.data['status'], 'NEW')
1945 self.assertEqual(A.reported, 2)
1946 self.assertEqual(B.data['status'], 'NEW')
1947 self.assertEqual(B.reported, 2)
1948 self.assertEqual(C.data['status'], 'NEW')
1949 self.assertEqual(C.reported, 2)
1950 self.assertEqual(D.data['status'], 'MERGED')
1951 self.assertEqual(D.reported, 2)
1952 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001953
1954 def test_new_patchset_dequeues_old_without_dependents(self):
1955 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001956 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001957 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1958 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1959 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001960 A.addApproval('Code-Review', 2)
1961 B.addApproval('Code-Review', 2)
1962 C.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001963
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001964 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1965 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1966 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001967 self.waitUntilSettled()
1968
1969 B.addPatchset()
1970 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1971 self.waitUntilSettled()
1972
Paul Belanger174a8272017-03-14 13:20:10 -04001973 self.executor_server.hold_jobs_in_build = False
1974 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001975 self.waitUntilSettled()
1976
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001977 self.assertEqual(A.data['status'], 'MERGED')
1978 self.assertEqual(A.reported, 2)
1979 self.assertEqual(B.data['status'], 'NEW')
1980 self.assertEqual(B.reported, 2)
1981 self.assertEqual(C.data['status'], 'MERGED')
1982 self.assertEqual(C.reported, 2)
1983 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001984
1985 def test_new_patchset_dequeues_old_independent_queue(self):
1986 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001987 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001988 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1989 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1990 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1991 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1992 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1993 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1994 self.waitUntilSettled()
1995
1996 B.addPatchset()
1997 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1998 self.waitUntilSettled()
1999
Paul Belanger174a8272017-03-14 13:20:10 -04002000 self.executor_server.hold_jobs_in_build = False
2001 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002002 self.waitUntilSettled()
2003
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002004 self.assertEqual(A.data['status'], 'NEW')
2005 self.assertEqual(A.reported, 1)
2006 self.assertEqual(B.data['status'], 'NEW')
2007 self.assertEqual(B.reported, 1)
2008 self.assertEqual(C.data['status'], 'NEW')
2009 self.assertEqual(C.reported, 1)
2010 self.assertEqual(len(self.history), 10)
2011 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002012
James E. Blaircc020532017-04-19 13:04:44 -07002013 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07002014 def test_noop_job(self):
2015 "Test that the internal noop job works"
2016 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002017 A.addApproval('Code-Review', 2)
2018 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07002019 self.waitUntilSettled()
2020
2021 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2022 self.assertTrue(self.sched._areAllBuildsComplete())
2023 self.assertEqual(len(self.history), 0)
2024 self.assertEqual(A.data['status'], 'MERGED')
2025 self.assertEqual(A.reported, 2)
2026
James E. Blair06cc3922017-04-19 10:08:10 -07002027 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002028 def test_no_job_project(self):
2029 "Test that reports with no jobs don't get sent"
2030 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
2031 'master', 'A')
2032 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2033 self.waitUntilSettled()
2034
2035 # Change wasn't reported to
2036 self.assertEqual(A.reported, False)
2037
2038 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08002039 tenant = self.sched.abide.tenants.get('tenant-one')
2040 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002041 items = check_pipeline.getAllItems()
2042 self.assertEqual(len(items), 0)
2043
2044 self.assertEqual(len(self.history), 0)
2045
James E. Blair7d0dedc2013-02-21 17:26:09 -08002046 def test_zuul_refs(self):
2047 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04002048 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002049 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2050 M1.setMerged()
2051 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2052 M2.setMerged()
2053
2054 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2055 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2056 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2057 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002058 A.addApproval('Code-Review', 2)
2059 B.addApproval('Code-Review', 2)
2060 C.addApproval('Code-Review', 2)
2061 D.addApproval('Code-Review', 2)
2062 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2063 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2064 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2065 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002066
2067 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002068 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002069 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002070 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002071 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002072 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002073 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002074 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002075 self.waitUntilSettled()
2076
Clint Byrum3343e3e2016-11-15 16:05:03 -08002077 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002078 for x in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07002079 if x.parameters['zuul']['change'] == '3':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002080 a_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002081 elif x.parameters['zuul']['change'] == '4':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002082 b_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002083 elif x.parameters['zuul']['change'] == '5':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002084 c_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002085 elif x.parameters['zuul']['change'] == '6':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002086 d_build = x
2087 if a_build and b_build and c_build and d_build:
2088 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002089
Clint Byrum3343e3e2016-11-15 16:05:03 -08002090 # should have a, not b, and should not be in project2
2091 self.assertTrue(a_build.hasChanges(A))
2092 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002093
Clint Byrum3343e3e2016-11-15 16:05:03 -08002094 # should have a and b, and should not be in project2
2095 self.assertTrue(b_build.hasChanges(A, B))
2096 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002097
Clint Byrum3343e3e2016-11-15 16:05:03 -08002098 # should have a and b in 1, c in 2
2099 self.assertTrue(c_build.hasChanges(A, B, C))
2100 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002101
Clint Byrum3343e3e2016-11-15 16:05:03 -08002102 # should have a and b in 1, c and d in 2
2103 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002104
Paul Belanger174a8272017-03-14 13:20:10 -04002105 self.executor_server.hold_jobs_in_build = False
2106 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002107 self.waitUntilSettled()
2108
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002109 self.assertEqual(A.data['status'], 'MERGED')
2110 self.assertEqual(A.reported, 2)
2111 self.assertEqual(B.data['status'], 'MERGED')
2112 self.assertEqual(B.reported, 2)
2113 self.assertEqual(C.data['status'], 'MERGED')
2114 self.assertEqual(C.reported, 2)
2115 self.assertEqual(D.data['status'], 'MERGED')
2116 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002117
James E. Blair4a28a882013-08-23 15:17:33 -07002118 def test_rerun_on_error(self):
2119 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002120 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002121 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002122 A.addApproval('Code-Review', 2)
2123 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002124 self.waitUntilSettled()
2125
James E. Blair412fba82017-01-26 15:00:50 -08002126 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002127 self.executor_server.hold_jobs_in_build = False
2128 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002129 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002130 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002131 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2132
James E. Blair412e5582013-04-22 15:50:12 -07002133 def test_statsd(self):
2134 "Test each of the statsd methods used in the scheduler"
2135 import extras
2136 statsd = extras.try_import('statsd.statsd')
2137 statsd.incr('test-incr')
2138 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002139 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002140 self.assertReportedStat('test-incr', '1|c')
2141 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002142 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002143
James E. Blairdad52252014-02-07 16:59:17 -08002144 def test_stuck_job_cleanup(self):
2145 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002146
2147 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002148 self.gearman_server.hold_jobs_in_queue = True
2149 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002150 A.addApproval('Code-Review', 2)
2151 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002152 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002153 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002154 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2155
James E. Blair31cb3e82017-04-20 13:08:56 -07002156 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002157 self.sched.reconfigure(self.config)
2158 self.waitUntilSettled()
2159
James E. Blair18c64442014-03-18 10:14:45 -07002160 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002161 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002162 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002163 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2164 self.assertTrue(self.sched._areAllBuildsComplete())
2165
2166 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002167 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002168 self.assertEqual(self.history[0].result, 'SUCCESS')
2169
James E. Blair879dafb2015-07-17 14:04:49 -07002170 def test_file_head(self):
2171 # This is a regression test for an observed bug. A change
2172 # with a file named "HEAD" in the root directory of the repo
2173 # was processed by a merger. It then was unable to reset the
2174 # repo because of:
2175 # GitCommandError: 'git reset --hard HEAD' returned
2176 # with exit code 128
2177 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2178 # and filename
2179 # Use '--' to separate filenames from revisions'
2180
2181 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002182 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002183 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2184
2185 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2186 self.waitUntilSettled()
2187
2188 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2189 self.waitUntilSettled()
2190
2191 self.assertIn('Build succeeded', A.messages[0])
2192 self.assertIn('Build succeeded', B.messages[0])
2193
James E. Blair70c71582013-03-06 08:50:50 -08002194 def test_file_jobs(self):
2195 "Test that file jobs run only when appropriate"
2196 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002197 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002198 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002199 A.addApproval('Code-Review', 2)
2200 B.addApproval('Code-Review', 2)
2201 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2202 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002203 self.waitUntilSettled()
2204
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002205 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002206 if x.name == 'project-testfile']
2207
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002208 self.assertEqual(len(testfile_jobs), 1)
2209 self.assertEqual(testfile_jobs[0].changes, '1,2')
2210 self.assertEqual(A.data['status'], 'MERGED')
2211 self.assertEqual(A.reported, 2)
2212 self.assertEqual(B.data['status'], 'MERGED')
2213 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002214
Clint Byrum3ee39f32016-11-17 23:45:07 -08002215 def _test_irrelevant_files_jobs(self, should_skip):
2216 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002217 if should_skip:
2218 files = {'ignoreme': 'ignored\n'}
2219 else:
2220 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002221
2222 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002223 'master',
2224 'test irrelevant-files',
2225 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002226 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2227 self.waitUntilSettled()
2228
2229 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002230 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002231
2232 if should_skip:
2233 self.assertEqual([], tested_change_ids)
2234 else:
2235 self.assertIn(change.data['number'], tested_change_ids)
2236
James E. Blaircc324192017-04-19 14:36:08 -07002237 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002238 def test_irrelevant_files_match_skips_job(self):
2239 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002240
James E. Blaircc324192017-04-19 14:36:08 -07002241 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002242 def test_irrelevant_files_no_match_runs_job(self):
2243 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002244
James E. Blaircc324192017-04-19 14:36:08 -07002245 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002246 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002247 files = {'ignoreme': 'ignored\n'}
2248
2249 change = self.fake_gerrit.addFakeChange('org/project',
2250 'master',
2251 'test irrelevant-files',
2252 files=files)
2253 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2254 self.waitUntilSettled()
2255
2256 run_jobs = set([build.name for build in self.history])
2257
2258 self.assertEqual(set(['project-test-nomatch-starts-empty',
2259 'project-test-nomatch-starts-full']), run_jobs)
2260
James E. Blairc8a1e052014-02-25 09:29:26 -08002261 def test_queue_names(self):
2262 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002263 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002264 (trusted, project1) = tenant.getProject('org/project1')
2265 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002266 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2267 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2268 self.assertEqual(q1.name, 'integrated')
2269 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002270
James E. Blair64ed6f22013-07-10 14:07:23 -07002271 def test_queue_precedence(self):
2272 "Test that queue precedence works"
2273
2274 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002275 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002276 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2277 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002278 A.addApproval('Code-Review', 2)
2279 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002280
2281 self.waitUntilSettled()
2282 self.gearman_server.hold_jobs_in_queue = False
2283 self.gearman_server.release()
2284 self.waitUntilSettled()
2285
James E. Blair8de58bd2013-07-18 16:23:33 -07002286 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002287 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002288 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002289 self.waitUntilSettled()
2290
James E. Blair64ed6f22013-07-10 14:07:23 -07002291 self.log.debug(self.history)
2292 self.assertEqual(self.history[0].pipeline, 'gate')
2293 self.assertEqual(self.history[1].pipeline, 'check')
2294 self.assertEqual(self.history[2].pipeline, 'gate')
2295 self.assertEqual(self.history[3].pipeline, 'gate')
2296 self.assertEqual(self.history[4].pipeline, 'check')
2297 self.assertEqual(self.history[5].pipeline, 'check')
2298
Clark Boylana5edbe42014-06-03 16:39:10 -07002299 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002300 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002301 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002302 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002303 A.addApproval('Code-Review', 2)
2304 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002305 self.waitUntilSettled()
2306
Paul Belanger174a8272017-03-14 13:20:10 -04002307 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002308 self.waitUntilSettled()
2309
James E. Blair1843a552013-07-03 14:19:52 -07002310 port = self.webapp.server.socket.getsockname()[1]
2311
Adam Gandelman77a12c72017-02-03 17:43:43 -08002312 req = urllib.request.Request(
2313 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002314 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002315 headers = f.info()
2316 self.assertIn('Content-Length', headers)
2317 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002318 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2319 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002320 self.assertIn('Access-Control-Allow-Origin', headers)
2321 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002322 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002323 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002324 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002325
Paul Belanger174a8272017-03-14 13:20:10 -04002326 self.executor_server.hold_jobs_in_build = False
2327 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002328 self.waitUntilSettled()
2329
2330 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002331 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002332 for p in data['pipelines']:
2333 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002334 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002335 self.assertEqual(q['window'], 20)
2336 else:
2337 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002338 for head in q['heads']:
2339 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002340 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002341 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002342 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002343 status_jobs.append(job)
2344 self.assertEqual('project-merge', status_jobs[0]['name'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002345 # TODO(mordred) pull uuids from self.builds
Monty Taylor51139a02016-05-24 11:28:10 -05002346 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002347 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2348 uuid=status_jobs[0]['uuid']),
2349 status_jobs[0]['url'])
2350 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002351 'finger://{hostname}/{uuid}'.format(
2352 hostname=self.executor_server.hostname,
2353 uuid=status_jobs[0]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002354 status_jobs[0]['finger_url'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002355 # TOOD(mordred) configure a success-url on the base job
Monty Taylor51139a02016-05-24 11:28:10 -05002356 self.assertEqual(
2357 'finger://{hostname}/{uuid}'.format(
2358 hostname=self.executor_server.hostname,
2359 uuid=status_jobs[0]['uuid']),
2360 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002361 self.assertEqual('project-test1', status_jobs[1]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002362 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002363 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2364 uuid=status_jobs[1]['uuid']),
2365 status_jobs[1]['url'])
2366 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002367 'finger://{hostname}/{uuid}'.format(
2368 hostname=self.executor_server.hostname,
2369 uuid=status_jobs[1]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002370 status_jobs[1]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002371 self.assertEqual(
2372 'finger://{hostname}/{uuid}'.format(
2373 hostname=self.executor_server.hostname,
2374 uuid=status_jobs[1]['uuid']),
2375 status_jobs[1]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002376
2377 self.assertEqual('project-test2', status_jobs[2]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002378 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002379 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2380 uuid=status_jobs[2]['uuid']),
2381 status_jobs[2]['url'])
2382 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002383 'finger://{hostname}/{uuid}'.format(
2384 hostname=self.executor_server.hostname,
2385 uuid=status_jobs[2]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002386 status_jobs[2]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002387 self.assertEqual(
2388 'finger://{hostname}/{uuid}'.format(
2389 hostname=self.executor_server.hostname,
2390 uuid=status_jobs[2]['uuid']),
2391 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002392
Tobias Henkel65639f82017-07-10 10:25:42 +02002393 # check job dependencies
2394 self.assertIsNotNone(status_jobs[0]['dependencies'])
2395 self.assertIsNotNone(status_jobs[1]['dependencies'])
2396 self.assertIsNotNone(status_jobs[2]['dependencies'])
2397 self.assertEqual(len(status_jobs[0]['dependencies']), 0)
2398 self.assertEqual(len(status_jobs[1]['dependencies']), 1)
2399 self.assertEqual(len(status_jobs[2]['dependencies']), 1)
2400 self.assertIn('project-merge', status_jobs[1]['dependencies'])
2401 self.assertIn('project-merge', status_jobs[2]['dependencies'])
2402
James E. Blaircdccd972013-07-01 12:10:22 -07002403 def test_live_reconfiguration(self):
2404 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002405 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002406 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002407 A.addApproval('Code-Review', 2)
2408 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002409 self.waitUntilSettled()
2410
2411 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002412 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002413
Paul Belanger174a8272017-03-14 13:20:10 -04002414 self.executor_server.hold_jobs_in_build = False
2415 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002416 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002417 self.assertEqual(self.getJobFromHistory('project-merge').result,
2418 'SUCCESS')
2419 self.assertEqual(self.getJobFromHistory('project-test1').result,
2420 'SUCCESS')
2421 self.assertEqual(self.getJobFromHistory('project-test2').result,
2422 'SUCCESS')
2423 self.assertEqual(A.data['status'], 'MERGED')
2424 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002425
James E. Blair6bc782d2015-07-17 16:20:21 -07002426 def test_live_reconfiguration_merge_conflict(self):
2427 # A real-world bug: a change in a gate queue has a merge
2428 # conflict and a job is added to its project while it's
2429 # sitting in the queue. The job gets added to the change and
2430 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002431 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002432
2433 # This change is fine. It's here to stop the queue long
2434 # enough for the next change to be subject to the
2435 # reconfiguration, as well as to provide a conflict for the
2436 # next change. This change will succeed and merge.
2437 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002438 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002439 A.addApproval('Code-Review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002440
2441 # This change will be in merge conflict. During the
2442 # reconfiguration, we will add a job. We want to make sure
2443 # that doesn't cause it to get stuck.
2444 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002445 B.addPatchset({'conflict': 'B'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002446 B.addApproval('Code-Review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002447
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002448 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2449 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002450
2451 self.waitUntilSettled()
2452
2453 # No jobs have run yet
2454 self.assertEqual(A.data['status'], 'NEW')
2455 self.assertEqual(A.reported, 1)
2456 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002457 self.assertEqual(len(self.history), 0)
2458
2459 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002460 self.commitConfigUpdate('common-config',
2461 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002462 self.sched.reconfigure(self.config)
2463 self.waitUntilSettled()
2464
Paul Belanger174a8272017-03-14 13:20:10 -04002465 self.executor_server.hold_jobs_in_build = False
2466 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002467 self.waitUntilSettled()
2468
2469 self.assertEqual(A.data['status'], 'MERGED')
2470 self.assertEqual(A.reported, 2)
2471 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002472 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002473 self.assertEqual(self.getJobFromHistory('project-merge').result,
2474 'SUCCESS')
2475 self.assertEqual(self.getJobFromHistory('project-test1').result,
2476 'SUCCESS')
2477 self.assertEqual(self.getJobFromHistory('project-test2').result,
2478 'SUCCESS')
2479 self.assertEqual(self.getJobFromHistory('project-test3').result,
2480 'SUCCESS')
2481 self.assertEqual(len(self.history), 4)
2482
James E. Blair400e8fd2015-07-30 17:44:45 -07002483 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002484 # An extrapolation of test_live_reconfiguration_merge_conflict
2485 # that tests a job added to a job tree with a failed root does
2486 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002487 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002488
2489 # This change is fine. It's here to stop the queue long
2490 # enough for the next change to be subject to the
2491 # reconfiguration. This change will succeed and merge.
2492 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002493 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002494 A.addApproval('Code-Review', 2)
2495 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002496 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002497 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002498 self.waitUntilSettled()
2499
2500 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002501 self.executor_server.failJob('project-merge', B)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002502 B.addApproval('Code-Review', 2)
2503 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002504 self.waitUntilSettled()
2505
Paul Belanger174a8272017-03-14 13:20:10 -04002506 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002507 self.waitUntilSettled()
2508
2509 # Both -merge jobs have run, but no others.
2510 self.assertEqual(A.data['status'], 'NEW')
2511 self.assertEqual(A.reported, 1)
2512 self.assertEqual(B.data['status'], 'NEW')
2513 self.assertEqual(B.reported, 1)
2514 self.assertEqual(self.history[0].result, 'SUCCESS')
2515 self.assertEqual(self.history[0].name, 'project-merge')
2516 self.assertEqual(self.history[1].result, 'FAILURE')
2517 self.assertEqual(self.history[1].name, 'project-merge')
2518 self.assertEqual(len(self.history), 2)
2519
2520 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002521 self.commitConfigUpdate('common-config',
2522 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002523 self.sched.reconfigure(self.config)
2524 self.waitUntilSettled()
2525
Paul Belanger174a8272017-03-14 13:20:10 -04002526 self.executor_server.hold_jobs_in_build = False
2527 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002528 self.waitUntilSettled()
2529
2530 self.assertEqual(A.data['status'], 'MERGED')
2531 self.assertEqual(A.reported, 2)
2532 self.assertEqual(B.data['status'], 'NEW')
2533 self.assertEqual(B.reported, 2)
2534 self.assertEqual(self.history[0].result, 'SUCCESS')
2535 self.assertEqual(self.history[0].name, 'project-merge')
2536 self.assertEqual(self.history[1].result, 'FAILURE')
2537 self.assertEqual(self.history[1].name, 'project-merge')
2538 self.assertEqual(self.history[2].result, 'SUCCESS')
2539 self.assertEqual(self.history[3].result, 'SUCCESS')
2540 self.assertEqual(self.history[4].result, 'SUCCESS')
2541 self.assertEqual(len(self.history), 5)
2542
James E. Blair400e8fd2015-07-30 17:44:45 -07002543 def test_live_reconfiguration_failed_job(self):
2544 # Test that a change with a removed failing job does not
2545 # disrupt reconfiguration. If a change has a failed job and
2546 # that job is removed during a reconfiguration, we observed a
2547 # bug where the code to re-set build statuses would run on
2548 # that build and raise an exception because the job no longer
2549 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002550 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002551
2552 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2553
2554 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002555 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002556
2557 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2558 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002559 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002560 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002561 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002562 self.waitUntilSettled()
2563
2564 self.assertEqual(A.data['status'], 'NEW')
2565 self.assertEqual(A.reported, 0)
2566
2567 self.assertEqual(self.getJobFromHistory('project-merge').result,
2568 'SUCCESS')
2569 self.assertEqual(self.getJobFromHistory('project-test1').result,
2570 'FAILURE')
2571 self.assertEqual(len(self.history), 2)
2572
2573 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002574 self.commitConfigUpdate('common-config',
2575 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002576 self.sched.reconfigure(self.config)
2577 self.waitUntilSettled()
2578
Paul Belanger174a8272017-03-14 13:20:10 -04002579 self.executor_server.hold_jobs_in_build = False
2580 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002581 self.waitUntilSettled()
2582
2583 self.assertEqual(self.getJobFromHistory('project-test2').result,
2584 'SUCCESS')
2585 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2586 'SUCCESS')
2587 self.assertEqual(len(self.history), 4)
2588
2589 self.assertEqual(A.data['status'], 'NEW')
2590 self.assertEqual(A.reported, 1)
2591 self.assertIn('Build succeeded', A.messages[0])
2592 # Ensure the removed job was not included in the report.
2593 self.assertNotIn('project-test1', A.messages[0])
2594
James E. Blairfe707d12015-08-05 15:18:15 -07002595 def test_live_reconfiguration_shared_queue(self):
2596 # Test that a change with a failing job which was removed from
2597 # this project but otherwise still exists in the system does
2598 # not disrupt reconfiguration.
2599
Paul Belanger174a8272017-03-14 13:20:10 -04002600 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002601
2602 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2603
Paul Belanger174a8272017-03-14 13:20:10 -04002604 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002605
2606 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2607 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002608 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002609 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002610 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002611 self.waitUntilSettled()
2612
2613 self.assertEqual(A.data['status'], 'NEW')
2614 self.assertEqual(A.reported, 0)
2615
Clint Byrum69663d42017-05-01 14:51:04 -07002616 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002617 'SUCCESS')
2618 self.assertEqual(self.getJobFromHistory(
2619 'project1-project2-integration').result, 'FAILURE')
2620 self.assertEqual(len(self.history), 2)
2621
2622 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002623 self.commitConfigUpdate(
2624 'common-config',
2625 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002626 self.sched.reconfigure(self.config)
2627 self.waitUntilSettled()
2628
Paul Belanger174a8272017-03-14 13:20:10 -04002629 self.executor_server.hold_jobs_in_build = False
2630 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002631 self.waitUntilSettled()
2632
Clint Byrum69663d42017-05-01 14:51:04 -07002633 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002634 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002635 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002636 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002637 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002638 'SUCCESS')
2639 self.assertEqual(self.getJobFromHistory(
2640 'project1-project2-integration').result, 'FAILURE')
2641 self.assertEqual(len(self.history), 4)
2642
2643 self.assertEqual(A.data['status'], 'NEW')
2644 self.assertEqual(A.reported, 1)
2645 self.assertIn('Build succeeded', A.messages[0])
2646 # Ensure the removed job was not included in the report.
2647 self.assertNotIn('project1-project2-integration', A.messages[0])
2648
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002649 def test_double_live_reconfiguration_shared_queue(self):
2650 # This was a real-world regression. A change is added to
2651 # gate; a reconfigure happens, a second change which depends
2652 # on the first is added, and a second reconfiguration happens.
2653 # Ensure that both changes merge.
2654
2655 # A failure may indicate incorrect caching or cleaning up of
2656 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002657 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002658
2659 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2660 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2661 B.setDependsOn(A, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002662 A.addApproval('Code-Review', 2)
2663 B.addApproval('Code-Review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002664
2665 # Add the parent change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002666 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002667 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002668 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002669 self.waitUntilSettled()
2670
2671 # Reconfigure (with only one change in the pipeline).
2672 self.sched.reconfigure(self.config)
2673 self.waitUntilSettled()
2674
2675 # Add the child change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002676 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002677 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002678 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002679 self.waitUntilSettled()
2680
2681 # Reconfigure (with both in the pipeline).
2682 self.sched.reconfigure(self.config)
2683 self.waitUntilSettled()
2684
Paul Belanger174a8272017-03-14 13:20:10 -04002685 self.executor_server.hold_jobs_in_build = False
2686 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002687 self.waitUntilSettled()
2688
2689 self.assertEqual(len(self.history), 8)
2690
2691 self.assertEqual(A.data['status'], 'MERGED')
2692 self.assertEqual(A.reported, 2)
2693 self.assertEqual(B.data['status'], 'MERGED')
2694 self.assertEqual(B.reported, 2)
2695
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002696 def test_live_reconfiguration_del_project(self):
2697 # Test project deletion from layout
2698 # while changes are enqueued
2699
Paul Belanger174a8272017-03-14 13:20:10 -04002700 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002701 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2702 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2703 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2704
2705 # A Depends-On: B
2706 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2707 A.subject, B.data['id'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002708 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002709
2710 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2711 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2712 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002713 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002714 self.waitUntilSettled()
2715 self.assertEqual(len(self.builds), 5)
2716
2717 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002718 self.commitConfigUpdate(
2719 'common-config',
2720 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002721 self.sched.reconfigure(self.config)
2722 self.waitUntilSettled()
2723
2724 # Builds for C aborted, builds for A succeed,
2725 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002726 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002727 self.assertEqual(job_c.changes, '3,1')
2728 self.assertEqual(job_c.result, 'ABORTED')
2729
Paul Belanger174a8272017-03-14 13:20:10 -04002730 self.executor_server.hold_jobs_in_build = False
2731 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002732 self.waitUntilSettled()
2733
Clint Byrum0149ada2017-04-04 12:22:01 -07002734 self.assertEqual(
2735 self.getJobFromHistory('project-test1', 'org/project').changes,
2736 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002737
2738 self.assertEqual(A.data['status'], 'NEW')
2739 self.assertEqual(B.data['status'], 'NEW')
2740 self.assertEqual(C.data['status'], 'NEW')
2741 self.assertEqual(A.reported, 1)
2742 self.assertEqual(B.reported, 0)
2743 self.assertEqual(C.reported, 0)
2744
Clint Byrum0149ada2017-04-04 12:22:01 -07002745 tenant = self.sched.abide.tenants.get('tenant-one')
2746 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002747 self.assertIn('Build succeeded', A.messages[0])
2748
James E. Blairec056492016-07-22 09:45:56 -07002749 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002750 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002751 self.updateConfigLayout(
2752 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002753 self.sched.reconfigure(self.config)
2754
2755 self.init_repo("org/new-project")
2756 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2757
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002758 A.addApproval('Code-Review', 2)
2759 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002760 self.waitUntilSettled()
2761 self.assertEqual(self.getJobFromHistory('project-merge').result,
2762 'SUCCESS')
2763 self.assertEqual(self.getJobFromHistory('project-test1').result,
2764 'SUCCESS')
2765 self.assertEqual(self.getJobFromHistory('project-test2').result,
2766 'SUCCESS')
2767 self.assertEqual(A.data['status'], 'MERGED')
2768 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002769
James E. Blair9ea70072017-04-19 16:05:30 -07002770 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002771 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002772 self.init_repo("org/delete-project")
2773 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2774
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002775 A.addApproval('Code-Review', 2)
2776 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002777 self.waitUntilSettled()
2778 self.assertEqual(self.getJobFromHistory('project-merge').result,
2779 'SUCCESS')
2780 self.assertEqual(self.getJobFromHistory('project-test1').result,
2781 'SUCCESS')
2782 self.assertEqual(self.getJobFromHistory('project-test2').result,
2783 'SUCCESS')
2784 self.assertEqual(A.data['status'], 'MERGED')
2785 self.assertEqual(A.reported, 2)
2786
2787 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002788 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002789 if os.path.exists(os.path.join(self.merger_src_root, p)):
2790 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002791 if os.path.exists(os.path.join(self.executor_src_root, p)):
2792 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002793
2794 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2795
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002796 B.addApproval('Code-Review', 2)
2797 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002798 self.waitUntilSettled()
2799 self.assertEqual(self.getJobFromHistory('project-merge').result,
2800 'SUCCESS')
2801 self.assertEqual(self.getJobFromHistory('project-test1').result,
2802 'SUCCESS')
2803 self.assertEqual(self.getJobFromHistory('project-test2').result,
2804 'SUCCESS')
2805 self.assertEqual(B.data['status'], 'MERGED')
2806 self.assertEqual(B.reported, 2)
2807
James E. Blaire68d43f2017-04-19 14:03:30 -07002808 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002809 def test_tags(self):
2810 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002811 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2812 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2813 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2814 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2815 self.waitUntilSettled()
2816
James E. Blaire68d43f2017-04-19 14:03:30 -07002817 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002818
2819 results = {self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002820 project='org/project1').uuid: ['extratag', 'merge'],
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002821 self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002822 project='org/project2').uuid: ['merge']}
James E. Blair456f2fb2016-02-09 09:29:33 -08002823
2824 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002825 self.assertEqual(results.get(build.uuid, ''),
James E. Blair9d46f092017-07-20 16:06:20 -07002826 build.parameters['zuul'].get('jobtags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002827
James E. Blair63bb0ef2013-07-29 17:14:51 -07002828 def test_timer(self):
2829 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002830 # This test can not use simple_layout because it must start
2831 # with a configuration which does not include a
2832 # timer-triggered job so that we have an opportunity to set
2833 # the hold flag before the first job.
James E. Blair21037782017-07-19 11:56:55 -07002834 self.create_branch('org/project', 'stable')
Paul Belanger174a8272017-03-14 13:20:10 -04002835 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002836 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002837 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002838
Clark Boylan3ee090a2014-04-03 20:55:09 -07002839 # The pipeline triggers every second, so we should have seen
2840 # several by now.
2841 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002842 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002843
2844 self.assertEqual(len(self.builds), 2)
2845
James E. Blair63bb0ef2013-07-29 17:14:51 -07002846 port = self.webapp.server.socket.getsockname()[1]
2847
Clint Byrum5e729e92017-02-08 06:34:57 -08002848 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002849 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002850 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002851 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002852
Paul Belanger174a8272017-03-14 13:20:10 -04002853 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002854 # Stop queuing timer triggered jobs so that the assertions
2855 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002856 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002857 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04002858 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002859 self.waitUntilSettled()
2860
James E. Blair21037782017-07-19 11:56:55 -07002861 self.assertHistory([
2862 dict(name='project-bitrot', result='SUCCESS',
2863 ref='refs/heads/master'),
2864 dict(name='project-bitrot', result='SUCCESS',
2865 ref='refs/heads/stable'),
2866 ], ordered=False)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002867
2868 data = json.loads(data)
2869 status_jobs = set()
2870 for p in data['pipelines']:
2871 for q in p['change_queues']:
2872 for head in q['heads']:
2873 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002874 for job in change['jobs']:
2875 status_jobs.add(job['name'])
James E. Blair21037782017-07-19 11:56:55 -07002876 self.assertIn('project-bitrot', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002877
James E. Blair4f6033c2014-03-27 15:49:09 -07002878 def test_idle(self):
2879 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002880 # This test can not use simple_layout because it must start
2881 # with a configuration which does not include a
2882 # timer-triggered job so that we have an opportunity to set
2883 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002884 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002885
Clark Boylan3ee090a2014-04-03 20:55:09 -07002886 for x in range(1, 3):
2887 # Test that timer triggers periodic jobs even across
2888 # layout config reloads.
2889 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002890 self.commitConfigUpdate('common-config',
2891 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002892 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002893 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002894
Clark Boylan3ee090a2014-04-03 20:55:09 -07002895 # The pipeline triggers every second, so we should have seen
2896 # several by now.
2897 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002898
2899 # Stop queuing timer triggered jobs so that the assertions
2900 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002901 self.commitConfigUpdate('common-config',
2902 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002903 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002904 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07002905 self.assertEqual(len(self.builds), 1,
Clint Byrum58264dc2017-02-07 21:21:22 -08002906 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002907 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002908 self.waitUntilSettled()
2909 self.assertEqual(len(self.builds), 0)
James E. Blair21037782017-07-19 11:56:55 -07002910 self.assertEqual(len(self.history), x)
James E. Blair4f6033c2014-03-27 15:49:09 -07002911
James E. Blaircc324192017-04-19 14:36:08 -07002912 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002913 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002914 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2915 self.waitUntilSettled()
2916
2917 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2918 self.waitUntilSettled()
2919
James E. Blairff80a2f2013-12-27 13:24:06 -08002920 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002921
2922 # A.messages only holds what FakeGerrit places in it. Thus we
2923 # work on the knowledge of what the first message should be as
2924 # it is only configured to go to SMTP.
2925
2926 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002927 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002928 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002929 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002930 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002931 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002932
2933 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002934 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002935 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002936 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002937 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002938 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002939
James E. Blaire5910202013-12-27 09:50:31 -08002940 def test_timer_smtp(self):
2941 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002942 # This test can not use simple_layout because it must start
2943 # with a configuration which does not include a
2944 # timer-triggered job so that we have an opportunity to set
2945 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002946 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002947 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002948 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002949
Clark Boylan3ee090a2014-04-03 20:55:09 -07002950 # The pipeline triggers every second, so we should have seen
2951 # several by now.
2952 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002953 self.waitUntilSettled()
2954
Clark Boylan3ee090a2014-04-03 20:55:09 -07002955 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04002956 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002957 self.waitUntilSettled()
2958 self.assertEqual(len(self.history), 2)
2959
James E. Blaire5910202013-12-27 09:50:31 -08002960 self.assertEqual(self.getJobFromHistory(
2961 'project-bitrot-stable-old').result, 'SUCCESS')
2962 self.assertEqual(self.getJobFromHistory(
2963 'project-bitrot-stable-older').result, 'SUCCESS')
2964
James E. Blairff80a2f2013-12-27 13:24:06 -08002965 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002966
2967 # A.messages only holds what FakeGerrit places in it. Thus we
2968 # work on the knowledge of what the first message should be as
2969 # it is only configured to go to SMTP.
2970
2971 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002972 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002973 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002974 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002975 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002976 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002977
Clark Boylan3ee090a2014-04-03 20:55:09 -07002978 # Stop queuing timer triggered jobs and let any that may have
2979 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002980 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002981 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07002982 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002983 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002984 self.waitUntilSettled()
2985
Joshua Hesketh25695cb2017-03-06 12:50:04 +11002986 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00002987 def test_timer_sshkey(self):
2988 "Test that a periodic job can setup SSH key authentication"
2989 self.worker.hold_jobs_in_build = True
2990 self.config.set('zuul', 'layout_config',
2991 'tests/fixtures/layout-timer.yaml')
2992 self.sched.reconfigure(self.config)
2993 self.registerJobs()
2994
2995 # The pipeline triggers every second, so we should have seen
2996 # several by now.
2997 time.sleep(5)
2998 self.waitUntilSettled()
2999
3000 self.assertEqual(len(self.builds), 2)
3001
3002 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3003 self.assertTrue(os.path.isfile(ssh_wrapper))
3004 with open(ssh_wrapper) as f:
3005 ssh_wrapper_content = f.read()
3006 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3007 # In the unit tests Merger runs in the same process,
3008 # so we see its' environment variables
3009 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3010
3011 self.worker.release('.*')
3012 self.waitUntilSettled()
3013 self.assertEqual(len(self.history), 2)
3014
3015 self.assertEqual(self.getJobFromHistory(
3016 'project-bitrot-stable-old').result, 'SUCCESS')
3017 self.assertEqual(self.getJobFromHistory(
3018 'project-bitrot-stable-older').result, 'SUCCESS')
3019
3020 # Stop queuing timer triggered jobs and let any that may have
3021 # queued through so that end of test assertions pass.
3022 self.config.set('zuul', 'layout_config',
3023 'tests/fixtures/layout-no-timer.yaml')
3024 self.sched.reconfigure(self.config)
3025 self.registerJobs()
3026 self.waitUntilSettled()
3027 self.worker.release('.*')
3028 self.waitUntilSettled()
3029
James E. Blair91e34592015-07-31 16:45:59 -07003030 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003031 "Test that the RPC client can enqueue a change"
3032 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003033 A.addApproval('Code-Review', 2)
3034 A.addApproval('Approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003035
3036 client = zuul.rpcclient.RPCClient('127.0.0.1',
3037 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003038 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003039 r = client.enqueue(tenant='tenant-one',
3040 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003041 project='org/project',
3042 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003043 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003044 self.waitUntilSettled()
3045 self.assertEqual(self.getJobFromHistory('project-merge').result,
3046 'SUCCESS')
3047 self.assertEqual(self.getJobFromHistory('project-test1').result,
3048 'SUCCESS')
3049 self.assertEqual(self.getJobFromHistory('project-test2').result,
3050 'SUCCESS')
3051 self.assertEqual(A.data['status'], 'MERGED')
3052 self.assertEqual(A.reported, 2)
3053 self.assertEqual(r, True)
3054
James E. Blair91e34592015-07-31 16:45:59 -07003055 def test_client_enqueue_ref(self):
3056 "Test that the RPC client can enqueue a ref"
3057
3058 client = zuul.rpcclient.RPCClient('127.0.0.1',
3059 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003060 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07003061 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003062 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003063 pipeline='post',
3064 project='org/project',
3065 trigger='gerrit',
3066 ref='master',
3067 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
3068 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
3069 self.waitUntilSettled()
3070 job_names = [x.name for x in self.history]
3071 self.assertEqual(len(self.history), 1)
3072 self.assertIn('project-post', job_names)
3073 self.assertEqual(r, True)
3074
James E. Blairad28e912013-11-27 10:43:22 -08003075 def test_client_enqueue_negative(self):
3076 "Test that the RPC client returns errors"
3077 client = zuul.rpcclient.RPCClient('127.0.0.1',
3078 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003079 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003080 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003081 "Invalid tenant"):
3082 r = client.enqueue(tenant='tenant-foo',
3083 pipeline='gate',
3084 project='org/project',
3085 trigger='gerrit',
3086 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003087 self.assertEqual(r, False)
3088
3089 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003090 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003091 r = client.enqueue(tenant='tenant-one',
3092 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003093 project='project-does-not-exist',
3094 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003095 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003096 self.assertEqual(r, False)
3097
3098 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3099 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003100 r = client.enqueue(tenant='tenant-one',
3101 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003102 project='org/project',
3103 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003104 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003105 self.assertEqual(r, False)
3106
3107 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3108 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003109 r = client.enqueue(tenant='tenant-one',
3110 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003111 project='org/project',
3112 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003113 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003114 self.assertEqual(r, False)
3115
3116 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3117 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003118 r = client.enqueue(tenant='tenant-one',
3119 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003120 project='org/project',
3121 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003122 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003123 self.assertEqual(r, False)
3124
3125 self.waitUntilSettled()
3126 self.assertEqual(len(self.history), 0)
3127 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003128
3129 def test_client_promote(self):
3130 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003131 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003132 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3133 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3134 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003135 A.addApproval('Code-Review', 2)
3136 B.addApproval('Code-Review', 2)
3137 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003138
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003139 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3140 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3141 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003142
3143 self.waitUntilSettled()
3144
Paul Belangerbaca3132016-11-04 12:49:54 -04003145 tenant = self.sched.abide.tenants.get('tenant-one')
3146 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003147 enqueue_times = {}
3148 for item in items:
3149 enqueue_times[str(item.change)] = item.enqueue_time
3150
James E. Blair36658cf2013-12-06 17:53:48 -08003151 client = zuul.rpcclient.RPCClient('127.0.0.1',
3152 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003153 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003154 r = client.promote(tenant='tenant-one',
3155 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003156 change_ids=['2,1', '3,1'])
3157
Sean Daguef39b9ca2014-01-10 21:34:35 -05003158 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003159 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003160 for item in items:
3161 self.assertEqual(
3162 enqueue_times[str(item.change)], item.enqueue_time)
3163
James E. Blair78acec92014-02-06 07:11:32 -08003164 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003165 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003166 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003167 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003168 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003169 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003170 self.waitUntilSettled()
3171
3172 self.assertEqual(len(self.builds), 6)
3173 self.assertEqual(self.builds[0].name, 'project-test1')
3174 self.assertEqual(self.builds[1].name, 'project-test2')
3175 self.assertEqual(self.builds[2].name, 'project-test1')
3176 self.assertEqual(self.builds[3].name, 'project-test2')
3177 self.assertEqual(self.builds[4].name, 'project-test1')
3178 self.assertEqual(self.builds[5].name, 'project-test2')
3179
Paul Belangerbaca3132016-11-04 12:49:54 -04003180 self.assertTrue(self.builds[0].hasChanges(B))
3181 self.assertFalse(self.builds[0].hasChanges(A))
3182 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003183
Paul Belangerbaca3132016-11-04 12:49:54 -04003184 self.assertTrue(self.builds[2].hasChanges(B))
3185 self.assertTrue(self.builds[2].hasChanges(C))
3186 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003187
Paul Belangerbaca3132016-11-04 12:49:54 -04003188 self.assertTrue(self.builds[4].hasChanges(B))
3189 self.assertTrue(self.builds[4].hasChanges(C))
3190 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003191
Paul Belanger174a8272017-03-14 13:20:10 -04003192 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003193 self.waitUntilSettled()
3194
3195 self.assertEqual(A.data['status'], 'MERGED')
3196 self.assertEqual(A.reported, 2)
3197 self.assertEqual(B.data['status'], 'MERGED')
3198 self.assertEqual(B.reported, 2)
3199 self.assertEqual(C.data['status'], 'MERGED')
3200 self.assertEqual(C.reported, 2)
3201
James E. Blair36658cf2013-12-06 17:53:48 -08003202 self.assertEqual(r, True)
3203
3204 def test_client_promote_dependent(self):
3205 "Test that the RPC client can promote a dependent change"
3206 # C (depends on B) -> B -> A ; then promote C to get:
3207 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003208 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003209 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3210 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3211 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3212
3213 C.setDependsOn(B, 1)
3214
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003215 A.addApproval('Code-Review', 2)
3216 B.addApproval('Code-Review', 2)
3217 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003218
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003219 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3220 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3221 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003222
3223 self.waitUntilSettled()
3224
3225 client = zuul.rpcclient.RPCClient('127.0.0.1',
3226 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003227 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003228 r = client.promote(tenant='tenant-one',
3229 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003230 change_ids=['3,1'])
3231
James E. Blair78acec92014-02-06 07:11:32 -08003232 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003233 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003234 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003235 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003236 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003237 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003238 self.waitUntilSettled()
3239
3240 self.assertEqual(len(self.builds), 6)
3241 self.assertEqual(self.builds[0].name, 'project-test1')
3242 self.assertEqual(self.builds[1].name, 'project-test2')
3243 self.assertEqual(self.builds[2].name, 'project-test1')
3244 self.assertEqual(self.builds[3].name, 'project-test2')
3245 self.assertEqual(self.builds[4].name, 'project-test1')
3246 self.assertEqual(self.builds[5].name, 'project-test2')
3247
Paul Belangerbaca3132016-11-04 12:49:54 -04003248 self.assertTrue(self.builds[0].hasChanges(B))
3249 self.assertFalse(self.builds[0].hasChanges(A))
3250 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003251
Paul Belangerbaca3132016-11-04 12:49:54 -04003252 self.assertTrue(self.builds[2].hasChanges(B))
3253 self.assertTrue(self.builds[2].hasChanges(C))
3254 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003255
Paul Belangerbaca3132016-11-04 12:49:54 -04003256 self.assertTrue(self.builds[4].hasChanges(B))
3257 self.assertTrue(self.builds[4].hasChanges(C))
3258 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003259
Paul Belanger174a8272017-03-14 13:20:10 -04003260 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003261 self.waitUntilSettled()
3262
3263 self.assertEqual(A.data['status'], 'MERGED')
3264 self.assertEqual(A.reported, 2)
3265 self.assertEqual(B.data['status'], 'MERGED')
3266 self.assertEqual(B.reported, 2)
3267 self.assertEqual(C.data['status'], 'MERGED')
3268 self.assertEqual(C.reported, 2)
3269
James E. Blair36658cf2013-12-06 17:53:48 -08003270 self.assertEqual(r, True)
3271
3272 def test_client_promote_negative(self):
3273 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003274 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003275 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003276 A.addApproval('Code-Review', 2)
3277 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003278 self.waitUntilSettled()
3279
3280 client = zuul.rpcclient.RPCClient('127.0.0.1',
3281 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003282 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003283
3284 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003285 r = client.promote(tenant='tenant-one',
3286 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003287 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003288 self.assertEqual(r, False)
3289
3290 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003291 r = client.promote(tenant='tenant-one',
3292 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003293 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003294 self.assertEqual(r, False)
3295
Paul Belanger174a8272017-03-14 13:20:10 -04003296 self.executor_server.hold_jobs_in_build = False
3297 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003298 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003299
James E. Blaircc324192017-04-19 14:36:08 -07003300 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003301 def test_queue_rate_limiting(self):
3302 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003303 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003304 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3305 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3306 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3307
3308 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003309 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003310
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003311 A.addApproval('Code-Review', 2)
3312 B.addApproval('Code-Review', 2)
3313 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003314
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003315 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3316 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3317 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003318 self.waitUntilSettled()
3319
3320 # Only A and B will have their merge jobs queued because
3321 # window is 2.
3322 self.assertEqual(len(self.builds), 2)
3323 self.assertEqual(self.builds[0].name, 'project-merge')
3324 self.assertEqual(self.builds[1].name, 'project-merge')
3325
James E. Blair732ee862017-04-21 07:49:26 -07003326 # Release the merge jobs one at a time.
3327 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003328 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003329 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003330 self.waitUntilSettled()
3331
3332 # Only A and B will have their test jobs queued because
3333 # window is 2.
3334 self.assertEqual(len(self.builds), 4)
3335 self.assertEqual(self.builds[0].name, 'project-test1')
3336 self.assertEqual(self.builds[1].name, 'project-test2')
3337 self.assertEqual(self.builds[2].name, 'project-test1')
3338 self.assertEqual(self.builds[3].name, 'project-test2')
3339
Paul Belanger174a8272017-03-14 13:20:10 -04003340 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003341 self.waitUntilSettled()
3342
James E. Blaircc324192017-04-19 14:36:08 -07003343 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003344 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003345 # A failed so window is reduced by 1 to 1.
3346 self.assertEqual(queue.window, 1)
3347 self.assertEqual(queue.window_floor, 1)
3348 self.assertEqual(A.data['status'], 'NEW')
3349
3350 # Gate is reset and only B's merge job is queued because
3351 # window shrunk to 1.
3352 self.assertEqual(len(self.builds), 1)
3353 self.assertEqual(self.builds[0].name, 'project-merge')
3354
Paul Belanger174a8272017-03-14 13:20:10 -04003355 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003356 self.waitUntilSettled()
3357
3358 # Only B's test jobs are queued because window is still 1.
3359 self.assertEqual(len(self.builds), 2)
3360 self.assertEqual(self.builds[0].name, 'project-test1')
3361 self.assertEqual(self.builds[1].name, 'project-test2')
3362
Paul Belanger174a8272017-03-14 13:20:10 -04003363 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003364 self.waitUntilSettled()
3365
3366 # B was successfully merged so window is increased to 2.
3367 self.assertEqual(queue.window, 2)
3368 self.assertEqual(queue.window_floor, 1)
3369 self.assertEqual(B.data['status'], 'MERGED')
3370
3371 # Only C is left and its merge job is queued.
3372 self.assertEqual(len(self.builds), 1)
3373 self.assertEqual(self.builds[0].name, 'project-merge')
3374
Paul Belanger174a8272017-03-14 13:20:10 -04003375 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003376 self.waitUntilSettled()
3377
3378 # After successful merge job the test jobs for C are queued.
3379 self.assertEqual(len(self.builds), 2)
3380 self.assertEqual(self.builds[0].name, 'project-test1')
3381 self.assertEqual(self.builds[1].name, 'project-test2')
3382
Paul Belanger174a8272017-03-14 13:20:10 -04003383 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003384 self.waitUntilSettled()
3385
3386 # C successfully merged so window is bumped to 3.
3387 self.assertEqual(queue.window, 3)
3388 self.assertEqual(queue.window_floor, 1)
3389 self.assertEqual(C.data['status'], 'MERGED')
3390
James E. Blaircc324192017-04-19 14:36:08 -07003391 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003392 def test_queue_rate_limiting_dependent(self):
3393 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003394 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003395 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3396 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3397 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3398
3399 B.setDependsOn(A, 1)
3400
Paul Belanger174a8272017-03-14 13:20:10 -04003401 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003402
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003403 A.addApproval('Code-Review', 2)
3404 B.addApproval('Code-Review', 2)
3405 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003406
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003407 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3408 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3409 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003410 self.waitUntilSettled()
3411
3412 # Only A and B will have their merge jobs queued because
3413 # window is 2.
3414 self.assertEqual(len(self.builds), 2)
3415 self.assertEqual(self.builds[0].name, 'project-merge')
3416 self.assertEqual(self.builds[1].name, 'project-merge')
3417
James E. Blairf4a5f022017-04-18 14:01:10 -07003418 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003419
3420 # Only A and B will have their test jobs queued because
3421 # window is 2.
3422 self.assertEqual(len(self.builds), 4)
3423 self.assertEqual(self.builds[0].name, 'project-test1')
3424 self.assertEqual(self.builds[1].name, 'project-test2')
3425 self.assertEqual(self.builds[2].name, 'project-test1')
3426 self.assertEqual(self.builds[3].name, 'project-test2')
3427
Paul Belanger174a8272017-03-14 13:20:10 -04003428 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003429 self.waitUntilSettled()
3430
James E. Blaircc324192017-04-19 14:36:08 -07003431 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003432 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003433 # A failed so window is reduced by 1 to 1.
3434 self.assertEqual(queue.window, 1)
3435 self.assertEqual(queue.window_floor, 1)
3436 self.assertEqual(A.data['status'], 'NEW')
3437 self.assertEqual(B.data['status'], 'NEW')
3438
3439 # Gate is reset and only C's merge job is queued because
3440 # window shrunk to 1 and A and B were dequeued.
3441 self.assertEqual(len(self.builds), 1)
3442 self.assertEqual(self.builds[0].name, 'project-merge')
3443
James E. Blairf4a5f022017-04-18 14:01:10 -07003444 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003445
3446 # Only C's test jobs are queued because window is still 1.
3447 self.assertEqual(len(self.builds), 2)
James E. Blairdf25ddc2017-07-08 07:57:09 -07003448 builds = self.getSortedBuilds()
3449 self.assertEqual(builds[0].name, 'project-test1')
3450 self.assertEqual(builds[1].name, 'project-test2')
Clark Boylan7603a372014-01-21 11:43:20 -08003451
Paul Belanger174a8272017-03-14 13:20:10 -04003452 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003453 self.waitUntilSettled()
3454
3455 # C was successfully merged so window is increased to 2.
3456 self.assertEqual(queue.window, 2)
3457 self.assertEqual(queue.window_floor, 1)
3458 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003459
3460 def test_worker_update_metadata(self):
3461 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003462 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003463
3464 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003465 A.addApproval('Code-Review', 2)
3466 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003467 self.waitUntilSettled()
3468
Clint Byrumd4de79a2017-05-27 11:39:13 -07003469 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003470
3471 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07003472 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003473
3474 start = time.time()
3475 while True:
3476 if time.time() - start > 10:
3477 raise Exception("Timeout waiting for gearman server to report "
3478 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07003479 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003480 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08003481 break
3482 else:
3483 time.sleep(0)
3484
3485 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05003486 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003487
Paul Belanger174a8272017-03-14 13:20:10 -04003488 self.executor_server.hold_jobs_in_build = False
3489 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003490 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003491
James E. Blaircc324192017-04-19 14:36:08 -07003492 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003493 def test_footer_message(self):
3494 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003495 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003496 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003497 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003498 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003499 self.waitUntilSettled()
3500
3501 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003502 B.addApproval('Code-Review', 2)
3503 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003504 self.waitUntilSettled()
3505
3506 self.assertEqual(2, len(self.smtp_messages))
3507
Adam Gandelman82258fd2017-02-07 16:51:58 -08003508 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003509Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003510http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003511
Adam Gandelman82258fd2017-02-07 16:51:58 -08003512 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003513For CI problems and help debugging, contact ci@example.org"""
3514
Adam Gandelman82258fd2017-02-07 16:51:58 -08003515 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3516 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3517 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3518 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003519
Tobias Henkel9842bd72017-05-16 13:40:03 +02003520 @simple_layout('layouts/unmanaged-project.yaml')
3521 def test_unmanaged_project_start_message(self):
3522 "Test start reporting is not done for unmanaged projects."
3523 self.init_repo("org/project", tag='init')
3524 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3525 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3526 self.waitUntilSettled()
3527
3528 self.assertEqual(0, len(A.messages))
3529
Clint Byrumb9de8be2017-05-27 13:49:19 -07003530 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003531 def test_merge_failure_reporters(self):
3532 """Check that the config is set up correctly"""
3533
Clint Byrumb9de8be2017-05-27 13:49:19 -07003534 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11003535 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003536 "Merge Failed.\n\nThis change or one of its cross-repo "
3537 "dependencies was unable to be automatically merged with the "
3538 "current state of its repository. Please rebase the change and "
3539 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003540 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003541 self.assertEqual(
3542 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003543 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003544
3545 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003546 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11003547 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003548 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11003549
3550 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003551 tenant.layout.pipelines['check'].merge_failure_actions[0],
3552 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003553
3554 self.assertTrue(
3555 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003556 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003557 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003558 zuul.driver.smtp.smtpreporter.SMTPReporter) and
3559 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003560 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003561 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003562 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003563 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003564 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003565 gerritreporter.GerritReporter) and
3566 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003567 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003568 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003569 )
3570 )
3571
James E. Blairec056492016-07-22 09:45:56 -07003572 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003573 def test_merge_failure_reports(self):
3574 """Check that when a change fails to merge the correct message is sent
3575 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003576 self.updateConfigLayout(
3577 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003578 self.sched.reconfigure(self.config)
3579 self.registerJobs()
3580
3581 # Check a test failure isn't reported to SMTP
3582 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003583 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003584 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003585 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003586 self.waitUntilSettled()
3587
3588 self.assertEqual(3, len(self.history)) # 3 jobs
3589 self.assertEqual(0, len(self.smtp_messages))
3590
3591 # Check a merge failure is reported to SMTP
3592 # B should be merged, but C will conflict with B
3593 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3594 B.addPatchset(['conflict'])
3595 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3596 C.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003597 B.addApproval('Code-Review', 2)
3598 C.addApproval('Code-Review', 2)
3599 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3600 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003601 self.waitUntilSettled()
3602
3603 self.assertEqual(6, len(self.history)) # A and B jobs
3604 self.assertEqual(1, len(self.smtp_messages))
3605 self.assertEqual('The merge failed! For more information...',
3606 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003607
James E. Blairec056492016-07-22 09:45:56 -07003608 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003609 def test_default_merge_failure_reports(self):
3610 """Check that the default merge failure reports are correct."""
3611
3612 # A should report success, B should report merge failure.
3613 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3614 A.addPatchset(['conflict'])
3615 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3616 B.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003617 A.addApproval('Code-Review', 2)
3618 B.addApproval('Code-Review', 2)
3619 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3620 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003621 self.waitUntilSettled()
3622
3623 self.assertEqual(3, len(self.history)) # A jobs
3624 self.assertEqual(A.reported, 2)
3625 self.assertEqual(B.reported, 2)
3626 self.assertEqual(A.data['status'], 'MERGED')
3627 self.assertEqual(B.data['status'], 'NEW')
3628 self.assertIn('Build succeeded', A.messages[1])
3629 self.assertIn('Merge Failed', B.messages[1])
3630 self.assertIn('automatically merged', B.messages[1])
3631 self.assertNotIn('logs.example.com', B.messages[1])
3632 self.assertNotIn('SKIPPED', B.messages[1])
3633
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003634 def test_client_get_running_jobs(self):
3635 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003636 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003637 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003638 A.addApproval('Code-Review', 2)
3639 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003640 self.waitUntilSettled()
3641
3642 client = zuul.rpcclient.RPCClient('127.0.0.1',
3643 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003644 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003645
3646 # Wait for gearman server to send the initial workData back to zuul
3647 start = time.time()
3648 while True:
3649 if time.time() - start > 10:
3650 raise Exception("Timeout waiting for gearman server to report "
3651 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003652 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003653 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003654 break
3655 else:
3656 time.sleep(0)
3657
3658 running_items = client.get_running_jobs()
3659
3660 self.assertEqual(1, len(running_items))
3661 running_item = running_items[0]
3662 self.assertEqual([], running_item['failing_reasons'])
3663 self.assertEqual([], running_item['items_behind'])
3664 self.assertEqual('https://hostname/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003665 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003666 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003667 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003668 self.assertEqual(True, running_item['active'])
3669 self.assertEqual('1,1', running_item['id'])
3670
3671 self.assertEqual(3, len(running_item['jobs']))
3672 for job in running_item['jobs']:
3673 if job['name'] == 'project-merge':
3674 self.assertEqual('project-merge', job['name'])
3675 self.assertEqual('gate', job['pipeline'])
3676 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05003677 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003678 'static/stream.html?uuid={uuid}&logfile=console.log'
3679 .format(uuid=job['uuid']), job['url'])
3680 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05003681 'finger://{hostname}/{uuid}'.format(
3682 hostname=self.executor_server.hostname,
3683 uuid=job['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003684 job['finger_url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07003685 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003686 self.assertEqual(False, job['canceled'])
3687 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003688 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003689 self.assertEqual('gate', job['pipeline'])
3690 break
3691
Paul Belanger174a8272017-03-14 13:20:10 -04003692 self.executor_server.hold_jobs_in_build = False
3693 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003694 self.waitUntilSettled()
3695
3696 running_items = client.get_running_jobs()
3697 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003698
James E. Blairb70e55a2017-04-19 12:57:02 -07003699 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003700 def test_nonvoting_pipeline(self):
3701 "Test that a nonvoting pipeline (experimental) can still report"
3702
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003703 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3704 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003705 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3706 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003707
3708 self.assertEqual(self.getJobFromHistory('project-merge').result,
3709 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003710 self.assertEqual(
3711 self.getJobFromHistory('experimental-project-test').result,
3712 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003713 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003714
3715 def test_crd_gate(self):
3716 "Test cross-repo dependencies"
3717 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3718 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003719 A.addApproval('Code-Review', 2)
3720 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003721
3722 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3723 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3724 AM2.setMerged()
3725 AM1.setMerged()
3726
3727 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3728 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3729 BM2.setMerged()
3730 BM1.setMerged()
3731
3732 # A -> AM1 -> AM2
3733 # B -> BM1 -> BM2
3734 # A Depends-On: B
3735 # M2 is here to make sure it is never queried. If it is, it
3736 # means zuul is walking down the entire history of merged
3737 # changes.
3738
3739 B.setDependsOn(BM1, 1)
3740 BM1.setDependsOn(BM2, 1)
3741
3742 A.setDependsOn(AM1, 1)
3743 AM1.setDependsOn(AM2, 1)
3744
3745 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3746 A.subject, B.data['id'])
3747
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003748 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003749 self.waitUntilSettled()
3750
3751 self.assertEqual(A.data['status'], 'NEW')
3752 self.assertEqual(B.data['status'], 'NEW')
3753
Paul Belanger5dccbe72016-11-14 11:17:37 -05003754 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003755 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003756
Paul Belanger174a8272017-03-14 13:20:10 -04003757 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003758 B.addApproval('Approved', 1)
3759 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003760 self.waitUntilSettled()
3761
Paul Belanger174a8272017-03-14 13:20:10 -04003762 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003763 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003764 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003765 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003766 self.executor_server.hold_jobs_in_build = False
3767 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003768 self.waitUntilSettled()
3769
3770 self.assertEqual(AM2.queried, 0)
3771 self.assertEqual(BM2.queried, 0)
3772 self.assertEqual(A.data['status'], 'MERGED')
3773 self.assertEqual(B.data['status'], 'MERGED')
3774 self.assertEqual(A.reported, 2)
3775 self.assertEqual(B.reported, 2)
3776
Paul Belanger5dccbe72016-11-14 11:17:37 -05003777 changes = self.getJobFromHistory(
3778 'project-merge', 'org/project1').changes
3779 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003780
3781 def test_crd_branch(self):
3782 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003783
3784 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003785 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3786 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3787 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3788 C.data['id'] = B.data['id']
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003789 A.addApproval('Code-Review', 2)
3790 B.addApproval('Code-Review', 2)
3791 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003792
3793 # A Depends-On: B+C
3794 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3795 A.subject, B.data['id'])
3796
Paul Belanger174a8272017-03-14 13:20:10 -04003797 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003798 B.addApproval('Approved', 1)
3799 C.addApproval('Approved', 1)
3800 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003801 self.waitUntilSettled()
3802
Paul Belanger174a8272017-03-14 13:20:10 -04003803 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003804 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003805 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003806 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003807 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003808 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003809 self.executor_server.hold_jobs_in_build = False
3810 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003811 self.waitUntilSettled()
3812
3813 self.assertEqual(A.data['status'], 'MERGED')
3814 self.assertEqual(B.data['status'], 'MERGED')
3815 self.assertEqual(C.data['status'], 'MERGED')
3816 self.assertEqual(A.reported, 2)
3817 self.assertEqual(B.reported, 2)
3818 self.assertEqual(C.reported, 2)
3819
Paul Belanger6379db12016-11-14 13:57:54 -05003820 changes = self.getJobFromHistory(
3821 'project-merge', 'org/project1').changes
3822 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003823
3824 def test_crd_multiline(self):
3825 "Test multiple depends-on lines in commit"
3826 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3827 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3828 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003829 A.addApproval('Code-Review', 2)
3830 B.addApproval('Code-Review', 2)
3831 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003832
3833 # A Depends-On: B+C
3834 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3835 A.subject, B.data['id'], C.data['id'])
3836
Paul Belanger174a8272017-03-14 13:20:10 -04003837 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003838 B.addApproval('Approved', 1)
3839 C.addApproval('Approved', 1)
3840 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003841 self.waitUntilSettled()
3842
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.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003848 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003849 self.executor_server.hold_jobs_in_build = False
3850 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003851 self.waitUntilSettled()
3852
3853 self.assertEqual(A.data['status'], 'MERGED')
3854 self.assertEqual(B.data['status'], 'MERGED')
3855 self.assertEqual(C.data['status'], 'MERGED')
3856 self.assertEqual(A.reported, 2)
3857 self.assertEqual(B.reported, 2)
3858 self.assertEqual(C.reported, 2)
3859
Paul Belanger5dccbe72016-11-14 11:17:37 -05003860 changes = self.getJobFromHistory(
3861 'project-merge', 'org/project1').changes
3862 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003863
3864 def test_crd_unshared_gate(self):
3865 "Test cross-repo dependencies in unshared gate queues"
3866 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3867 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003868 A.addApproval('Code-Review', 2)
3869 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003870
3871 # A Depends-On: B
3872 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3873 A.subject, B.data['id'])
3874
3875 # A and B do not share a queue, make sure that A is unable to
3876 # enqueue B (and therefore, A is unable to be enqueued).
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003877 B.addApproval('Approved', 1)
3878 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003879 self.waitUntilSettled()
3880
3881 self.assertEqual(A.data['status'], 'NEW')
3882 self.assertEqual(B.data['status'], 'NEW')
3883 self.assertEqual(A.reported, 0)
3884 self.assertEqual(B.reported, 0)
3885 self.assertEqual(len(self.history), 0)
3886
3887 # Enqueue and merge B alone.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003888 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003889 self.waitUntilSettled()
3890
3891 self.assertEqual(B.data['status'], 'MERGED')
3892 self.assertEqual(B.reported, 2)
3893
3894 # Now that B is merged, A should be able to be enqueued and
3895 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003896 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003897 self.waitUntilSettled()
3898
3899 self.assertEqual(A.data['status'], 'MERGED')
3900 self.assertEqual(A.reported, 2)
3901
James E. Blair96698e22015-04-02 07:48:21 -07003902 def test_crd_gate_reverse(self):
3903 "Test reverse cross-repo dependencies"
3904 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3905 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003906 A.addApproval('Code-Review', 2)
3907 B.addApproval('Code-Review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003908
3909 # A Depends-On: B
3910
3911 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3912 A.subject, B.data['id'])
3913
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003914 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003915 self.waitUntilSettled()
3916
3917 self.assertEqual(A.data['status'], 'NEW')
3918 self.assertEqual(B.data['status'], 'NEW')
3919
Paul Belanger174a8272017-03-14 13:20:10 -04003920 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003921 A.addApproval('Approved', 1)
3922 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003923 self.waitUntilSettled()
3924
Paul Belanger174a8272017-03-14 13:20:10 -04003925 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003926 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003927 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003928 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003929 self.executor_server.hold_jobs_in_build = False
3930 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003931 self.waitUntilSettled()
3932
3933 self.assertEqual(A.data['status'], 'MERGED')
3934 self.assertEqual(B.data['status'], 'MERGED')
3935 self.assertEqual(A.reported, 2)
3936 self.assertEqual(B.reported, 2)
3937
Paul Belanger5dccbe72016-11-14 11:17:37 -05003938 changes = self.getJobFromHistory(
3939 'project-merge', 'org/project1').changes
3940 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003941
James E. Blair5ee24252014-12-30 10:12:29 -08003942 def test_crd_cycle(self):
3943 "Test cross-repo dependency cycles"
3944 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3945 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003946 A.addApproval('Code-Review', 2)
3947 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003948
3949 # A -> B -> A (via commit-depends)
3950
3951 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3952 A.subject, B.data['id'])
3953 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3954 B.subject, A.data['id'])
3955
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003956 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003957 self.waitUntilSettled()
3958
3959 self.assertEqual(A.reported, 0)
3960 self.assertEqual(B.reported, 0)
3961 self.assertEqual(A.data['status'], 'NEW')
3962 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003963
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003964 def test_crd_gate_unknown(self):
3965 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07003966 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003967 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3968 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003969 A.addApproval('Code-Review', 2)
3970 B.addApproval('Code-Review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003971
3972 # A Depends-On: B
3973 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3974 A.subject, B.data['id'])
3975
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003976 B.addApproval('Approved', 1)
3977 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003978 self.waitUntilSettled()
3979
3980 # Unknown projects cannot share a queue with any other
3981 # since they don't have common jobs with any other (they have no jobs).
3982 # Changes which depend on unknown project changes
3983 # should not be processed in dependent pipeline
3984 self.assertEqual(A.data['status'], 'NEW')
3985 self.assertEqual(B.data['status'], 'NEW')
3986 self.assertEqual(A.reported, 0)
3987 self.assertEqual(B.reported, 0)
3988 self.assertEqual(len(self.history), 0)
3989
James E. Blair7720b402017-06-07 08:51:49 -07003990 # Simulate change B being gated outside this layout Set the
3991 # change merged before submitting the event so that when the
3992 # event triggers a gerrit query to update the change, we get
3993 # the information that it was merged.
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003994 B.setMerged()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003995 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003996 self.waitUntilSettled()
3997 self.assertEqual(len(self.history), 0)
3998
3999 # Now that B is merged, A should be able to be enqueued and
4000 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004001 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004002 self.waitUntilSettled()
4003
4004 self.assertEqual(A.data['status'], 'MERGED')
4005 self.assertEqual(A.reported, 2)
4006 self.assertEqual(B.data['status'], 'MERGED')
4007 self.assertEqual(B.reported, 0)
4008
James E. Blairbfb8e042014-12-30 17:01:44 -08004009 def test_crd_check(self):
4010 "Test cross-repo dependencies in independent pipelines"
4011
Paul Belanger174a8272017-03-14 13:20:10 -04004012 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004013 self.gearman_server.hold_jobs_in_queue = True
4014 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4015 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4016
4017 # A Depends-On: B
4018 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4019 A.subject, B.data['id'])
4020
4021 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4022 self.waitUntilSettled()
4023
James E. Blairbfb8e042014-12-30 17:01:44 -08004024 self.gearman_server.hold_jobs_in_queue = False
4025 self.gearman_server.release()
4026 self.waitUntilSettled()
4027
Paul Belanger174a8272017-03-14 13:20:10 -04004028 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05004029 self.waitUntilSettled()
4030
James E. Blair2a535672017-04-27 12:03:15 -07004031 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08004032
Paul Belanger174a8272017-03-14 13:20:10 -04004033 self.executor_server.hold_jobs_in_build = False
4034 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05004035 self.waitUntilSettled()
4036
James E. Blairbfb8e042014-12-30 17:01:44 -08004037 self.assertEqual(A.data['status'], 'NEW')
4038 self.assertEqual(B.data['status'], 'NEW')
4039 self.assertEqual(A.reported, 1)
4040 self.assertEqual(B.reported, 0)
4041
4042 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004043 tenant = self.sched.abide.tenants.get('tenant-one')
4044 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004045
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004046 def test_crd_check_git_depends(self):
4047 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004048 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004049 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4050 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4051
4052 # Add two git-dependent changes and make sure they both report
4053 # success.
4054 B.setDependsOn(A, 1)
4055 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4056 self.waitUntilSettled()
4057 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4058 self.waitUntilSettled()
4059
James E. Blairb8c16472015-05-05 14:55:26 -07004060 self.orderedRelease()
4061 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004062 self.waitUntilSettled()
4063
4064 self.assertEqual(A.data['status'], 'NEW')
4065 self.assertEqual(B.data['status'], 'NEW')
4066 self.assertEqual(A.reported, 1)
4067 self.assertEqual(B.reported, 1)
4068
4069 self.assertEqual(self.history[0].changes, '1,1')
4070 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004071 tenant = self.sched.abide.tenants.get('tenant-one')
4072 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004073
4074 self.assertIn('Build succeeded', A.messages[0])
4075 self.assertIn('Build succeeded', B.messages[0])
4076
4077 def test_crd_check_duplicate(self):
4078 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004079 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004080 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4081 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004082 tenant = self.sched.abide.tenants.get('tenant-one')
4083 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004084
4085 # Add two git-dependent changes...
4086 B.setDependsOn(A, 1)
4087 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4088 self.waitUntilSettled()
4089 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4090
4091 # ...make sure the live one is not duplicated...
4092 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4093 self.waitUntilSettled()
4094 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4095
4096 # ...but the non-live one is able to be.
4097 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4098 self.waitUntilSettled()
4099 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4100
Clark Boylandd849822015-03-02 12:38:14 -08004101 # Release jobs in order to avoid races with change A jobs
4102 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004103 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004104 self.executor_server.hold_jobs_in_build = False
4105 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004106 self.waitUntilSettled()
4107
4108 self.assertEqual(A.data['status'], 'NEW')
4109 self.assertEqual(B.data['status'], 'NEW')
4110 self.assertEqual(A.reported, 1)
4111 self.assertEqual(B.reported, 1)
4112
4113 self.assertEqual(self.history[0].changes, '1,1 2,1')
4114 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004115 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004116
4117 self.assertIn('Build succeeded', A.messages[0])
4118 self.assertIn('Build succeeded', B.messages[0])
4119
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004120 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004121 "Test cross-repo dependencies re-enqueued in independent pipelines"
4122
4123 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004124 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4125 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004126
4127 # A Depends-On: B
4128 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4129 A.subject, B.data['id'])
4130
4131 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4132 self.waitUntilSettled()
4133
4134 self.sched.reconfigure(self.config)
4135
4136 # Make sure the items still share a change queue, and the
4137 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004138 tenant = self.sched.abide.tenants.get('tenant-one')
4139 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4140 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004141 first_item = queue.queue[0]
4142 for item in queue.queue:
4143 self.assertEqual(item.queue, first_item.queue)
4144 self.assertFalse(first_item.live)
4145 self.assertTrue(queue.queue[1].live)
4146
4147 self.gearman_server.hold_jobs_in_queue = False
4148 self.gearman_server.release()
4149 self.waitUntilSettled()
4150
4151 self.assertEqual(A.data['status'], 'NEW')
4152 self.assertEqual(B.data['status'], 'NEW')
4153 self.assertEqual(A.reported, 1)
4154 self.assertEqual(B.reported, 0)
4155
4156 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004157 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004158
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004159 def test_crd_check_reconfiguration(self):
4160 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4161
4162 def test_crd_undefined_project(self):
4163 """Test that undefined projects in dependencies are handled for
4164 independent pipelines"""
4165 # It's a hack for fake gerrit,
4166 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004167 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004168 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4169
Jesse Keatingea68ab42017-04-19 15:03:30 -07004170 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004171 def test_crd_check_ignore_dependencies(self):
4172 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004173
4174 self.gearman_server.hold_jobs_in_queue = True
4175 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4176 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4177 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4178
4179 # A Depends-On: B
4180 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4181 A.subject, B.data['id'])
4182 # C git-depends on B
4183 C.setDependsOn(B, 1)
4184 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4185 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4186 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4187 self.waitUntilSettled()
4188
4189 # Make sure none of the items share a change queue, and all
4190 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004191 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004192 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004193 self.assertEqual(len(check_pipeline.queues), 3)
4194 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4195 for item in check_pipeline.getAllItems():
4196 self.assertTrue(item.live)
4197
4198 self.gearman_server.hold_jobs_in_queue = False
4199 self.gearman_server.release()
4200 self.waitUntilSettled()
4201
4202 self.assertEqual(A.data['status'], 'NEW')
4203 self.assertEqual(B.data['status'], 'NEW')
4204 self.assertEqual(C.data['status'], 'NEW')
4205 self.assertEqual(A.reported, 1)
4206 self.assertEqual(B.reported, 1)
4207 self.assertEqual(C.reported, 1)
4208
4209 # Each job should have tested exactly one change
4210 for job in self.history:
4211 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004212
James E. Blair7cb84542017-04-19 13:35:05 -07004213 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004214 def test_crd_check_transitive(self):
4215 "Test transitive cross-repo dependencies"
4216 # Specifically, if A -> B -> C, and C gets a new patchset and
4217 # A gets a new patchset, ensure the test of A,2 includes B,1
4218 # and C,2 (not C,1 which would indicate stale data in the
4219 # cache for B).
4220 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4221 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4222 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4223
4224 # A Depends-On: B
4225 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4226 A.subject, B.data['id'])
4227
4228 # B Depends-On: C
4229 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4230 B.subject, C.data['id'])
4231
4232 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4233 self.waitUntilSettled()
4234 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4235
4236 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4237 self.waitUntilSettled()
4238 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4239
4240 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4241 self.waitUntilSettled()
4242 self.assertEqual(self.history[-1].changes, '3,1')
4243
4244 C.addPatchset()
4245 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4246 self.waitUntilSettled()
4247 self.assertEqual(self.history[-1].changes, '3,2')
4248
4249 A.addPatchset()
4250 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4251 self.waitUntilSettled()
4252 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004253
James E. Blaira8b90b32016-08-24 15:18:50 -07004254 def test_crd_check_unknown(self):
4255 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004256 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004257 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4258 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4259 # A Depends-On: B
4260 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4261 A.subject, B.data['id'])
4262
4263 # Make sure zuul has seen an event on B.
4264 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4265 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4266 self.waitUntilSettled()
4267
4268 self.assertEqual(A.data['status'], 'NEW')
4269 self.assertEqual(A.reported, 1)
4270 self.assertEqual(B.data['status'], 'NEW')
4271 self.assertEqual(B.reported, 0)
4272
James E. Blair92464a22016-04-05 10:21:26 -07004273 def test_crd_cycle_join(self):
4274 "Test an updated change creates a cycle"
4275 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4276
4277 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4278 self.waitUntilSettled()
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004279 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004280
4281 # Create B->A
4282 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4283 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4284 B.subject, A.data['id'])
4285 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4286 self.waitUntilSettled()
4287
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004288 # Dep is there so zuul should have reported on B
4289 self.assertEqual(B.reported, 1)
4290
James E. Blair92464a22016-04-05 10:21:26 -07004291 # Update A to add A->B (a cycle).
4292 A.addPatchset()
4293 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4294 A.subject, B.data['id'])
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004295 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4296 self.waitUntilSettled()
James E. Blair92464a22016-04-05 10:21:26 -07004297
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004298 # Dependency cycle injected so zuul should not have reported again on A
4299 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004300
4301 # Now if we update B to remove the depends-on, everything
4302 # should be okay. B; A->B
4303
4304 B.addPatchset()
4305 B.data['commitMessage'] = '%s\n' % (B.subject,)
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004306 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4307 self.waitUntilSettled()
Paul Belanger0e155e22016-11-14 14:12:23 -05004308
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004309 # Cycle was removed so now zuul should have reported again on A
4310 self.assertEqual(A.reported, 2)
4311
4312 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
4313 self.waitUntilSettled()
4314 self.assertEqual(B.reported, 2)
James E. Blair92464a22016-04-05 10:21:26 -07004315
Jesse Keatingcc67b122017-04-19 14:54:57 -07004316 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004317 def test_disable_at(self):
4318 "Test a pipeline will only report to the disabled trigger when failing"
4319
Jesse Keatingcc67b122017-04-19 14:54:57 -07004320 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004321 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004322 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004323 0, tenant.layout.pipelines['check']._consecutive_failures)
4324 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004325
4326 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4327 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4328 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4329 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4330 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4331 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4332 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4333 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4334 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4335 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4336 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4337
Paul Belanger174a8272017-03-14 13:20:10 -04004338 self.executor_server.failJob('project-test1', A)
4339 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004340 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004341 self.executor_server.failJob('project-test1', D)
4342 self.executor_server.failJob('project-test1', E)
4343 self.executor_server.failJob('project-test1', F)
4344 self.executor_server.failJob('project-test1', G)
4345 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004346 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004347 self.executor_server.failJob('project-test1', J)
4348 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004349
4350 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4351 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4352 self.waitUntilSettled()
4353
4354 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004355 2, tenant.layout.pipelines['check']._consecutive_failures)
4356 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004357
4358 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4359 self.waitUntilSettled()
4360
4361 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004362 0, tenant.layout.pipelines['check']._consecutive_failures)
4363 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004364
4365 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4366 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4367 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4368 self.waitUntilSettled()
4369
4370 # We should be disabled now
4371 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004372 3, tenant.layout.pipelines['check']._consecutive_failures)
4373 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004374
4375 # We need to wait between each of these patches to make sure the
4376 # smtp messages come back in an expected order
4377 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4378 self.waitUntilSettled()
4379 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4380 self.waitUntilSettled()
4381 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4382 self.waitUntilSettled()
4383
4384 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4385 # leaving a message on each change
4386 self.assertEqual(1, len(A.messages))
4387 self.assertIn('Build failed.', A.messages[0])
4388 self.assertEqual(1, len(B.messages))
4389 self.assertIn('Build failed.', B.messages[0])
4390 self.assertEqual(1, len(C.messages))
4391 self.assertIn('Build succeeded.', C.messages[0])
4392 self.assertEqual(1, len(D.messages))
4393 self.assertIn('Build failed.', D.messages[0])
4394 self.assertEqual(1, len(E.messages))
4395 self.assertIn('Build failed.', E.messages[0])
4396 self.assertEqual(1, len(F.messages))
4397 self.assertIn('Build failed.', F.messages[0])
4398
4399 # The last 3 (GHI) would have only reported via smtp.
4400 self.assertEqual(3, len(self.smtp_messages))
4401 self.assertEqual(0, len(G.messages))
4402 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004403 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004404 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004405 self.assertEqual(0, len(H.messages))
4406 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004407 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004408 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004409 self.assertEqual(0, len(I.messages))
4410 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004411 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004412 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004413
4414 # Now reload the configuration (simulate a HUP) to check the pipeline
4415 # comes out of disabled
4416 self.sched.reconfigure(self.config)
4417
Jesse Keatingcc67b122017-04-19 14:54:57 -07004418 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004419
4420 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004421 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004422 0, tenant.layout.pipelines['check']._consecutive_failures)
4423 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004424
4425 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4426 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4427 self.waitUntilSettled()
4428
4429 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004430 2, tenant.layout.pipelines['check']._consecutive_failures)
4431 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004432
4433 # J and K went back to gerrit
4434 self.assertEqual(1, len(J.messages))
4435 self.assertIn('Build failed.', J.messages[0])
4436 self.assertEqual(1, len(K.messages))
4437 self.assertIn('Build failed.', K.messages[0])
4438 # No more messages reported via smtp
4439 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004440
James E. Blaircc020532017-04-19 13:04:44 -07004441 @simple_layout('layouts/one-job-project.yaml')
4442 def test_one_job_project(self):
4443 "Test that queueing works with one job"
4444 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4445 'master', 'A')
4446 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4447 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004448 A.addApproval('Code-Review', 2)
4449 B.addApproval('Code-Review', 2)
4450 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4451 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blaircc020532017-04-19 13:04:44 -07004452 self.waitUntilSettled()
4453
4454 self.assertEqual(A.data['status'], 'MERGED')
4455 self.assertEqual(A.reported, 2)
4456 self.assertEqual(B.data['status'], 'MERGED')
4457 self.assertEqual(B.reported, 2)
4458
Paul Belanger71d98172016-11-08 10:56:31 -05004459 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004460 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004461
Paul Belanger174a8272017-03-14 13:20:10 -04004462 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004463 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4464 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4465 self.waitUntilSettled()
4466
Paul Belanger174a8272017-03-14 13:20:10 -04004467 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004468 self.waitUntilSettled()
4469
4470 self.assertEqual(len(self.builds), 2)
4471 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004472 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004473 self.waitUntilSettled()
4474
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004475 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004476 self.assertEqual(len(self.builds), 1,
4477 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004478 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004479 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004480 self.waitUntilSettled()
4481
Paul Belanger174a8272017-03-14 13:20:10 -04004482 self.executor_server.hold_jobs_in_build = False
4483 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004484 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004485 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004486 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4487 self.assertEqual(A.reported, 1)
4488 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004489
James E. Blair15be0e12017-01-03 13:45:20 -08004490 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004491 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004492
4493 self.fake_nodepool.paused = True
4494 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004495 A.addApproval('Code-Review', 2)
4496 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair15be0e12017-01-03 13:45:20 -08004497 self.waitUntilSettled()
4498
4499 self.zk.client.stop()
4500 self.zk.client.start()
4501 self.fake_nodepool.paused = False
4502 self.waitUntilSettled()
4503
4504 self.assertEqual(A.data['status'], 'MERGED')
4505 self.assertEqual(A.reported, 2)
4506
James E. Blair6ab79e02017-01-06 10:10:17 -08004507 def test_nodepool_failure(self):
4508 "Test that jobs are reported after a nodepool failure"
4509
4510 self.fake_nodepool.paused = True
4511 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004512 A.addApproval('Code-Review', 2)
4513 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6ab79e02017-01-06 10:10:17 -08004514 self.waitUntilSettled()
4515
4516 req = self.fake_nodepool.getNodeRequests()[0]
4517 self.fake_nodepool.addFailRequest(req)
4518
4519 self.fake_nodepool.paused = False
4520 self.waitUntilSettled()
4521
4522 self.assertEqual(A.data['status'], 'NEW')
4523 self.assertEqual(A.reported, 2)
4524 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4525 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4526 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4527
Arieb6f068c2016-10-09 13:11:06 +03004528
James E. Blaira002b032017-04-18 10:35:48 -07004529class TestExecutor(ZuulTestCase):
4530 tenant_config_file = 'config/single-tenant/main.yaml'
4531
4532 def assertFinalState(self):
4533 # In this test, we expect to shut down in a non-final state,
4534 # so skip these checks.
4535 pass
4536
4537 def assertCleanShutdown(self):
4538 self.log.debug("Assert clean shutdown")
4539
4540 # After shutdown, make sure no jobs are running
4541 self.assertEqual({}, self.executor_server.job_workers)
4542
4543 # Make sure that git.Repo objects have been garbage collected.
4544 repos = []
4545 gc.collect()
4546 for obj in gc.get_objects():
4547 if isinstance(obj, git.Repo):
4548 self.log.debug("Leaked git repo object: %s" % repr(obj))
4549 repos.append(obj)
4550 self.assertEqual(len(repos), 0)
4551
4552 def test_executor_shutdown(self):
4553 "Test that the executor can shut down with jobs running"
4554
4555 self.executor_server.hold_jobs_in_build = True
4556 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004557 A.addApproval('Code-Review', 2)
4558 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaira002b032017-04-18 10:35:48 -07004559 self.waitUntilSettled()
4560
4561
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004562class TestDependencyGraph(ZuulTestCase):
4563 tenant_config_file = 'config/dependency-graph/main.yaml'
4564
4565 def test_dependeny_graph_dispatch_jobs_once(self):
4566 "Test a job in a dependency graph is queued only once"
4567 # Job dependencies, starting with A
4568 # A
4569 # / \
4570 # B C
4571 # / \ / \
4572 # D F E
4573 # |
4574 # G
4575
4576 self.executor_server.hold_jobs_in_build = True
4577 change = self.fake_gerrit.addFakeChange(
4578 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004579 change.addApproval('Code-Review', 2)
4580 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004581
4582 self.waitUntilSettled()
4583 self.assertEqual([b.name for b in self.builds], ['A'])
4584
4585 self.executor_server.release('A')
4586 self.waitUntilSettled()
4587 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4588
4589 self.executor_server.release('B')
4590 self.waitUntilSettled()
4591 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4592
4593 self.executor_server.release('D')
4594 self.waitUntilSettled()
4595 self.assertEqual([b.name for b in self.builds], ['C'])
4596
4597 self.executor_server.release('C')
4598 self.waitUntilSettled()
4599 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4600
4601 self.executor_server.release('F')
4602 self.waitUntilSettled()
4603 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4604
4605 self.executor_server.release('G')
4606 self.waitUntilSettled()
4607 self.assertEqual([b.name for b in self.builds], ['E'])
4608
4609 self.executor_server.release('E')
4610 self.waitUntilSettled()
4611 self.assertEqual(len(self.builds), 0)
4612
4613 self.executor_server.hold_jobs_in_build = False
4614 self.executor_server.release()
4615 self.waitUntilSettled()
4616
4617 self.assertEqual(len(self.builds), 0)
4618 self.assertEqual(len(self.history), 7)
4619
4620 self.assertEqual(change.data['status'], 'MERGED')
4621 self.assertEqual(change.reported, 2)
4622
4623 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4624 "Test that a job waits till all dependencies are successful"
4625 # Job dependencies, starting with A
4626 # A
4627 # / \
4628 # B C*
4629 # / \ / \
4630 # D F E
4631 # |
4632 # G
4633
4634 self.executor_server.hold_jobs_in_build = True
4635 change = self.fake_gerrit.addFakeChange(
4636 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004637 change.addApproval('Code-Review', 2)
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004638
4639 self.executor_server.failJob('C', change)
4640
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004641 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004642
4643 self.waitUntilSettled()
4644 self.assertEqual([b.name for b in self.builds], ['A'])
4645
4646 self.executor_server.release('A')
4647 self.waitUntilSettled()
4648 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4649
4650 self.executor_server.release('B')
4651 self.waitUntilSettled()
4652 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4653
4654 self.executor_server.release('D')
4655 self.waitUntilSettled()
4656 self.assertEqual([b.name for b in self.builds], ['C'])
4657
4658 self.executor_server.release('C')
4659 self.waitUntilSettled()
4660 self.assertEqual(len(self.builds), 0)
4661
4662 self.executor_server.hold_jobs_in_build = False
4663 self.executor_server.release()
4664 self.waitUntilSettled()
4665
4666 self.assertEqual(len(self.builds), 0)
4667 self.assertEqual(len(self.history), 4)
4668
4669 self.assertEqual(change.data['status'], 'NEW')
4670 self.assertEqual(change.reported, 2)
4671
4672
Arieb6f068c2016-10-09 13:11:06 +03004673class TestDuplicatePipeline(ZuulTestCase):
4674 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4675
4676 def test_duplicate_pipelines(self):
4677 "Test that a change matching multiple pipelines works"
4678
4679 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4680 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4681 self.waitUntilSettled()
4682
4683 self.assertHistory([
4684 dict(name='project-test1', result='SUCCESS', changes='1,1',
4685 pipeline='dup1'),
4686 dict(name='project-test1', result='SUCCESS', changes='1,1',
4687 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004688 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004689
4690 self.assertEqual(len(A.messages), 2)
4691
Paul Belangera46a3742016-11-09 14:23:03 -05004692 if 'dup1' in A.messages[0]:
4693 self.assertIn('dup1', A.messages[0])
4694 self.assertNotIn('dup2', A.messages[0])
4695 self.assertIn('project-test1', A.messages[0])
4696 self.assertIn('dup2', A.messages[1])
4697 self.assertNotIn('dup1', A.messages[1])
4698 self.assertIn('project-test1', A.messages[1])
4699 else:
4700 self.assertIn('dup1', A.messages[1])
4701 self.assertNotIn('dup2', A.messages[1])
4702 self.assertIn('project-test1', A.messages[1])
4703 self.assertIn('dup2', A.messages[0])
4704 self.assertNotIn('dup1', A.messages[0])
4705 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004706
4707
Paul Belanger86085b32016-11-03 12:48:57 -04004708class TestSchedulerTemplatedProject(ZuulTestCase):
4709 tenant_config_file = 'config/templated-project/main.yaml'
4710
Paul Belanger174a8272017-03-14 13:20:10 -04004711 def test_job_from_templates_executed(self):
4712 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004713
4714 A = self.fake_gerrit.addFakeChange(
4715 'org/templated-project', 'master', 'A')
4716 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4717 self.waitUntilSettled()
4718
4719 self.assertEqual(self.getJobFromHistory('project-test1').result,
4720 'SUCCESS')
4721 self.assertEqual(self.getJobFromHistory('project-test2').result,
4722 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004723
4724 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004725 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004726
4727 A = self.fake_gerrit.addFakeChange(
4728 'org/layered-project', 'master', 'A')
4729 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4730 self.waitUntilSettled()
4731
4732 self.assertEqual(self.getJobFromHistory('project-test1').result,
4733 'SUCCESS')
4734 self.assertEqual(self.getJobFromHistory('project-test2').result,
4735 'SUCCESS')
4736 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4737 ).result, 'SUCCESS')
4738 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4739 ).result, 'SUCCESS')
4740 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4741 ).result, 'SUCCESS')
4742 self.assertEqual(self.getJobFromHistory('project-test6').result,
4743 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004744
4745
4746class TestSchedulerSuccessURL(ZuulTestCase):
4747 tenant_config_file = 'config/success-url/main.yaml'
4748
4749 def test_success_url(self):
4750 "Ensure bad build params are ignored"
4751 self.sched.reconfigure(self.config)
4752 self.init_repo('org/docs')
4753
4754 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4755 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4756 self.waitUntilSettled()
4757
4758 # Both builds ran: docs-draft-test + docs-draft-test2
4759 self.assertEqual(len(self.history), 2)
4760
4761 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004762 for build in self.history:
4763 if build.name == 'docs-draft-test':
4764 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06004765 elif build.name == 'docs-draft-test2':
4766 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08004767
4768 # Two msgs: 'Starting...' + results
4769 self.assertEqual(len(self.smtp_messages), 2)
4770 body = self.smtp_messages[1]['body'].splitlines()
4771 self.assertEqual('Build succeeded.', body[0])
4772
4773 self.assertIn(
4774 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4775 'docs-draft-test/%s/publish-docs/' % uuid,
4776 body[2])
4777
Paul Belanger174a8272017-03-14 13:20:10 -04004778 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004779 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05004780 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
4781 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06004782 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08004783 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004784
4785
Adam Gandelman4da00f62016-12-09 15:47:33 -08004786class TestSchedulerMerges(ZuulTestCase):
4787 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004788
4789 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004790 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004791 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004792 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004793 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4794 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4795 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004796 A.addApproval('Code-Review', 2)
4797 B.addApproval('Code-Review', 2)
4798 C.addApproval('Code-Review', 2)
4799 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4800 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4801 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman8bd57102016-12-02 12:58:42 -08004802 self.waitUntilSettled()
4803
4804 build = self.builds[-1]
James E. Blair2a535672017-04-27 12:03:15 -07004805 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4806 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004807 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004808 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman8bd57102016-12-02 12:58:42 -08004809 repo_messages.reverse()
4810
Paul Belanger174a8272017-03-14 13:20:10 -04004811 self.executor_server.hold_jobs_in_build = False
4812 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004813 self.waitUntilSettled()
4814
4815 return repo_messages
4816
4817 def _test_merge(self, mode):
4818 us_path = os.path.join(
4819 self.upstream_root, 'org/project-%s' % mode)
4820 expected_messages = [
4821 'initial commit',
4822 'add content from fixture',
4823 # the intermediate commits order is nondeterministic
4824 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4825 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4826 ]
4827 result = self._test_project_merge_mode(mode)
4828 self.assertEqual(result[:2], expected_messages[:2])
4829 self.assertEqual(result[-2:], expected_messages[-2:])
4830
4831 def test_project_merge_mode_merge(self):
4832 self._test_merge('merge')
4833
4834 def test_project_merge_mode_merge_resolve(self):
4835 self._test_merge('merge-resolve')
4836
4837 def test_project_merge_mode_cherrypick(self):
4838 expected_messages = [
4839 'initial commit',
4840 'add content from fixture',
4841 'A-1',
4842 'B-1',
4843 'C-1']
4844 result = self._test_project_merge_mode('cherry-pick')
4845 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004846
4847 def test_merge_branch(self):
4848 "Test that the right commits are on alternate branches"
4849 self.create_branch('org/project-merge-branches', 'mp')
4850
Paul Belanger174a8272017-03-14 13:20:10 -04004851 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004852 A = self.fake_gerrit.addFakeChange(
4853 'org/project-merge-branches', 'mp', 'A')
4854 B = self.fake_gerrit.addFakeChange(
4855 'org/project-merge-branches', 'mp', 'B')
4856 C = self.fake_gerrit.addFakeChange(
4857 'org/project-merge-branches', 'mp', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004858 A.addApproval('Code-Review', 2)
4859 B.addApproval('Code-Review', 2)
4860 C.addApproval('Code-Review', 2)
4861 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4862 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4863 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08004864 self.waitUntilSettled()
4865
Paul Belanger174a8272017-03-14 13:20:10 -04004866 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004867 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004868 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004869 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004870 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004871 self.waitUntilSettled()
4872
4873 build = self.builds[-1]
James E. Blair3b222492017-07-21 15:17:37 -07004874 self.assertEqual(build.parameters['zuul']['branch'], 'mp')
James E. Blair2a535672017-04-27 12:03:15 -07004875 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4876 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004877 repo = git.Repo(path)
4878
James E. Blair247cab72017-07-20 16:52:36 -07004879 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004880 repo_messages.reverse()
4881 correct_messages = [
4882 'initial commit',
4883 'add content from fixture',
4884 'mp commit',
4885 'A-1', 'B-1', 'C-1']
4886 self.assertEqual(repo_messages, correct_messages)
4887
Paul Belanger174a8272017-03-14 13:20:10 -04004888 self.executor_server.hold_jobs_in_build = False
4889 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004890 self.waitUntilSettled()
4891
4892 def test_merge_multi_branch(self):
4893 "Test that dependent changes on multiple branches are merged"
4894 self.create_branch('org/project-merge-branches', 'mp')
4895
Paul Belanger174a8272017-03-14 13:20:10 -04004896 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004897 A = self.fake_gerrit.addFakeChange(
4898 'org/project-merge-branches', 'master', 'A')
4899 B = self.fake_gerrit.addFakeChange(
4900 'org/project-merge-branches', 'mp', 'B')
4901 C = self.fake_gerrit.addFakeChange(
4902 'org/project-merge-branches', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004903 A.addApproval('Code-Review', 2)
4904 B.addApproval('Code-Review', 2)
4905 C.addApproval('Code-Review', 2)
4906 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4907 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4908 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08004909 self.waitUntilSettled()
4910
4911 job_A = None
4912 for job in self.builds:
4913 if 'project-merge' in job.name:
4914 job_A = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08004915
James E. Blair2a535672017-04-27 12:03:15 -07004916 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
4917 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004918 repo = git.Repo(path)
4919 repo_messages = [c.message.strip()
James E. Blair247cab72017-07-20 16:52:36 -07004920 for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004921 repo_messages.reverse()
4922 correct_messages = [
4923 'initial commit', 'add content from fixture', 'A-1']
4924 self.assertEqual(repo_messages, correct_messages)
4925
Paul Belanger174a8272017-03-14 13:20:10 -04004926 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004927 self.waitUntilSettled()
4928
4929 job_B = None
4930 for job in self.builds:
4931 if 'project-merge' in job.name:
4932 job_B = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08004933
James E. Blair2a535672017-04-27 12:03:15 -07004934 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
4935 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004936 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004937 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004938 repo_messages.reverse()
4939 correct_messages = [
4940 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4941 self.assertEqual(repo_messages, correct_messages)
4942
Paul Belanger174a8272017-03-14 13:20:10 -04004943 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004944 self.waitUntilSettled()
4945
4946 job_C = None
4947 for job in self.builds:
4948 if 'project-merge' in job.name:
4949 job_C = job
James E. Blairb1345832017-07-21 14:51:35 -07004950
James E. Blair2a535672017-04-27 12:03:15 -07004951 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
4952 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004953 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004954 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004955
4956 repo_messages.reverse()
4957 correct_messages = [
4958 'initial commit', 'add content from fixture',
4959 'A-1', 'C-1']
4960 # Ensure the right commits are in the history for this ref
4961 self.assertEqual(repo_messages, correct_messages)
4962
Paul Belanger174a8272017-03-14 13:20:10 -04004963 self.executor_server.hold_jobs_in_build = False
4964 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004965 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01004966
4967
James E. Blair9ea0d0b2017-04-20 09:27:15 -07004968class TestSemaphore(ZuulTestCase):
4969 tenant_config_file = 'config/semaphore/main.yaml'
4970
4971 def test_semaphore_one(self):
4972 "Test semaphores with max=1 (mutex)"
4973 tenant = self.sched.abide.tenants.get('tenant-one')
4974
4975 self.executor_server.hold_jobs_in_build = True
4976
4977 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4978 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4979 self.assertFalse('test-semaphore' in
4980 tenant.semaphore_handler.semaphores)
4981
4982 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4983 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4984 self.waitUntilSettled()
4985
4986 self.assertEqual(len(self.builds), 3)
4987 self.assertEqual(self.builds[0].name, 'project-test1')
4988 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
4989 self.assertEqual(self.builds[2].name, 'project-test1')
4990
4991 self.executor_server.release('semaphore-one-test1')
4992 self.waitUntilSettled()
4993
4994 self.assertEqual(len(self.builds), 3)
4995 self.assertEqual(self.builds[0].name, 'project-test1')
4996 self.assertEqual(self.builds[1].name, 'project-test1')
4997 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4998 self.assertTrue('test-semaphore' in
4999 tenant.semaphore_handler.semaphores)
5000
5001 self.executor_server.release('semaphore-one-test2')
5002 self.waitUntilSettled()
5003
5004 self.assertEqual(len(self.builds), 3)
5005 self.assertEqual(self.builds[0].name, 'project-test1')
5006 self.assertEqual(self.builds[1].name, 'project-test1')
5007 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
5008 self.assertTrue('test-semaphore' in
5009 tenant.semaphore_handler.semaphores)
5010
5011 self.executor_server.release('semaphore-one-test1')
5012 self.waitUntilSettled()
5013
5014 self.assertEqual(len(self.builds), 3)
5015 self.assertEqual(self.builds[0].name, 'project-test1')
5016 self.assertEqual(self.builds[1].name, 'project-test1')
5017 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5018 self.assertTrue('test-semaphore' in
5019 tenant.semaphore_handler.semaphores)
5020
5021 self.executor_server.release('semaphore-one-test2')
5022 self.waitUntilSettled()
5023
5024 self.assertEqual(len(self.builds), 2)
5025 self.assertEqual(self.builds[0].name, 'project-test1')
5026 self.assertEqual(self.builds[1].name, 'project-test1')
5027 self.assertFalse('test-semaphore' in
5028 tenant.semaphore_handler.semaphores)
5029
5030 self.executor_server.hold_jobs_in_build = False
5031 self.executor_server.release()
5032
5033 self.waitUntilSettled()
5034 self.assertEqual(len(self.builds), 0)
5035
5036 self.assertEqual(A.reported, 1)
5037 self.assertEqual(B.reported, 1)
5038 self.assertFalse('test-semaphore' in
5039 tenant.semaphore_handler.semaphores)
5040
5041 def test_semaphore_two(self):
5042 "Test semaphores with max>1"
5043 tenant = self.sched.abide.tenants.get('tenant-one')
5044
5045 self.executor_server.hold_jobs_in_build = True
5046 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5047 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5048 self.assertFalse('test-semaphore-two' in
5049 tenant.semaphore_handler.semaphores)
5050
5051 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5052 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5053 self.waitUntilSettled()
5054
5055 self.assertEqual(len(self.builds), 4)
5056 self.assertEqual(self.builds[0].name, 'project-test1')
5057 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
5058 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5059 self.assertEqual(self.builds[3].name, 'project-test1')
5060 self.assertTrue('test-semaphore-two' in
5061 tenant.semaphore_handler.semaphores)
5062 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5063 'test-semaphore-two', [])), 2)
5064
5065 self.executor_server.release('semaphore-two-test1')
5066 self.waitUntilSettled()
5067
5068 self.assertEqual(len(self.builds), 4)
5069 self.assertEqual(self.builds[0].name, 'project-test1')
5070 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
5071 self.assertEqual(self.builds[2].name, 'project-test1')
5072 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
5073 self.assertTrue('test-semaphore-two' in
5074 tenant.semaphore_handler.semaphores)
5075 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5076 'test-semaphore-two', [])), 2)
5077
5078 self.executor_server.release('semaphore-two-test2')
5079 self.waitUntilSettled()
5080
5081 self.assertEqual(len(self.builds), 4)
5082 self.assertEqual(self.builds[0].name, 'project-test1')
5083 self.assertEqual(self.builds[1].name, 'project-test1')
5084 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5085 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
5086 self.assertTrue('test-semaphore-two' in
5087 tenant.semaphore_handler.semaphores)
5088 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5089 'test-semaphore-two', [])), 2)
5090
5091 self.executor_server.release('semaphore-two-test1')
5092 self.waitUntilSettled()
5093
5094 self.assertEqual(len(self.builds), 3)
5095 self.assertEqual(self.builds[0].name, 'project-test1')
5096 self.assertEqual(self.builds[1].name, 'project-test1')
5097 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
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', [])), 1)
5102
5103 self.executor_server.release('semaphore-two-test2')
5104 self.waitUntilSettled()
5105
5106 self.assertEqual(len(self.builds), 2)
5107 self.assertEqual(self.builds[0].name, 'project-test1')
5108 self.assertEqual(self.builds[1].name, 'project-test1')
5109 self.assertFalse('test-semaphore-two' in
5110 tenant.semaphore_handler.semaphores)
5111
5112 self.executor_server.hold_jobs_in_build = False
5113 self.executor_server.release()
5114
5115 self.waitUntilSettled()
5116 self.assertEqual(len(self.builds), 0)
5117
5118 self.assertEqual(A.reported, 1)
5119 self.assertEqual(B.reported, 1)
5120
5121 def test_semaphore_abandon(self):
5122 "Test abandon with job semaphores"
5123 self.executor_server.hold_jobs_in_build = True
5124 tenant = self.sched.abide.tenants.get('tenant-one')
5125 check_pipeline = tenant.layout.pipelines['check']
5126
5127 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5128 self.assertFalse('test-semaphore' in
5129 tenant.semaphore_handler.semaphores)
5130
5131 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5132 self.waitUntilSettled()
5133
5134 self.assertTrue('test-semaphore' in
5135 tenant.semaphore_handler.semaphores)
5136
5137 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5138 self.waitUntilSettled()
5139
5140 # The check pipeline should be empty
5141 items = check_pipeline.getAllItems()
5142 self.assertEqual(len(items), 0)
5143
5144 # The semaphore should be released
5145 self.assertFalse('test-semaphore' in
5146 tenant.semaphore_handler.semaphores)
5147
5148 self.executor_server.hold_jobs_in_build = False
5149 self.executor_server.release()
5150 self.waitUntilSettled()
5151
Tobias Henkel0f714002017-06-30 23:30:52 +02005152 def test_semaphore_new_patchset(self):
5153 "Test new patchset with job semaphores"
5154 self.executor_server.hold_jobs_in_build = True
5155 tenant = self.sched.abide.tenants.get('tenant-one')
5156 check_pipeline = tenant.layout.pipelines['check']
5157
5158 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5159 self.assertFalse('test-semaphore' in
5160 tenant.semaphore_handler.semaphores)
5161
5162 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5163 self.waitUntilSettled()
5164
5165 self.assertTrue('test-semaphore' in
5166 tenant.semaphore_handler.semaphores)
5167 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5168 self.assertEqual(len(semaphore), 1)
5169
5170 A.addPatchset()
5171 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
5172 self.waitUntilSettled()
5173
5174 self.assertTrue('test-semaphore' in
5175 tenant.semaphore_handler.semaphores)
5176 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5177 self.assertEqual(len(semaphore), 1)
5178
5179 items = check_pipeline.getAllItems()
5180 self.assertEqual(items[0].change.number, '1')
5181 self.assertEqual(items[0].change.patchset, '2')
5182 self.assertTrue(items[0].live)
5183
5184 self.executor_server.hold_jobs_in_build = False
5185 self.executor_server.release()
5186 self.waitUntilSettled()
5187
5188 # The semaphore should be released
5189 self.assertFalse('test-semaphore' in
5190 tenant.semaphore_handler.semaphores)
5191
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005192 def test_semaphore_reconfigure(self):
5193 "Test reconfigure with job semaphores"
5194 self.executor_server.hold_jobs_in_build = True
5195 tenant = self.sched.abide.tenants.get('tenant-one')
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
5206 # reconfigure without layout change
5207 self.sched.reconfigure(self.config)
5208 self.waitUntilSettled()
5209 tenant = self.sched.abide.tenants.get('tenant-one')
5210
5211 # semaphore still must be held
5212 self.assertTrue('test-semaphore' in
5213 tenant.semaphore_handler.semaphores)
5214
5215 self.commitConfigUpdate(
5216 'common-config',
5217 'config/semaphore/zuul-reconfiguration.yaml')
5218 self.sched.reconfigure(self.config)
5219 self.waitUntilSettled()
5220 tenant = self.sched.abide.tenants.get('tenant-one')
5221
5222 self.executor_server.release('project-test1')
5223 self.waitUntilSettled()
5224
5225 # There should be no builds anymore
5226 self.assertEqual(len(self.builds), 0)
5227
5228 # The semaphore should be released
5229 self.assertFalse('test-semaphore' in
5230 tenant.semaphore_handler.semaphores)
5231
5232
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005233class TestSemaphoreMultiTenant(ZuulTestCase):
5234 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5235
5236 def test_semaphore_tenant_isolation(self):
5237 "Test semaphores in multiple tenants"
5238
5239 self.waitUntilSettled()
5240 tenant_one = self.sched.abide.tenants.get('tenant-one')
5241 tenant_two = self.sched.abide.tenants.get('tenant-two')
5242
5243 self.executor_server.hold_jobs_in_build = True
5244 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5245 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5246 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5247 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5248 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5249 self.assertFalse('test-semaphore' in
5250 tenant_one.semaphore_handler.semaphores)
5251 self.assertFalse('test-semaphore' in
5252 tenant_two.semaphore_handler.semaphores)
5253
5254 # add patches to project1 of tenant-one
5255 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5256 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5257 self.waitUntilSettled()
5258
5259 # one build of project1-test1 must run
5260 # semaphore of tenant-one must be acquired once
5261 # semaphore of tenant-two must not be acquired
5262 self.assertEqual(len(self.builds), 1)
5263 self.assertEqual(self.builds[0].name, 'project1-test1')
5264 self.assertTrue('test-semaphore' in
5265 tenant_one.semaphore_handler.semaphores)
5266 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5267 'test-semaphore', [])), 1)
5268 self.assertFalse('test-semaphore' in
5269 tenant_two.semaphore_handler.semaphores)
5270
5271 # add patches to project2 of tenant-two
5272 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5273 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5274 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5275 self.waitUntilSettled()
5276
5277 # one build of project1-test1 must run
5278 # two builds of project2-test1 must run
5279 # semaphore of tenant-one must be acquired once
5280 # semaphore of tenant-two must be acquired twice
5281 self.assertEqual(len(self.builds), 3)
5282 self.assertEqual(self.builds[0].name, 'project1-test1')
5283 self.assertEqual(self.builds[1].name, 'project2-test1')
5284 self.assertEqual(self.builds[2].name, 'project2-test1')
5285 self.assertTrue('test-semaphore' in
5286 tenant_one.semaphore_handler.semaphores)
5287 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5288 'test-semaphore', [])), 1)
5289 self.assertTrue('test-semaphore' in
5290 tenant_two.semaphore_handler.semaphores)
5291 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5292 'test-semaphore', [])), 2)
5293
5294 self.executor_server.release('project1-test1')
5295 self.waitUntilSettled()
5296
5297 # one build of project1-test1 must run
5298 # two builds of project2-test1 must run
5299 # semaphore of tenant-one must be acquired once
5300 # semaphore of tenant-two must be acquired twice
5301 self.assertEqual(len(self.builds), 3)
5302 self.assertEqual(self.builds[0].name, 'project2-test1')
5303 self.assertEqual(self.builds[1].name, 'project2-test1')
5304 self.assertEqual(self.builds[2].name, 'project1-test1')
5305 self.assertTrue('test-semaphore' in
5306 tenant_one.semaphore_handler.semaphores)
5307 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5308 'test-semaphore', [])), 1)
5309 self.assertTrue('test-semaphore' in
5310 tenant_two.semaphore_handler.semaphores)
5311 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5312 'test-semaphore', [])), 2)
5313
5314 self.executor_server.release('project2-test1')
5315 self.waitUntilSettled()
5316
5317 # one build of project1-test1 must run
5318 # one build of project2-test1 must run
5319 # semaphore of tenant-one must be acquired once
5320 # semaphore of tenant-two must be acquired once
5321 self.assertEqual(len(self.builds), 2)
5322 self.assertTrue('test-semaphore' in
5323 tenant_one.semaphore_handler.semaphores)
5324 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5325 'test-semaphore', [])), 1)
5326 self.assertTrue('test-semaphore' in
5327 tenant_two.semaphore_handler.semaphores)
5328 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5329 'test-semaphore', [])), 1)
5330
5331 self.executor_server.hold_jobs_in_build = False
5332 self.executor_server.release()
5333
5334 self.waitUntilSettled()
5335
5336 # no build must run
5337 # semaphore of tenant-one must not be acquired
5338 # semaphore of tenant-two must not be acquired
5339 self.assertEqual(len(self.builds), 0)
5340 self.assertFalse('test-semaphore' in
5341 tenant_one.semaphore_handler.semaphores)
5342 self.assertFalse('test-semaphore' in
5343 tenant_two.semaphore_handler.semaphores)
5344
5345 self.assertEqual(A.reported, 1)
5346 self.assertEqual(B.reported, 1)
5347
5348
5349class TestSemaphoreInRepo(ZuulTestCase):
Tobias Henkelabf973e2017-07-28 10:07:34 +02005350 config_file = 'zuul-connections-gerrit-and-github.conf'
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005351 tenant_config_file = 'config/in-repo/main.yaml'
5352
5353 def test_semaphore_in_repo(self):
5354 "Test semaphores in repo config"
5355
5356 # This tests dynamic semaphore handling in project repos. The semaphore
5357 # max value should not be evaluated dynamically but must be updated
5358 # after the change lands.
5359
5360 self.waitUntilSettled()
5361 tenant = self.sched.abide.tenants.get('tenant-one')
5362
5363 in_repo_conf = textwrap.dedent(
5364 """
5365 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +02005366 name: project-test1
5367
5368 - job:
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005369 name: project-test2
5370 semaphore: test-semaphore
5371
5372 - project:
5373 name: org/project
5374 tenant-one-gate:
5375 jobs:
5376 - project-test2
5377
5378 # the max value in dynamic layout must be ignored
5379 - semaphore:
5380 name: test-semaphore
5381 max: 2
5382 """)
5383
5384 in_repo_playbook = textwrap.dedent(
5385 """
5386 - hosts: all
5387 tasks: []
5388 """)
5389
5390 file_dict = {'.zuul.yaml': in_repo_conf,
5391 'playbooks/project-test2.yaml': in_repo_playbook}
5392 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5393 files=file_dict)
5394 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5395 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5396 B.setDependsOn(A, 1)
5397 C.setDependsOn(A, 1)
5398
5399 self.executor_server.hold_jobs_in_build = True
5400
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005401 A.addApproval('Code-Review', 2)
5402 B.addApproval('Code-Review', 2)
5403 C.addApproval('Code-Review', 2)
5404 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5405 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5406 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005407 self.waitUntilSettled()
5408
5409 # check that the layout in a queue item still has max value of 1
5410 # for test-semaphore
5411 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5412 queue = None
5413 for queue_candidate in pipeline.queues:
5414 if queue_candidate.name == 'org/project':
5415 queue = queue_candidate
5416 break
5417 queue_item = queue.queue[0]
5418 item_dynamic_layout = queue_item.current_build_set.layout
5419 dynamic_test_semaphore = \
5420 item_dynamic_layout.semaphores.get('test-semaphore')
5421 self.assertEqual(dynamic_test_semaphore.max, 1)
5422
5423 # one build must be in queue, one semaphores acquired
5424 self.assertEqual(len(self.builds), 1)
5425 self.assertEqual(self.builds[0].name, 'project-test2')
5426 self.assertTrue('test-semaphore' in
5427 tenant.semaphore_handler.semaphores)
5428 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5429 'test-semaphore', [])), 1)
5430
5431 self.executor_server.release('project-test2')
5432 self.waitUntilSettled()
5433
5434 # change A must be merged
5435 self.assertEqual(A.data['status'], 'MERGED')
5436 self.assertEqual(A.reported, 2)
5437
5438 # send change-merged event as the gerrit mock doesn't send it
5439 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5440 self.waitUntilSettled()
5441
5442 # now that change A was merged, the new semaphore max must be effective
5443 tenant = self.sched.abide.tenants.get('tenant-one')
5444 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5445
5446 # two builds must be in queue, two semaphores acquired
5447 self.assertEqual(len(self.builds), 2)
5448 self.assertEqual(self.builds[0].name, 'project-test2')
5449 self.assertEqual(self.builds[1].name, 'project-test2')
5450 self.assertTrue('test-semaphore' in
5451 tenant.semaphore_handler.semaphores)
5452 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5453 'test-semaphore', [])), 2)
5454
5455 self.executor_server.release('project-test2')
5456 self.waitUntilSettled()
5457
5458 self.assertEqual(len(self.builds), 0)
5459 self.assertFalse('test-semaphore' in
5460 tenant.semaphore_handler.semaphores)
5461
5462 self.executor_server.hold_jobs_in_build = False
5463 self.executor_server.release()
5464
5465 self.waitUntilSettled()
5466 self.assertEqual(len(self.builds), 0)
5467
5468 self.assertEqual(A.reported, 2)
5469 self.assertEqual(B.reported, 2)
5470 self.assertEqual(C.reported, 2)