blob: e7cc93dc995889382a7d1e1051fdff52dd099fa6 [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 Shrewsburyf9af9df2017-08-01 15:19:26 -04001473 self.assertEqual(held_node['comment'], "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. Blair78ae4782017-08-02 14:19:10 -07001895 # If APScheduler is in mid-event when we remove the job, we
1896 # can end up with one more event firing, so give it an extra
1897 # second to settle.
1898 time.sleep(1)
1899 self.waitUntilSettled()
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001900
James E. Blair21037782017-07-19 11:56:55 -07001901 self.assertEqual(len(self.builds), 1, "One timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001902
1903 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1904 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1905 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07001906 self.assertEqual(len(self.builds), 2, "One change plus one timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001907
1908 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1909 self.waitUntilSettled()
1910
James E. Blair21037782017-07-19 11:56:55 -07001911 self.assertEqual(len(self.builds), 1, "One timer job remains")
Steve Varnau7b78b312015-04-03 14:49:46 -07001912
Paul Belanger174a8272017-03-14 13:20:10 -04001913 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001914 self.waitUntilSettled()
1915
James E. Blair2fa50962013-01-30 21:50:41 -08001916 def test_new_patchset_dequeues_old_on_head(self):
1917 "Test that a new patchset causes the old to be dequeued (at head)"
1918 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001919 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001920 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1921 M.setMerged()
1922 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1923 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1924 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1925 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001926 A.addApproval('Code-Review', 2)
1927 B.addApproval('Code-Review', 2)
1928 C.addApproval('Code-Review', 2)
1929 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001930
1931 C.setDependsOn(B, 1)
1932 B.setDependsOn(A, 1)
1933 A.setDependsOn(M, 1)
1934
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001935 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1936 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1937 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1938 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001939 self.waitUntilSettled()
1940
1941 A.addPatchset()
1942 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1943 self.waitUntilSettled()
1944
Paul Belanger174a8272017-03-14 13:20:10 -04001945 self.executor_server.hold_jobs_in_build = False
1946 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001947 self.waitUntilSettled()
1948
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001949 self.assertEqual(A.data['status'], 'NEW')
1950 self.assertEqual(A.reported, 2)
1951 self.assertEqual(B.data['status'], 'NEW')
1952 self.assertEqual(B.reported, 2)
1953 self.assertEqual(C.data['status'], 'NEW')
1954 self.assertEqual(C.reported, 2)
1955 self.assertEqual(D.data['status'], 'MERGED')
1956 self.assertEqual(D.reported, 2)
1957 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001958
1959 def test_new_patchset_dequeues_old_without_dependents(self):
1960 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001961 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001962 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1963 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1964 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001965 A.addApproval('Code-Review', 2)
1966 B.addApproval('Code-Review', 2)
1967 C.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001968
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001969 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1970 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1971 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001972 self.waitUntilSettled()
1973
1974 B.addPatchset()
1975 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1976 self.waitUntilSettled()
1977
Paul Belanger174a8272017-03-14 13:20:10 -04001978 self.executor_server.hold_jobs_in_build = False
1979 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001980 self.waitUntilSettled()
1981
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001982 self.assertEqual(A.data['status'], 'MERGED')
1983 self.assertEqual(A.reported, 2)
1984 self.assertEqual(B.data['status'], 'NEW')
1985 self.assertEqual(B.reported, 2)
1986 self.assertEqual(C.data['status'], 'MERGED')
1987 self.assertEqual(C.reported, 2)
1988 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001989
1990 def test_new_patchset_dequeues_old_independent_queue(self):
1991 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001992 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001993 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1994 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1995 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1996 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1997 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1998 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1999 self.waitUntilSettled()
2000
2001 B.addPatchset()
2002 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2003 self.waitUntilSettled()
2004
Paul Belanger174a8272017-03-14 13:20:10 -04002005 self.executor_server.hold_jobs_in_build = False
2006 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002007 self.waitUntilSettled()
2008
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002009 self.assertEqual(A.data['status'], 'NEW')
2010 self.assertEqual(A.reported, 1)
2011 self.assertEqual(B.data['status'], 'NEW')
2012 self.assertEqual(B.reported, 1)
2013 self.assertEqual(C.data['status'], 'NEW')
2014 self.assertEqual(C.reported, 1)
2015 self.assertEqual(len(self.history), 10)
2016 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002017
James E. Blaircc020532017-04-19 13:04:44 -07002018 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07002019 def test_noop_job(self):
2020 "Test that the internal noop job works"
2021 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002022 A.addApproval('Code-Review', 2)
2023 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07002024 self.waitUntilSettled()
2025
2026 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2027 self.assertTrue(self.sched._areAllBuildsComplete())
2028 self.assertEqual(len(self.history), 0)
2029 self.assertEqual(A.data['status'], 'MERGED')
2030 self.assertEqual(A.reported, 2)
2031
James E. Blair06cc3922017-04-19 10:08:10 -07002032 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002033 def test_no_job_project(self):
2034 "Test that reports with no jobs don't get sent"
2035 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
2036 'master', 'A')
2037 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2038 self.waitUntilSettled()
2039
2040 # Change wasn't reported to
2041 self.assertEqual(A.reported, False)
2042
2043 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08002044 tenant = self.sched.abide.tenants.get('tenant-one')
2045 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002046 items = check_pipeline.getAllItems()
2047 self.assertEqual(len(items), 0)
2048
2049 self.assertEqual(len(self.history), 0)
2050
James E. Blair7d0dedc2013-02-21 17:26:09 -08002051 def test_zuul_refs(self):
2052 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04002053 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002054 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2055 M1.setMerged()
2056 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2057 M2.setMerged()
2058
2059 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2060 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2061 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2062 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002063 A.addApproval('Code-Review', 2)
2064 B.addApproval('Code-Review', 2)
2065 C.addApproval('Code-Review', 2)
2066 D.addApproval('Code-Review', 2)
2067 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2068 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2069 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2070 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002071
2072 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002073 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002074 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002075 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002076 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002077 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002078 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002079 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002080 self.waitUntilSettled()
2081
Clint Byrum3343e3e2016-11-15 16:05:03 -08002082 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002083 for x in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07002084 if x.parameters['zuul']['change'] == '3':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002085 a_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002086 elif x.parameters['zuul']['change'] == '4':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002087 b_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002088 elif x.parameters['zuul']['change'] == '5':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002089 c_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002090 elif x.parameters['zuul']['change'] == '6':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002091 d_build = x
2092 if a_build and b_build and c_build and d_build:
2093 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002094
Clint Byrum3343e3e2016-11-15 16:05:03 -08002095 # should have a, not b, and should not be in project2
2096 self.assertTrue(a_build.hasChanges(A))
2097 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002098
Clint Byrum3343e3e2016-11-15 16:05:03 -08002099 # should have a and b, and should not be in project2
2100 self.assertTrue(b_build.hasChanges(A, B))
2101 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002102
Clint Byrum3343e3e2016-11-15 16:05:03 -08002103 # should have a and b in 1, c in 2
2104 self.assertTrue(c_build.hasChanges(A, B, C))
2105 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002106
Clint Byrum3343e3e2016-11-15 16:05:03 -08002107 # should have a and b in 1, c and d in 2
2108 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002109
Paul Belanger174a8272017-03-14 13:20:10 -04002110 self.executor_server.hold_jobs_in_build = False
2111 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002112 self.waitUntilSettled()
2113
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002114 self.assertEqual(A.data['status'], 'MERGED')
2115 self.assertEqual(A.reported, 2)
2116 self.assertEqual(B.data['status'], 'MERGED')
2117 self.assertEqual(B.reported, 2)
2118 self.assertEqual(C.data['status'], 'MERGED')
2119 self.assertEqual(C.reported, 2)
2120 self.assertEqual(D.data['status'], 'MERGED')
2121 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002122
James E. Blair4a28a882013-08-23 15:17:33 -07002123 def test_rerun_on_error(self):
2124 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002125 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002126 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002127 A.addApproval('Code-Review', 2)
2128 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002129 self.waitUntilSettled()
2130
James E. Blair412fba82017-01-26 15:00:50 -08002131 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002132 self.executor_server.hold_jobs_in_build = False
2133 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002134 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002135 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002136 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2137
James E. Blair412e5582013-04-22 15:50:12 -07002138 def test_statsd(self):
2139 "Test each of the statsd methods used in the scheduler"
2140 import extras
2141 statsd = extras.try_import('statsd.statsd')
2142 statsd.incr('test-incr')
2143 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002144 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002145 self.assertReportedStat('test-incr', '1|c')
2146 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002147 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002148
James E. Blairdad52252014-02-07 16:59:17 -08002149 def test_stuck_job_cleanup(self):
2150 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002151
2152 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002153 self.gearman_server.hold_jobs_in_queue = True
2154 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002155 A.addApproval('Code-Review', 2)
2156 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002157 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002158 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002159 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2160
James E. Blair31cb3e82017-04-20 13:08:56 -07002161 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002162 self.sched.reconfigure(self.config)
2163 self.waitUntilSettled()
2164
James E. Blair18c64442014-03-18 10:14:45 -07002165 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002166 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002167 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002168 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2169 self.assertTrue(self.sched._areAllBuildsComplete())
2170
2171 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002172 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002173 self.assertEqual(self.history[0].result, 'SUCCESS')
2174
James E. Blair879dafb2015-07-17 14:04:49 -07002175 def test_file_head(self):
2176 # This is a regression test for an observed bug. A change
2177 # with a file named "HEAD" in the root directory of the repo
2178 # was processed by a merger. It then was unable to reset the
2179 # repo because of:
2180 # GitCommandError: 'git reset --hard HEAD' returned
2181 # with exit code 128
2182 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2183 # and filename
2184 # Use '--' to separate filenames from revisions'
2185
2186 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002187 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002188 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2189
2190 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2191 self.waitUntilSettled()
2192
2193 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2194 self.waitUntilSettled()
2195
2196 self.assertIn('Build succeeded', A.messages[0])
2197 self.assertIn('Build succeeded', B.messages[0])
2198
James E. Blair70c71582013-03-06 08:50:50 -08002199 def test_file_jobs(self):
2200 "Test that file jobs run only when appropriate"
2201 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002202 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002203 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002204 A.addApproval('Code-Review', 2)
2205 B.addApproval('Code-Review', 2)
2206 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2207 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002208 self.waitUntilSettled()
2209
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002210 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002211 if x.name == 'project-testfile']
2212
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002213 self.assertEqual(len(testfile_jobs), 1)
2214 self.assertEqual(testfile_jobs[0].changes, '1,2')
2215 self.assertEqual(A.data['status'], 'MERGED')
2216 self.assertEqual(A.reported, 2)
2217 self.assertEqual(B.data['status'], 'MERGED')
2218 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002219
Clint Byrum3ee39f32016-11-17 23:45:07 -08002220 def _test_irrelevant_files_jobs(self, should_skip):
2221 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002222 if should_skip:
2223 files = {'ignoreme': 'ignored\n'}
2224 else:
2225 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002226
2227 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002228 'master',
2229 'test irrelevant-files',
2230 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002231 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2232 self.waitUntilSettled()
2233
2234 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002235 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002236
2237 if should_skip:
2238 self.assertEqual([], tested_change_ids)
2239 else:
2240 self.assertIn(change.data['number'], tested_change_ids)
2241
James E. Blaircc324192017-04-19 14:36:08 -07002242 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002243 def test_irrelevant_files_match_skips_job(self):
2244 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002245
James E. Blaircc324192017-04-19 14:36:08 -07002246 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002247 def test_irrelevant_files_no_match_runs_job(self):
2248 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002249
James E. Blaircc324192017-04-19 14:36:08 -07002250 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002251 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002252 files = {'ignoreme': 'ignored\n'}
2253
2254 change = self.fake_gerrit.addFakeChange('org/project',
2255 'master',
2256 'test irrelevant-files',
2257 files=files)
2258 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2259 self.waitUntilSettled()
2260
2261 run_jobs = set([build.name for build in self.history])
2262
2263 self.assertEqual(set(['project-test-nomatch-starts-empty',
2264 'project-test-nomatch-starts-full']), run_jobs)
2265
James E. Blairc8a1e052014-02-25 09:29:26 -08002266 def test_queue_names(self):
2267 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002268 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002269 (trusted, project1) = tenant.getProject('org/project1')
2270 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002271 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2272 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2273 self.assertEqual(q1.name, 'integrated')
2274 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002275
James E. Blair64ed6f22013-07-10 14:07:23 -07002276 def test_queue_precedence(self):
2277 "Test that queue precedence works"
2278
2279 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002280 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002281 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2282 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002283 A.addApproval('Code-Review', 2)
2284 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002285
2286 self.waitUntilSettled()
2287 self.gearman_server.hold_jobs_in_queue = False
2288 self.gearman_server.release()
2289 self.waitUntilSettled()
2290
James E. Blair8de58bd2013-07-18 16:23:33 -07002291 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002292 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002293 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002294 self.waitUntilSettled()
2295
James E. Blair64ed6f22013-07-10 14:07:23 -07002296 self.log.debug(self.history)
2297 self.assertEqual(self.history[0].pipeline, 'gate')
2298 self.assertEqual(self.history[1].pipeline, 'check')
2299 self.assertEqual(self.history[2].pipeline, 'gate')
2300 self.assertEqual(self.history[3].pipeline, 'gate')
2301 self.assertEqual(self.history[4].pipeline, 'check')
2302 self.assertEqual(self.history[5].pipeline, 'check')
2303
Clark Boylana5edbe42014-06-03 16:39:10 -07002304 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002305 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002306 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002307 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002308 A.addApproval('Code-Review', 2)
2309 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002310 self.waitUntilSettled()
2311
Paul Belanger174a8272017-03-14 13:20:10 -04002312 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002313 self.waitUntilSettled()
2314
James E. Blair1843a552013-07-03 14:19:52 -07002315 port = self.webapp.server.socket.getsockname()[1]
2316
Adam Gandelman77a12c72017-02-03 17:43:43 -08002317 req = urllib.request.Request(
2318 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002319 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002320 headers = f.info()
2321 self.assertIn('Content-Length', headers)
2322 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002323 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2324 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002325 self.assertIn('Access-Control-Allow-Origin', headers)
2326 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002327 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002328 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002329 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002330
Paul Belanger174a8272017-03-14 13:20:10 -04002331 self.executor_server.hold_jobs_in_build = False
2332 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002333 self.waitUntilSettled()
2334
2335 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002336 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002337 for p in data['pipelines']:
2338 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002339 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002340 self.assertEqual(q['window'], 20)
2341 else:
2342 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002343 for head in q['heads']:
2344 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002345 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002346 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002347 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002348 status_jobs.append(job)
2349 self.assertEqual('project-merge', status_jobs[0]['name'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002350 # TODO(mordred) pull uuids from self.builds
Monty Taylor51139a02016-05-24 11:28:10 -05002351 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002352 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2353 uuid=status_jobs[0]['uuid']),
2354 status_jobs[0]['url'])
2355 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002356 'finger://{hostname}/{uuid}'.format(
2357 hostname=self.executor_server.hostname,
2358 uuid=status_jobs[0]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002359 status_jobs[0]['finger_url'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002360 # TOOD(mordred) configure a success-url on the base job
Monty Taylor51139a02016-05-24 11:28:10 -05002361 self.assertEqual(
2362 'finger://{hostname}/{uuid}'.format(
2363 hostname=self.executor_server.hostname,
2364 uuid=status_jobs[0]['uuid']),
2365 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002366 self.assertEqual('project-test1', status_jobs[1]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002367 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002368 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2369 uuid=status_jobs[1]['uuid']),
2370 status_jobs[1]['url'])
2371 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002372 'finger://{hostname}/{uuid}'.format(
2373 hostname=self.executor_server.hostname,
2374 uuid=status_jobs[1]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002375 status_jobs[1]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002376 self.assertEqual(
2377 'finger://{hostname}/{uuid}'.format(
2378 hostname=self.executor_server.hostname,
2379 uuid=status_jobs[1]['uuid']),
2380 status_jobs[1]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002381
2382 self.assertEqual('project-test2', status_jobs[2]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002383 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002384 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2385 uuid=status_jobs[2]['uuid']),
2386 status_jobs[2]['url'])
2387 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002388 'finger://{hostname}/{uuid}'.format(
2389 hostname=self.executor_server.hostname,
2390 uuid=status_jobs[2]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002391 status_jobs[2]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002392 self.assertEqual(
2393 'finger://{hostname}/{uuid}'.format(
2394 hostname=self.executor_server.hostname,
2395 uuid=status_jobs[2]['uuid']),
2396 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002397
Tobias Henkel65639f82017-07-10 10:25:42 +02002398 # check job dependencies
2399 self.assertIsNotNone(status_jobs[0]['dependencies'])
2400 self.assertIsNotNone(status_jobs[1]['dependencies'])
2401 self.assertIsNotNone(status_jobs[2]['dependencies'])
2402 self.assertEqual(len(status_jobs[0]['dependencies']), 0)
2403 self.assertEqual(len(status_jobs[1]['dependencies']), 1)
2404 self.assertEqual(len(status_jobs[2]['dependencies']), 1)
2405 self.assertIn('project-merge', status_jobs[1]['dependencies'])
2406 self.assertIn('project-merge', status_jobs[2]['dependencies'])
2407
James E. Blaircdccd972013-07-01 12:10:22 -07002408 def test_live_reconfiguration(self):
2409 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002410 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002411 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002412 A.addApproval('Code-Review', 2)
2413 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002414 self.waitUntilSettled()
2415
2416 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002417 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002418
Paul Belanger174a8272017-03-14 13:20:10 -04002419 self.executor_server.hold_jobs_in_build = False
2420 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002421 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002422 self.assertEqual(self.getJobFromHistory('project-merge').result,
2423 'SUCCESS')
2424 self.assertEqual(self.getJobFromHistory('project-test1').result,
2425 'SUCCESS')
2426 self.assertEqual(self.getJobFromHistory('project-test2').result,
2427 'SUCCESS')
2428 self.assertEqual(A.data['status'], 'MERGED')
2429 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002430
James E. Blair6bc782d2015-07-17 16:20:21 -07002431 def test_live_reconfiguration_merge_conflict(self):
2432 # A real-world bug: a change in a gate queue has a merge
2433 # conflict and a job is added to its project while it's
2434 # sitting in the queue. The job gets added to the change and
2435 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002436 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002437
2438 # This change is fine. It's here to stop the queue long
2439 # enough for the next change to be subject to the
2440 # reconfiguration, as well as to provide a conflict for the
2441 # next change. This change will succeed and merge.
2442 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002443 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002444 A.addApproval('Code-Review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002445
2446 # This change will be in merge conflict. During the
2447 # reconfiguration, we will add a job. We want to make sure
2448 # that doesn't cause it to get stuck.
2449 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002450 B.addPatchset({'conflict': 'B'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002451 B.addApproval('Code-Review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002452
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002453 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2454 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002455
2456 self.waitUntilSettled()
2457
2458 # No jobs have run yet
2459 self.assertEqual(A.data['status'], 'NEW')
2460 self.assertEqual(A.reported, 1)
2461 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002462 self.assertEqual(len(self.history), 0)
2463
2464 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002465 self.commitConfigUpdate('common-config',
2466 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002467 self.sched.reconfigure(self.config)
2468 self.waitUntilSettled()
2469
Paul Belanger174a8272017-03-14 13:20:10 -04002470 self.executor_server.hold_jobs_in_build = False
2471 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002472 self.waitUntilSettled()
2473
2474 self.assertEqual(A.data['status'], 'MERGED')
2475 self.assertEqual(A.reported, 2)
2476 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002477 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002478 self.assertEqual(self.getJobFromHistory('project-merge').result,
2479 'SUCCESS')
2480 self.assertEqual(self.getJobFromHistory('project-test1').result,
2481 'SUCCESS')
2482 self.assertEqual(self.getJobFromHistory('project-test2').result,
2483 'SUCCESS')
2484 self.assertEqual(self.getJobFromHistory('project-test3').result,
2485 'SUCCESS')
2486 self.assertEqual(len(self.history), 4)
2487
James E. Blair400e8fd2015-07-30 17:44:45 -07002488 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002489 # An extrapolation of test_live_reconfiguration_merge_conflict
2490 # that tests a job added to a job tree with a failed root does
2491 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002492 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002493
2494 # This change is fine. It's here to stop the queue long
2495 # enough for the next change to be subject to the
2496 # reconfiguration. This change will succeed and merge.
2497 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002498 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002499 A.addApproval('Code-Review', 2)
2500 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002501 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002502 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002503 self.waitUntilSettled()
2504
2505 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002506 self.executor_server.failJob('project-merge', B)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002507 B.addApproval('Code-Review', 2)
2508 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002509 self.waitUntilSettled()
2510
Paul Belanger174a8272017-03-14 13:20:10 -04002511 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002512 self.waitUntilSettled()
2513
2514 # Both -merge jobs have run, but no others.
2515 self.assertEqual(A.data['status'], 'NEW')
2516 self.assertEqual(A.reported, 1)
2517 self.assertEqual(B.data['status'], 'NEW')
2518 self.assertEqual(B.reported, 1)
2519 self.assertEqual(self.history[0].result, 'SUCCESS')
2520 self.assertEqual(self.history[0].name, 'project-merge')
2521 self.assertEqual(self.history[1].result, 'FAILURE')
2522 self.assertEqual(self.history[1].name, 'project-merge')
2523 self.assertEqual(len(self.history), 2)
2524
2525 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002526 self.commitConfigUpdate('common-config',
2527 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002528 self.sched.reconfigure(self.config)
2529 self.waitUntilSettled()
2530
Paul Belanger174a8272017-03-14 13:20:10 -04002531 self.executor_server.hold_jobs_in_build = False
2532 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002533 self.waitUntilSettled()
2534
2535 self.assertEqual(A.data['status'], 'MERGED')
2536 self.assertEqual(A.reported, 2)
2537 self.assertEqual(B.data['status'], 'NEW')
2538 self.assertEqual(B.reported, 2)
2539 self.assertEqual(self.history[0].result, 'SUCCESS')
2540 self.assertEqual(self.history[0].name, 'project-merge')
2541 self.assertEqual(self.history[1].result, 'FAILURE')
2542 self.assertEqual(self.history[1].name, 'project-merge')
2543 self.assertEqual(self.history[2].result, 'SUCCESS')
2544 self.assertEqual(self.history[3].result, 'SUCCESS')
2545 self.assertEqual(self.history[4].result, 'SUCCESS')
2546 self.assertEqual(len(self.history), 5)
2547
James E. Blair400e8fd2015-07-30 17:44:45 -07002548 def test_live_reconfiguration_failed_job(self):
2549 # Test that a change with a removed failing job does not
2550 # disrupt reconfiguration. If a change has a failed job and
2551 # that job is removed during a reconfiguration, we observed a
2552 # bug where the code to re-set build statuses would run on
2553 # that build and raise an exception because the job no longer
2554 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002555 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002556
2557 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2558
2559 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002560 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002561
2562 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2563 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002564 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002565 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002566 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002567 self.waitUntilSettled()
2568
2569 self.assertEqual(A.data['status'], 'NEW')
2570 self.assertEqual(A.reported, 0)
2571
2572 self.assertEqual(self.getJobFromHistory('project-merge').result,
2573 'SUCCESS')
2574 self.assertEqual(self.getJobFromHistory('project-test1').result,
2575 'FAILURE')
2576 self.assertEqual(len(self.history), 2)
2577
2578 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002579 self.commitConfigUpdate('common-config',
2580 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002581 self.sched.reconfigure(self.config)
2582 self.waitUntilSettled()
2583
Paul Belanger174a8272017-03-14 13:20:10 -04002584 self.executor_server.hold_jobs_in_build = False
2585 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002586 self.waitUntilSettled()
2587
2588 self.assertEqual(self.getJobFromHistory('project-test2').result,
2589 'SUCCESS')
2590 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2591 'SUCCESS')
2592 self.assertEqual(len(self.history), 4)
2593
2594 self.assertEqual(A.data['status'], 'NEW')
2595 self.assertEqual(A.reported, 1)
2596 self.assertIn('Build succeeded', A.messages[0])
2597 # Ensure the removed job was not included in the report.
2598 self.assertNotIn('project-test1', A.messages[0])
2599
James E. Blairfe707d12015-08-05 15:18:15 -07002600 def test_live_reconfiguration_shared_queue(self):
2601 # Test that a change with a failing job which was removed from
2602 # this project but otherwise still exists in the system does
2603 # not disrupt reconfiguration.
2604
Paul Belanger174a8272017-03-14 13:20:10 -04002605 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002606
2607 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2608
Paul Belanger174a8272017-03-14 13:20:10 -04002609 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002610
2611 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2612 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002613 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002614 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002615 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002616 self.waitUntilSettled()
2617
2618 self.assertEqual(A.data['status'], 'NEW')
2619 self.assertEqual(A.reported, 0)
2620
Clint Byrum69663d42017-05-01 14:51:04 -07002621 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002622 'SUCCESS')
2623 self.assertEqual(self.getJobFromHistory(
2624 'project1-project2-integration').result, 'FAILURE')
2625 self.assertEqual(len(self.history), 2)
2626
2627 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002628 self.commitConfigUpdate(
2629 'common-config',
2630 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002631 self.sched.reconfigure(self.config)
2632 self.waitUntilSettled()
2633
Paul Belanger174a8272017-03-14 13:20:10 -04002634 self.executor_server.hold_jobs_in_build = False
2635 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002636 self.waitUntilSettled()
2637
Clint Byrum69663d42017-05-01 14:51:04 -07002638 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002639 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002640 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002641 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002642 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002643 'SUCCESS')
2644 self.assertEqual(self.getJobFromHistory(
2645 'project1-project2-integration').result, 'FAILURE')
2646 self.assertEqual(len(self.history), 4)
2647
2648 self.assertEqual(A.data['status'], 'NEW')
2649 self.assertEqual(A.reported, 1)
2650 self.assertIn('Build succeeded', A.messages[0])
2651 # Ensure the removed job was not included in the report.
2652 self.assertNotIn('project1-project2-integration', A.messages[0])
2653
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002654 def test_double_live_reconfiguration_shared_queue(self):
2655 # This was a real-world regression. A change is added to
2656 # gate; a reconfigure happens, a second change which depends
2657 # on the first is added, and a second reconfiguration happens.
2658 # Ensure that both changes merge.
2659
2660 # A failure may indicate incorrect caching or cleaning up of
2661 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002662 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002663
2664 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2665 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2666 B.setDependsOn(A, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002667 A.addApproval('Code-Review', 2)
2668 B.addApproval('Code-Review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002669
2670 # Add the parent change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002671 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002672 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002673 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002674 self.waitUntilSettled()
2675
2676 # Reconfigure (with only one change in the pipeline).
2677 self.sched.reconfigure(self.config)
2678 self.waitUntilSettled()
2679
2680 # Add the child change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002681 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002682 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002683 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002684 self.waitUntilSettled()
2685
2686 # Reconfigure (with both in the pipeline).
2687 self.sched.reconfigure(self.config)
2688 self.waitUntilSettled()
2689
Paul Belanger174a8272017-03-14 13:20:10 -04002690 self.executor_server.hold_jobs_in_build = False
2691 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002692 self.waitUntilSettled()
2693
2694 self.assertEqual(len(self.history), 8)
2695
2696 self.assertEqual(A.data['status'], 'MERGED')
2697 self.assertEqual(A.reported, 2)
2698 self.assertEqual(B.data['status'], 'MERGED')
2699 self.assertEqual(B.reported, 2)
2700
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002701 def test_live_reconfiguration_del_project(self):
2702 # Test project deletion from layout
2703 # while changes are enqueued
2704
Paul Belanger174a8272017-03-14 13:20:10 -04002705 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002706 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2707 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2708 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2709
2710 # A Depends-On: B
2711 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2712 A.subject, B.data['id'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002713 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002714
2715 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2716 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2717 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002718 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002719 self.waitUntilSettled()
2720 self.assertEqual(len(self.builds), 5)
2721
2722 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002723 self.commitConfigUpdate(
2724 'common-config',
2725 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002726 self.sched.reconfigure(self.config)
2727 self.waitUntilSettled()
2728
2729 # Builds for C aborted, builds for A succeed,
2730 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002731 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002732 self.assertEqual(job_c.changes, '3,1')
2733 self.assertEqual(job_c.result, 'ABORTED')
2734
Paul Belanger174a8272017-03-14 13:20:10 -04002735 self.executor_server.hold_jobs_in_build = False
2736 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002737 self.waitUntilSettled()
2738
Clint Byrum0149ada2017-04-04 12:22:01 -07002739 self.assertEqual(
2740 self.getJobFromHistory('project-test1', 'org/project').changes,
2741 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002742
2743 self.assertEqual(A.data['status'], 'NEW')
2744 self.assertEqual(B.data['status'], 'NEW')
2745 self.assertEqual(C.data['status'], 'NEW')
2746 self.assertEqual(A.reported, 1)
2747 self.assertEqual(B.reported, 0)
2748 self.assertEqual(C.reported, 0)
2749
Clint Byrum0149ada2017-04-04 12:22:01 -07002750 tenant = self.sched.abide.tenants.get('tenant-one')
2751 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002752 self.assertIn('Build succeeded', A.messages[0])
2753
James E. Blairec056492016-07-22 09:45:56 -07002754 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002755 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002756 self.updateConfigLayout(
2757 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002758 self.sched.reconfigure(self.config)
2759
2760 self.init_repo("org/new-project")
2761 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2762
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002763 A.addApproval('Code-Review', 2)
2764 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002765 self.waitUntilSettled()
2766 self.assertEqual(self.getJobFromHistory('project-merge').result,
2767 'SUCCESS')
2768 self.assertEqual(self.getJobFromHistory('project-test1').result,
2769 'SUCCESS')
2770 self.assertEqual(self.getJobFromHistory('project-test2').result,
2771 'SUCCESS')
2772 self.assertEqual(A.data['status'], 'MERGED')
2773 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002774
James E. Blair9ea70072017-04-19 16:05:30 -07002775 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002776 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002777 self.init_repo("org/delete-project")
2778 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2779
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002780 A.addApproval('Code-Review', 2)
2781 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002782 self.waitUntilSettled()
2783 self.assertEqual(self.getJobFromHistory('project-merge').result,
2784 'SUCCESS')
2785 self.assertEqual(self.getJobFromHistory('project-test1').result,
2786 'SUCCESS')
2787 self.assertEqual(self.getJobFromHistory('project-test2').result,
2788 'SUCCESS')
2789 self.assertEqual(A.data['status'], 'MERGED')
2790 self.assertEqual(A.reported, 2)
2791
2792 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002793 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002794 if os.path.exists(os.path.join(self.merger_src_root, p)):
2795 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002796 if os.path.exists(os.path.join(self.executor_src_root, p)):
2797 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002798
2799 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2800
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002801 B.addApproval('Code-Review', 2)
2802 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002803 self.waitUntilSettled()
2804 self.assertEqual(self.getJobFromHistory('project-merge').result,
2805 'SUCCESS')
2806 self.assertEqual(self.getJobFromHistory('project-test1').result,
2807 'SUCCESS')
2808 self.assertEqual(self.getJobFromHistory('project-test2').result,
2809 'SUCCESS')
2810 self.assertEqual(B.data['status'], 'MERGED')
2811 self.assertEqual(B.reported, 2)
2812
James E. Blaire68d43f2017-04-19 14:03:30 -07002813 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002814 def test_tags(self):
2815 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002816 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2817 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2818 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2819 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2820 self.waitUntilSettled()
2821
James E. Blaire68d43f2017-04-19 14:03:30 -07002822 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002823
2824 results = {self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002825 project='org/project1').uuid: ['extratag', 'merge'],
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002826 self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002827 project='org/project2').uuid: ['merge']}
James E. Blair456f2fb2016-02-09 09:29:33 -08002828
2829 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002830 self.assertEqual(results.get(build.uuid, ''),
James E. Blair9d46f092017-07-20 16:06:20 -07002831 build.parameters['zuul'].get('jobtags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002832
James E. Blair63bb0ef2013-07-29 17:14:51 -07002833 def test_timer(self):
2834 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002835 # This test can not use simple_layout because it must start
2836 # with a configuration which does not include a
2837 # timer-triggered job so that we have an opportunity to set
2838 # the hold flag before the first job.
James E. Blair21037782017-07-19 11:56:55 -07002839 self.create_branch('org/project', 'stable')
Paul Belanger174a8272017-03-14 13:20:10 -04002840 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002841 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002842 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002843
Clark Boylan3ee090a2014-04-03 20:55:09 -07002844 # The pipeline triggers every second, so we should have seen
2845 # several by now.
2846 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002847 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002848
2849 self.assertEqual(len(self.builds), 2)
2850
James E. Blair63bb0ef2013-07-29 17:14:51 -07002851 port = self.webapp.server.socket.getsockname()[1]
2852
Clint Byrum5e729e92017-02-08 06:34:57 -08002853 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002854 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002855 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002856 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002857
Paul Belanger174a8272017-03-14 13:20:10 -04002858 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002859 # Stop queuing timer triggered jobs so that the assertions
2860 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002861 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002862 self.sched.reconfigure(self.config)
James E. Blair78ae4782017-08-02 14:19:10 -07002863 self.waitUntilSettled()
2864 # If APScheduler is in mid-event when we remove the job, we
2865 # can end up with one more event firing, so give it an extra
2866 # second to settle.
2867 time.sleep(1)
2868 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002869 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002870 self.waitUntilSettled()
2871
James E. Blair21037782017-07-19 11:56:55 -07002872 self.assertHistory([
2873 dict(name='project-bitrot', result='SUCCESS',
2874 ref='refs/heads/master'),
2875 dict(name='project-bitrot', result='SUCCESS',
2876 ref='refs/heads/stable'),
2877 ], ordered=False)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002878
2879 data = json.loads(data)
2880 status_jobs = set()
2881 for p in data['pipelines']:
2882 for q in p['change_queues']:
2883 for head in q['heads']:
2884 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002885 for job in change['jobs']:
2886 status_jobs.add(job['name'])
James E. Blair21037782017-07-19 11:56:55 -07002887 self.assertIn('project-bitrot', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002888
James E. Blair4f6033c2014-03-27 15:49:09 -07002889 def test_idle(self):
2890 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002891 # This test can not use simple_layout because it must start
2892 # with a configuration which does not include a
2893 # timer-triggered job so that we have an opportunity to set
2894 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002895 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002896
Clark Boylan3ee090a2014-04-03 20:55:09 -07002897 for x in range(1, 3):
2898 # Test that timer triggers periodic jobs even across
2899 # layout config reloads.
2900 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002901 self.commitConfigUpdate('common-config',
2902 'layouts/idle.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. Blair4f6033c2014-03-27 15:49:09 -07002905
Clark Boylan3ee090a2014-04-03 20:55:09 -07002906 # The pipeline triggers every second, so we should have seen
2907 # several by now.
2908 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002909
2910 # Stop queuing timer triggered jobs so that the assertions
2911 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002912 self.commitConfigUpdate('common-config',
2913 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002914 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002915 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07002916 # If APScheduler is in mid-event when we remove the job,
2917 # we can end up with one more event firing, so give it an
2918 # extra second to settle.
2919 time.sleep(1)
2920 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07002921 self.assertEqual(len(self.builds), 1,
Clint Byrum58264dc2017-02-07 21:21:22 -08002922 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002923 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002924 self.waitUntilSettled()
2925 self.assertEqual(len(self.builds), 0)
James E. Blair21037782017-07-19 11:56:55 -07002926 self.assertEqual(len(self.history), x)
James E. Blair4f6033c2014-03-27 15:49:09 -07002927
James E. Blaircc324192017-04-19 14:36:08 -07002928 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002929 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002930 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2931 self.waitUntilSettled()
2932
2933 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2934 self.waitUntilSettled()
2935
James E. Blairff80a2f2013-12-27 13:24:06 -08002936 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002937
2938 # A.messages only holds what FakeGerrit places in it. Thus we
2939 # work on the knowledge of what the first message should be as
2940 # it is only configured to go to SMTP.
2941
2942 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002943 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002944 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002945 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002946 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002947 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002948
2949 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002950 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002951 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002952 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002953 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002954 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002955
James E. Blaire5910202013-12-27 09:50:31 -08002956 def test_timer_smtp(self):
2957 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002958 # This test can not use simple_layout because it must start
2959 # with a configuration which does not include a
2960 # timer-triggered job so that we have an opportunity to set
2961 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002962 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002963 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002964 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002965
Clark Boylan3ee090a2014-04-03 20:55:09 -07002966 # The pipeline triggers every second, so we should have seen
2967 # several by now.
2968 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002969 self.waitUntilSettled()
2970
Clark Boylan3ee090a2014-04-03 20:55:09 -07002971 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04002972 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002973 self.waitUntilSettled()
2974 self.assertEqual(len(self.history), 2)
2975
James E. Blaire5910202013-12-27 09:50:31 -08002976 self.assertEqual(self.getJobFromHistory(
2977 'project-bitrot-stable-old').result, 'SUCCESS')
2978 self.assertEqual(self.getJobFromHistory(
2979 'project-bitrot-stable-older').result, 'SUCCESS')
2980
James E. Blairff80a2f2013-12-27 13:24:06 -08002981 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002982
2983 # A.messages only holds what FakeGerrit places in it. Thus we
2984 # work on the knowledge of what the first message should be as
2985 # it is only configured to go to SMTP.
2986
2987 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002988 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002989 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002990 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002991 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002992 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002993
Clark Boylan3ee090a2014-04-03 20:55:09 -07002994 # Stop queuing timer triggered jobs and let any that may have
2995 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002996 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002997 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07002998 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07002999 # If APScheduler is in mid-event when we remove the job, we
3000 # can end up with one more event firing, so give it an extra
3001 # second to settle.
3002 time.sleep(1)
3003 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003004 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003005 self.waitUntilSettled()
3006
Joshua Hesketh25695cb2017-03-06 12:50:04 +11003007 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003008 def test_timer_sshkey(self):
3009 "Test that a periodic job can setup SSH key authentication"
3010 self.worker.hold_jobs_in_build = True
3011 self.config.set('zuul', 'layout_config',
3012 'tests/fixtures/layout-timer.yaml')
3013 self.sched.reconfigure(self.config)
3014 self.registerJobs()
3015
3016 # The pipeline triggers every second, so we should have seen
3017 # several by now.
3018 time.sleep(5)
3019 self.waitUntilSettled()
3020
3021 self.assertEqual(len(self.builds), 2)
3022
3023 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3024 self.assertTrue(os.path.isfile(ssh_wrapper))
3025 with open(ssh_wrapper) as f:
3026 ssh_wrapper_content = f.read()
3027 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3028 # In the unit tests Merger runs in the same process,
3029 # so we see its' environment variables
3030 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3031
3032 self.worker.release('.*')
3033 self.waitUntilSettled()
3034 self.assertEqual(len(self.history), 2)
3035
3036 self.assertEqual(self.getJobFromHistory(
3037 'project-bitrot-stable-old').result, 'SUCCESS')
3038 self.assertEqual(self.getJobFromHistory(
3039 'project-bitrot-stable-older').result, 'SUCCESS')
3040
3041 # Stop queuing timer triggered jobs and let any that may have
3042 # queued through so that end of test assertions pass.
3043 self.config.set('zuul', 'layout_config',
3044 'tests/fixtures/layout-no-timer.yaml')
3045 self.sched.reconfigure(self.config)
3046 self.registerJobs()
3047 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003048 # If APScheduler is in mid-event when we remove the job, we
3049 # can end up with one more event firing, so give it an extra
3050 # second to settle.
3051 time.sleep(1)
3052 self.waitUntilSettled()
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003053 self.worker.release('.*')
3054 self.waitUntilSettled()
3055
James E. Blair91e34592015-07-31 16:45:59 -07003056 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003057 "Test that the RPC client can enqueue a change"
3058 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003059 A.addApproval('Code-Review', 2)
3060 A.addApproval('Approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003061
3062 client = zuul.rpcclient.RPCClient('127.0.0.1',
3063 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003064 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003065 r = client.enqueue(tenant='tenant-one',
3066 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003067 project='org/project',
3068 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003069 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003070 self.waitUntilSettled()
3071 self.assertEqual(self.getJobFromHistory('project-merge').result,
3072 'SUCCESS')
3073 self.assertEqual(self.getJobFromHistory('project-test1').result,
3074 'SUCCESS')
3075 self.assertEqual(self.getJobFromHistory('project-test2').result,
3076 'SUCCESS')
3077 self.assertEqual(A.data['status'], 'MERGED')
3078 self.assertEqual(A.reported, 2)
3079 self.assertEqual(r, True)
3080
James E. Blair91e34592015-07-31 16:45:59 -07003081 def test_client_enqueue_ref(self):
3082 "Test that the RPC client can enqueue a ref"
3083
3084 client = zuul.rpcclient.RPCClient('127.0.0.1',
3085 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003086 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07003087 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003088 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003089 pipeline='post',
3090 project='org/project',
3091 trigger='gerrit',
3092 ref='master',
3093 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
3094 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
3095 self.waitUntilSettled()
3096 job_names = [x.name for x in self.history]
3097 self.assertEqual(len(self.history), 1)
3098 self.assertIn('project-post', job_names)
3099 self.assertEqual(r, True)
3100
James E. Blairad28e912013-11-27 10:43:22 -08003101 def test_client_enqueue_negative(self):
3102 "Test that the RPC client returns errors"
3103 client = zuul.rpcclient.RPCClient('127.0.0.1',
3104 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003105 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003106 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003107 "Invalid tenant"):
3108 r = client.enqueue(tenant='tenant-foo',
3109 pipeline='gate',
3110 project='org/project',
3111 trigger='gerrit',
3112 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003113 self.assertEqual(r, False)
3114
3115 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003116 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003117 r = client.enqueue(tenant='tenant-one',
3118 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003119 project='project-does-not-exist',
3120 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003121 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003122 self.assertEqual(r, False)
3123
3124 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3125 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003126 r = client.enqueue(tenant='tenant-one',
3127 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003128 project='org/project',
3129 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003130 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003131 self.assertEqual(r, False)
3132
3133 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3134 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003135 r = client.enqueue(tenant='tenant-one',
3136 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003137 project='org/project',
3138 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003139 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003140 self.assertEqual(r, False)
3141
3142 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3143 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003144 r = client.enqueue(tenant='tenant-one',
3145 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003146 project='org/project',
3147 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003148 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003149 self.assertEqual(r, False)
3150
3151 self.waitUntilSettled()
3152 self.assertEqual(len(self.history), 0)
3153 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003154
3155 def test_client_promote(self):
3156 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003157 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003158 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3159 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3160 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003161 A.addApproval('Code-Review', 2)
3162 B.addApproval('Code-Review', 2)
3163 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003164
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003165 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3166 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3167 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003168
3169 self.waitUntilSettled()
3170
Paul Belangerbaca3132016-11-04 12:49:54 -04003171 tenant = self.sched.abide.tenants.get('tenant-one')
3172 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003173 enqueue_times = {}
3174 for item in items:
3175 enqueue_times[str(item.change)] = item.enqueue_time
3176
James E. Blair36658cf2013-12-06 17:53:48 -08003177 client = zuul.rpcclient.RPCClient('127.0.0.1',
3178 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003179 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003180 r = client.promote(tenant='tenant-one',
3181 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003182 change_ids=['2,1', '3,1'])
3183
Sean Daguef39b9ca2014-01-10 21:34:35 -05003184 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003185 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003186 for item in items:
3187 self.assertEqual(
3188 enqueue_times[str(item.change)], item.enqueue_time)
3189
James E. Blair78acec92014-02-06 07:11:32 -08003190 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003191 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003192 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003193 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003194 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003195 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003196 self.waitUntilSettled()
3197
3198 self.assertEqual(len(self.builds), 6)
3199 self.assertEqual(self.builds[0].name, 'project-test1')
3200 self.assertEqual(self.builds[1].name, 'project-test2')
3201 self.assertEqual(self.builds[2].name, 'project-test1')
3202 self.assertEqual(self.builds[3].name, 'project-test2')
3203 self.assertEqual(self.builds[4].name, 'project-test1')
3204 self.assertEqual(self.builds[5].name, 'project-test2')
3205
Paul Belangerbaca3132016-11-04 12:49:54 -04003206 self.assertTrue(self.builds[0].hasChanges(B))
3207 self.assertFalse(self.builds[0].hasChanges(A))
3208 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003209
Paul Belangerbaca3132016-11-04 12:49:54 -04003210 self.assertTrue(self.builds[2].hasChanges(B))
3211 self.assertTrue(self.builds[2].hasChanges(C))
3212 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003213
Paul Belangerbaca3132016-11-04 12:49:54 -04003214 self.assertTrue(self.builds[4].hasChanges(B))
3215 self.assertTrue(self.builds[4].hasChanges(C))
3216 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003217
Paul Belanger174a8272017-03-14 13:20:10 -04003218 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003219 self.waitUntilSettled()
3220
3221 self.assertEqual(A.data['status'], 'MERGED')
3222 self.assertEqual(A.reported, 2)
3223 self.assertEqual(B.data['status'], 'MERGED')
3224 self.assertEqual(B.reported, 2)
3225 self.assertEqual(C.data['status'], 'MERGED')
3226 self.assertEqual(C.reported, 2)
3227
James E. Blair36658cf2013-12-06 17:53:48 -08003228 self.assertEqual(r, True)
3229
3230 def test_client_promote_dependent(self):
3231 "Test that the RPC client can promote a dependent change"
3232 # C (depends on B) -> B -> A ; then promote C to get:
3233 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003234 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003235 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3236 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3237 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3238
3239 C.setDependsOn(B, 1)
3240
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003241 A.addApproval('Code-Review', 2)
3242 B.addApproval('Code-Review', 2)
3243 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003244
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003245 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3246 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3247 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003248
3249 self.waitUntilSettled()
3250
3251 client = zuul.rpcclient.RPCClient('127.0.0.1',
3252 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003253 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003254 r = client.promote(tenant='tenant-one',
3255 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003256 change_ids=['3,1'])
3257
James E. Blair78acec92014-02-06 07:11:32 -08003258 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003259 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003260 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003261 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003262 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003263 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003264 self.waitUntilSettled()
3265
3266 self.assertEqual(len(self.builds), 6)
3267 self.assertEqual(self.builds[0].name, 'project-test1')
3268 self.assertEqual(self.builds[1].name, 'project-test2')
3269 self.assertEqual(self.builds[2].name, 'project-test1')
3270 self.assertEqual(self.builds[3].name, 'project-test2')
3271 self.assertEqual(self.builds[4].name, 'project-test1')
3272 self.assertEqual(self.builds[5].name, 'project-test2')
3273
Paul Belangerbaca3132016-11-04 12:49:54 -04003274 self.assertTrue(self.builds[0].hasChanges(B))
3275 self.assertFalse(self.builds[0].hasChanges(A))
3276 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003277
Paul Belangerbaca3132016-11-04 12:49:54 -04003278 self.assertTrue(self.builds[2].hasChanges(B))
3279 self.assertTrue(self.builds[2].hasChanges(C))
3280 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003281
Paul Belangerbaca3132016-11-04 12:49:54 -04003282 self.assertTrue(self.builds[4].hasChanges(B))
3283 self.assertTrue(self.builds[4].hasChanges(C))
3284 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003285
Paul Belanger174a8272017-03-14 13:20:10 -04003286 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003287 self.waitUntilSettled()
3288
3289 self.assertEqual(A.data['status'], 'MERGED')
3290 self.assertEqual(A.reported, 2)
3291 self.assertEqual(B.data['status'], 'MERGED')
3292 self.assertEqual(B.reported, 2)
3293 self.assertEqual(C.data['status'], 'MERGED')
3294 self.assertEqual(C.reported, 2)
3295
James E. Blair36658cf2013-12-06 17:53:48 -08003296 self.assertEqual(r, True)
3297
3298 def test_client_promote_negative(self):
3299 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003300 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003301 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003302 A.addApproval('Code-Review', 2)
3303 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003304 self.waitUntilSettled()
3305
3306 client = zuul.rpcclient.RPCClient('127.0.0.1',
3307 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003308 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003309
3310 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003311 r = client.promote(tenant='tenant-one',
3312 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003313 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003314 self.assertEqual(r, False)
3315
3316 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003317 r = client.promote(tenant='tenant-one',
3318 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003319 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003320 self.assertEqual(r, False)
3321
Paul Belanger174a8272017-03-14 13:20:10 -04003322 self.executor_server.hold_jobs_in_build = False
3323 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003324 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003325
James E. Blaircc324192017-04-19 14:36:08 -07003326 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003327 def test_queue_rate_limiting(self):
3328 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003329 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003330 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3331 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3332 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3333
3334 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003335 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003336
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003337 A.addApproval('Code-Review', 2)
3338 B.addApproval('Code-Review', 2)
3339 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003340
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003341 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3342 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3343 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003344 self.waitUntilSettled()
3345
3346 # Only A and B will have their merge jobs queued because
3347 # window is 2.
3348 self.assertEqual(len(self.builds), 2)
3349 self.assertEqual(self.builds[0].name, 'project-merge')
3350 self.assertEqual(self.builds[1].name, 'project-merge')
3351
James E. Blair732ee862017-04-21 07:49:26 -07003352 # Release the merge jobs one at a time.
3353 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003354 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003355 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003356 self.waitUntilSettled()
3357
3358 # Only A and B will have their test jobs queued because
3359 # window is 2.
3360 self.assertEqual(len(self.builds), 4)
3361 self.assertEqual(self.builds[0].name, 'project-test1')
3362 self.assertEqual(self.builds[1].name, 'project-test2')
3363 self.assertEqual(self.builds[2].name, 'project-test1')
3364 self.assertEqual(self.builds[3].name, 'project-test2')
3365
Paul Belanger174a8272017-03-14 13:20:10 -04003366 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003367 self.waitUntilSettled()
3368
James E. Blaircc324192017-04-19 14:36:08 -07003369 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003370 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003371 # A failed so window is reduced by 1 to 1.
3372 self.assertEqual(queue.window, 1)
3373 self.assertEqual(queue.window_floor, 1)
3374 self.assertEqual(A.data['status'], 'NEW')
3375
3376 # Gate is reset and only B's merge job is queued because
3377 # window shrunk to 1.
3378 self.assertEqual(len(self.builds), 1)
3379 self.assertEqual(self.builds[0].name, 'project-merge')
3380
Paul Belanger174a8272017-03-14 13:20:10 -04003381 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003382 self.waitUntilSettled()
3383
3384 # Only B's test jobs are queued because window is still 1.
3385 self.assertEqual(len(self.builds), 2)
3386 self.assertEqual(self.builds[0].name, 'project-test1')
3387 self.assertEqual(self.builds[1].name, 'project-test2')
3388
Paul Belanger174a8272017-03-14 13:20:10 -04003389 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003390 self.waitUntilSettled()
3391
3392 # B was successfully merged so window is increased to 2.
3393 self.assertEqual(queue.window, 2)
3394 self.assertEqual(queue.window_floor, 1)
3395 self.assertEqual(B.data['status'], 'MERGED')
3396
3397 # Only C is left and its merge job is queued.
3398 self.assertEqual(len(self.builds), 1)
3399 self.assertEqual(self.builds[0].name, 'project-merge')
3400
Paul Belanger174a8272017-03-14 13:20:10 -04003401 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003402 self.waitUntilSettled()
3403
3404 # After successful merge job the test jobs for C are queued.
3405 self.assertEqual(len(self.builds), 2)
3406 self.assertEqual(self.builds[0].name, 'project-test1')
3407 self.assertEqual(self.builds[1].name, 'project-test2')
3408
Paul Belanger174a8272017-03-14 13:20:10 -04003409 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003410 self.waitUntilSettled()
3411
3412 # C successfully merged so window is bumped to 3.
3413 self.assertEqual(queue.window, 3)
3414 self.assertEqual(queue.window_floor, 1)
3415 self.assertEqual(C.data['status'], 'MERGED')
3416
James E. Blaircc324192017-04-19 14:36:08 -07003417 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003418 def test_queue_rate_limiting_dependent(self):
3419 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003420 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003421 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3422 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3423 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3424
3425 B.setDependsOn(A, 1)
3426
Paul Belanger174a8272017-03-14 13:20:10 -04003427 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003428
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003429 A.addApproval('Code-Review', 2)
3430 B.addApproval('Code-Review', 2)
3431 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003432
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003433 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3434 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3435 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003436 self.waitUntilSettled()
3437
3438 # Only A and B will have their merge jobs queued because
3439 # window is 2.
3440 self.assertEqual(len(self.builds), 2)
3441 self.assertEqual(self.builds[0].name, 'project-merge')
3442 self.assertEqual(self.builds[1].name, 'project-merge')
3443
James E. Blairf4a5f022017-04-18 14:01:10 -07003444 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003445
3446 # Only A and B will have their test jobs queued because
3447 # window is 2.
3448 self.assertEqual(len(self.builds), 4)
3449 self.assertEqual(self.builds[0].name, 'project-test1')
3450 self.assertEqual(self.builds[1].name, 'project-test2')
3451 self.assertEqual(self.builds[2].name, 'project-test1')
3452 self.assertEqual(self.builds[3].name, 'project-test2')
3453
Paul Belanger174a8272017-03-14 13:20:10 -04003454 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003455 self.waitUntilSettled()
3456
James E. Blaircc324192017-04-19 14:36:08 -07003457 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003458 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003459 # A failed so window is reduced by 1 to 1.
3460 self.assertEqual(queue.window, 1)
3461 self.assertEqual(queue.window_floor, 1)
3462 self.assertEqual(A.data['status'], 'NEW')
3463 self.assertEqual(B.data['status'], 'NEW')
3464
3465 # Gate is reset and only C's merge job is queued because
3466 # window shrunk to 1 and A and B were dequeued.
3467 self.assertEqual(len(self.builds), 1)
3468 self.assertEqual(self.builds[0].name, 'project-merge')
3469
James E. Blairf4a5f022017-04-18 14:01:10 -07003470 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003471
3472 # Only C's test jobs are queued because window is still 1.
3473 self.assertEqual(len(self.builds), 2)
James E. Blairdf25ddc2017-07-08 07:57:09 -07003474 builds = self.getSortedBuilds()
3475 self.assertEqual(builds[0].name, 'project-test1')
3476 self.assertEqual(builds[1].name, 'project-test2')
Clark Boylan7603a372014-01-21 11:43:20 -08003477
Paul Belanger174a8272017-03-14 13:20:10 -04003478 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003479 self.waitUntilSettled()
3480
3481 # C was successfully merged so window is increased to 2.
3482 self.assertEqual(queue.window, 2)
3483 self.assertEqual(queue.window_floor, 1)
3484 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003485
3486 def test_worker_update_metadata(self):
3487 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003488 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003489
3490 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003491 A.addApproval('Code-Review', 2)
3492 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003493 self.waitUntilSettled()
3494
Clint Byrumd4de79a2017-05-27 11:39:13 -07003495 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003496
3497 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07003498 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003499
3500 start = time.time()
3501 while True:
3502 if time.time() - start > 10:
3503 raise Exception("Timeout waiting for gearman server to report "
3504 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07003505 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003506 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08003507 break
3508 else:
3509 time.sleep(0)
3510
3511 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05003512 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003513
Paul Belanger174a8272017-03-14 13:20:10 -04003514 self.executor_server.hold_jobs_in_build = False
3515 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003516 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003517
James E. Blaircc324192017-04-19 14:36:08 -07003518 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003519 def test_footer_message(self):
3520 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003521 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003522 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003523 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003524 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003525 self.waitUntilSettled()
3526
3527 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003528 B.addApproval('Code-Review', 2)
3529 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003530 self.waitUntilSettled()
3531
3532 self.assertEqual(2, len(self.smtp_messages))
3533
Adam Gandelman82258fd2017-02-07 16:51:58 -08003534 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003535Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003536http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003537
Adam Gandelman82258fd2017-02-07 16:51:58 -08003538 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003539For CI problems and help debugging, contact ci@example.org"""
3540
Adam Gandelman82258fd2017-02-07 16:51:58 -08003541 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3542 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3543 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3544 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003545
Tobias Henkel9842bd72017-05-16 13:40:03 +02003546 @simple_layout('layouts/unmanaged-project.yaml')
3547 def test_unmanaged_project_start_message(self):
3548 "Test start reporting is not done for unmanaged projects."
3549 self.init_repo("org/project", tag='init')
3550 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3551 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3552 self.waitUntilSettled()
3553
3554 self.assertEqual(0, len(A.messages))
3555
Clint Byrumb9de8be2017-05-27 13:49:19 -07003556 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003557 def test_merge_failure_reporters(self):
3558 """Check that the config is set up correctly"""
3559
Clint Byrumb9de8be2017-05-27 13:49:19 -07003560 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11003561 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003562 "Merge Failed.\n\nThis change or one of its cross-repo "
3563 "dependencies was unable to be automatically merged with the "
3564 "current state of its repository. Please rebase the change and "
3565 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003566 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003567 self.assertEqual(
3568 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003569 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003570
3571 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003572 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11003573 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003574 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11003575
3576 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003577 tenant.layout.pipelines['check'].merge_failure_actions[0],
3578 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003579
3580 self.assertTrue(
3581 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003582 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003583 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003584 zuul.driver.smtp.smtpreporter.SMTPReporter) and
3585 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003586 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003587 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003588 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003589 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003590 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003591 gerritreporter.GerritReporter) and
3592 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003593 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003594 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003595 )
3596 )
3597
James E. Blairec056492016-07-22 09:45:56 -07003598 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003599 def test_merge_failure_reports(self):
3600 """Check that when a change fails to merge the correct message is sent
3601 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003602 self.updateConfigLayout(
3603 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003604 self.sched.reconfigure(self.config)
3605 self.registerJobs()
3606
3607 # Check a test failure isn't reported to SMTP
3608 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003609 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003610 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003611 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003612 self.waitUntilSettled()
3613
3614 self.assertEqual(3, len(self.history)) # 3 jobs
3615 self.assertEqual(0, len(self.smtp_messages))
3616
3617 # Check a merge failure is reported to SMTP
3618 # B should be merged, but C will conflict with B
3619 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3620 B.addPatchset(['conflict'])
3621 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3622 C.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003623 B.addApproval('Code-Review', 2)
3624 C.addApproval('Code-Review', 2)
3625 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3626 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003627 self.waitUntilSettled()
3628
3629 self.assertEqual(6, len(self.history)) # A and B jobs
3630 self.assertEqual(1, len(self.smtp_messages))
3631 self.assertEqual('The merge failed! For more information...',
3632 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003633
James E. Blairec056492016-07-22 09:45:56 -07003634 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003635 def test_default_merge_failure_reports(self):
3636 """Check that the default merge failure reports are correct."""
3637
3638 # A should report success, B should report merge failure.
3639 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3640 A.addPatchset(['conflict'])
3641 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3642 B.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003643 A.addApproval('Code-Review', 2)
3644 B.addApproval('Code-Review', 2)
3645 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3646 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003647 self.waitUntilSettled()
3648
3649 self.assertEqual(3, len(self.history)) # A jobs
3650 self.assertEqual(A.reported, 2)
3651 self.assertEqual(B.reported, 2)
3652 self.assertEqual(A.data['status'], 'MERGED')
3653 self.assertEqual(B.data['status'], 'NEW')
3654 self.assertIn('Build succeeded', A.messages[1])
3655 self.assertIn('Merge Failed', B.messages[1])
3656 self.assertIn('automatically merged', B.messages[1])
3657 self.assertNotIn('logs.example.com', B.messages[1])
3658 self.assertNotIn('SKIPPED', B.messages[1])
3659
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003660 def test_client_get_running_jobs(self):
3661 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003662 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003663 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003664 A.addApproval('Code-Review', 2)
3665 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003666 self.waitUntilSettled()
3667
3668 client = zuul.rpcclient.RPCClient('127.0.0.1',
3669 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003670 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003671
3672 # Wait for gearman server to send the initial workData back to zuul
3673 start = time.time()
3674 while True:
3675 if time.time() - start > 10:
3676 raise Exception("Timeout waiting for gearman server to report "
3677 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003678 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003679 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003680 break
3681 else:
3682 time.sleep(0)
3683
3684 running_items = client.get_running_jobs()
3685
3686 self.assertEqual(1, len(running_items))
3687 running_item = running_items[0]
3688 self.assertEqual([], running_item['failing_reasons'])
3689 self.assertEqual([], running_item['items_behind'])
3690 self.assertEqual('https://hostname/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003691 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003692 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003693 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003694 self.assertEqual(True, running_item['active'])
3695 self.assertEqual('1,1', running_item['id'])
3696
3697 self.assertEqual(3, len(running_item['jobs']))
3698 for job in running_item['jobs']:
3699 if job['name'] == 'project-merge':
3700 self.assertEqual('project-merge', job['name'])
3701 self.assertEqual('gate', job['pipeline'])
3702 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05003703 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003704 'static/stream.html?uuid={uuid}&logfile=console.log'
3705 .format(uuid=job['uuid']), job['url'])
3706 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05003707 'finger://{hostname}/{uuid}'.format(
3708 hostname=self.executor_server.hostname,
3709 uuid=job['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003710 job['finger_url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07003711 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003712 self.assertEqual(False, job['canceled'])
3713 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003714 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003715 self.assertEqual('gate', job['pipeline'])
3716 break
3717
Paul Belanger174a8272017-03-14 13:20:10 -04003718 self.executor_server.hold_jobs_in_build = False
3719 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003720 self.waitUntilSettled()
3721
3722 running_items = client.get_running_jobs()
3723 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003724
James E. Blairb70e55a2017-04-19 12:57:02 -07003725 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003726 def test_nonvoting_pipeline(self):
3727 "Test that a nonvoting pipeline (experimental) can still report"
3728
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003729 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3730 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003731 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3732 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003733
3734 self.assertEqual(self.getJobFromHistory('project-merge').result,
3735 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003736 self.assertEqual(
3737 self.getJobFromHistory('experimental-project-test').result,
3738 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003739 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003740
3741 def test_crd_gate(self):
3742 "Test cross-repo dependencies"
3743 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3744 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003745 A.addApproval('Code-Review', 2)
3746 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003747
3748 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3749 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3750 AM2.setMerged()
3751 AM1.setMerged()
3752
3753 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3754 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3755 BM2.setMerged()
3756 BM1.setMerged()
3757
3758 # A -> AM1 -> AM2
3759 # B -> BM1 -> BM2
3760 # A Depends-On: B
3761 # M2 is here to make sure it is never queried. If it is, it
3762 # means zuul is walking down the entire history of merged
3763 # changes.
3764
3765 B.setDependsOn(BM1, 1)
3766 BM1.setDependsOn(BM2, 1)
3767
3768 A.setDependsOn(AM1, 1)
3769 AM1.setDependsOn(AM2, 1)
3770
3771 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3772 A.subject, B.data['id'])
3773
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003774 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003775 self.waitUntilSettled()
3776
3777 self.assertEqual(A.data['status'], 'NEW')
3778 self.assertEqual(B.data['status'], 'NEW')
3779
Paul Belanger5dccbe72016-11-14 11:17:37 -05003780 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003781 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003782
Paul Belanger174a8272017-03-14 13:20:10 -04003783 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003784 B.addApproval('Approved', 1)
3785 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003786 self.waitUntilSettled()
3787
Paul Belanger174a8272017-03-14 13:20:10 -04003788 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003789 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003790 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003791 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003792 self.executor_server.hold_jobs_in_build = False
3793 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003794 self.waitUntilSettled()
3795
3796 self.assertEqual(AM2.queried, 0)
3797 self.assertEqual(BM2.queried, 0)
3798 self.assertEqual(A.data['status'], 'MERGED')
3799 self.assertEqual(B.data['status'], 'MERGED')
3800 self.assertEqual(A.reported, 2)
3801 self.assertEqual(B.reported, 2)
3802
Paul Belanger5dccbe72016-11-14 11:17:37 -05003803 changes = self.getJobFromHistory(
3804 'project-merge', 'org/project1').changes
3805 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003806
3807 def test_crd_branch(self):
3808 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003809
3810 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003811 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3812 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3813 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3814 C.data['id'] = B.data['id']
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003815 A.addApproval('Code-Review', 2)
3816 B.addApproval('Code-Review', 2)
3817 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003818
3819 # A Depends-On: B+C
3820 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3821 A.subject, B.data['id'])
3822
Paul Belanger174a8272017-03-14 13:20:10 -04003823 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003824 B.addApproval('Approved', 1)
3825 C.addApproval('Approved', 1)
3826 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003827 self.waitUntilSettled()
3828
Paul Belanger174a8272017-03-14 13:20:10 -04003829 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003830 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003831 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003832 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003833 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003834 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003835 self.executor_server.hold_jobs_in_build = False
3836 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003837 self.waitUntilSettled()
3838
3839 self.assertEqual(A.data['status'], 'MERGED')
3840 self.assertEqual(B.data['status'], 'MERGED')
3841 self.assertEqual(C.data['status'], 'MERGED')
3842 self.assertEqual(A.reported, 2)
3843 self.assertEqual(B.reported, 2)
3844 self.assertEqual(C.reported, 2)
3845
Paul Belanger6379db12016-11-14 13:57:54 -05003846 changes = self.getJobFromHistory(
3847 'project-merge', 'org/project1').changes
3848 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003849
3850 def test_crd_multiline(self):
3851 "Test multiple depends-on lines in commit"
3852 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3853 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3854 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003855 A.addApproval('Code-Review', 2)
3856 B.addApproval('Code-Review', 2)
3857 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003858
3859 # A Depends-On: B+C
3860 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3861 A.subject, B.data['id'], C.data['id'])
3862
Paul Belanger174a8272017-03-14 13:20:10 -04003863 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003864 B.addApproval('Approved', 1)
3865 C.addApproval('Approved', 1)
3866 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003867 self.waitUntilSettled()
3868
Paul Belanger174a8272017-03-14 13:20:10 -04003869 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003870 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003871 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003872 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003873 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003874 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003875 self.executor_server.hold_jobs_in_build = False
3876 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003877 self.waitUntilSettled()
3878
3879 self.assertEqual(A.data['status'], 'MERGED')
3880 self.assertEqual(B.data['status'], 'MERGED')
3881 self.assertEqual(C.data['status'], 'MERGED')
3882 self.assertEqual(A.reported, 2)
3883 self.assertEqual(B.reported, 2)
3884 self.assertEqual(C.reported, 2)
3885
Paul Belanger5dccbe72016-11-14 11:17:37 -05003886 changes = self.getJobFromHistory(
3887 'project-merge', 'org/project1').changes
3888 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003889
3890 def test_crd_unshared_gate(self):
3891 "Test cross-repo dependencies in unshared gate queues"
3892 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3893 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003894 A.addApproval('Code-Review', 2)
3895 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003896
3897 # A Depends-On: B
3898 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3899 A.subject, B.data['id'])
3900
3901 # A and B do not share a queue, make sure that A is unable to
3902 # enqueue B (and therefore, A is unable to be enqueued).
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003903 B.addApproval('Approved', 1)
3904 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003905 self.waitUntilSettled()
3906
3907 self.assertEqual(A.data['status'], 'NEW')
3908 self.assertEqual(B.data['status'], 'NEW')
3909 self.assertEqual(A.reported, 0)
3910 self.assertEqual(B.reported, 0)
3911 self.assertEqual(len(self.history), 0)
3912
3913 # Enqueue and merge B alone.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003914 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003915 self.waitUntilSettled()
3916
3917 self.assertEqual(B.data['status'], 'MERGED')
3918 self.assertEqual(B.reported, 2)
3919
3920 # Now that B is merged, A should be able to be enqueued and
3921 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003922 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003923 self.waitUntilSettled()
3924
3925 self.assertEqual(A.data['status'], 'MERGED')
3926 self.assertEqual(A.reported, 2)
3927
James E. Blair96698e22015-04-02 07:48:21 -07003928 def test_crd_gate_reverse(self):
3929 "Test reverse cross-repo dependencies"
3930 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3931 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003932 A.addApproval('Code-Review', 2)
3933 B.addApproval('Code-Review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003934
3935 # A Depends-On: B
3936
3937 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3938 A.subject, B.data['id'])
3939
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003940 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003941 self.waitUntilSettled()
3942
3943 self.assertEqual(A.data['status'], 'NEW')
3944 self.assertEqual(B.data['status'], 'NEW')
3945
Paul Belanger174a8272017-03-14 13:20:10 -04003946 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003947 A.addApproval('Approved', 1)
3948 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003949 self.waitUntilSettled()
3950
Paul Belanger174a8272017-03-14 13:20:10 -04003951 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003952 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003953 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003954 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003955 self.executor_server.hold_jobs_in_build = False
3956 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003957 self.waitUntilSettled()
3958
3959 self.assertEqual(A.data['status'], 'MERGED')
3960 self.assertEqual(B.data['status'], 'MERGED')
3961 self.assertEqual(A.reported, 2)
3962 self.assertEqual(B.reported, 2)
3963
Paul Belanger5dccbe72016-11-14 11:17:37 -05003964 changes = self.getJobFromHistory(
3965 'project-merge', 'org/project1').changes
3966 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003967
James E. Blair5ee24252014-12-30 10:12:29 -08003968 def test_crd_cycle(self):
3969 "Test cross-repo dependency cycles"
3970 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3971 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003972 A.addApproval('Code-Review', 2)
3973 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003974
3975 # A -> B -> A (via commit-depends)
3976
3977 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3978 A.subject, B.data['id'])
3979 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3980 B.subject, A.data['id'])
3981
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003982 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003983 self.waitUntilSettled()
3984
3985 self.assertEqual(A.reported, 0)
3986 self.assertEqual(B.reported, 0)
3987 self.assertEqual(A.data['status'], 'NEW')
3988 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003989
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003990 def test_crd_gate_unknown(self):
3991 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07003992 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003993 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3994 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003995 A.addApproval('Code-Review', 2)
3996 B.addApproval('Code-Review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003997
3998 # A Depends-On: B
3999 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4000 A.subject, B.data['id'])
4001
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004002 B.addApproval('Approved', 1)
4003 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004004 self.waitUntilSettled()
4005
4006 # Unknown projects cannot share a queue with any other
4007 # since they don't have common jobs with any other (they have no jobs).
4008 # Changes which depend on unknown project changes
4009 # should not be processed in dependent pipeline
4010 self.assertEqual(A.data['status'], 'NEW')
4011 self.assertEqual(B.data['status'], 'NEW')
4012 self.assertEqual(A.reported, 0)
4013 self.assertEqual(B.reported, 0)
4014 self.assertEqual(len(self.history), 0)
4015
James E. Blair7720b402017-06-07 08:51:49 -07004016 # Simulate change B being gated outside this layout Set the
4017 # change merged before submitting the event so that when the
4018 # event triggers a gerrit query to update the change, we get
4019 # the information that it was merged.
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004020 B.setMerged()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004021 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004022 self.waitUntilSettled()
4023 self.assertEqual(len(self.history), 0)
4024
4025 # Now that B is merged, A should be able to be enqueued and
4026 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004027 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004028 self.waitUntilSettled()
4029
4030 self.assertEqual(A.data['status'], 'MERGED')
4031 self.assertEqual(A.reported, 2)
4032 self.assertEqual(B.data['status'], 'MERGED')
4033 self.assertEqual(B.reported, 0)
4034
James E. Blairbfb8e042014-12-30 17:01:44 -08004035 def test_crd_check(self):
4036 "Test cross-repo dependencies in independent pipelines"
4037
Paul Belanger174a8272017-03-14 13:20:10 -04004038 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004039 self.gearman_server.hold_jobs_in_queue = True
4040 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4041 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4042
4043 # A Depends-On: B
4044 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4045 A.subject, B.data['id'])
4046
4047 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4048 self.waitUntilSettled()
4049
James E. Blairbfb8e042014-12-30 17:01:44 -08004050 self.gearman_server.hold_jobs_in_queue = False
4051 self.gearman_server.release()
4052 self.waitUntilSettled()
4053
Paul Belanger174a8272017-03-14 13:20:10 -04004054 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05004055 self.waitUntilSettled()
4056
James E. Blair2a535672017-04-27 12:03:15 -07004057 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08004058
Paul Belanger174a8272017-03-14 13:20:10 -04004059 self.executor_server.hold_jobs_in_build = False
4060 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05004061 self.waitUntilSettled()
4062
James E. Blairbfb8e042014-12-30 17:01:44 -08004063 self.assertEqual(A.data['status'], 'NEW')
4064 self.assertEqual(B.data['status'], 'NEW')
4065 self.assertEqual(A.reported, 1)
4066 self.assertEqual(B.reported, 0)
4067
4068 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004069 tenant = self.sched.abide.tenants.get('tenant-one')
4070 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004071
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004072 def test_crd_check_git_depends(self):
4073 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004074 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004075 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4076 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4077
4078 # Add two git-dependent changes and make sure they both report
4079 # success.
4080 B.setDependsOn(A, 1)
4081 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4082 self.waitUntilSettled()
4083 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4084 self.waitUntilSettled()
4085
James E. Blairb8c16472015-05-05 14:55:26 -07004086 self.orderedRelease()
4087 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004088 self.waitUntilSettled()
4089
4090 self.assertEqual(A.data['status'], 'NEW')
4091 self.assertEqual(B.data['status'], 'NEW')
4092 self.assertEqual(A.reported, 1)
4093 self.assertEqual(B.reported, 1)
4094
4095 self.assertEqual(self.history[0].changes, '1,1')
4096 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004097 tenant = self.sched.abide.tenants.get('tenant-one')
4098 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004099
4100 self.assertIn('Build succeeded', A.messages[0])
4101 self.assertIn('Build succeeded', B.messages[0])
4102
4103 def test_crd_check_duplicate(self):
4104 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004105 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004106 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4107 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004108 tenant = self.sched.abide.tenants.get('tenant-one')
4109 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004110
4111 # Add two git-dependent changes...
4112 B.setDependsOn(A, 1)
4113 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4114 self.waitUntilSettled()
4115 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4116
4117 # ...make sure the live one is not duplicated...
4118 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4119 self.waitUntilSettled()
4120 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4121
4122 # ...but the non-live one is able to be.
4123 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4124 self.waitUntilSettled()
4125 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4126
Clark Boylandd849822015-03-02 12:38:14 -08004127 # Release jobs in order to avoid races with change A jobs
4128 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004129 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004130 self.executor_server.hold_jobs_in_build = False
4131 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004132 self.waitUntilSettled()
4133
4134 self.assertEqual(A.data['status'], 'NEW')
4135 self.assertEqual(B.data['status'], 'NEW')
4136 self.assertEqual(A.reported, 1)
4137 self.assertEqual(B.reported, 1)
4138
4139 self.assertEqual(self.history[0].changes, '1,1 2,1')
4140 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004141 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004142
4143 self.assertIn('Build succeeded', A.messages[0])
4144 self.assertIn('Build succeeded', B.messages[0])
4145
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004146 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004147 "Test cross-repo dependencies re-enqueued in independent pipelines"
4148
4149 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004150 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4151 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004152
4153 # A Depends-On: B
4154 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4155 A.subject, B.data['id'])
4156
4157 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4158 self.waitUntilSettled()
4159
4160 self.sched.reconfigure(self.config)
4161
4162 # Make sure the items still share a change queue, and the
4163 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004164 tenant = self.sched.abide.tenants.get('tenant-one')
4165 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4166 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004167 first_item = queue.queue[0]
4168 for item in queue.queue:
4169 self.assertEqual(item.queue, first_item.queue)
4170 self.assertFalse(first_item.live)
4171 self.assertTrue(queue.queue[1].live)
4172
4173 self.gearman_server.hold_jobs_in_queue = False
4174 self.gearman_server.release()
4175 self.waitUntilSettled()
4176
4177 self.assertEqual(A.data['status'], 'NEW')
4178 self.assertEqual(B.data['status'], 'NEW')
4179 self.assertEqual(A.reported, 1)
4180 self.assertEqual(B.reported, 0)
4181
4182 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004183 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004184
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004185 def test_crd_check_reconfiguration(self):
4186 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4187
4188 def test_crd_undefined_project(self):
4189 """Test that undefined projects in dependencies are handled for
4190 independent pipelines"""
4191 # It's a hack for fake gerrit,
4192 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004193 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004194 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4195
Jesse Keatingea68ab42017-04-19 15:03:30 -07004196 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004197 def test_crd_check_ignore_dependencies(self):
4198 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004199
4200 self.gearman_server.hold_jobs_in_queue = True
4201 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4202 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4203 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4204
4205 # A Depends-On: B
4206 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4207 A.subject, B.data['id'])
4208 # C git-depends on B
4209 C.setDependsOn(B, 1)
4210 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4211 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4212 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4213 self.waitUntilSettled()
4214
4215 # Make sure none of the items share a change queue, and all
4216 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004217 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004218 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004219 self.assertEqual(len(check_pipeline.queues), 3)
4220 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4221 for item in check_pipeline.getAllItems():
4222 self.assertTrue(item.live)
4223
4224 self.gearman_server.hold_jobs_in_queue = False
4225 self.gearman_server.release()
4226 self.waitUntilSettled()
4227
4228 self.assertEqual(A.data['status'], 'NEW')
4229 self.assertEqual(B.data['status'], 'NEW')
4230 self.assertEqual(C.data['status'], 'NEW')
4231 self.assertEqual(A.reported, 1)
4232 self.assertEqual(B.reported, 1)
4233 self.assertEqual(C.reported, 1)
4234
4235 # Each job should have tested exactly one change
4236 for job in self.history:
4237 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004238
James E. Blair7cb84542017-04-19 13:35:05 -07004239 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004240 def test_crd_check_transitive(self):
4241 "Test transitive cross-repo dependencies"
4242 # Specifically, if A -> B -> C, and C gets a new patchset and
4243 # A gets a new patchset, ensure the test of A,2 includes B,1
4244 # and C,2 (not C,1 which would indicate stale data in the
4245 # cache for B).
4246 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4247 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4248 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4249
4250 # A Depends-On: B
4251 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4252 A.subject, B.data['id'])
4253
4254 # B Depends-On: C
4255 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4256 B.subject, C.data['id'])
4257
4258 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4259 self.waitUntilSettled()
4260 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4261
4262 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4263 self.waitUntilSettled()
4264 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4265
4266 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4267 self.waitUntilSettled()
4268 self.assertEqual(self.history[-1].changes, '3,1')
4269
4270 C.addPatchset()
4271 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4272 self.waitUntilSettled()
4273 self.assertEqual(self.history[-1].changes, '3,2')
4274
4275 A.addPatchset()
4276 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4277 self.waitUntilSettled()
4278 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004279
James E. Blaira8b90b32016-08-24 15:18:50 -07004280 def test_crd_check_unknown(self):
4281 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004282 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004283 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4284 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4285 # A Depends-On: B
4286 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4287 A.subject, B.data['id'])
4288
4289 # Make sure zuul has seen an event on B.
4290 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4291 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4292 self.waitUntilSettled()
4293
4294 self.assertEqual(A.data['status'], 'NEW')
4295 self.assertEqual(A.reported, 1)
4296 self.assertEqual(B.data['status'], 'NEW')
4297 self.assertEqual(B.reported, 0)
4298
James E. Blair92464a22016-04-05 10:21:26 -07004299 def test_crd_cycle_join(self):
4300 "Test an updated change creates a cycle"
4301 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4302
4303 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4304 self.waitUntilSettled()
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004305 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004306
4307 # Create B->A
4308 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4309 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4310 B.subject, A.data['id'])
4311 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4312 self.waitUntilSettled()
4313
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004314 # Dep is there so zuul should have reported on B
4315 self.assertEqual(B.reported, 1)
4316
James E. Blair92464a22016-04-05 10:21:26 -07004317 # Update A to add A->B (a cycle).
4318 A.addPatchset()
4319 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4320 A.subject, B.data['id'])
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004321 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4322 self.waitUntilSettled()
James E. Blair92464a22016-04-05 10:21:26 -07004323
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004324 # Dependency cycle injected so zuul should not have reported again on A
4325 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004326
4327 # Now if we update B to remove the depends-on, everything
4328 # should be okay. B; A->B
4329
4330 B.addPatchset()
4331 B.data['commitMessage'] = '%s\n' % (B.subject,)
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004332 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4333 self.waitUntilSettled()
Paul Belanger0e155e22016-11-14 14:12:23 -05004334
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004335 # Cycle was removed so now zuul should have reported again on A
4336 self.assertEqual(A.reported, 2)
4337
4338 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
4339 self.waitUntilSettled()
4340 self.assertEqual(B.reported, 2)
James E. Blair92464a22016-04-05 10:21:26 -07004341
Jesse Keatingcc67b122017-04-19 14:54:57 -07004342 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004343 def test_disable_at(self):
4344 "Test a pipeline will only report to the disabled trigger when failing"
4345
Jesse Keatingcc67b122017-04-19 14:54:57 -07004346 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004347 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004348 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004349 0, tenant.layout.pipelines['check']._consecutive_failures)
4350 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004351
4352 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4353 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4354 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4355 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4356 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4357 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4358 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4359 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4360 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4361 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4362 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4363
Paul Belanger174a8272017-03-14 13:20:10 -04004364 self.executor_server.failJob('project-test1', A)
4365 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004366 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004367 self.executor_server.failJob('project-test1', D)
4368 self.executor_server.failJob('project-test1', E)
4369 self.executor_server.failJob('project-test1', F)
4370 self.executor_server.failJob('project-test1', G)
4371 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004372 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004373 self.executor_server.failJob('project-test1', J)
4374 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004375
4376 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4377 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4378 self.waitUntilSettled()
4379
4380 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004381 2, tenant.layout.pipelines['check']._consecutive_failures)
4382 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004383
4384 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4385 self.waitUntilSettled()
4386
4387 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004388 0, tenant.layout.pipelines['check']._consecutive_failures)
4389 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004390
4391 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4392 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4393 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4394 self.waitUntilSettled()
4395
4396 # We should be disabled now
4397 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004398 3, tenant.layout.pipelines['check']._consecutive_failures)
4399 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004400
4401 # We need to wait between each of these patches to make sure the
4402 # smtp messages come back in an expected order
4403 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4404 self.waitUntilSettled()
4405 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4406 self.waitUntilSettled()
4407 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4408 self.waitUntilSettled()
4409
4410 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4411 # leaving a message on each change
4412 self.assertEqual(1, len(A.messages))
4413 self.assertIn('Build failed.', A.messages[0])
4414 self.assertEqual(1, len(B.messages))
4415 self.assertIn('Build failed.', B.messages[0])
4416 self.assertEqual(1, len(C.messages))
4417 self.assertIn('Build succeeded.', C.messages[0])
4418 self.assertEqual(1, len(D.messages))
4419 self.assertIn('Build failed.', D.messages[0])
4420 self.assertEqual(1, len(E.messages))
4421 self.assertIn('Build failed.', E.messages[0])
4422 self.assertEqual(1, len(F.messages))
4423 self.assertIn('Build failed.', F.messages[0])
4424
4425 # The last 3 (GHI) would have only reported via smtp.
4426 self.assertEqual(3, len(self.smtp_messages))
4427 self.assertEqual(0, len(G.messages))
4428 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004429 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004430 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004431 self.assertEqual(0, len(H.messages))
4432 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004433 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004434 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004435 self.assertEqual(0, len(I.messages))
4436 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004437 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004438 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004439
4440 # Now reload the configuration (simulate a HUP) to check the pipeline
4441 # comes out of disabled
4442 self.sched.reconfigure(self.config)
4443
Jesse Keatingcc67b122017-04-19 14:54:57 -07004444 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004445
4446 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004447 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004448 0, tenant.layout.pipelines['check']._consecutive_failures)
4449 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004450
4451 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4452 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4453 self.waitUntilSettled()
4454
4455 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004456 2, tenant.layout.pipelines['check']._consecutive_failures)
4457 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004458
4459 # J and K went back to gerrit
4460 self.assertEqual(1, len(J.messages))
4461 self.assertIn('Build failed.', J.messages[0])
4462 self.assertEqual(1, len(K.messages))
4463 self.assertIn('Build failed.', K.messages[0])
4464 # No more messages reported via smtp
4465 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004466
James E. Blaircc020532017-04-19 13:04:44 -07004467 @simple_layout('layouts/one-job-project.yaml')
4468 def test_one_job_project(self):
4469 "Test that queueing works with one job"
4470 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4471 'master', 'A')
4472 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4473 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004474 A.addApproval('Code-Review', 2)
4475 B.addApproval('Code-Review', 2)
4476 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4477 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blaircc020532017-04-19 13:04:44 -07004478 self.waitUntilSettled()
4479
4480 self.assertEqual(A.data['status'], 'MERGED')
4481 self.assertEqual(A.reported, 2)
4482 self.assertEqual(B.data['status'], 'MERGED')
4483 self.assertEqual(B.reported, 2)
4484
Paul Belanger71d98172016-11-08 10:56:31 -05004485 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004486 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004487
Paul Belanger174a8272017-03-14 13:20:10 -04004488 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004489 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4490 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4491 self.waitUntilSettled()
4492
Paul Belanger174a8272017-03-14 13:20:10 -04004493 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004494 self.waitUntilSettled()
4495
4496 self.assertEqual(len(self.builds), 2)
4497 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004498 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004499 self.waitUntilSettled()
4500
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004501 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004502 self.assertEqual(len(self.builds), 1,
4503 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004504 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004505 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004506 self.waitUntilSettled()
4507
Paul Belanger174a8272017-03-14 13:20:10 -04004508 self.executor_server.hold_jobs_in_build = False
4509 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004510 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004511 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004512 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4513 self.assertEqual(A.reported, 1)
4514 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004515
James E. Blair15be0e12017-01-03 13:45:20 -08004516 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004517 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004518
4519 self.fake_nodepool.paused = True
4520 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004521 A.addApproval('Code-Review', 2)
4522 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair15be0e12017-01-03 13:45:20 -08004523 self.waitUntilSettled()
4524
4525 self.zk.client.stop()
4526 self.zk.client.start()
4527 self.fake_nodepool.paused = False
4528 self.waitUntilSettled()
4529
4530 self.assertEqual(A.data['status'], 'MERGED')
4531 self.assertEqual(A.reported, 2)
4532
James E. Blair6ab79e02017-01-06 10:10:17 -08004533 def test_nodepool_failure(self):
4534 "Test that jobs are reported after a nodepool failure"
4535
4536 self.fake_nodepool.paused = True
4537 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004538 A.addApproval('Code-Review', 2)
4539 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6ab79e02017-01-06 10:10:17 -08004540 self.waitUntilSettled()
4541
4542 req = self.fake_nodepool.getNodeRequests()[0]
4543 self.fake_nodepool.addFailRequest(req)
4544
4545 self.fake_nodepool.paused = False
4546 self.waitUntilSettled()
4547
4548 self.assertEqual(A.data['status'], 'NEW')
4549 self.assertEqual(A.reported, 2)
4550 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4551 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4552 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4553
Arieb6f068c2016-10-09 13:11:06 +03004554
James E. Blaira002b032017-04-18 10:35:48 -07004555class TestExecutor(ZuulTestCase):
4556 tenant_config_file = 'config/single-tenant/main.yaml'
4557
4558 def assertFinalState(self):
4559 # In this test, we expect to shut down in a non-final state,
4560 # so skip these checks.
4561 pass
4562
4563 def assertCleanShutdown(self):
4564 self.log.debug("Assert clean shutdown")
4565
4566 # After shutdown, make sure no jobs are running
4567 self.assertEqual({}, self.executor_server.job_workers)
4568
4569 # Make sure that git.Repo objects have been garbage collected.
4570 repos = []
4571 gc.collect()
4572 for obj in gc.get_objects():
4573 if isinstance(obj, git.Repo):
4574 self.log.debug("Leaked git repo object: %s" % repr(obj))
4575 repos.append(obj)
4576 self.assertEqual(len(repos), 0)
4577
4578 def test_executor_shutdown(self):
4579 "Test that the executor can shut down with jobs running"
4580
4581 self.executor_server.hold_jobs_in_build = True
4582 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004583 A.addApproval('Code-Review', 2)
4584 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaira002b032017-04-18 10:35:48 -07004585 self.waitUntilSettled()
4586
4587
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004588class TestDependencyGraph(ZuulTestCase):
4589 tenant_config_file = 'config/dependency-graph/main.yaml'
4590
4591 def test_dependeny_graph_dispatch_jobs_once(self):
4592 "Test a job in a dependency graph is queued only once"
4593 # Job dependencies, starting with A
4594 # A
4595 # / \
4596 # B C
4597 # / \ / \
4598 # D F E
4599 # |
4600 # G
4601
4602 self.executor_server.hold_jobs_in_build = True
4603 change = self.fake_gerrit.addFakeChange(
4604 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004605 change.addApproval('Code-Review', 2)
4606 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004607
4608 self.waitUntilSettled()
4609 self.assertEqual([b.name for b in self.builds], ['A'])
4610
4611 self.executor_server.release('A')
4612 self.waitUntilSettled()
4613 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4614
4615 self.executor_server.release('B')
4616 self.waitUntilSettled()
4617 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4618
4619 self.executor_server.release('D')
4620 self.waitUntilSettled()
4621 self.assertEqual([b.name for b in self.builds], ['C'])
4622
4623 self.executor_server.release('C')
4624 self.waitUntilSettled()
4625 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4626
4627 self.executor_server.release('F')
4628 self.waitUntilSettled()
4629 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4630
4631 self.executor_server.release('G')
4632 self.waitUntilSettled()
4633 self.assertEqual([b.name for b in self.builds], ['E'])
4634
4635 self.executor_server.release('E')
4636 self.waitUntilSettled()
4637 self.assertEqual(len(self.builds), 0)
4638
4639 self.executor_server.hold_jobs_in_build = False
4640 self.executor_server.release()
4641 self.waitUntilSettled()
4642
4643 self.assertEqual(len(self.builds), 0)
4644 self.assertEqual(len(self.history), 7)
4645
4646 self.assertEqual(change.data['status'], 'MERGED')
4647 self.assertEqual(change.reported, 2)
4648
4649 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4650 "Test that a job waits till all dependencies are successful"
4651 # Job dependencies, starting with A
4652 # A
4653 # / \
4654 # B C*
4655 # / \ / \
4656 # D F E
4657 # |
4658 # G
4659
4660 self.executor_server.hold_jobs_in_build = True
4661 change = self.fake_gerrit.addFakeChange(
4662 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004663 change.addApproval('Code-Review', 2)
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004664
4665 self.executor_server.failJob('C', change)
4666
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004667 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004668
4669 self.waitUntilSettled()
4670 self.assertEqual([b.name for b in self.builds], ['A'])
4671
4672 self.executor_server.release('A')
4673 self.waitUntilSettled()
4674 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4675
4676 self.executor_server.release('B')
4677 self.waitUntilSettled()
4678 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4679
4680 self.executor_server.release('D')
4681 self.waitUntilSettled()
4682 self.assertEqual([b.name for b in self.builds], ['C'])
4683
4684 self.executor_server.release('C')
4685 self.waitUntilSettled()
4686 self.assertEqual(len(self.builds), 0)
4687
4688 self.executor_server.hold_jobs_in_build = False
4689 self.executor_server.release()
4690 self.waitUntilSettled()
4691
4692 self.assertEqual(len(self.builds), 0)
4693 self.assertEqual(len(self.history), 4)
4694
4695 self.assertEqual(change.data['status'], 'NEW')
4696 self.assertEqual(change.reported, 2)
4697
4698
Arieb6f068c2016-10-09 13:11:06 +03004699class TestDuplicatePipeline(ZuulTestCase):
4700 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4701
4702 def test_duplicate_pipelines(self):
4703 "Test that a change matching multiple pipelines works"
4704
4705 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4706 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4707 self.waitUntilSettled()
4708
4709 self.assertHistory([
4710 dict(name='project-test1', result='SUCCESS', changes='1,1',
4711 pipeline='dup1'),
4712 dict(name='project-test1', result='SUCCESS', changes='1,1',
4713 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004714 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004715
4716 self.assertEqual(len(A.messages), 2)
4717
Paul Belangera46a3742016-11-09 14:23:03 -05004718 if 'dup1' in A.messages[0]:
4719 self.assertIn('dup1', A.messages[0])
4720 self.assertNotIn('dup2', A.messages[0])
4721 self.assertIn('project-test1', A.messages[0])
4722 self.assertIn('dup2', A.messages[1])
4723 self.assertNotIn('dup1', A.messages[1])
4724 self.assertIn('project-test1', A.messages[1])
4725 else:
4726 self.assertIn('dup1', A.messages[1])
4727 self.assertNotIn('dup2', A.messages[1])
4728 self.assertIn('project-test1', A.messages[1])
4729 self.assertIn('dup2', A.messages[0])
4730 self.assertNotIn('dup1', A.messages[0])
4731 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004732
4733
Paul Belanger86085b32016-11-03 12:48:57 -04004734class TestSchedulerTemplatedProject(ZuulTestCase):
4735 tenant_config_file = 'config/templated-project/main.yaml'
4736
Paul Belanger174a8272017-03-14 13:20:10 -04004737 def test_job_from_templates_executed(self):
4738 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004739
4740 A = self.fake_gerrit.addFakeChange(
4741 'org/templated-project', 'master', 'A')
4742 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4743 self.waitUntilSettled()
4744
4745 self.assertEqual(self.getJobFromHistory('project-test1').result,
4746 'SUCCESS')
4747 self.assertEqual(self.getJobFromHistory('project-test2').result,
4748 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004749
4750 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004751 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004752
4753 A = self.fake_gerrit.addFakeChange(
4754 'org/layered-project', 'master', 'A')
4755 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4756 self.waitUntilSettled()
4757
4758 self.assertEqual(self.getJobFromHistory('project-test1').result,
4759 'SUCCESS')
4760 self.assertEqual(self.getJobFromHistory('project-test2').result,
4761 'SUCCESS')
4762 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4763 ).result, 'SUCCESS')
4764 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4765 ).result, 'SUCCESS')
4766 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4767 ).result, 'SUCCESS')
4768 self.assertEqual(self.getJobFromHistory('project-test6').result,
4769 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004770
4771
4772class TestSchedulerSuccessURL(ZuulTestCase):
4773 tenant_config_file = 'config/success-url/main.yaml'
4774
4775 def test_success_url(self):
4776 "Ensure bad build params are ignored"
4777 self.sched.reconfigure(self.config)
4778 self.init_repo('org/docs')
4779
4780 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4781 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4782 self.waitUntilSettled()
4783
4784 # Both builds ran: docs-draft-test + docs-draft-test2
4785 self.assertEqual(len(self.history), 2)
4786
4787 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004788 for build in self.history:
4789 if build.name == 'docs-draft-test':
4790 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06004791 elif build.name == 'docs-draft-test2':
4792 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08004793
4794 # Two msgs: 'Starting...' + results
4795 self.assertEqual(len(self.smtp_messages), 2)
4796 body = self.smtp_messages[1]['body'].splitlines()
4797 self.assertEqual('Build succeeded.', body[0])
4798
4799 self.assertIn(
4800 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4801 'docs-draft-test/%s/publish-docs/' % uuid,
4802 body[2])
4803
Paul Belanger174a8272017-03-14 13:20:10 -04004804 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004805 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05004806 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
4807 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06004808 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08004809 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004810
4811
Adam Gandelman4da00f62016-12-09 15:47:33 -08004812class TestSchedulerMerges(ZuulTestCase):
4813 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004814
4815 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004816 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004817 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004818 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004819 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4820 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4821 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004822 A.addApproval('Code-Review', 2)
4823 B.addApproval('Code-Review', 2)
4824 C.addApproval('Code-Review', 2)
4825 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4826 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4827 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman8bd57102016-12-02 12:58:42 -08004828 self.waitUntilSettled()
4829
4830 build = self.builds[-1]
James E. Blair2a535672017-04-27 12:03:15 -07004831 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4832 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004833 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004834 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman8bd57102016-12-02 12:58:42 -08004835 repo_messages.reverse()
4836
Paul Belanger174a8272017-03-14 13:20:10 -04004837 self.executor_server.hold_jobs_in_build = False
4838 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004839 self.waitUntilSettled()
4840
4841 return repo_messages
4842
4843 def _test_merge(self, mode):
4844 us_path = os.path.join(
4845 self.upstream_root, 'org/project-%s' % mode)
4846 expected_messages = [
4847 'initial commit',
4848 'add content from fixture',
4849 # the intermediate commits order is nondeterministic
4850 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4851 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4852 ]
4853 result = self._test_project_merge_mode(mode)
4854 self.assertEqual(result[:2], expected_messages[:2])
4855 self.assertEqual(result[-2:], expected_messages[-2:])
4856
4857 def test_project_merge_mode_merge(self):
4858 self._test_merge('merge')
4859
4860 def test_project_merge_mode_merge_resolve(self):
4861 self._test_merge('merge-resolve')
4862
4863 def test_project_merge_mode_cherrypick(self):
4864 expected_messages = [
4865 'initial commit',
4866 'add content from fixture',
4867 'A-1',
4868 'B-1',
4869 'C-1']
4870 result = self._test_project_merge_mode('cherry-pick')
4871 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004872
4873 def test_merge_branch(self):
4874 "Test that the right commits are on alternate branches"
4875 self.create_branch('org/project-merge-branches', 'mp')
4876
Paul Belanger174a8272017-03-14 13:20:10 -04004877 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004878 A = self.fake_gerrit.addFakeChange(
4879 'org/project-merge-branches', 'mp', 'A')
4880 B = self.fake_gerrit.addFakeChange(
4881 'org/project-merge-branches', 'mp', 'B')
4882 C = self.fake_gerrit.addFakeChange(
4883 'org/project-merge-branches', 'mp', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004884 A.addApproval('Code-Review', 2)
4885 B.addApproval('Code-Review', 2)
4886 C.addApproval('Code-Review', 2)
4887 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4888 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4889 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08004890 self.waitUntilSettled()
4891
Paul Belanger174a8272017-03-14 13:20:10 -04004892 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004893 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004894 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004895 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004896 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004897 self.waitUntilSettled()
4898
4899 build = self.builds[-1]
James E. Blair3b222492017-07-21 15:17:37 -07004900 self.assertEqual(build.parameters['zuul']['branch'], 'mp')
James E. Blair2a535672017-04-27 12:03:15 -07004901 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4902 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004903 repo = git.Repo(path)
4904
James E. Blair247cab72017-07-20 16:52:36 -07004905 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004906 repo_messages.reverse()
4907 correct_messages = [
4908 'initial commit',
4909 'add content from fixture',
4910 'mp commit',
4911 'A-1', 'B-1', 'C-1']
4912 self.assertEqual(repo_messages, correct_messages)
4913
Paul Belanger174a8272017-03-14 13:20:10 -04004914 self.executor_server.hold_jobs_in_build = False
4915 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004916 self.waitUntilSettled()
4917
4918 def test_merge_multi_branch(self):
4919 "Test that dependent changes on multiple branches are merged"
4920 self.create_branch('org/project-merge-branches', 'mp')
4921
Paul Belanger174a8272017-03-14 13:20:10 -04004922 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004923 A = self.fake_gerrit.addFakeChange(
4924 'org/project-merge-branches', 'master', 'A')
4925 B = self.fake_gerrit.addFakeChange(
4926 'org/project-merge-branches', 'mp', 'B')
4927 C = self.fake_gerrit.addFakeChange(
4928 'org/project-merge-branches', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004929 A.addApproval('Code-Review', 2)
4930 B.addApproval('Code-Review', 2)
4931 C.addApproval('Code-Review', 2)
4932 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4933 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4934 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08004935 self.waitUntilSettled()
4936
4937 job_A = None
4938 for job in self.builds:
4939 if 'project-merge' in job.name:
4940 job_A = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08004941
James E. Blair2a535672017-04-27 12:03:15 -07004942 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
4943 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004944 repo = git.Repo(path)
4945 repo_messages = [c.message.strip()
James E. Blair247cab72017-07-20 16:52:36 -07004946 for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004947 repo_messages.reverse()
4948 correct_messages = [
4949 'initial commit', 'add content from fixture', 'A-1']
4950 self.assertEqual(repo_messages, correct_messages)
4951
Paul Belanger174a8272017-03-14 13:20:10 -04004952 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004953 self.waitUntilSettled()
4954
4955 job_B = None
4956 for job in self.builds:
4957 if 'project-merge' in job.name:
4958 job_B = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08004959
James E. Blair2a535672017-04-27 12:03:15 -07004960 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
4961 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004962 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004963 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004964 repo_messages.reverse()
4965 correct_messages = [
4966 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4967 self.assertEqual(repo_messages, correct_messages)
4968
Paul Belanger174a8272017-03-14 13:20:10 -04004969 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004970 self.waitUntilSettled()
4971
4972 job_C = None
4973 for job in self.builds:
4974 if 'project-merge' in job.name:
4975 job_C = job
James E. Blairb1345832017-07-21 14:51:35 -07004976
James E. Blair2a535672017-04-27 12:03:15 -07004977 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
4978 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004979 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004980 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004981
4982 repo_messages.reverse()
4983 correct_messages = [
4984 'initial commit', 'add content from fixture',
4985 'A-1', 'C-1']
4986 # Ensure the right commits are in the history for this ref
4987 self.assertEqual(repo_messages, correct_messages)
4988
Paul Belanger174a8272017-03-14 13:20:10 -04004989 self.executor_server.hold_jobs_in_build = False
4990 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004991 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01004992
4993
James E. Blair9ea0d0b2017-04-20 09:27:15 -07004994class TestSemaphore(ZuulTestCase):
4995 tenant_config_file = 'config/semaphore/main.yaml'
4996
4997 def test_semaphore_one(self):
4998 "Test semaphores with max=1 (mutex)"
4999 tenant = self.sched.abide.tenants.get('tenant-one')
5000
5001 self.executor_server.hold_jobs_in_build = True
5002
5003 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5004 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5005 self.assertFalse('test-semaphore' in
5006 tenant.semaphore_handler.semaphores)
5007
5008 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5009 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5010 self.waitUntilSettled()
5011
5012 self.assertEqual(len(self.builds), 3)
5013 self.assertEqual(self.builds[0].name, 'project-test1')
5014 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
5015 self.assertEqual(self.builds[2].name, 'project-test1')
5016
5017 self.executor_server.release('semaphore-one-test1')
5018 self.waitUntilSettled()
5019
5020 self.assertEqual(len(self.builds), 3)
5021 self.assertEqual(self.builds[0].name, 'project-test1')
5022 self.assertEqual(self.builds[1].name, 'project-test1')
5023 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5024 self.assertTrue('test-semaphore' in
5025 tenant.semaphore_handler.semaphores)
5026
5027 self.executor_server.release('semaphore-one-test2')
5028 self.waitUntilSettled()
5029
5030 self.assertEqual(len(self.builds), 3)
5031 self.assertEqual(self.builds[0].name, 'project-test1')
5032 self.assertEqual(self.builds[1].name, 'project-test1')
5033 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
5034 self.assertTrue('test-semaphore' in
5035 tenant.semaphore_handler.semaphores)
5036
5037 self.executor_server.release('semaphore-one-test1')
5038 self.waitUntilSettled()
5039
5040 self.assertEqual(len(self.builds), 3)
5041 self.assertEqual(self.builds[0].name, 'project-test1')
5042 self.assertEqual(self.builds[1].name, 'project-test1')
5043 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5044 self.assertTrue('test-semaphore' in
5045 tenant.semaphore_handler.semaphores)
5046
5047 self.executor_server.release('semaphore-one-test2')
5048 self.waitUntilSettled()
5049
5050 self.assertEqual(len(self.builds), 2)
5051 self.assertEqual(self.builds[0].name, 'project-test1')
5052 self.assertEqual(self.builds[1].name, 'project-test1')
5053 self.assertFalse('test-semaphore' in
5054 tenant.semaphore_handler.semaphores)
5055
5056 self.executor_server.hold_jobs_in_build = False
5057 self.executor_server.release()
5058
5059 self.waitUntilSettled()
5060 self.assertEqual(len(self.builds), 0)
5061
5062 self.assertEqual(A.reported, 1)
5063 self.assertEqual(B.reported, 1)
5064 self.assertFalse('test-semaphore' in
5065 tenant.semaphore_handler.semaphores)
5066
5067 def test_semaphore_two(self):
5068 "Test semaphores with max>1"
5069 tenant = self.sched.abide.tenants.get('tenant-one')
5070
5071 self.executor_server.hold_jobs_in_build = True
5072 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5073 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5074 self.assertFalse('test-semaphore-two' in
5075 tenant.semaphore_handler.semaphores)
5076
5077 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5078 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
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, 'semaphore-two-test1')
5084 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5085 self.assertEqual(self.builds[3].name, 'project-test1')
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), 4)
5095 self.assertEqual(self.builds[0].name, 'project-test1')
5096 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
5097 self.assertEqual(self.builds[2].name, 'project-test1')
5098 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
5099 self.assertTrue('test-semaphore-two' in
5100 tenant.semaphore_handler.semaphores)
5101 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5102 'test-semaphore-two', [])), 2)
5103
5104 self.executor_server.release('semaphore-two-test2')
5105 self.waitUntilSettled()
5106
5107 self.assertEqual(len(self.builds), 4)
5108 self.assertEqual(self.builds[0].name, 'project-test1')
5109 self.assertEqual(self.builds[1].name, 'project-test1')
5110 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5111 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
5112 self.assertTrue('test-semaphore-two' in
5113 tenant.semaphore_handler.semaphores)
5114 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5115 'test-semaphore-two', [])), 2)
5116
5117 self.executor_server.release('semaphore-two-test1')
5118 self.waitUntilSettled()
5119
5120 self.assertEqual(len(self.builds), 3)
5121 self.assertEqual(self.builds[0].name, 'project-test1')
5122 self.assertEqual(self.builds[1].name, 'project-test1')
5123 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5124 self.assertTrue('test-semaphore-two' in
5125 tenant.semaphore_handler.semaphores)
5126 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5127 'test-semaphore-two', [])), 1)
5128
5129 self.executor_server.release('semaphore-two-test2')
5130 self.waitUntilSettled()
5131
5132 self.assertEqual(len(self.builds), 2)
5133 self.assertEqual(self.builds[0].name, 'project-test1')
5134 self.assertEqual(self.builds[1].name, 'project-test1')
5135 self.assertFalse('test-semaphore-two' in
5136 tenant.semaphore_handler.semaphores)
5137
5138 self.executor_server.hold_jobs_in_build = False
5139 self.executor_server.release()
5140
5141 self.waitUntilSettled()
5142 self.assertEqual(len(self.builds), 0)
5143
5144 self.assertEqual(A.reported, 1)
5145 self.assertEqual(B.reported, 1)
5146
5147 def test_semaphore_abandon(self):
5148 "Test abandon with job semaphores"
5149 self.executor_server.hold_jobs_in_build = True
5150 tenant = self.sched.abide.tenants.get('tenant-one')
5151 check_pipeline = tenant.layout.pipelines['check']
5152
5153 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5154 self.assertFalse('test-semaphore' in
5155 tenant.semaphore_handler.semaphores)
5156
5157 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5158 self.waitUntilSettled()
5159
5160 self.assertTrue('test-semaphore' in
5161 tenant.semaphore_handler.semaphores)
5162
5163 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5164 self.waitUntilSettled()
5165
5166 # The check pipeline should be empty
5167 items = check_pipeline.getAllItems()
5168 self.assertEqual(len(items), 0)
5169
5170 # The semaphore should be released
5171 self.assertFalse('test-semaphore' in
5172 tenant.semaphore_handler.semaphores)
5173
5174 self.executor_server.hold_jobs_in_build = False
5175 self.executor_server.release()
5176 self.waitUntilSettled()
5177
Tobias Henkel0f714002017-06-30 23:30:52 +02005178 def test_semaphore_new_patchset(self):
5179 "Test new patchset with job semaphores"
5180 self.executor_server.hold_jobs_in_build = True
5181 tenant = self.sched.abide.tenants.get('tenant-one')
5182 check_pipeline = tenant.layout.pipelines['check']
5183
5184 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5185 self.assertFalse('test-semaphore' in
5186 tenant.semaphore_handler.semaphores)
5187
5188 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5189 self.waitUntilSettled()
5190
5191 self.assertTrue('test-semaphore' in
5192 tenant.semaphore_handler.semaphores)
5193 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5194 self.assertEqual(len(semaphore), 1)
5195
5196 A.addPatchset()
5197 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
5198 self.waitUntilSettled()
5199
5200 self.assertTrue('test-semaphore' in
5201 tenant.semaphore_handler.semaphores)
5202 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5203 self.assertEqual(len(semaphore), 1)
5204
5205 items = check_pipeline.getAllItems()
5206 self.assertEqual(items[0].change.number, '1')
5207 self.assertEqual(items[0].change.patchset, '2')
5208 self.assertTrue(items[0].live)
5209
5210 self.executor_server.hold_jobs_in_build = False
5211 self.executor_server.release()
5212 self.waitUntilSettled()
5213
5214 # The semaphore should be released
5215 self.assertFalse('test-semaphore' in
5216 tenant.semaphore_handler.semaphores)
5217
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005218 def test_semaphore_reconfigure(self):
5219 "Test reconfigure with job semaphores"
5220 self.executor_server.hold_jobs_in_build = True
5221 tenant = self.sched.abide.tenants.get('tenant-one')
5222 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5223 self.assertFalse('test-semaphore' in
5224 tenant.semaphore_handler.semaphores)
5225
5226 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5227 self.waitUntilSettled()
5228
5229 self.assertTrue('test-semaphore' in
5230 tenant.semaphore_handler.semaphores)
5231
5232 # reconfigure without layout change
5233 self.sched.reconfigure(self.config)
5234 self.waitUntilSettled()
5235 tenant = self.sched.abide.tenants.get('tenant-one')
5236
5237 # semaphore still must be held
5238 self.assertTrue('test-semaphore' in
5239 tenant.semaphore_handler.semaphores)
5240
5241 self.commitConfigUpdate(
5242 'common-config',
5243 'config/semaphore/zuul-reconfiguration.yaml')
5244 self.sched.reconfigure(self.config)
5245 self.waitUntilSettled()
5246 tenant = self.sched.abide.tenants.get('tenant-one')
5247
5248 self.executor_server.release('project-test1')
5249 self.waitUntilSettled()
5250
5251 # There should be no builds anymore
5252 self.assertEqual(len(self.builds), 0)
5253
5254 # The semaphore should be released
5255 self.assertFalse('test-semaphore' in
5256 tenant.semaphore_handler.semaphores)
5257
5258
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005259class TestSemaphoreMultiTenant(ZuulTestCase):
5260 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5261
5262 def test_semaphore_tenant_isolation(self):
5263 "Test semaphores in multiple tenants"
5264
5265 self.waitUntilSettled()
5266 tenant_one = self.sched.abide.tenants.get('tenant-one')
5267 tenant_two = self.sched.abide.tenants.get('tenant-two')
5268
5269 self.executor_server.hold_jobs_in_build = True
5270 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5271 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5272 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5273 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5274 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5275 self.assertFalse('test-semaphore' in
5276 tenant_one.semaphore_handler.semaphores)
5277 self.assertFalse('test-semaphore' in
5278 tenant_two.semaphore_handler.semaphores)
5279
5280 # add patches to project1 of tenant-one
5281 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5282 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5283 self.waitUntilSettled()
5284
5285 # one build of project1-test1 must run
5286 # semaphore of tenant-one must be acquired once
5287 # semaphore of tenant-two must not be acquired
5288 self.assertEqual(len(self.builds), 1)
5289 self.assertEqual(self.builds[0].name, 'project1-test1')
5290 self.assertTrue('test-semaphore' in
5291 tenant_one.semaphore_handler.semaphores)
5292 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5293 'test-semaphore', [])), 1)
5294 self.assertFalse('test-semaphore' in
5295 tenant_two.semaphore_handler.semaphores)
5296
5297 # add patches to project2 of tenant-two
5298 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5299 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5300 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5301 self.waitUntilSettled()
5302
5303 # one build of project1-test1 must run
5304 # two builds of project2-test1 must run
5305 # semaphore of tenant-one must be acquired once
5306 # semaphore of tenant-two must be acquired twice
5307 self.assertEqual(len(self.builds), 3)
5308 self.assertEqual(self.builds[0].name, 'project1-test1')
5309 self.assertEqual(self.builds[1].name, 'project2-test1')
5310 self.assertEqual(self.builds[2].name, 'project2-test1')
5311 self.assertTrue('test-semaphore' in
5312 tenant_one.semaphore_handler.semaphores)
5313 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5314 'test-semaphore', [])), 1)
5315 self.assertTrue('test-semaphore' in
5316 tenant_two.semaphore_handler.semaphores)
5317 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5318 'test-semaphore', [])), 2)
5319
5320 self.executor_server.release('project1-test1')
5321 self.waitUntilSettled()
5322
5323 # one build of project1-test1 must run
5324 # two builds of project2-test1 must run
5325 # semaphore of tenant-one must be acquired once
5326 # semaphore of tenant-two must be acquired twice
5327 self.assertEqual(len(self.builds), 3)
5328 self.assertEqual(self.builds[0].name, 'project2-test1')
5329 self.assertEqual(self.builds[1].name, 'project2-test1')
5330 self.assertEqual(self.builds[2].name, 'project1-test1')
5331 self.assertTrue('test-semaphore' in
5332 tenant_one.semaphore_handler.semaphores)
5333 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5334 'test-semaphore', [])), 1)
5335 self.assertTrue('test-semaphore' in
5336 tenant_two.semaphore_handler.semaphores)
5337 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5338 'test-semaphore', [])), 2)
5339
5340 self.executor_server.release('project2-test1')
5341 self.waitUntilSettled()
5342
5343 # one build of project1-test1 must run
5344 # one build of project2-test1 must run
5345 # semaphore of tenant-one must be acquired once
5346 # semaphore of tenant-two must be acquired once
5347 self.assertEqual(len(self.builds), 2)
5348 self.assertTrue('test-semaphore' in
5349 tenant_one.semaphore_handler.semaphores)
5350 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5351 'test-semaphore', [])), 1)
5352 self.assertTrue('test-semaphore' in
5353 tenant_two.semaphore_handler.semaphores)
5354 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5355 'test-semaphore', [])), 1)
5356
5357 self.executor_server.hold_jobs_in_build = False
5358 self.executor_server.release()
5359
5360 self.waitUntilSettled()
5361
5362 # no build must run
5363 # semaphore of tenant-one must not be acquired
5364 # semaphore of tenant-two must not be acquired
5365 self.assertEqual(len(self.builds), 0)
5366 self.assertFalse('test-semaphore' in
5367 tenant_one.semaphore_handler.semaphores)
5368 self.assertFalse('test-semaphore' in
5369 tenant_two.semaphore_handler.semaphores)
5370
5371 self.assertEqual(A.reported, 1)
5372 self.assertEqual(B.reported, 1)
5373
5374
5375class TestSemaphoreInRepo(ZuulTestCase):
Tobias Henkelabf973e2017-07-28 10:07:34 +02005376 config_file = 'zuul-connections-gerrit-and-github.conf'
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005377 tenant_config_file = 'config/in-repo/main.yaml'
5378
5379 def test_semaphore_in_repo(self):
5380 "Test semaphores in repo config"
5381
5382 # This tests dynamic semaphore handling in project repos. The semaphore
5383 # max value should not be evaluated dynamically but must be updated
5384 # after the change lands.
5385
5386 self.waitUntilSettled()
5387 tenant = self.sched.abide.tenants.get('tenant-one')
5388
5389 in_repo_conf = textwrap.dedent(
5390 """
5391 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +02005392 name: project-test1
5393
5394 - job:
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005395 name: project-test2
5396 semaphore: test-semaphore
5397
5398 - project:
5399 name: org/project
5400 tenant-one-gate:
5401 jobs:
5402 - project-test2
5403
5404 # the max value in dynamic layout must be ignored
5405 - semaphore:
5406 name: test-semaphore
5407 max: 2
5408 """)
5409
5410 in_repo_playbook = textwrap.dedent(
5411 """
5412 - hosts: all
5413 tasks: []
5414 """)
5415
5416 file_dict = {'.zuul.yaml': in_repo_conf,
5417 'playbooks/project-test2.yaml': in_repo_playbook}
5418 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5419 files=file_dict)
5420 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5421 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5422 B.setDependsOn(A, 1)
5423 C.setDependsOn(A, 1)
5424
5425 self.executor_server.hold_jobs_in_build = True
5426
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005427 A.addApproval('Code-Review', 2)
5428 B.addApproval('Code-Review', 2)
5429 C.addApproval('Code-Review', 2)
5430 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5431 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5432 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005433 self.waitUntilSettled()
5434
5435 # check that the layout in a queue item still has max value of 1
5436 # for test-semaphore
5437 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5438 queue = None
5439 for queue_candidate in pipeline.queues:
5440 if queue_candidate.name == 'org/project':
5441 queue = queue_candidate
5442 break
5443 queue_item = queue.queue[0]
5444 item_dynamic_layout = queue_item.current_build_set.layout
5445 dynamic_test_semaphore = \
5446 item_dynamic_layout.semaphores.get('test-semaphore')
5447 self.assertEqual(dynamic_test_semaphore.max, 1)
5448
5449 # one build must be in queue, one semaphores acquired
5450 self.assertEqual(len(self.builds), 1)
5451 self.assertEqual(self.builds[0].name, 'project-test2')
5452 self.assertTrue('test-semaphore' in
5453 tenant.semaphore_handler.semaphores)
5454 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5455 'test-semaphore', [])), 1)
5456
5457 self.executor_server.release('project-test2')
5458 self.waitUntilSettled()
5459
5460 # change A must be merged
5461 self.assertEqual(A.data['status'], 'MERGED')
5462 self.assertEqual(A.reported, 2)
5463
5464 # send change-merged event as the gerrit mock doesn't send it
5465 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5466 self.waitUntilSettled()
5467
5468 # now that change A was merged, the new semaphore max must be effective
5469 tenant = self.sched.abide.tenants.get('tenant-one')
5470 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5471
5472 # two builds must be in queue, two semaphores acquired
5473 self.assertEqual(len(self.builds), 2)
5474 self.assertEqual(self.builds[0].name, 'project-test2')
5475 self.assertEqual(self.builds[1].name, 'project-test2')
5476 self.assertTrue('test-semaphore' in
5477 tenant.semaphore_handler.semaphores)
5478 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5479 'test-semaphore', [])), 2)
5480
5481 self.executor_server.release('project-test2')
5482 self.waitUntilSettled()
5483
5484 self.assertEqual(len(self.builds), 0)
5485 self.assertFalse('test-semaphore' in
5486 tenant.semaphore_handler.semaphores)
5487
5488 self.executor_server.hold_jobs_in_build = False
5489 self.executor_server.release()
5490
5491 self.waitUntilSettled()
5492 self.assertEqual(len(self.builds), 0)
5493
5494 self.assertEqual(A.reported, 2)
5495 self.assertEqual(B.reported, 2)
5496 self.assertEqual(C.reported, 2)