blob: bab5162e5369b0b56e1d24ddcba23892dada6cae [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')
James E. Blairfcbf95d2017-07-25 16:38:09 -070065 self.assertEqual(self.getJobFromHistory('project-test2').node,
66 'label1')
Paul Belanger0a21f0a2017-06-13 13:14:42 -040067
68
Clark Boylanb640e052014-04-03 16:41:46 -070069class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070070 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010071
Paul Belanger174a8272017-03-14 13:20:10 -040072 def test_jobs_executed(self):
73 "Test that jobs are executed and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070074
James E. Blairb0fcae42012-07-17 11:12:10 -070075 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020076 A.addApproval('Code-Review', 2)
77 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070078 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040079 self.assertEqual(self.getJobFromHistory('project-merge').result,
80 'SUCCESS')
81 self.assertEqual(self.getJobFromHistory('project-test1').result,
82 'SUCCESS')
83 self.assertEqual(self.getJobFromHistory('project-test2').result,
84 'SUCCESS')
85 self.assertEqual(A.data['status'], 'MERGED')
86 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070087 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -070088 'label1')
James E. Blairfcbf95d2017-07-25 16:38:09 -070089 self.assertEqual(self.getJobFromHistory('project-test2').node,
90 'label1')
James E. Blairb0fcae42012-07-17 11:12:10 -070091
James E. Blair80ac1582017-10-09 07:02:40 -070092 for stat in self.statsd.stats:
93 k, v = stat.decode('utf-8').split(':')
94 self.log.debug('stat %s:%s', k, v)
James E. Blair552b54f2016-07-22 13:55:32 -070095 # TODOv3(jeblair): we may want to report stats by tenant (also?).
Monty Taylorf1385042017-05-27 12:25:05 -050096 # Per-driver
97 self.assertReportedStat('zuul.event.gerrit.comment-added', value='1|c')
98 # Per-driver per-connection
99 self.assertReportedStat('zuul.event.gerrit.gerrit.comment-added',
100 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -0700101 self.assertReportedStat(
James E. Blair80ac1582017-10-09 07:02:40 -0700102 'zuul.tenant.tenant-one.pipeline.gate.current_changes',
103 value='1|g')
James E. Blair412e5582013-04-22 15:50:12 -0700104 self.assertReportedStat(
James E. Blair80ac1582017-10-09 07:02:40 -0700105 'zuul.tenant.tenant-one.pipeline.gate.project.review_example_com.'
106 'org_project.master.job.project-merge.SUCCESS', kind='ms')
107 self.assertReportedStat(
108 'zuul.tenant.tenant-one.pipeline.gate.project.review_example_com.'
109 'org_project.master.job.project-merge.SUCCESS', value='1|c')
110 self.assertReportedStat(
111 'zuul.tenant.tenant-one.pipeline.gate.resident_time', kind='ms')
112 self.assertReportedStat(
113 'zuul.tenant.tenant-one.pipeline.gate.total_changes', value='1|c')
114 self.assertReportedStat(
115 'zuul.tenant.tenant-one.pipeline.gate.project.review_example_com.'
116 'org_project.master.resident_time', kind='ms')
117 self.assertReportedStat(
118 'zuul.tenant.tenant-one.pipeline.gate.project.review_example_com.'
119 'org_project.master.total_changes', value='1|c')
James E. Blairfaf81982017-10-10 15:42:26 -0700120 exec_key = 'zuul.executor.%s' % self.executor_server.hostname
121 self.assertReportedStat(exec_key + '.builds', value='1|c')
James E. Blair4f1731b2017-10-10 18:11:42 -0700122 self.assertReportedStat('zuul.nodepool.requested', value='1|c')
123 self.assertReportedStat('zuul.nodepool.requested.label.label1',
124 value='1|c')
125 self.assertReportedStat('zuul.nodepool.fulfilled.label.label1',
126 value='1|c')
127 self.assertReportedStat('zuul.nodepool.requested.size.1', value='1|c')
128 self.assertReportedStat('zuul.nodepool.fulfilled.size.1', value='1|c')
129 self.assertReportedStat('zuul.nodepool.current_requests', value='1|g')
James E. Blair412e5582013-04-22 15:50:12 -0700130
James E. Blair2ef29e92017-07-21 15:25:05 -0700131 for build in self.history:
132 self.assertTrue(build.parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -0700133
James E. Blair3cb10702013-08-24 08:56:03 -0700134 def test_initial_pipeline_gauges(self):
135 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -0700136 self.assertReportedStat('zuul.pipeline.gate.current_changes',
137 value='0|g')
138 self.assertReportedStat('zuul.pipeline.check.current_changes',
139 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -0700140
James E. Blair34776ee2016-08-25 13:53:54 -0700141 def test_job_branch(self):
142 "Test the correct variant of a job runs on a branch"
143 self.create_branch('org/project', 'stable')
144 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200145 A.addApproval('Code-Review', 2)
146 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair34776ee2016-08-25 13:53:54 -0700147 self.waitUntilSettled()
148 self.assertEqual(self.getJobFromHistory('project-test1').result,
149 'SUCCESS')
150 self.assertEqual(self.getJobFromHistory('project-test2').result,
151 'SUCCESS')
152 self.assertEqual(A.data['status'], 'MERGED')
153 self.assertEqual(A.reported, 2,
154 "A should report start and success")
155 self.assertIn('gate', A.messages[1],
156 "A should transit gate")
157 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -0700158 'label2')
James E. Blair34776ee2016-08-25 13:53:54 -0700159
James E. Blairb0fcae42012-07-17 11:12:10 -0700160 def test_parallel_changes(self):
161 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700162
Paul Belanger174a8272017-03-14 13:20:10 -0400163 self.executor_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700164 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
165 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
166 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200167 A.addApproval('Code-Review', 2)
168 B.addApproval('Code-Review', 2)
169 C.addApproval('Code-Review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700170
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200171 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
172 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
173 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700174
175 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400176 self.assertEqual(len(self.builds), 1)
177 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700178 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700179
Paul Belanger174a8272017-03-14 13:20:10 -0400180 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700181 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400182 self.assertEqual(len(self.builds), 3)
183 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700184 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400185 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700186 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400187 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700188 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700189
Paul Belanger174a8272017-03-14 13:20:10 -0400190 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700191 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400192 self.assertEqual(len(self.builds), 5)
193 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700194 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400195 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700196 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700197
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400198 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700199 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400200 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700201 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700202
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400203 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700204 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700205
Paul Belanger174a8272017-03-14 13:20:10 -0400206 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700207 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400208 self.assertEqual(len(self.builds), 6)
209 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700210 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400211 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700212 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700213
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400214 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700215 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400216 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700217 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700218
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400219 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700220 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400221 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700222 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700223
Paul Belanger174a8272017-03-14 13:20:10 -0400224 self.executor_server.hold_jobs_in_build = False
225 self.executor_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700226 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400227 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700228
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400229 self.assertEqual(len(self.history), 9)
230 self.assertEqual(A.data['status'], 'MERGED')
231 self.assertEqual(B.data['status'], 'MERGED')
232 self.assertEqual(C.data['status'], 'MERGED')
233 self.assertEqual(A.reported, 2)
234 self.assertEqual(B.reported, 2)
235 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700236
237 def test_failed_changes(self):
238 "Test that a change behind a failed change is retested"
Paul Belanger174a8272017-03-14 13:20:10 -0400239 self.executor_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700240
James E. Blairb02a3bb2012-07-30 17:49:55 -0700241 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
242 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200243 A.addApproval('Code-Review', 2)
244 B.addApproval('Code-Review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700245
Paul Belanger174a8272017-03-14 13:20:10 -0400246 self.executor_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700247
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200248 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
249 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700250 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700251 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400252
Paul Belanger174a8272017-03-14 13:20:10 -0400253 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400254 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700255 # A/project-merge is complete
256 self.assertBuilds([
257 dict(name='project-test1', changes='1,1'),
258 dict(name='project-test2', changes='1,1'),
259 dict(name='project-merge', changes='1,1 2,1'),
260 ])
James E. Blaire2819012013-06-28 17:17:26 -0400261
Paul Belanger174a8272017-03-14 13:20:10 -0400262 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400263 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700264 # A/project-merge is complete
265 # B/project-merge is complete
266 self.assertBuilds([
267 dict(name='project-test1', changes='1,1'),
268 dict(name='project-test2', changes='1,1'),
269 dict(name='project-test1', changes='1,1 2,1'),
270 dict(name='project-test2', changes='1,1 2,1'),
271 ])
272
273 # Release project-test1 for A which will fail. This will
Paul Belanger174a8272017-03-14 13:20:10 -0400274 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700275 self.builds[0].release()
276 self.waitUntilSettled()
277
278 self.orderedRelease()
279 self.assertHistory([
280 dict(name='project-merge', result='SUCCESS', changes='1,1'),
281 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
282 dict(name='project-test1', result='FAILURE', changes='1,1'),
283 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
284 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
285 dict(name='project-test2', result='SUCCESS', changes='1,1'),
286 dict(name='project-merge', result='SUCCESS', changes='2,1'),
287 dict(name='project-test1', result='SUCCESS', changes='2,1'),
288 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700289 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700290
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400291 self.assertEqual(A.data['status'], 'NEW')
292 self.assertEqual(B.data['status'], 'MERGED')
293 self.assertEqual(A.reported, 2)
294 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700295
296 def test_independent_queues(self):
297 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700298
Paul Belanger174a8272017-03-14 13:20:10 -0400299 self.executor_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900300 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700301 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
302 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200303 A.addApproval('Code-Review', 2)
304 B.addApproval('Code-Review', 2)
305 C.addApproval('Code-Review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700306
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200307 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800308 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700309
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200310 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800311 self.waitUntilSettled()
312
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200313 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700314 self.waitUntilSettled()
315
316 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700317 self.assertBuilds([
318 dict(name='project-merge', changes='1,1'),
319 dict(name='project-merge', changes='2,1'),
320 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700321
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700322 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700323 self.builds[0].release()
324 self.waitUntilSettled()
325 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700326 self.waitUntilSettled()
327 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400328 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700329 self.waitUntilSettled()
330
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700331 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700332 self.assertBuilds([
333 dict(name='project-test1', changes='1,1'),
334 dict(name='project-test2', changes='1,1'),
335 dict(name='project-test1', changes='2,1'),
336 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500337 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700338 dict(name='project-test1', changes='2,1 3,1'),
339 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800340 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700341 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700342
James E. Blair5f0d49e2016-08-19 09:36:50 -0700343 self.orderedRelease()
344 self.assertHistory([
345 dict(name='project-merge', result='SUCCESS', changes='1,1'),
346 dict(name='project-merge', result='SUCCESS', changes='2,1'),
347 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
348 dict(name='project-test1', result='SUCCESS', changes='1,1'),
349 dict(name='project-test2', result='SUCCESS', changes='1,1'),
350 dict(name='project-test1', result='SUCCESS', changes='2,1'),
351 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500352 dict(
353 name='project1-project2-integration',
354 result='SUCCESS',
355 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700356 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
357 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800358 dict(name='project1-project2-integration',
359 result='SUCCESS',
360 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700361 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700362
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400363 self.assertEqual(A.data['status'], 'MERGED')
364 self.assertEqual(B.data['status'], 'MERGED')
365 self.assertEqual(C.data['status'], 'MERGED')
366 self.assertEqual(A.reported, 2)
367 self.assertEqual(B.reported, 2)
368 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700369
370 def test_failed_change_at_head(self):
371 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700372
Paul Belanger174a8272017-03-14 13:20:10 -0400373 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700374 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
375 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
376 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200377 A.addApproval('Code-Review', 2)
378 B.addApproval('Code-Review', 2)
379 C.addApproval('Code-Review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700380
Paul Belanger174a8272017-03-14 13:20:10 -0400381 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700382
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200383 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
384 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
385 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700386
387 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700388
James E. Blairb536ecc2016-08-31 10:11:42 -0700389 self.assertBuilds([
390 dict(name='project-merge', changes='1,1'),
391 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700392
Paul Belanger174a8272017-03-14 13:20:10 -0400393 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700394 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400395 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700396 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400397 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700398 self.waitUntilSettled()
399
James E. Blairb536ecc2016-08-31 10:11:42 -0700400 self.assertBuilds([
401 dict(name='project-test1', changes='1,1'),
402 dict(name='project-test2', changes='1,1'),
403 dict(name='project-test1', changes='1,1 2,1'),
404 dict(name='project-test2', changes='1,1 2,1'),
405 dict(name='project-test1', changes='1,1 2,1 3,1'),
406 dict(name='project-test2', changes='1,1 2,1 3,1'),
407 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700408
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400409 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700410 self.waitUntilSettled()
411
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400412 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700413 self.assertBuilds([
414 dict(name='project-test2', changes='1,1'),
415 dict(name='project-merge', changes='2,1'),
416 ])
417 # Unordered history comparison because the aborts can finish
418 # in any order.
419 self.assertHistory([
420 dict(name='project-merge', result='SUCCESS',
421 changes='1,1'),
422 dict(name='project-merge', result='SUCCESS',
423 changes='1,1 2,1'),
424 dict(name='project-merge', result='SUCCESS',
425 changes='1,1 2,1 3,1'),
426 dict(name='project-test1', result='FAILURE',
427 changes='1,1'),
428 dict(name='project-test1', result='ABORTED',
429 changes='1,1 2,1'),
430 dict(name='project-test2', result='ABORTED',
431 changes='1,1 2,1'),
432 dict(name='project-test1', result='ABORTED',
433 changes='1,1 2,1 3,1'),
434 dict(name='project-test2', result='ABORTED',
435 changes='1,1 2,1 3,1'),
436 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700437
Paul Belanger174a8272017-03-14 13:20:10 -0400438 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700439 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400440 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700441 self.waitUntilSettled()
442 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700443
James E. Blairb536ecc2016-08-31 10:11:42 -0700444 self.assertBuilds([])
445 self.assertHistory([
446 dict(name='project-merge', result='SUCCESS',
447 changes='1,1'),
448 dict(name='project-merge', result='SUCCESS',
449 changes='1,1 2,1'),
450 dict(name='project-merge', result='SUCCESS',
451 changes='1,1 2,1 3,1'),
452 dict(name='project-test1', result='FAILURE',
453 changes='1,1'),
454 dict(name='project-test1', result='ABORTED',
455 changes='1,1 2,1'),
456 dict(name='project-test2', result='ABORTED',
457 changes='1,1 2,1'),
458 dict(name='project-test1', result='ABORTED',
459 changes='1,1 2,1 3,1'),
460 dict(name='project-test2', result='ABORTED',
461 changes='1,1 2,1 3,1'),
462 dict(name='project-merge', result='SUCCESS',
463 changes='2,1'),
464 dict(name='project-merge', result='SUCCESS',
465 changes='2,1 3,1'),
466 dict(name='project-test2', result='SUCCESS',
467 changes='1,1'),
468 dict(name='project-test1', result='SUCCESS',
469 changes='2,1'),
470 dict(name='project-test2', result='SUCCESS',
471 changes='2,1'),
472 dict(name='project-test1', result='SUCCESS',
473 changes='2,1 3,1'),
474 dict(name='project-test2', result='SUCCESS',
475 changes='2,1 3,1'),
476 ], ordered=False)
477
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400478 self.assertEqual(A.data['status'], 'NEW')
479 self.assertEqual(B.data['status'], 'MERGED')
480 self.assertEqual(C.data['status'], 'MERGED')
481 self.assertEqual(A.reported, 2)
482 self.assertEqual(B.reported, 2)
483 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700484
James E. Blair0aac4872013-08-23 14:02:38 -0700485 def test_failed_change_in_middle(self):
486 "Test a failed change in the middle of the queue"
487
Paul Belanger174a8272017-03-14 13:20:10 -0400488 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700489 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
490 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
491 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200492 A.addApproval('Code-Review', 2)
493 B.addApproval('Code-Review', 2)
494 C.addApproval('Code-Review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700495
Paul Belanger174a8272017-03-14 13:20:10 -0400496 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700497
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200498 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
499 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
500 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700501
502 self.waitUntilSettled()
503
Paul Belanger174a8272017-03-14 13:20:10 -0400504 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700505 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400506 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700507 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400508 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700509 self.waitUntilSettled()
510
511 self.assertEqual(len(self.builds), 6)
512 self.assertEqual(self.builds[0].name, 'project-test1')
513 self.assertEqual(self.builds[1].name, 'project-test2')
514 self.assertEqual(self.builds[2].name, 'project-test1')
515 self.assertEqual(self.builds[3].name, 'project-test2')
516 self.assertEqual(self.builds[4].name, 'project-test1')
517 self.assertEqual(self.builds[5].name, 'project-test2')
518
519 self.release(self.builds[2])
520 self.waitUntilSettled()
521
James E. Blair972e3c72013-08-29 12:04:55 -0700522 # project-test1 and project-test2 for A
523 # project-test2 for B
524 # project-merge for C (without B)
525 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700526 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
527
Paul Belanger174a8272017-03-14 13:20:10 -0400528 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700529 self.waitUntilSettled()
530
531 # project-test1 and project-test2 for A
532 # project-test2 for B
533 # project-test1 and project-test2 for C
534 self.assertEqual(len(self.builds), 5)
535
Paul Belanger2e2a0492016-10-30 18:33:37 -0400536 tenant = self.sched.abide.tenants.get('tenant-one')
537 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700538 builds = items[0].current_build_set.getBuilds()
539 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
540 self.assertEqual(self.countJobResults(builds, None), 2)
541 builds = items[1].current_build_set.getBuilds()
542 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
543 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
544 self.assertEqual(self.countJobResults(builds, None), 1)
545 builds = items[2].current_build_set.getBuilds()
546 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700547 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700548
Paul Belanger174a8272017-03-14 13:20:10 -0400549 self.executor_server.hold_jobs_in_build = False
550 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700551 self.waitUntilSettled()
552
553 self.assertEqual(len(self.builds), 0)
554 self.assertEqual(len(self.history), 12)
555 self.assertEqual(A.data['status'], 'MERGED')
556 self.assertEqual(B.data['status'], 'NEW')
557 self.assertEqual(C.data['status'], 'MERGED')
558 self.assertEqual(A.reported, 2)
559 self.assertEqual(B.reported, 2)
560 self.assertEqual(C.reported, 2)
561
James E. Blaird466dc42012-07-31 10:42:56 -0700562 def test_failed_change_at_head_with_queue(self):
563 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700564
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700565 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700566 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
567 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
568 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200569 A.addApproval('Code-Review', 2)
570 B.addApproval('Code-Review', 2)
571 C.addApproval('Code-Review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700572
Paul Belanger174a8272017-03-14 13:20:10 -0400573 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700574
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200575 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
576 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
577 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700578
579 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700580 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400581 self.assertEqual(len(self.builds), 0)
582 self.assertEqual(len(queue), 1)
Clint Byrumf322fe22017-05-10 20:53:12 -0700583 self.assertEqual(queue[0].name, b'executor:execute')
584 job_args = json.loads(queue[0].arguments.decode('utf8'))
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800585 self.assertEqual(job_args['job'], 'project-merge')
586 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700587
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700588 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700589 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700590 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700591 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700592 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700593 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700594 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700595
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400596 self.assertEqual(len(self.builds), 0)
597 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800598
599 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700600 json.loads(queue[0].arguments.decode('utf8'))['job'],
601 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800602 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700603 json.loads(queue[1].arguments.decode('utf8'))['job'],
604 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800605 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700606 json.loads(queue[2].arguments.decode('utf8'))['job'],
607 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800608 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700609 json.loads(queue[3].arguments.decode('utf8'))['job'],
610 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800611 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700612 json.loads(queue[4].arguments.decode('utf8'))['job'],
613 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800614 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700615 json.loads(queue[5].arguments.decode('utf8'))['job'],
616 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700617
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700618 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700619 self.waitUntilSettled()
620
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400621 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700622 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400623 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
624 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700625
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700626 self.gearman_server.hold_jobs_in_queue = False
627 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700628 self.waitUntilSettled()
629
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400630 self.assertEqual(len(self.builds), 0)
631 self.assertEqual(len(self.history), 11)
632 self.assertEqual(A.data['status'], 'NEW')
633 self.assertEqual(B.data['status'], 'MERGED')
634 self.assertEqual(C.data['status'], 'MERGED')
635 self.assertEqual(A.reported, 2)
636 self.assertEqual(B.reported, 2)
637 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700638
James E. Blairce8a2132016-05-19 15:21:52 -0700639 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400640 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700641 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200642 A.addApproval('Code-Review', 2)
643 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700644 self.waitUntilSettled()
645 time.sleep(2)
646
James E. Blairae0f23c2017-09-13 10:55:15 -0600647 data = json.loads(self.sched.formatStatusJSON('tenant-one'))
James E. Blairce8a2132016-05-19 15:21:52 -0700648 found_job = None
649 for pipeline in data['pipelines']:
650 if pipeline['name'] != 'gate':
651 continue
652 for queue in pipeline['change_queues']:
653 for head in queue['heads']:
654 for item in head:
655 for job in item['jobs']:
656 if job['name'] == 'project-merge':
657 found_job = job
658 break
659
660 self.assertIsNotNone(found_job)
661 if iteration == 1:
662 self.assertIsNotNone(found_job['estimated_time'])
663 self.assertIsNone(found_job['remaining_time'])
664 else:
665 self.assertIsNotNone(found_job['estimated_time'])
666 self.assertTrue(found_job['estimated_time'] >= 2)
667 self.assertIsNotNone(found_job['remaining_time'])
668
Paul Belanger174a8272017-03-14 13:20:10 -0400669 self.executor_server.hold_jobs_in_build = False
670 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700671 self.waitUntilSettled()
672
673 def test_time_database(self):
674 "Test the time database"
675
676 self._test_time_database(1)
677 self._test_time_database(2)
678
James E. Blairfef71632013-09-23 11:15:47 -0700679 def test_two_failed_changes_at_head(self):
680 "Test that changes are reparented correctly if 2 fail at head"
681
Paul Belanger174a8272017-03-14 13:20:10 -0400682 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700683 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
684 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
685 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200686 A.addApproval('Code-Review', 2)
687 B.addApproval('Code-Review', 2)
688 C.addApproval('Code-Review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700689
Paul Belanger174a8272017-03-14 13:20:10 -0400690 self.executor_server.failJob('project-test1', A)
691 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700692
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200693 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
694 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
695 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700696 self.waitUntilSettled()
697
Paul Belanger174a8272017-03-14 13:20:10 -0400698 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700699 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400700 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700701 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400702 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700703 self.waitUntilSettled()
704
705 self.assertEqual(len(self.builds), 6)
706 self.assertEqual(self.builds[0].name, 'project-test1')
707 self.assertEqual(self.builds[1].name, 'project-test2')
708 self.assertEqual(self.builds[2].name, 'project-test1')
709 self.assertEqual(self.builds[3].name, 'project-test2')
710 self.assertEqual(self.builds[4].name, 'project-test1')
711 self.assertEqual(self.builds[5].name, 'project-test2')
712
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400713 self.assertTrue(self.builds[0].hasChanges(A))
714 self.assertTrue(self.builds[2].hasChanges(A))
715 self.assertTrue(self.builds[2].hasChanges(B))
716 self.assertTrue(self.builds[4].hasChanges(A))
717 self.assertTrue(self.builds[4].hasChanges(B))
718 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700719
720 # Fail change B first
721 self.release(self.builds[2])
722 self.waitUntilSettled()
723
724 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400725 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700726 self.waitUntilSettled()
727
728 self.assertEqual(len(self.builds), 5)
729 self.assertEqual(self.builds[0].name, 'project-test1')
730 self.assertEqual(self.builds[1].name, 'project-test2')
731 self.assertEqual(self.builds[2].name, 'project-test2')
732 self.assertEqual(self.builds[3].name, 'project-test1')
733 self.assertEqual(self.builds[4].name, 'project-test2')
734
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400735 self.assertTrue(self.builds[1].hasChanges(A))
736 self.assertTrue(self.builds[2].hasChanges(A))
737 self.assertTrue(self.builds[2].hasChanges(B))
738 self.assertTrue(self.builds[4].hasChanges(A))
739 self.assertFalse(self.builds[4].hasChanges(B))
740 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700741
742 # Finish running all passing jobs for change A
743 self.release(self.builds[1])
744 self.waitUntilSettled()
745 # Fail and report change A
746 self.release(self.builds[0])
747 self.waitUntilSettled()
748
749 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400750 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700751 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400752 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700753 self.waitUntilSettled()
754
755 self.assertEqual(len(self.builds), 4)
756 self.assertEqual(self.builds[0].name, 'project-test1') # B
757 self.assertEqual(self.builds[1].name, 'project-test2') # B
758 self.assertEqual(self.builds[2].name, 'project-test1') # C
759 self.assertEqual(self.builds[3].name, 'project-test2') # C
760
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400761 self.assertFalse(self.builds[1].hasChanges(A))
762 self.assertTrue(self.builds[1].hasChanges(B))
763 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700764
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400765 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700766 # After A failed and B and C restarted, B should be back in
767 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400768 self.assertTrue(self.builds[2].hasChanges(B))
769 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700770
Paul Belanger174a8272017-03-14 13:20:10 -0400771 self.executor_server.hold_jobs_in_build = False
772 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700773 self.waitUntilSettled()
774
775 self.assertEqual(len(self.builds), 0)
776 self.assertEqual(len(self.history), 21)
777 self.assertEqual(A.data['status'], 'NEW')
778 self.assertEqual(B.data['status'], 'NEW')
779 self.assertEqual(C.data['status'], 'MERGED')
780 self.assertEqual(A.reported, 2)
781 self.assertEqual(B.reported, 2)
782 self.assertEqual(C.reported, 2)
783
James E. Blair8c803f82012-07-31 16:25:42 -0700784 def test_patch_order(self):
785 "Test that dependent patches are tested in the right order"
786 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
787 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
788 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200789 A.addApproval('Code-Review', 2)
790 B.addApproval('Code-Review', 2)
791 C.addApproval('Code-Review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700792
793 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
794 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
795 M2.setMerged()
796 M1.setMerged()
797
798 # C -> B -> A -> M1 -> M2
799 # M2 is here to make sure it is never queried. If it is, it
800 # means zuul is walking down the entire history of merged
801 # changes.
802
803 C.setDependsOn(B, 1)
804 B.setDependsOn(A, 1)
805 A.setDependsOn(M1, 1)
806 M1.setDependsOn(M2, 1)
807
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200808 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700809
810 self.waitUntilSettled()
811
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400812 self.assertEqual(A.data['status'], 'NEW')
813 self.assertEqual(B.data['status'], 'NEW')
814 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700815
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200816 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
817 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700818
819 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400820 self.assertEqual(M2.queried, 0)
821 self.assertEqual(A.data['status'], 'MERGED')
822 self.assertEqual(B.data['status'], 'MERGED')
823 self.assertEqual(C.data['status'], 'MERGED')
824 self.assertEqual(A.reported, 2)
825 self.assertEqual(B.reported, 2)
826 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700827
James E. Blair063672f2015-01-29 13:09:12 -0800828 def test_needed_changes_enqueue(self):
829 "Test that a needed change is enqueued ahead"
830 # A Given a git tree like this, if we enqueue
831 # / \ change C, we should walk up and down the tree
832 # B G and enqueue changes in the order ABCDEFG.
833 # /|\ This is also the order that you would get if
834 # *C E F you enqueued changes in the order ABCDEFG, so
835 # / the ordering is stable across re-enqueue events.
836 # D
837
838 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
839 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
840 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
841 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
842 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
843 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
844 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
845 B.setDependsOn(A, 1)
846 C.setDependsOn(B, 1)
847 D.setDependsOn(C, 1)
848 E.setDependsOn(B, 1)
849 F.setDependsOn(B, 1)
850 G.setDependsOn(A, 1)
851
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200852 A.addApproval('Code-Review', 2)
853 B.addApproval('Code-Review', 2)
854 C.addApproval('Code-Review', 2)
855 D.addApproval('Code-Review', 2)
856 E.addApproval('Code-Review', 2)
857 F.addApproval('Code-Review', 2)
858 G.addApproval('Code-Review', 2)
859 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800860
861 self.waitUntilSettled()
862
863 self.assertEqual(A.data['status'], 'NEW')
864 self.assertEqual(B.data['status'], 'NEW')
865 self.assertEqual(C.data['status'], 'NEW')
866 self.assertEqual(D.data['status'], 'NEW')
867 self.assertEqual(E.data['status'], 'NEW')
868 self.assertEqual(F.data['status'], 'NEW')
869 self.assertEqual(G.data['status'], 'NEW')
870
871 # We're about to add approvals to changes without adding the
872 # triggering events to Zuul, so that we can be sure that it is
873 # enqueing the changes based on dependencies, not because of
874 # triggering events. Since it will have the changes cached
875 # already (without approvals), we need to clear the cache
876 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400877 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100878 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800879
Paul Belanger174a8272017-03-14 13:20:10 -0400880 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200881 A.addApproval('Approved', 1)
882 B.addApproval('Approved', 1)
883 D.addApproval('Approved', 1)
884 E.addApproval('Approved', 1)
885 F.addApproval('Approved', 1)
886 G.addApproval('Approved', 1)
887 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800888
889 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400890 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800891 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400892 self.executor_server.hold_jobs_in_build = False
893 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800894 self.waitUntilSettled()
895
896 self.assertEqual(A.data['status'], 'MERGED')
897 self.assertEqual(B.data['status'], 'MERGED')
898 self.assertEqual(C.data['status'], 'MERGED')
899 self.assertEqual(D.data['status'], 'MERGED')
900 self.assertEqual(E.data['status'], 'MERGED')
901 self.assertEqual(F.data['status'], 'MERGED')
902 self.assertEqual(G.data['status'], 'MERGED')
903 self.assertEqual(A.reported, 2)
904 self.assertEqual(B.reported, 2)
905 self.assertEqual(C.reported, 2)
906 self.assertEqual(D.reported, 2)
907 self.assertEqual(E.reported, 2)
908 self.assertEqual(F.reported, 2)
909 self.assertEqual(G.reported, 2)
910 self.assertEqual(self.history[6].changes,
911 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
912
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100913 def test_source_cache(self):
914 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400915 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700916
917 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
918 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
919 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200920 A.addApproval('Code-Review', 2)
921 B.addApproval('Code-Review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700922
923 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
924 M1.setMerged()
925
926 B.setDependsOn(A, 1)
927 A.setDependsOn(M1, 1)
928
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200929 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700930 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
931
932 self.waitUntilSettled()
933
934 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700935 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700936 build.release()
937 self.waitUntilSettled()
938 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700939 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700940 build.release()
941 self.waitUntilSettled()
942
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200943 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700944 self.waitUntilSettled()
945
Joshua Hesketh352264b2015-08-11 23:42:08 +1000946 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700947 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000948 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700949
Paul Belanger174a8272017-03-14 13:20:10 -0400950 self.executor_server.hold_jobs_in_build = False
951 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700952 self.waitUntilSettled()
953
954 self.assertEqual(A.data['status'], 'MERGED')
955 self.assertEqual(B.data['status'], 'MERGED')
956 self.assertEqual(A.queried, 2) # Initial and isMerged
957 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
958
James E. Blair8c803f82012-07-31 16:25:42 -0700959 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700960 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700961 # TODO: move to test_gerrit (this is a unit test!)
962 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400963 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -0700964 (trusted, project) = tenant.getProject('org/project')
965 source = project.source
Paul Belanger9bba4902016-11-02 16:07:33 -0400966
967 # TODO(pabelanger): As we add more source / trigger APIs we should make
968 # it easier for users to create events for testing.
969 event = zuul.model.TriggerEvent()
970 event.trigger_name = 'gerrit'
971 event.change_number = '1'
972 event.patch_number = '2'
973
974 a = source.getChange(event)
975 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700976 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700977
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200978 A.addApproval('Code-Review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400979 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700980 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700981
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200982 A.addApproval('Approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400983 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700984 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700985
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700986 def test_project_merge_conflict(self):
987 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700988
989 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700990 A = self.fake_gerrit.addFakeChange('org/project',
991 'master', 'A',
992 files={'conflict': 'foo'})
993 B = self.fake_gerrit.addFakeChange('org/project',
994 'master', 'B',
995 files={'conflict': 'bar'})
996 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -0700997 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200998 A.addApproval('Code-Review', 2)
999 B.addApproval('Code-Review', 2)
1000 C.addApproval('Code-Review', 2)
1001 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1002 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1003 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -07001004 self.waitUntilSettled()
1005
James E. Blair6736beb2013-07-11 15:18:15 -07001006 self.assertEqual(A.reported, 1)
James E. Blair6736beb2013-07-11 15:18:15 -07001007 self.assertEqual(C.reported, 1)
1008
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001009 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001010 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001011 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001012 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001013 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001014 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001015
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001016 self.gearman_server.hold_jobs_in_queue = False
1017 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001018 self.waitUntilSettled()
1019
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001020 self.assertEqual(A.data['status'], 'MERGED')
1021 self.assertEqual(B.data['status'], 'NEW')
1022 self.assertEqual(C.data['status'], 'MERGED')
1023 self.assertEqual(A.reported, 2)
Tobias Henkel9842bd72017-05-16 13:40:03 +02001024 self.assertIn('Merge Failed', B.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001025 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001026
1027 self.assertHistory([
1028 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1029 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1030 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1031 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
1032 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
1033 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
1034 ], ordered=False)
1035
1036 def test_delayed_merge_conflict(self):
1037 "Test that delayed check merge conflicts are handled properly"
1038
1039 # Hold jobs in the gearman queue so that we can test whether
James E. Blair1960d682017-04-28 15:44:14 -07001040 # the executor sucesfully merges a change based on an old
1041 # repo state (frozen by the scheduler) which would otherwise
1042 # conflict.
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001043 self.gearman_server.hold_jobs_in_queue = True
1044 A = self.fake_gerrit.addFakeChange('org/project',
1045 'master', 'A',
1046 files={'conflict': 'foo'})
1047 B = self.fake_gerrit.addFakeChange('org/project',
1048 'master', 'B',
1049 files={'conflict': 'bar'})
1050 C = self.fake_gerrit.addFakeChange('org/project',
1051 'master', 'C')
1052 C.setDependsOn(B, 1)
1053
1054 # A enters the gate queue; B and C enter the check queue
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001055 A.addApproval('Code-Review', 2)
1056 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001057 self.waitUntilSettled()
1058 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
James E. Blair8d144dc2017-05-05 10:13:45 -07001059 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001060 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1061 self.waitUntilSettled()
1062
1063 self.assertEqual(A.reported, 1)
1064 self.assertEqual(B.reported, 0) # Check does not report start
1065 self.assertEqual(C.reported, 0) # Check does not report start
1066
1067 # A merges while B and C are queued in check
1068 # Release A project-merge
1069 queue = self.gearman_server.getQueue()
1070 self.release(queue[0])
1071 self.waitUntilSettled()
1072
1073 # Release A project-test*
1074 # gate has higher precedence, so A's test jobs are added in
1075 # front of the merge jobs for B and C
1076 queue = self.gearman_server.getQueue()
1077 self.release(queue[0])
1078 self.release(queue[1])
1079 self.waitUntilSettled()
1080
1081 self.assertEqual(A.data['status'], 'MERGED')
1082 self.assertEqual(B.data['status'], 'NEW')
1083 self.assertEqual(C.data['status'], 'NEW')
1084 self.assertEqual(A.reported, 2)
1085 self.assertEqual(B.reported, 0)
1086 self.assertEqual(C.reported, 0)
1087 self.assertHistory([
1088 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1089 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1090 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1091 ], ordered=False)
1092
1093 # B and C report merge conflicts
1094 # Release B project-merge
1095 queue = self.gearman_server.getQueue()
1096 self.release(queue[0])
1097 self.waitUntilSettled()
1098
1099 # Release C
1100 self.gearman_server.hold_jobs_in_queue = False
1101 self.gearman_server.release()
1102 self.waitUntilSettled()
1103
1104 self.assertEqual(A.data['status'], 'MERGED')
1105 self.assertEqual(B.data['status'], 'NEW')
1106 self.assertEqual(C.data['status'], 'NEW')
1107 self.assertEqual(A.reported, 2)
1108 self.assertEqual(B.reported, 1)
1109 self.assertEqual(C.reported, 1)
1110
1111 self.assertHistory([
1112 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1113 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1114 dict(name='project-test2', result='SUCCESS', changes='1,1'),
James E. Blair1960d682017-04-28 15:44:14 -07001115 dict(name='project-merge', result='SUCCESS', changes='2,1'),
1116 dict(name='project-test1', result='SUCCESS', changes='2,1'),
1117 dict(name='project-test2', result='SUCCESS', changes='2,1'),
1118 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
1119 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
1120 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001121 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001122
James E. Blairdaabed22012-08-15 15:38:57 -07001123 def test_post(self):
1124 "Test that post jobs run"
James E. Blair289f5932017-07-27 15:02:29 -07001125 p = "review.example.com/org/project"
1126 upstream = self.getUpstreamRepos([p])
1127 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1128 A.setMerged()
1129 A_commit = str(upstream[p].commit('master'))
1130 self.log.debug("A commit: %s" % A_commit)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001131
Zhongyue Luo5d556072012-09-21 02:00:47 +09001132 e = {
1133 "type": "ref-updated",
1134 "submitter": {
1135 "name": "User Name",
1136 },
1137 "refUpdate": {
1138 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
James E. Blair289f5932017-07-27 15:02:29 -07001139 "newRev": A_commit,
Zhongyue Luo5d556072012-09-21 02:00:47 +09001140 "refName": "master",
1141 "project": "org/project",
1142 }
1143 }
James E. Blairdaabed22012-08-15 15:38:57 -07001144 self.fake_gerrit.addEvent(e)
1145 self.waitUntilSettled()
1146
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001147 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001148 self.assertEqual(len(self.history), 1)
1149 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001150
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001151 def test_post_ignore_deletes(self):
1152 "Test that deleting refs does not trigger post jobs"
1153
1154 e = {
1155 "type": "ref-updated",
1156 "submitter": {
1157 "name": "User Name",
1158 },
1159 "refUpdate": {
1160 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1161 "newRev": "0000000000000000000000000000000000000000",
1162 "refName": "master",
1163 "project": "org/project",
1164 }
1165 }
1166 self.fake_gerrit.addEvent(e)
1167 self.waitUntilSettled()
1168
1169 job_names = [x.name for x in self.history]
1170 self.assertEqual(len(self.history), 0)
1171 self.assertNotIn('project-post', job_names)
1172
James E. Blaircc324192017-04-19 14:36:08 -07001173 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001174 def test_post_ignore_deletes_negative(self):
1175 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001176 e = {
1177 "type": "ref-updated",
1178 "submitter": {
1179 "name": "User Name",
1180 },
1181 "refUpdate": {
1182 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1183 "newRev": "0000000000000000000000000000000000000000",
James E. Blair289f5932017-07-27 15:02:29 -07001184 "refName": "testbranch",
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001185 "project": "org/project",
1186 }
1187 }
1188 self.fake_gerrit.addEvent(e)
1189 self.waitUntilSettled()
1190
1191 job_names = [x.name for x in self.history]
1192 self.assertEqual(len(self.history), 1)
1193 self.assertIn('project-post', job_names)
1194
James E. Blairec056492016-07-22 09:45:56 -07001195 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001196 def test_build_configuration_branch_interaction(self):
1197 "Test that switching between branches works"
1198 self.test_build_configuration()
1199 self.test_build_configuration_branch()
1200 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001201 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001202 repo = git.Repo(path)
1203 repo.heads.master.commit = repo.commit('init')
1204 self.test_build_configuration()
1205
James E. Blairdb93b302017-07-19 15:33:11 -07001206 def test_dependent_changes_rebase(self):
1207 # Test that no errors occur when we walk a dependency tree
1208 # with an unused leaf node due to a rebase.
1209 # Start by constructing: C -> B -> A
1210 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1211 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1212 B.setDependsOn(A, 1)
1213
1214 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1215 C.setDependsOn(B, 1)
1216
1217 # Then rebase to form: D -> C -> A
1218 C.addPatchset() # C,2
1219 C.setDependsOn(A, 1)
1220
1221 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1222 D.setDependsOn(C, 2)
1223
1224 # Walk the entire tree
1225 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1226 self.waitUntilSettled()
1227 self.assertEqual(len(self.history), 3)
1228
1229 # Verify that walking just part of the tree still works
1230 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
1231 self.waitUntilSettled()
1232 self.assertEqual(len(self.history), 6)
1233
James E. Blaircaec0c52012-08-22 14:52:22 -07001234 def test_dependent_changes_dequeue(self):
1235 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001236
James E. Blaircaec0c52012-08-22 14:52:22 -07001237 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1238 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1239 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001240 A.addApproval('Code-Review', 2)
1241 B.addApproval('Code-Review', 2)
1242 C.addApproval('Code-Review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001243
1244 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1245 M1.setMerged()
1246
1247 # C -> B -> A -> M1
1248
1249 C.setDependsOn(B, 1)
1250 B.setDependsOn(A, 1)
1251 A.setDependsOn(M1, 1)
1252
Paul Belanger174a8272017-03-14 13:20:10 -04001253 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001254
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001255 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1256 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1257 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001258
1259 self.waitUntilSettled()
1260
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001261 self.assertEqual(A.data['status'], 'NEW')
1262 self.assertEqual(A.reported, 2)
1263 self.assertEqual(B.data['status'], 'NEW')
1264 self.assertEqual(B.reported, 2)
1265 self.assertEqual(C.data['status'], 'NEW')
James E. Blair0d3e83b2017-06-05 13:51:57 -07001266 self.assertIn('This change depends on a change that failed to merge.',
1267 C.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001268 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001269
James E. Blair972e3c72013-08-29 12:04:55 -07001270 def test_failing_dependent_changes(self):
1271 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001272 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001273 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1274 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1275 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1276 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1277 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001278 A.addApproval('Code-Review', 2)
1279 B.addApproval('Code-Review', 2)
1280 C.addApproval('Code-Review', 2)
1281 D.addApproval('Code-Review', 2)
1282 E.addApproval('Code-Review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001283
1284 # E, D -> C -> B, A
1285
1286 D.setDependsOn(C, 1)
1287 C.setDependsOn(B, 1)
1288
Paul Belanger174a8272017-03-14 13:20:10 -04001289 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001290
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001291 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1292 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
1293 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1294 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1295 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001296
1297 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001298 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001299 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001300 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001301 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001302 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001303 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001304 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001305 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001306 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001307 self.waitUntilSettled()
1308
Paul Belanger174a8272017-03-14 13:20:10 -04001309 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001310 for build in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07001311 if build.parameters['zuul']['change'] != '1':
James E. Blair972e3c72013-08-29 12:04:55 -07001312 build.release()
1313 self.waitUntilSettled()
1314
Paul Belanger174a8272017-03-14 13:20:10 -04001315 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001316 self.waitUntilSettled()
1317
1318 self.assertEqual(A.data['status'], 'MERGED')
1319 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001320 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001321 self.assertEqual(B.data['status'], 'NEW')
1322 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001323 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001324 self.assertEqual(C.data['status'], 'NEW')
1325 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001326 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001327 self.assertEqual(D.data['status'], 'NEW')
1328 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001329 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001330 self.assertEqual(E.data['status'], 'MERGED')
1331 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001332 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001333 self.assertEqual(len(self.history), 18)
1334
James E. Blairec590122012-08-22 15:19:31 -07001335 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001336 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001337 # If it's dequeued more than once, we should see extra
1338 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001339
Paul Belanger174a8272017-03-14 13:20:10 -04001340 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001341 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1342 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1343 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001344 A.addApproval('Code-Review', 2)
1345 B.addApproval('Code-Review', 2)
1346 C.addApproval('Code-Review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001347
Paul Belanger174a8272017-03-14 13:20:10 -04001348 self.executor_server.failJob('project-test1', A)
1349 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001350
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001351 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1352 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1353 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001354
1355 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001356
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001357 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001358 self.assertEqual(self.builds[0].name, 'project-merge')
1359 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001360
Paul Belanger174a8272017-03-14 13:20:10 -04001361 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001362 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001363 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001364 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001365 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001366 self.waitUntilSettled()
1367
James E. Blair6df67132017-04-19 13:53:20 -07001368 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001369 self.assertEqual(self.builds[0].name, 'project-test1')
1370 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001371 self.assertEqual(self.builds[2].name, 'project-test1')
1372 self.assertEqual(self.builds[3].name, 'project-test2')
1373 self.assertEqual(self.builds[4].name, 'project-test1')
1374 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001375
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001376 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001377 self.waitUntilSettled()
1378
James E. Blair6df67132017-04-19 13:53:20 -07001379 self.assertEqual(len(self.builds), 2) # test2, merge for B
1380 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001381
Paul Belanger174a8272017-03-14 13:20:10 -04001382 self.executor_server.hold_jobs_in_build = False
1383 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001384 self.waitUntilSettled()
1385
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001386 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001387 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001388
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001389 self.assertEqual(A.data['status'], 'NEW')
1390 self.assertEqual(B.data['status'], 'MERGED')
1391 self.assertEqual(C.data['status'], 'MERGED')
1392 self.assertEqual(A.reported, 2)
1393 self.assertEqual(B.reported, 2)
1394 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001395
James E. Blaircc020532017-04-19 13:04:44 -07001396 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001397 def test_nonvoting_job(self):
1398 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001399
James E. Blair4ec821f2012-08-23 15:28:28 -07001400 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1401 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001402 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001403 self.executor_server.failJob('nonvoting-project-test2', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001404 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001405
1406 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001407
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001408 self.assertEqual(A.data['status'], 'MERGED')
1409 self.assertEqual(A.reported, 2)
1410 self.assertEqual(
1411 self.getJobFromHistory('nonvoting-project-merge').result,
1412 'SUCCESS')
1413 self.assertEqual(
1414 self.getJobFromHistory('nonvoting-project-test1').result,
1415 'SUCCESS')
1416 self.assertEqual(
1417 self.getJobFromHistory('nonvoting-project-test2').result,
1418 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001419
James E. Blair2ef29e92017-07-21 15:25:05 -07001420 self.assertTrue(self.getJobFromHistory('nonvoting-project-merge').
1421 parameters['zuul']['voting'])
1422 self.assertTrue(self.getJobFromHistory('nonvoting-project-test1').
1423 parameters['zuul']['voting'])
1424 self.assertFalse(self.getJobFromHistory('nonvoting-project-test2').
1425 parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -07001426
James E. Blaire0487072012-08-29 17:38:31 -07001427 def test_check_queue_success(self):
1428 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001429
James E. Blaire0487072012-08-29 17:38:31 -07001430 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1431 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1432
1433 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001434
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001435 self.assertEqual(A.data['status'], 'NEW')
1436 self.assertEqual(A.reported, 1)
1437 self.assertEqual(self.getJobFromHistory('project-merge').result,
1438 'SUCCESS')
1439 self.assertEqual(self.getJobFromHistory('project-test1').result,
1440 'SUCCESS')
1441 self.assertEqual(self.getJobFromHistory('project-test2').result,
1442 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001443
1444 def test_check_queue_failure(self):
1445 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001446
James E. Blaire0487072012-08-29 17:38:31 -07001447 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001448 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001449 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1450
1451 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001452
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001453 self.assertEqual(A.data['status'], 'NEW')
1454 self.assertEqual(A.reported, 1)
1455 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001456 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001457 self.assertEqual(self.getJobFromHistory('project-test1').result,
1458 'SUCCESS')
1459 self.assertEqual(self.getJobFromHistory('project-test2').result,
1460 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001461
David Shrewsburyffab07a2017-07-24 12:45:07 -04001462 @simple_layout('layouts/autohold.yaml')
1463 def test_autohold(self):
1464 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1465
1466 client = zuul.rpcclient.RPCClient('127.0.0.1',
1467 self.gearman_server.port)
1468 self.addCleanup(client.shutdown)
David Shrewsbury36b2adf2017-07-31 15:40:13 -04001469 r = client.autohold('tenant-one', 'org/project', 'project-test2',
1470 "reason text", 1)
David Shrewsburyffab07a2017-07-24 12:45:07 -04001471 self.assertTrue(r)
1472
1473 self.executor_server.failJob('project-test2', A)
1474 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1475
1476 self.waitUntilSettled()
1477
1478 self.assertEqual(A.data['status'], 'NEW')
1479 self.assertEqual(A.reported, 1)
1480 self.assertEqual(self.getJobFromHistory('project-test2').result,
1481 'FAILURE')
1482
1483 # Check nodepool for a held node
1484 held_node = None
1485 for node in self.fake_nodepool.getNodes():
1486 if node['state'] == zuul.model.STATE_HOLD:
1487 held_node = node
1488 break
1489 self.assertIsNotNone(held_node)
1490
1491 # Validate node has recorded the failed job
1492 self.assertEqual(
1493 held_node['hold_job'],
1494 " ".join(['tenant-one',
1495 'review.example.com/org/project',
1496 'project-test2'])
1497 )
David Shrewsburyf9af9df2017-08-01 15:19:26 -04001498 self.assertEqual(held_node['comment'], "reason text")
David Shrewsburyffab07a2017-07-24 12:45:07 -04001499
1500 # Another failed change should not hold any more nodes
1501 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1502 self.executor_server.failJob('project-test2', B)
1503 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1504 self.waitUntilSettled()
1505 self.assertEqual(B.data['status'], 'NEW')
1506 self.assertEqual(B.reported, 1)
1507 self.assertEqual(self.getJobFromHistory('project-test2').result,
1508 'FAILURE')
1509
1510 held_nodes = 0
1511 for node in self.fake_nodepool.getNodes():
1512 if node['state'] == zuul.model.STATE_HOLD:
1513 held_nodes += 1
1514 self.assertEqual(held_nodes, 1)
1515
James E. Blair7cb84542017-04-19 13:35:05 -07001516 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001517 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001518 # This particular test does a large amount of merges and needs a little
1519 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001520 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001521 "test that dependent changes behind dequeued changes work"
1522 # This complicated test is a reproduction of a real life bug
1523 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001524
Paul Belanger174a8272017-03-14 13:20:10 -04001525 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001526 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1527 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1528 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1529 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1530 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1531 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1532 D.setDependsOn(C, 1)
1533 E.setDependsOn(D, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001534 A.addApproval('Code-Review', 2)
1535 B.addApproval('Code-Review', 2)
1536 C.addApproval('Code-Review', 2)
1537 D.addApproval('Code-Review', 2)
1538 E.addApproval('Code-Review', 2)
1539 F.addApproval('Code-Review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001540
1541 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001542
1543 # Change object re-use in the gerrit trigger is hidden if
1544 # changes are added in quick succession; waiting makes it more
1545 # like real life.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001546 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001547 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001548 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001549 self.waitUntilSettled()
1550
Paul Belanger174a8272017-03-14 13:20:10 -04001551 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001552 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001553 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001554 self.waitUntilSettled()
1555
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001556 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001557 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001558 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001559 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001560 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001561 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001562 self.fake_gerrit.addEvent(F.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001563 self.waitUntilSettled()
1564
Paul Belanger174a8272017-03-14 13:20:10 -04001565 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001566 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001567 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001568 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001569 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001570 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001571 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001572 self.waitUntilSettled()
1573
1574 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001575
1576 # Grab pointers to the jobs we want to release before
1577 # releasing any, because list indexes may change as
1578 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001579 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001580 a.release()
1581 b.release()
1582 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001583 self.waitUntilSettled()
1584
Paul Belanger174a8272017-03-14 13:20:10 -04001585 self.executor_server.hold_jobs_in_build = False
1586 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001587 self.waitUntilSettled()
1588
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001589 self.assertEqual(A.data['status'], 'NEW')
1590 self.assertEqual(B.data['status'], 'MERGED')
1591 self.assertEqual(C.data['status'], 'MERGED')
1592 self.assertEqual(D.data['status'], 'MERGED')
1593 self.assertEqual(E.data['status'], 'MERGED')
1594 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001595
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001596 self.assertEqual(A.reported, 2)
1597 self.assertEqual(B.reported, 2)
1598 self.assertEqual(C.reported, 2)
1599 self.assertEqual(D.reported, 2)
1600 self.assertEqual(E.reported, 2)
1601 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001602
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001603 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1604 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001605
1606 def test_merger_repack(self):
1607 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001608
James E. Blair05fed602012-09-07 12:45:24 -07001609 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001610 A.addApproval('Code-Review', 2)
1611 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001612 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001613 self.assertEqual(self.getJobFromHistory('project-merge').result,
1614 'SUCCESS')
1615 self.assertEqual(self.getJobFromHistory('project-test1').result,
1616 'SUCCESS')
1617 self.assertEqual(self.getJobFromHistory('project-test2').result,
1618 'SUCCESS')
1619 self.assertEqual(A.data['status'], 'MERGED')
1620 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001621 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001622 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001623
James E. Blair2a535672017-04-27 12:03:15 -07001624 path = os.path.join(self.merger_src_root, "review.example.com",
1625 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001626 if os.path.exists(path):
1627 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001628 path = os.path.join(self.executor_src_root, "review.example.com",
1629 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001630 if os.path.exists(path):
1631 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001632
1633 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001634 A.addApproval('Code-Review', 2)
1635 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001636 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001637 self.assertEqual(self.getJobFromHistory('project-merge').result,
1638 'SUCCESS')
1639 self.assertEqual(self.getJobFromHistory('project-test1').result,
1640 'SUCCESS')
1641 self.assertEqual(self.getJobFromHistory('project-test2').result,
1642 'SUCCESS')
1643 self.assertEqual(A.data['status'], 'MERGED')
1644 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001645
James E. Blair4886f282012-11-15 09:27:33 -08001646 def test_merger_repack_large_change(self):
1647 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001648 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001649 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001650 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001651 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001652 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001653 self.executor_server.merger._addProject('review.example.com',
James E. Blair197e8202017-06-09 12:54:28 -07001654 'org/project', url, None)
James E. Blair6df67132017-04-19 13:53:20 -07001655 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001656 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001657 # TODOv3(jeblair): add hostname to upstream root
1658 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001659 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001660 path = os.path.join(self.merger_src_root, 'review.example.com',
1661 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001662 if os.path.exists(path):
1663 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001664 path = os.path.join(self.executor_src_root, 'review.example.com',
1665 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001666 if os.path.exists(path):
1667 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001668
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001669 A.addApproval('Code-Review', 2)
1670 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001671 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001672 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001673 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001674 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001675 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001676 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001677 'SUCCESS')
1678 self.assertEqual(A.data['status'], 'MERGED')
1679 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001680
James E. Blair2fa50962013-01-30 21:50:41 -08001681 def test_new_patchset_dequeues_old(self):
1682 "Test that a new patchset causes the old to be dequeued"
1683 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001684 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001685 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1686 M.setMerged()
1687
1688 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1689 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1690 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1691 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001692 A.addApproval('Code-Review', 2)
1693 B.addApproval('Code-Review', 2)
1694 C.addApproval('Code-Review', 2)
1695 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001696
1697 C.setDependsOn(B, 1)
1698 B.setDependsOn(A, 1)
1699 A.setDependsOn(M, 1)
1700
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001701 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1702 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1703 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1704 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001705 self.waitUntilSettled()
1706
1707 B.addPatchset()
1708 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1709 self.waitUntilSettled()
1710
Paul Belanger174a8272017-03-14 13:20:10 -04001711 self.executor_server.hold_jobs_in_build = False
1712 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001713 self.waitUntilSettled()
1714
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001715 self.assertEqual(A.data['status'], 'MERGED')
1716 self.assertEqual(A.reported, 2)
1717 self.assertEqual(B.data['status'], 'NEW')
1718 self.assertEqual(B.reported, 2)
1719 self.assertEqual(C.data['status'], 'NEW')
1720 self.assertEqual(C.reported, 2)
1721 self.assertEqual(D.data['status'], 'MERGED')
1722 self.assertEqual(D.reported, 2)
1723 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001724
James E. Blairba437362015-02-07 11:41:52 -08001725 def test_new_patchset_check(self):
1726 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001727
Paul Belanger174a8272017-03-14 13:20:10 -04001728 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001729
1730 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001731 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001732 tenant = self.sched.abide.tenants.get('tenant-one')
1733 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001734
1735 # Add two git-dependent changes
1736 B.setDependsOn(A, 1)
1737 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1738 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001739 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1740 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001741
1742 # A live item, and a non-live/live pair
1743 items = check_pipeline.getAllItems()
1744 self.assertEqual(len(items), 3)
1745
1746 self.assertEqual(items[0].change.number, '1')
1747 self.assertEqual(items[0].change.patchset, '1')
1748 self.assertFalse(items[0].live)
1749
1750 self.assertEqual(items[1].change.number, '2')
1751 self.assertEqual(items[1].change.patchset, '1')
1752 self.assertTrue(items[1].live)
1753
1754 self.assertEqual(items[2].change.number, '1')
1755 self.assertEqual(items[2].change.patchset, '1')
1756 self.assertTrue(items[2].live)
1757
1758 # Add a new patchset to A
1759 A.addPatchset()
1760 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1761 self.waitUntilSettled()
1762
1763 # The live copy of A,1 should be gone, but the non-live and B
1764 # should continue, and we should have a new A,2
1765 items = check_pipeline.getAllItems()
1766 self.assertEqual(len(items), 3)
1767
1768 self.assertEqual(items[0].change.number, '1')
1769 self.assertEqual(items[0].change.patchset, '1')
1770 self.assertFalse(items[0].live)
1771
1772 self.assertEqual(items[1].change.number, '2')
1773 self.assertEqual(items[1].change.patchset, '1')
1774 self.assertTrue(items[1].live)
1775
1776 self.assertEqual(items[2].change.number, '1')
1777 self.assertEqual(items[2].change.patchset, '2')
1778 self.assertTrue(items[2].live)
1779
1780 # Add a new patchset to B
1781 B.addPatchset()
1782 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1783 self.waitUntilSettled()
1784
1785 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1786 # but we should have a new B,2 (still based on A,1)
1787 items = check_pipeline.getAllItems()
1788 self.assertEqual(len(items), 3)
1789
1790 self.assertEqual(items[0].change.number, '1')
1791 self.assertEqual(items[0].change.patchset, '2')
1792 self.assertTrue(items[0].live)
1793
1794 self.assertEqual(items[1].change.number, '1')
1795 self.assertEqual(items[1].change.patchset, '1')
1796 self.assertFalse(items[1].live)
1797
1798 self.assertEqual(items[2].change.number, '2')
1799 self.assertEqual(items[2].change.patchset, '2')
1800 self.assertTrue(items[2].live)
1801
1802 self.builds[0].release()
1803 self.waitUntilSettled()
1804 self.builds[0].release()
1805 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001806 self.executor_server.hold_jobs_in_build = False
1807 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001808 self.waitUntilSettled()
1809
1810 self.assertEqual(A.reported, 1)
1811 self.assertEqual(B.reported, 1)
1812 self.assertEqual(self.history[0].result, 'ABORTED')
1813 self.assertEqual(self.history[0].changes, '1,1')
1814 self.assertEqual(self.history[1].result, 'ABORTED')
1815 self.assertEqual(self.history[1].changes, '1,1 2,1')
1816 self.assertEqual(self.history[2].result, 'SUCCESS')
1817 self.assertEqual(self.history[2].changes, '1,2')
1818 self.assertEqual(self.history[3].result, 'SUCCESS')
1819 self.assertEqual(self.history[3].changes, '1,1 2,2')
1820
1821 def test_abandoned_gate(self):
1822 "Test that an abandoned change is dequeued from gate"
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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001827 A.addApproval('Code-Review', 2)
1828 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001829 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001830 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1831 self.assertEqual(self.builds[0].name, 'project-merge')
1832
1833 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1834 self.waitUntilSettled()
1835
Paul Belanger174a8272017-03-14 13:20:10 -04001836 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001837 self.waitUntilSettled()
1838
Arie5b4048c2016-10-05 16:12:06 +03001839 self.assertBuilds([])
1840 self.assertHistory([
1841 dict(name='project-merge', result='ABORTED', changes='1,1')],
1842 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001843 self.assertEqual(A.reported, 1,
1844 "Abandoned gate change should report only start")
1845
1846 def test_abandoned_check(self):
1847 "Test that an abandoned change is dequeued from check"
1848
Paul Belanger174a8272017-03-14 13:20:10 -04001849 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001850
1851 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1852 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001853 tenant = self.sched.abide.tenants.get('tenant-one')
1854 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001855
1856 # Add two git-dependent changes
1857 B.setDependsOn(A, 1)
1858 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1859 self.waitUntilSettled()
1860 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1861 self.waitUntilSettled()
1862 # A live item, and a non-live/live pair
1863 items = check_pipeline.getAllItems()
1864 self.assertEqual(len(items), 3)
1865
1866 self.assertEqual(items[0].change.number, '1')
1867 self.assertFalse(items[0].live)
1868
1869 self.assertEqual(items[1].change.number, '2')
1870 self.assertTrue(items[1].live)
1871
1872 self.assertEqual(items[2].change.number, '1')
1873 self.assertTrue(items[2].live)
1874
1875 # Abandon A
1876 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1877 self.waitUntilSettled()
1878
1879 # The live copy of A should be gone, but the non-live and B
1880 # should continue
1881 items = check_pipeline.getAllItems()
1882 self.assertEqual(len(items), 2)
1883
1884 self.assertEqual(items[0].change.number, '1')
1885 self.assertFalse(items[0].live)
1886
1887 self.assertEqual(items[1].change.number, '2')
1888 self.assertTrue(items[1].live)
1889
Paul Belanger174a8272017-03-14 13:20:10 -04001890 self.executor_server.hold_jobs_in_build = False
1891 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001892 self.waitUntilSettled()
1893
1894 self.assertEqual(len(self.history), 4)
1895 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001896 'Build should have been aborted')
1897 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001898 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001899
Steve Varnau7b78b312015-04-03 14:49:46 -07001900 def test_abandoned_not_timer(self):
1901 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001902 # This test can not use simple_layout because it must start
1903 # with a configuration which does not include a
1904 # timer-triggered job so that we have an opportunity to set
1905 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001906 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001907 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001908 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001909 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001910 # The pipeline triggers every second, so we should have seen
1911 # several by now.
1912 time.sleep(5)
1913 self.waitUntilSettled()
1914 # Stop queuing timer triggered jobs so that the assertions
1915 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001916 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001917 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001918 self.sched.reconfigure(self.config)
James E. Blair0b137b42017-07-27 08:51:25 -07001919 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07001920 # If APScheduler is in mid-event when we remove the job, we
1921 # can end up with one more event firing, so give it an extra
1922 # second to settle.
1923 time.sleep(1)
1924 self.waitUntilSettled()
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001925
James E. Blair21037782017-07-19 11:56:55 -07001926 self.assertEqual(len(self.builds), 1, "One timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001927
1928 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1929 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1930 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07001931 self.assertEqual(len(self.builds), 2, "One change plus one timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001932
1933 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1934 self.waitUntilSettled()
1935
James E. Blair21037782017-07-19 11:56:55 -07001936 self.assertEqual(len(self.builds), 1, "One timer job remains")
Steve Varnau7b78b312015-04-03 14:49:46 -07001937
Paul Belanger174a8272017-03-14 13:20:10 -04001938 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001939 self.waitUntilSettled()
1940
James E. Blair2fa50962013-01-30 21:50:41 -08001941 def test_new_patchset_dequeues_old_on_head(self):
1942 "Test that a new patchset causes the old to be dequeued (at head)"
1943 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001944 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001945 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1946 M.setMerged()
1947 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1948 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1949 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1950 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001951 A.addApproval('Code-Review', 2)
1952 B.addApproval('Code-Review', 2)
1953 C.addApproval('Code-Review', 2)
1954 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001955
1956 C.setDependsOn(B, 1)
1957 B.setDependsOn(A, 1)
1958 A.setDependsOn(M, 1)
1959
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001960 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1961 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1962 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1963 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001964 self.waitUntilSettled()
1965
1966 A.addPatchset()
1967 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1968 self.waitUntilSettled()
1969
Paul Belanger174a8272017-03-14 13:20:10 -04001970 self.executor_server.hold_jobs_in_build = False
1971 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001972 self.waitUntilSettled()
1973
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001974 self.assertEqual(A.data['status'], 'NEW')
1975 self.assertEqual(A.reported, 2)
1976 self.assertEqual(B.data['status'], 'NEW')
1977 self.assertEqual(B.reported, 2)
1978 self.assertEqual(C.data['status'], 'NEW')
1979 self.assertEqual(C.reported, 2)
1980 self.assertEqual(D.data['status'], 'MERGED')
1981 self.assertEqual(D.reported, 2)
1982 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001983
1984 def test_new_patchset_dequeues_old_without_dependents(self):
1985 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001986 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001987 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1988 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1989 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001990 A.addApproval('Code-Review', 2)
1991 B.addApproval('Code-Review', 2)
1992 C.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001993
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001994 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1995 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1996 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001997 self.waitUntilSettled()
1998
1999 B.addPatchset()
2000 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2001 self.waitUntilSettled()
2002
Paul Belanger174a8272017-03-14 13:20:10 -04002003 self.executor_server.hold_jobs_in_build = False
2004 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002005 self.waitUntilSettled()
2006
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002007 self.assertEqual(A.data['status'], 'MERGED')
2008 self.assertEqual(A.reported, 2)
2009 self.assertEqual(B.data['status'], 'NEW')
2010 self.assertEqual(B.reported, 2)
2011 self.assertEqual(C.data['status'], 'MERGED')
2012 self.assertEqual(C.reported, 2)
2013 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002014
2015 def test_new_patchset_dequeues_old_independent_queue(self):
2016 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04002017 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002018 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2019 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2020 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2021 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2022 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2023 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2024 self.waitUntilSettled()
2025
2026 B.addPatchset()
2027 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2028 self.waitUntilSettled()
2029
Paul Belanger174a8272017-03-14 13:20:10 -04002030 self.executor_server.hold_jobs_in_build = False
2031 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002032 self.waitUntilSettled()
2033
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002034 self.assertEqual(A.data['status'], 'NEW')
2035 self.assertEqual(A.reported, 1)
2036 self.assertEqual(B.data['status'], 'NEW')
2037 self.assertEqual(B.reported, 1)
2038 self.assertEqual(C.data['status'], 'NEW')
2039 self.assertEqual(C.reported, 1)
2040 self.assertEqual(len(self.history), 10)
2041 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002042
James E. Blaircc020532017-04-19 13:04:44 -07002043 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07002044 def test_noop_job(self):
2045 "Test that the internal noop job works"
2046 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002047 A.addApproval('Code-Review', 2)
2048 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07002049 self.waitUntilSettled()
2050
2051 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2052 self.assertTrue(self.sched._areAllBuildsComplete())
2053 self.assertEqual(len(self.history), 0)
2054 self.assertEqual(A.data['status'], 'MERGED')
2055 self.assertEqual(A.reported, 2)
2056
James E. Blair06cc3922017-04-19 10:08:10 -07002057 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002058 def test_no_job_project(self):
2059 "Test that reports with no jobs don't get sent"
2060 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
2061 'master', 'A')
2062 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2063 self.waitUntilSettled()
2064
2065 # Change wasn't reported to
2066 self.assertEqual(A.reported, False)
2067
2068 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08002069 tenant = self.sched.abide.tenants.get('tenant-one')
2070 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002071 items = check_pipeline.getAllItems()
2072 self.assertEqual(len(items), 0)
2073
2074 self.assertEqual(len(self.history), 0)
2075
James E. Blair7d0dedc2013-02-21 17:26:09 -08002076 def test_zuul_refs(self):
2077 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04002078 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002079 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2080 M1.setMerged()
2081 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2082 M2.setMerged()
2083
2084 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2085 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2086 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2087 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002088 A.addApproval('Code-Review', 2)
2089 B.addApproval('Code-Review', 2)
2090 C.addApproval('Code-Review', 2)
2091 D.addApproval('Code-Review', 2)
2092 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2093 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2094 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2095 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002096
2097 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002098 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002099 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002100 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002101 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002102 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002103 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002104 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002105 self.waitUntilSettled()
2106
Clint Byrum3343e3e2016-11-15 16:05:03 -08002107 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002108 for x in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07002109 if x.parameters['zuul']['change'] == '3':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002110 a_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002111 elif x.parameters['zuul']['change'] == '4':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002112 b_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002113 elif x.parameters['zuul']['change'] == '5':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002114 c_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002115 elif x.parameters['zuul']['change'] == '6':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002116 d_build = x
2117 if a_build and b_build and c_build and d_build:
2118 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002119
Clint Byrum3343e3e2016-11-15 16:05:03 -08002120 # should have a, not b, and should not be in project2
2121 self.assertTrue(a_build.hasChanges(A))
2122 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002123
Clint Byrum3343e3e2016-11-15 16:05:03 -08002124 # should have a and b, and should not be in project2
2125 self.assertTrue(b_build.hasChanges(A, B))
2126 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002127
Clint Byrum3343e3e2016-11-15 16:05:03 -08002128 # should have a and b in 1, c in 2
2129 self.assertTrue(c_build.hasChanges(A, B, C))
2130 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002131
Clint Byrum3343e3e2016-11-15 16:05:03 -08002132 # should have a and b in 1, c and d in 2
2133 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002134
Paul Belanger174a8272017-03-14 13:20:10 -04002135 self.executor_server.hold_jobs_in_build = False
2136 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002137 self.waitUntilSettled()
2138
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002139 self.assertEqual(A.data['status'], 'MERGED')
2140 self.assertEqual(A.reported, 2)
2141 self.assertEqual(B.data['status'], 'MERGED')
2142 self.assertEqual(B.reported, 2)
2143 self.assertEqual(C.data['status'], 'MERGED')
2144 self.assertEqual(C.reported, 2)
2145 self.assertEqual(D.data['status'], 'MERGED')
2146 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002147
James E. Blair4a28a882013-08-23 15:17:33 -07002148 def test_rerun_on_error(self):
2149 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002150 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002151 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002152 A.addApproval('Code-Review', 2)
2153 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002154 self.waitUntilSettled()
2155
James E. Blair412fba82017-01-26 15:00:50 -08002156 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002157 self.executor_server.hold_jobs_in_build = False
2158 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002159 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002160 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002161 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2162
James E. Blair412e5582013-04-22 15:50:12 -07002163 def test_statsd(self):
2164 "Test each of the statsd methods used in the scheduler"
James E. Blairded241e2017-10-10 13:22:40 -07002165 statsd = self.sched.statsd
James E. Blair412e5582013-04-22 15:50:12 -07002166 statsd.incr('test-incr')
2167 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002168 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002169 self.assertReportedStat('test-incr', '1|c')
2170 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002171 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002172
James E. Blairdad52252014-02-07 16:59:17 -08002173 def test_stuck_job_cleanup(self):
2174 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002175
2176 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002177 self.gearman_server.hold_jobs_in_queue = True
2178 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002179 A.addApproval('Code-Review', 2)
2180 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002181 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002182 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002183 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2184
James E. Blair31cb3e82017-04-20 13:08:56 -07002185 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002186 self.sched.reconfigure(self.config)
2187 self.waitUntilSettled()
2188
James E. Blair18c64442014-03-18 10:14:45 -07002189 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002190 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002191 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002192 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2193 self.assertTrue(self.sched._areAllBuildsComplete())
2194
2195 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002196 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002197 self.assertEqual(self.history[0].result, 'SUCCESS')
2198
James E. Blair879dafb2015-07-17 14:04:49 -07002199 def test_file_head(self):
2200 # This is a regression test for an observed bug. A change
2201 # with a file named "HEAD" in the root directory of the repo
2202 # was processed by a merger. It then was unable to reset the
2203 # repo because of:
2204 # GitCommandError: 'git reset --hard HEAD' returned
2205 # with exit code 128
2206 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2207 # and filename
2208 # Use '--' to separate filenames from revisions'
2209
2210 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002211 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002212 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2213
2214 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2215 self.waitUntilSettled()
2216
2217 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2218 self.waitUntilSettled()
2219
2220 self.assertIn('Build succeeded', A.messages[0])
2221 self.assertIn('Build succeeded', B.messages[0])
2222
James E. Blair70c71582013-03-06 08:50:50 -08002223 def test_file_jobs(self):
2224 "Test that file jobs run only when appropriate"
2225 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002226 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002227 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002228 A.addApproval('Code-Review', 2)
2229 B.addApproval('Code-Review', 2)
2230 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2231 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002232 self.waitUntilSettled()
2233
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002234 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002235 if x.name == 'project-testfile']
2236
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002237 self.assertEqual(len(testfile_jobs), 1)
2238 self.assertEqual(testfile_jobs[0].changes, '1,2')
2239 self.assertEqual(A.data['status'], 'MERGED')
2240 self.assertEqual(A.reported, 2)
2241 self.assertEqual(B.data['status'], 'MERGED')
2242 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002243
Clint Byrum3ee39f32016-11-17 23:45:07 -08002244 def _test_irrelevant_files_jobs(self, should_skip):
2245 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002246 if should_skip:
2247 files = {'ignoreme': 'ignored\n'}
2248 else:
2249 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002250
2251 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002252 'master',
2253 'test irrelevant-files',
2254 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002255 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2256 self.waitUntilSettled()
2257
2258 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002259 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002260
2261 if should_skip:
2262 self.assertEqual([], tested_change_ids)
2263 else:
2264 self.assertIn(change.data['number'], tested_change_ids)
2265
James E. Blaircc324192017-04-19 14:36:08 -07002266 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002267 def test_irrelevant_files_match_skips_job(self):
2268 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002269
James E. Blaircc324192017-04-19 14:36:08 -07002270 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002271 def test_irrelevant_files_no_match_runs_job(self):
2272 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002273
James E. Blaircc324192017-04-19 14:36:08 -07002274 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002275 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002276 files = {'ignoreme': 'ignored\n'}
2277
2278 change = self.fake_gerrit.addFakeChange('org/project',
2279 'master',
2280 'test irrelevant-files',
2281 files=files)
2282 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2283 self.waitUntilSettled()
2284
2285 run_jobs = set([build.name for build in self.history])
2286
2287 self.assertEqual(set(['project-test-nomatch-starts-empty',
2288 'project-test-nomatch-starts-full']), run_jobs)
2289
James E. Blairaf8b2082017-10-03 15:38:27 -07002290 @simple_layout('layouts/job-vars.yaml')
2291 def test_inherited_job_variables(self):
2292 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2293 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2294 self.waitUntilSettled()
2295 self.assertHistory([
2296 dict(name='parentjob', result='SUCCESS'),
2297 dict(name='child1', result='SUCCESS'),
2298 dict(name='child2', result='SUCCESS'),
2299 dict(name='child3', result='SUCCESS'),
2300 ], ordered=False)
2301 j = self.getJobFromHistory('parentjob')
2302 rp = set([p['name'] for p in j.parameters['projects']])
2303 self.assertEqual(j.parameters['vars']['override'], 0)
2304 self.assertEqual(j.parameters['vars']['child1override'], 0)
2305 self.assertEqual(j.parameters['vars']['parent'], 0)
2306 self.assertFalse('child1' in j.parameters['vars'])
2307 self.assertFalse('child2' in j.parameters['vars'])
2308 self.assertFalse('child3' in j.parameters['vars'])
2309 self.assertEqual(rp, set(['org/project', 'org/project0',
2310 'org/project0']))
2311 j = self.getJobFromHistory('child1')
2312 rp = set([p['name'] for p in j.parameters['projects']])
2313 self.assertEqual(j.parameters['vars']['override'], 1)
2314 self.assertEqual(j.parameters['vars']['child1override'], 1)
2315 self.assertEqual(j.parameters['vars']['parent'], 0)
2316 self.assertEqual(j.parameters['vars']['child1'], 1)
2317 self.assertFalse('child2' in j.parameters['vars'])
2318 self.assertFalse('child3' in j.parameters['vars'])
2319 self.assertEqual(rp, set(['org/project', 'org/project0',
2320 'org/project1']))
2321 j = self.getJobFromHistory('child2')
2322 rp = set([p['name'] for p in j.parameters['projects']])
2323 self.assertEqual(j.parameters['vars']['override'], 2)
2324 self.assertEqual(j.parameters['vars']['child1override'], 0)
2325 self.assertEqual(j.parameters['vars']['parent'], 0)
2326 self.assertFalse('child1' in j.parameters['vars'])
2327 self.assertEqual(j.parameters['vars']['child2'], 2)
2328 self.assertFalse('child3' in j.parameters['vars'])
2329 self.assertEqual(rp, set(['org/project', 'org/project0',
2330 'org/project2']))
2331 j = self.getJobFromHistory('child3')
2332 rp = set([p['name'] for p in j.parameters['projects']])
2333 self.assertEqual(j.parameters['vars']['override'], 3)
2334 self.assertEqual(j.parameters['vars']['child1override'], 0)
2335 self.assertEqual(j.parameters['vars']['parent'], 0)
2336 self.assertFalse('child1' in j.parameters['vars'])
2337 self.assertFalse('child2' in j.parameters['vars'])
2338 self.assertEqual(j.parameters['vars']['child3'], 3)
2339 self.assertEqual(rp, set(['org/project', 'org/project0',
2340 'org/project3']))
2341
James E. Blairc8a1e052014-02-25 09:29:26 -08002342 def test_queue_names(self):
2343 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002344 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002345 (trusted, project1) = tenant.getProject('org/project1')
2346 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002347 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2348 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2349 self.assertEqual(q1.name, 'integrated')
2350 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002351
James E. Blair64ed6f22013-07-10 14:07:23 -07002352 def test_queue_precedence(self):
2353 "Test that queue precedence works"
2354
2355 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002356 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002357 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2358 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002359 A.addApproval('Code-Review', 2)
2360 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002361
2362 self.waitUntilSettled()
2363 self.gearman_server.hold_jobs_in_queue = False
2364 self.gearman_server.release()
2365 self.waitUntilSettled()
2366
James E. Blair8de58bd2013-07-18 16:23:33 -07002367 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002368 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002369 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002370 self.waitUntilSettled()
2371
James E. Blair64ed6f22013-07-10 14:07:23 -07002372 self.log.debug(self.history)
2373 self.assertEqual(self.history[0].pipeline, 'gate')
2374 self.assertEqual(self.history[1].pipeline, 'check')
2375 self.assertEqual(self.history[2].pipeline, 'gate')
2376 self.assertEqual(self.history[3].pipeline, 'gate')
2377 self.assertEqual(self.history[4].pipeline, 'check')
2378 self.assertEqual(self.history[5].pipeline, 'check')
2379
Clark Boylana5edbe42014-06-03 16:39:10 -07002380 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002381 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002382 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002383 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002384 A.addApproval('Code-Review', 2)
2385 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002386 self.waitUntilSettled()
2387
Paul Belanger174a8272017-03-14 13:20:10 -04002388 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002389 self.waitUntilSettled()
2390
James E. Blair1843a552013-07-03 14:19:52 -07002391 port = self.webapp.server.socket.getsockname()[1]
2392
Adam Gandelman77a12c72017-02-03 17:43:43 -08002393 req = urllib.request.Request(
2394 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002395 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002396 headers = f.info()
2397 self.assertIn('Content-Length', headers)
2398 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002399 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2400 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002401 self.assertIn('Access-Control-Allow-Origin', headers)
2402 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002403 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002404 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002405 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002406
Paul Belanger174a8272017-03-14 13:20:10 -04002407 self.executor_server.hold_jobs_in_build = False
2408 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002409 self.waitUntilSettled()
2410
2411 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002412 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002413 for p in data['pipelines']:
2414 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002415 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002416 self.assertEqual(q['window'], 20)
2417 else:
2418 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002419 for head in q['heads']:
2420 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002421 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002422 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002423 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002424 status_jobs.append(job)
2425 self.assertEqual('project-merge', status_jobs[0]['name'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002426 # TODO(mordred) pull uuids from self.builds
Monty Taylor51139a02016-05-24 11:28:10 -05002427 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002428 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2429 uuid=status_jobs[0]['uuid']),
2430 status_jobs[0]['url'])
2431 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002432 'finger://{hostname}/{uuid}'.format(
2433 hostname=self.executor_server.hostname,
2434 uuid=status_jobs[0]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002435 status_jobs[0]['finger_url'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002436 # TOOD(mordred) configure a success-url on the base job
Monty Taylor51139a02016-05-24 11:28:10 -05002437 self.assertEqual(
2438 'finger://{hostname}/{uuid}'.format(
2439 hostname=self.executor_server.hostname,
2440 uuid=status_jobs[0]['uuid']),
2441 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002442 self.assertEqual('project-test1', status_jobs[1]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002443 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002444 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2445 uuid=status_jobs[1]['uuid']),
2446 status_jobs[1]['url'])
2447 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002448 'finger://{hostname}/{uuid}'.format(
2449 hostname=self.executor_server.hostname,
2450 uuid=status_jobs[1]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002451 status_jobs[1]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002452 self.assertEqual(
2453 'finger://{hostname}/{uuid}'.format(
2454 hostname=self.executor_server.hostname,
2455 uuid=status_jobs[1]['uuid']),
2456 status_jobs[1]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002457
2458 self.assertEqual('project-test2', status_jobs[2]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002459 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002460 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2461 uuid=status_jobs[2]['uuid']),
2462 status_jobs[2]['url'])
2463 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002464 'finger://{hostname}/{uuid}'.format(
2465 hostname=self.executor_server.hostname,
2466 uuid=status_jobs[2]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002467 status_jobs[2]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002468 self.assertEqual(
2469 'finger://{hostname}/{uuid}'.format(
2470 hostname=self.executor_server.hostname,
2471 uuid=status_jobs[2]['uuid']),
2472 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002473
Tobias Henkel65639f82017-07-10 10:25:42 +02002474 # check job dependencies
2475 self.assertIsNotNone(status_jobs[0]['dependencies'])
2476 self.assertIsNotNone(status_jobs[1]['dependencies'])
2477 self.assertIsNotNone(status_jobs[2]['dependencies'])
2478 self.assertEqual(len(status_jobs[0]['dependencies']), 0)
2479 self.assertEqual(len(status_jobs[1]['dependencies']), 1)
2480 self.assertEqual(len(status_jobs[2]['dependencies']), 1)
2481 self.assertIn('project-merge', status_jobs[1]['dependencies'])
2482 self.assertIn('project-merge', status_jobs[2]['dependencies'])
2483
James E. Blaircdccd972013-07-01 12:10:22 -07002484 def test_live_reconfiguration(self):
2485 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002486 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002487 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002488 A.addApproval('Code-Review', 2)
2489 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002490 self.waitUntilSettled()
2491
2492 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002493 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002494
Paul Belanger174a8272017-03-14 13:20:10 -04002495 self.executor_server.hold_jobs_in_build = False
2496 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002497 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002498 self.assertEqual(self.getJobFromHistory('project-merge').result,
2499 'SUCCESS')
2500 self.assertEqual(self.getJobFromHistory('project-test1').result,
2501 'SUCCESS')
2502 self.assertEqual(self.getJobFromHistory('project-test2').result,
2503 'SUCCESS')
2504 self.assertEqual(A.data['status'], 'MERGED')
2505 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002506
James E. Blair027ba992017-09-20 13:48:32 -07002507 def test_live_reconfiguration_abort(self):
2508 # Raise an exception during reconfiguration and verify we
2509 # still function.
2510 self.executor_server.hold_jobs_in_build = True
2511 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2512 A.addApproval('Code-Review', 2)
2513 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2514 self.waitUntilSettled()
2515
2516 tenant = self.sched.abide.tenants.get('tenant-one')
2517 pipeline = tenant.layout.pipelines['gate']
2518 change = pipeline.getAllItems()[0].change
2519 # Set this to an invalid value to cause an exception during
2520 # reconfiguration.
2521 change.branch = None
2522
2523 self.sched.reconfigure(self.config)
2524 self.waitUntilSettled()
2525
2526 self.executor_server.hold_jobs_in_build = False
2527 self.executor_server.release()
2528
2529 self.waitUntilSettled()
2530 self.assertEqual(self.getJobFromHistory('project-merge').result,
2531 'ABORTED')
2532 self.assertEqual(A.data['status'], 'NEW')
2533 # The final report fails because of the invalid value set above.
2534 self.assertEqual(A.reported, 1)
2535
James E. Blair6bc782d2015-07-17 16:20:21 -07002536 def test_live_reconfiguration_merge_conflict(self):
2537 # A real-world bug: a change in a gate queue has a merge
2538 # conflict and a job is added to its project while it's
2539 # sitting in the queue. The job gets added to the change and
2540 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002541 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002542
2543 # This change is fine. It's here to stop the queue long
2544 # enough for the next change to be subject to the
2545 # reconfiguration, as well as to provide a conflict for the
2546 # next change. This change will succeed and merge.
2547 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002548 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002549 A.addApproval('Code-Review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002550
2551 # This change will be in merge conflict. During the
2552 # reconfiguration, we will add a job. We want to make sure
2553 # that doesn't cause it to get stuck.
2554 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002555 B.addPatchset({'conflict': 'B'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002556 B.addApproval('Code-Review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002557
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002558 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2559 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002560
2561 self.waitUntilSettled()
2562
2563 # No jobs have run yet
2564 self.assertEqual(A.data['status'], 'NEW')
2565 self.assertEqual(A.reported, 1)
2566 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002567 self.assertEqual(len(self.history), 0)
2568
2569 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002570 self.commitConfigUpdate('common-config',
2571 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002572 self.sched.reconfigure(self.config)
2573 self.waitUntilSettled()
2574
Paul Belanger174a8272017-03-14 13:20:10 -04002575 self.executor_server.hold_jobs_in_build = False
2576 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002577 self.waitUntilSettled()
2578
2579 self.assertEqual(A.data['status'], 'MERGED')
2580 self.assertEqual(A.reported, 2)
2581 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002582 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002583 self.assertEqual(self.getJobFromHistory('project-merge').result,
2584 'SUCCESS')
2585 self.assertEqual(self.getJobFromHistory('project-test1').result,
2586 'SUCCESS')
2587 self.assertEqual(self.getJobFromHistory('project-test2').result,
2588 'SUCCESS')
2589 self.assertEqual(self.getJobFromHistory('project-test3').result,
2590 'SUCCESS')
2591 self.assertEqual(len(self.history), 4)
2592
James E. Blair400e8fd2015-07-30 17:44:45 -07002593 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002594 # An extrapolation of test_live_reconfiguration_merge_conflict
2595 # that tests a job added to a job tree with a failed root does
2596 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002597 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002598
2599 # This change is fine. It's here to stop the queue long
2600 # enough for the next change to be subject to the
2601 # reconfiguration. This change will succeed and merge.
2602 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002603 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002604 A.addApproval('Code-Review', 2)
2605 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002606 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002607 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002608 self.waitUntilSettled()
2609
2610 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002611 self.executor_server.failJob('project-merge', B)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002612 B.addApproval('Code-Review', 2)
2613 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002614 self.waitUntilSettled()
2615
Paul Belanger174a8272017-03-14 13:20:10 -04002616 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002617 self.waitUntilSettled()
2618
2619 # Both -merge jobs have run, but no others.
2620 self.assertEqual(A.data['status'], 'NEW')
2621 self.assertEqual(A.reported, 1)
2622 self.assertEqual(B.data['status'], 'NEW')
2623 self.assertEqual(B.reported, 1)
2624 self.assertEqual(self.history[0].result, 'SUCCESS')
2625 self.assertEqual(self.history[0].name, 'project-merge')
2626 self.assertEqual(self.history[1].result, 'FAILURE')
2627 self.assertEqual(self.history[1].name, 'project-merge')
2628 self.assertEqual(len(self.history), 2)
2629
2630 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002631 self.commitConfigUpdate('common-config',
2632 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002633 self.sched.reconfigure(self.config)
2634 self.waitUntilSettled()
2635
Paul Belanger174a8272017-03-14 13:20:10 -04002636 self.executor_server.hold_jobs_in_build = False
2637 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002638 self.waitUntilSettled()
2639
2640 self.assertEqual(A.data['status'], 'MERGED')
2641 self.assertEqual(A.reported, 2)
2642 self.assertEqual(B.data['status'], 'NEW')
2643 self.assertEqual(B.reported, 2)
2644 self.assertEqual(self.history[0].result, 'SUCCESS')
2645 self.assertEqual(self.history[0].name, 'project-merge')
2646 self.assertEqual(self.history[1].result, 'FAILURE')
2647 self.assertEqual(self.history[1].name, 'project-merge')
2648 self.assertEqual(self.history[2].result, 'SUCCESS')
2649 self.assertEqual(self.history[3].result, 'SUCCESS')
2650 self.assertEqual(self.history[4].result, 'SUCCESS')
2651 self.assertEqual(len(self.history), 5)
2652
James E. Blair400e8fd2015-07-30 17:44:45 -07002653 def test_live_reconfiguration_failed_job(self):
2654 # Test that a change with a removed failing job does not
2655 # disrupt reconfiguration. If a change has a failed job and
2656 # that job is removed during a reconfiguration, we observed a
2657 # bug where the code to re-set build statuses would run on
2658 # that build and raise an exception because the job no longer
2659 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002660 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002661
2662 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2663
2664 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002665 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002666
2667 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2668 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002669 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002670 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002671 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002672 self.waitUntilSettled()
2673
2674 self.assertEqual(A.data['status'], 'NEW')
2675 self.assertEqual(A.reported, 0)
2676
2677 self.assertEqual(self.getJobFromHistory('project-merge').result,
2678 'SUCCESS')
2679 self.assertEqual(self.getJobFromHistory('project-test1').result,
2680 'FAILURE')
2681 self.assertEqual(len(self.history), 2)
2682
2683 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002684 self.commitConfigUpdate('common-config',
2685 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002686 self.sched.reconfigure(self.config)
2687 self.waitUntilSettled()
2688
Paul Belanger174a8272017-03-14 13:20:10 -04002689 self.executor_server.hold_jobs_in_build = False
2690 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002691 self.waitUntilSettled()
2692
2693 self.assertEqual(self.getJobFromHistory('project-test2').result,
2694 'SUCCESS')
2695 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2696 'SUCCESS')
2697 self.assertEqual(len(self.history), 4)
2698
2699 self.assertEqual(A.data['status'], 'NEW')
2700 self.assertEqual(A.reported, 1)
2701 self.assertIn('Build succeeded', A.messages[0])
2702 # Ensure the removed job was not included in the report.
2703 self.assertNotIn('project-test1', A.messages[0])
2704
James E. Blairfe707d12015-08-05 15:18:15 -07002705 def test_live_reconfiguration_shared_queue(self):
2706 # Test that a change with a failing job which was removed from
2707 # this project but otherwise still exists in the system does
2708 # not disrupt reconfiguration.
2709
Paul Belanger174a8272017-03-14 13:20:10 -04002710 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002711
2712 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2713
Paul Belanger174a8272017-03-14 13:20:10 -04002714 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002715
2716 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2717 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002718 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002719 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002720 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002721 self.waitUntilSettled()
2722
2723 self.assertEqual(A.data['status'], 'NEW')
2724 self.assertEqual(A.reported, 0)
2725
Clint Byrum69663d42017-05-01 14:51:04 -07002726 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002727 'SUCCESS')
2728 self.assertEqual(self.getJobFromHistory(
2729 'project1-project2-integration').result, 'FAILURE')
2730 self.assertEqual(len(self.history), 2)
2731
2732 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002733 self.commitConfigUpdate(
2734 'common-config',
2735 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002736 self.sched.reconfigure(self.config)
2737 self.waitUntilSettled()
2738
Paul Belanger174a8272017-03-14 13:20:10 -04002739 self.executor_server.hold_jobs_in_build = False
2740 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002741 self.waitUntilSettled()
2742
Clint Byrum69663d42017-05-01 14:51:04 -07002743 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002744 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002745 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002746 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002747 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002748 'SUCCESS')
2749 self.assertEqual(self.getJobFromHistory(
2750 'project1-project2-integration').result, 'FAILURE')
2751 self.assertEqual(len(self.history), 4)
2752
2753 self.assertEqual(A.data['status'], 'NEW')
2754 self.assertEqual(A.reported, 1)
2755 self.assertIn('Build succeeded', A.messages[0])
2756 # Ensure the removed job was not included in the report.
2757 self.assertNotIn('project1-project2-integration', A.messages[0])
2758
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002759 def test_double_live_reconfiguration_shared_queue(self):
2760 # This was a real-world regression. A change is added to
2761 # gate; a reconfigure happens, a second change which depends
2762 # on the first is added, and a second reconfiguration happens.
2763 # Ensure that both changes merge.
2764
2765 # A failure may indicate incorrect caching or cleaning up of
2766 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002767 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002768
2769 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2770 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2771 B.setDependsOn(A, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002772 A.addApproval('Code-Review', 2)
2773 B.addApproval('Code-Review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002774
2775 # Add the parent change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002776 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002777 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002778 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002779 self.waitUntilSettled()
2780
2781 # Reconfigure (with only one change in the pipeline).
2782 self.sched.reconfigure(self.config)
2783 self.waitUntilSettled()
2784
2785 # Add the child change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002786 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002787 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002788 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002789 self.waitUntilSettled()
2790
2791 # Reconfigure (with both in the pipeline).
2792 self.sched.reconfigure(self.config)
2793 self.waitUntilSettled()
2794
Paul Belanger174a8272017-03-14 13:20:10 -04002795 self.executor_server.hold_jobs_in_build = False
2796 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002797 self.waitUntilSettled()
2798
2799 self.assertEqual(len(self.history), 8)
2800
2801 self.assertEqual(A.data['status'], 'MERGED')
2802 self.assertEqual(A.reported, 2)
2803 self.assertEqual(B.data['status'], 'MERGED')
2804 self.assertEqual(B.reported, 2)
2805
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002806 def test_live_reconfiguration_del_project(self):
2807 # Test project deletion from layout
2808 # while changes are enqueued
2809
Paul Belanger174a8272017-03-14 13:20:10 -04002810 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002811 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2812 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2813 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2814
2815 # A Depends-On: B
2816 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2817 A.subject, B.data['id'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002818 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002819
2820 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2821 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2822 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002823 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002824 self.waitUntilSettled()
2825 self.assertEqual(len(self.builds), 5)
2826
2827 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002828 self.commitConfigUpdate(
2829 'common-config',
2830 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002831 self.sched.reconfigure(self.config)
2832 self.waitUntilSettled()
2833
2834 # Builds for C aborted, builds for A succeed,
2835 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002836 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002837 self.assertEqual(job_c.changes, '3,1')
2838 self.assertEqual(job_c.result, 'ABORTED')
2839
Paul Belanger174a8272017-03-14 13:20:10 -04002840 self.executor_server.hold_jobs_in_build = False
2841 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002842 self.waitUntilSettled()
2843
Clint Byrum0149ada2017-04-04 12:22:01 -07002844 self.assertEqual(
2845 self.getJobFromHistory('project-test1', 'org/project').changes,
2846 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002847
2848 self.assertEqual(A.data['status'], 'NEW')
2849 self.assertEqual(B.data['status'], 'NEW')
2850 self.assertEqual(C.data['status'], 'NEW')
2851 self.assertEqual(A.reported, 1)
2852 self.assertEqual(B.reported, 0)
2853 self.assertEqual(C.reported, 0)
2854
Clint Byrum0149ada2017-04-04 12:22:01 -07002855 tenant = self.sched.abide.tenants.get('tenant-one')
2856 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002857 self.assertIn('Build succeeded', A.messages[0])
2858
James E. Blair287c06d2013-07-24 10:39:30 -07002859 def test_delayed_repo_init(self):
James E. Blair287c06d2013-07-24 10:39:30 -07002860 self.init_repo("org/new-project")
Clint Byrum627ba362017-08-14 13:20:40 -07002861 files = {'README': ''}
2862 self.addCommitToRepo("org/new-project", 'Initial commit',
2863 files=files, tag='init')
2864 self.newTenantConfig('tenants/delayed-repo-init.yaml')
2865 self.commitConfigUpdate(
2866 'common-config',
2867 'layouts/delayed-repo-init.yaml')
2868 self.sched.reconfigure(self.config)
2869 self.waitUntilSettled()
2870
James E. Blair287c06d2013-07-24 10:39:30 -07002871 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2872
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002873 A.addApproval('Code-Review', 2)
2874 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002875 self.waitUntilSettled()
2876 self.assertEqual(self.getJobFromHistory('project-merge').result,
2877 'SUCCESS')
2878 self.assertEqual(self.getJobFromHistory('project-test1').result,
2879 'SUCCESS')
2880 self.assertEqual(self.getJobFromHistory('project-test2').result,
2881 'SUCCESS')
2882 self.assertEqual(A.data['status'], 'MERGED')
2883 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002884
James E. Blair9ea70072017-04-19 16:05:30 -07002885 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002886 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002887 self.init_repo("org/delete-project")
2888 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2889
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002890 A.addApproval('Code-Review', 2)
2891 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002892 self.waitUntilSettled()
2893 self.assertEqual(self.getJobFromHistory('project-merge').result,
2894 'SUCCESS')
2895 self.assertEqual(self.getJobFromHistory('project-test1').result,
2896 'SUCCESS')
2897 self.assertEqual(self.getJobFromHistory('project-test2').result,
2898 'SUCCESS')
2899 self.assertEqual(A.data['status'], 'MERGED')
2900 self.assertEqual(A.reported, 2)
2901
2902 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002903 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002904 if os.path.exists(os.path.join(self.merger_src_root, p)):
2905 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002906 if os.path.exists(os.path.join(self.executor_src_root, p)):
2907 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002908
2909 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2910
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002911 B.addApproval('Code-Review', 2)
2912 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002913 self.waitUntilSettled()
2914 self.assertEqual(self.getJobFromHistory('project-merge').result,
2915 'SUCCESS')
2916 self.assertEqual(self.getJobFromHistory('project-test1').result,
2917 'SUCCESS')
2918 self.assertEqual(self.getJobFromHistory('project-test2').result,
2919 'SUCCESS')
2920 self.assertEqual(B.data['status'], 'MERGED')
2921 self.assertEqual(B.reported, 2)
2922
James E. Blair892cca62017-08-09 11:36:58 -07002923 @simple_layout('layouts/untrusted-secrets.yaml')
2924 def test_untrusted_secrets(self):
2925 "Test untrusted secrets"
2926 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2927 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2928 self.waitUntilSettled()
2929
2930 self.assertHistory([])
2931 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
James E. Blair8eb564a2017-08-10 09:21:41 -07002932 self.assertIn('does not allow post-review job',
James E. Blair892cca62017-08-09 11:36:58 -07002933 A.messages[0])
2934
James E. Blaire68d43f2017-04-19 14:03:30 -07002935 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002936 def test_tags(self):
2937 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002938 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2939 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2940 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2941 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2942 self.waitUntilSettled()
2943
James E. Blaire68d43f2017-04-19 14:03:30 -07002944 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002945
2946 results = {self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002947 project='org/project1').uuid: ['extratag', 'merge'],
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002948 self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002949 project='org/project2').uuid: ['merge']}
James E. Blair456f2fb2016-02-09 09:29:33 -08002950
2951 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002952 self.assertEqual(results.get(build.uuid, ''),
James E. Blair9d46f092017-07-20 16:06:20 -07002953 build.parameters['zuul'].get('jobtags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002954
James E. Blair63bb0ef2013-07-29 17:14:51 -07002955 def test_timer(self):
2956 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002957 # This test can not use simple_layout because it must start
2958 # with a configuration which does not include a
2959 # timer-triggered job so that we have an opportunity to set
2960 # the hold flag before the first job.
James E. Blair21037782017-07-19 11:56:55 -07002961 self.create_branch('org/project', 'stable')
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.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002964 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002965
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. Blair63bb0ef2013-07-29 17:14:51 -07002969 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002970
2971 self.assertEqual(len(self.builds), 2)
2972
James E. Blair63bb0ef2013-07-29 17:14:51 -07002973 port = self.webapp.server.socket.getsockname()[1]
2974
Clint Byrum5e729e92017-02-08 06:34:57 -08002975 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002976 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002977 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002978 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002979
Paul Belanger174a8272017-03-14 13:20:10 -04002980 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002981 # Stop queuing timer triggered jobs so that the assertions
2982 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002983 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002984 self.sched.reconfigure(self.config)
James E. Blair78ae4782017-08-02 14:19:10 -07002985 self.waitUntilSettled()
2986 # If APScheduler is in mid-event when we remove the job, we
2987 # can end up with one more event firing, so give it an extra
2988 # second to settle.
2989 time.sleep(1)
2990 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002991 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002992 self.waitUntilSettled()
2993
James E. Blair21037782017-07-19 11:56:55 -07002994 self.assertHistory([
2995 dict(name='project-bitrot', result='SUCCESS',
2996 ref='refs/heads/master'),
2997 dict(name='project-bitrot', result='SUCCESS',
2998 ref='refs/heads/stable'),
2999 ], ordered=False)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003000
3001 data = json.loads(data)
3002 status_jobs = set()
3003 for p in data['pipelines']:
3004 for q in p['change_queues']:
3005 for head in q['heads']:
3006 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07003007 for job in change['jobs']:
3008 status_jobs.add(job['name'])
James E. Blair21037782017-07-19 11:56:55 -07003009 self.assertIn('project-bitrot', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003010
James E. Blair4f6033c2014-03-27 15:49:09 -07003011 def test_idle(self):
3012 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003013 # This test can not use simple_layout because it must start
3014 # with a configuration which does not include a
3015 # timer-triggered job so that we have an opportunity to set
3016 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04003017 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07003018
Clark Boylan3ee090a2014-04-03 20:55:09 -07003019 for x in range(1, 3):
3020 # Test that timer triggers periodic jobs even across
3021 # layout config reloads.
3022 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003023 self.commitConfigUpdate('common-config',
3024 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003025 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003026 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07003027
Clark Boylan3ee090a2014-04-03 20:55:09 -07003028 # The pipeline triggers every second, so we should have seen
3029 # several by now.
3030 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07003031
3032 # Stop queuing timer triggered jobs so that the assertions
3033 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003034 self.commitConfigUpdate('common-config',
3035 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003036 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003037 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003038 # If APScheduler is in mid-event when we remove the job,
3039 # we can end up with one more event firing, so give it an
3040 # extra second to settle.
3041 time.sleep(1)
3042 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07003043 self.assertEqual(len(self.builds), 1,
Clint Byrum58264dc2017-02-07 21:21:22 -08003044 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04003045 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003046 self.waitUntilSettled()
3047 self.assertEqual(len(self.builds), 0)
James E. Blair21037782017-07-19 11:56:55 -07003048 self.assertEqual(len(self.history), x)
James E. Blair4f6033c2014-03-27 15:49:09 -07003049
James E. Blaircc324192017-04-19 14:36:08 -07003050 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003051 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003052 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3053 self.waitUntilSettled()
3054
3055 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3056 self.waitUntilSettled()
3057
James E. Blairff80a2f2013-12-27 13:24:06 -08003058 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003059
3060 # A.messages only holds what FakeGerrit places in it. Thus we
3061 # work on the knowledge of what the first message should be as
3062 # it is only configured to go to SMTP.
3063
3064 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003065 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003066 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003067 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003068 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003069 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003070
3071 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003072 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003073 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003074 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003075 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003076 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003077
James E. Blaire5910202013-12-27 09:50:31 -08003078 def test_timer_smtp(self):
3079 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003080 # This test can not use simple_layout because it must start
3081 # with a configuration which does not include a
3082 # timer-triggered job so that we have an opportunity to set
3083 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04003084 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003085 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08003086 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08003087
Clark Boylan3ee090a2014-04-03 20:55:09 -07003088 # The pipeline triggers every second, so we should have seen
3089 # several by now.
3090 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003091 self.waitUntilSettled()
3092
Clark Boylan3ee090a2014-04-03 20:55:09 -07003093 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003094 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003095 self.waitUntilSettled()
3096 self.assertEqual(len(self.history), 2)
3097
James E. Blaire5910202013-12-27 09:50:31 -08003098 self.assertEqual(self.getJobFromHistory(
3099 'project-bitrot-stable-old').result, 'SUCCESS')
3100 self.assertEqual(self.getJobFromHistory(
3101 'project-bitrot-stable-older').result, 'SUCCESS')
3102
James E. Blairff80a2f2013-12-27 13:24:06 -08003103 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003104
3105 # A.messages only holds what FakeGerrit places in it. Thus we
3106 # work on the knowledge of what the first message should be as
3107 # it is only configured to go to SMTP.
3108
3109 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003110 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003111 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003112 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003113 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003114 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003115
Clark Boylan3ee090a2014-04-03 20:55:09 -07003116 # Stop queuing timer triggered jobs and let any that may have
3117 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003118 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003119 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07003120 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003121 # If APScheduler is in mid-event when we remove the job, we
3122 # can end up with one more event firing, so give it an extra
3123 # second to settle.
3124 time.sleep(1)
3125 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003126 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003127 self.waitUntilSettled()
3128
Joshua Hesketh25695cb2017-03-06 12:50:04 +11003129 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003130 def test_timer_sshkey(self):
3131 "Test that a periodic job can setup SSH key authentication"
3132 self.worker.hold_jobs_in_build = True
3133 self.config.set('zuul', 'layout_config',
3134 'tests/fixtures/layout-timer.yaml')
3135 self.sched.reconfigure(self.config)
3136 self.registerJobs()
3137
3138 # The pipeline triggers every second, so we should have seen
3139 # several by now.
3140 time.sleep(5)
3141 self.waitUntilSettled()
3142
3143 self.assertEqual(len(self.builds), 2)
3144
3145 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3146 self.assertTrue(os.path.isfile(ssh_wrapper))
3147 with open(ssh_wrapper) as f:
3148 ssh_wrapper_content = f.read()
3149 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3150 # In the unit tests Merger runs in the same process,
3151 # so we see its' environment variables
3152 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3153
3154 self.worker.release('.*')
3155 self.waitUntilSettled()
3156 self.assertEqual(len(self.history), 2)
3157
3158 self.assertEqual(self.getJobFromHistory(
3159 'project-bitrot-stable-old').result, 'SUCCESS')
3160 self.assertEqual(self.getJobFromHistory(
3161 'project-bitrot-stable-older').result, 'SUCCESS')
3162
3163 # Stop queuing timer triggered jobs and let any that may have
3164 # queued through so that end of test assertions pass.
3165 self.config.set('zuul', 'layout_config',
3166 'tests/fixtures/layout-no-timer.yaml')
3167 self.sched.reconfigure(self.config)
3168 self.registerJobs()
3169 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003170 # If APScheduler is in mid-event when we remove the job, we
3171 # can end up with one more event firing, so give it an extra
3172 # second to settle.
3173 time.sleep(1)
3174 self.waitUntilSettled()
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003175 self.worker.release('.*')
3176 self.waitUntilSettled()
3177
James E. Blair91e34592015-07-31 16:45:59 -07003178 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003179 "Test that the RPC client can enqueue a change"
3180 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003181 A.addApproval('Code-Review', 2)
3182 A.addApproval('Approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003183
3184 client = zuul.rpcclient.RPCClient('127.0.0.1',
3185 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003186 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003187 r = client.enqueue(tenant='tenant-one',
3188 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003189 project='org/project',
3190 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003191 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003192 self.waitUntilSettled()
3193 self.assertEqual(self.getJobFromHistory('project-merge').result,
3194 'SUCCESS')
3195 self.assertEqual(self.getJobFromHistory('project-test1').result,
3196 'SUCCESS')
3197 self.assertEqual(self.getJobFromHistory('project-test2').result,
3198 'SUCCESS')
3199 self.assertEqual(A.data['status'], 'MERGED')
3200 self.assertEqual(A.reported, 2)
3201 self.assertEqual(r, True)
3202
James E. Blair91e34592015-07-31 16:45:59 -07003203 def test_client_enqueue_ref(self):
3204 "Test that the RPC client can enqueue a ref"
James E. Blair289f5932017-07-27 15:02:29 -07003205 p = "review.example.com/org/project"
3206 upstream = self.getUpstreamRepos([p])
3207 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3208 A.setMerged()
3209 A_commit = str(upstream[p].commit('master'))
3210 self.log.debug("A commit: %s" % A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003211
3212 client = zuul.rpcclient.RPCClient('127.0.0.1',
3213 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003214 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07003215 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003216 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003217 pipeline='post',
3218 project='org/project',
3219 trigger='gerrit',
3220 ref='master',
3221 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
James E. Blair289f5932017-07-27 15:02:29 -07003222 newrev=A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003223 self.waitUntilSettled()
3224 job_names = [x.name for x in self.history]
3225 self.assertEqual(len(self.history), 1)
3226 self.assertIn('project-post', job_names)
3227 self.assertEqual(r, True)
3228
James E. Blairad28e912013-11-27 10:43:22 -08003229 def test_client_enqueue_negative(self):
3230 "Test that the RPC client returns errors"
3231 client = zuul.rpcclient.RPCClient('127.0.0.1',
3232 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003233 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003234 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003235 "Invalid tenant"):
3236 r = client.enqueue(tenant='tenant-foo',
3237 pipeline='gate',
3238 project='org/project',
3239 trigger='gerrit',
3240 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003241 self.assertEqual(r, False)
3242
3243 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003244 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003245 r = client.enqueue(tenant='tenant-one',
3246 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003247 project='project-does-not-exist',
3248 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003249 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003250 self.assertEqual(r, False)
3251
3252 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3253 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003254 r = client.enqueue(tenant='tenant-one',
3255 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003256 project='org/project',
3257 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003258 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003259 self.assertEqual(r, False)
3260
3261 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3262 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003263 r = client.enqueue(tenant='tenant-one',
3264 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003265 project='org/project',
3266 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003267 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003268 self.assertEqual(r, False)
3269
3270 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3271 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003272 r = client.enqueue(tenant='tenant-one',
3273 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003274 project='org/project',
3275 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003276 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003277 self.assertEqual(r, False)
3278
3279 self.waitUntilSettled()
3280 self.assertEqual(len(self.history), 0)
3281 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003282
3283 def test_client_promote(self):
3284 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003285 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003286 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3287 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3288 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003289 A.addApproval('Code-Review', 2)
3290 B.addApproval('Code-Review', 2)
3291 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003292
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003293 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3294 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3295 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003296
3297 self.waitUntilSettled()
3298
Paul Belangerbaca3132016-11-04 12:49:54 -04003299 tenant = self.sched.abide.tenants.get('tenant-one')
3300 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003301 enqueue_times = {}
3302 for item in items:
3303 enqueue_times[str(item.change)] = item.enqueue_time
3304
James E. Blair36658cf2013-12-06 17:53:48 -08003305 client = zuul.rpcclient.RPCClient('127.0.0.1',
3306 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003307 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003308 r = client.promote(tenant='tenant-one',
3309 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003310 change_ids=['2,1', '3,1'])
3311
Sean Daguef39b9ca2014-01-10 21:34:35 -05003312 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003313 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003314 for item in items:
3315 self.assertEqual(
3316 enqueue_times[str(item.change)], item.enqueue_time)
3317
James E. Blair78acec92014-02-06 07:11:32 -08003318 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003319 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003320 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003321 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003322 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003323 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003324 self.waitUntilSettled()
3325
3326 self.assertEqual(len(self.builds), 6)
3327 self.assertEqual(self.builds[0].name, 'project-test1')
3328 self.assertEqual(self.builds[1].name, 'project-test2')
3329 self.assertEqual(self.builds[2].name, 'project-test1')
3330 self.assertEqual(self.builds[3].name, 'project-test2')
3331 self.assertEqual(self.builds[4].name, 'project-test1')
3332 self.assertEqual(self.builds[5].name, 'project-test2')
3333
Paul Belangerbaca3132016-11-04 12:49:54 -04003334 self.assertTrue(self.builds[0].hasChanges(B))
3335 self.assertFalse(self.builds[0].hasChanges(A))
3336 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003337
Paul Belangerbaca3132016-11-04 12:49:54 -04003338 self.assertTrue(self.builds[2].hasChanges(B))
3339 self.assertTrue(self.builds[2].hasChanges(C))
3340 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003341
Paul Belangerbaca3132016-11-04 12:49:54 -04003342 self.assertTrue(self.builds[4].hasChanges(B))
3343 self.assertTrue(self.builds[4].hasChanges(C))
3344 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003345
Paul Belanger174a8272017-03-14 13:20:10 -04003346 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003347 self.waitUntilSettled()
3348
3349 self.assertEqual(A.data['status'], 'MERGED')
3350 self.assertEqual(A.reported, 2)
3351 self.assertEqual(B.data['status'], 'MERGED')
3352 self.assertEqual(B.reported, 2)
3353 self.assertEqual(C.data['status'], 'MERGED')
3354 self.assertEqual(C.reported, 2)
3355
James E. Blair36658cf2013-12-06 17:53:48 -08003356 self.assertEqual(r, True)
3357
3358 def test_client_promote_dependent(self):
3359 "Test that the RPC client can promote a dependent change"
3360 # C (depends on B) -> B -> A ; then promote C to get:
3361 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003362 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003363 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3364 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3365 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3366
3367 C.setDependsOn(B, 1)
3368
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003369 A.addApproval('Code-Review', 2)
3370 B.addApproval('Code-Review', 2)
3371 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003372
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003373 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3374 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3375 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003376
3377 self.waitUntilSettled()
3378
3379 client = zuul.rpcclient.RPCClient('127.0.0.1',
3380 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003381 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003382 r = client.promote(tenant='tenant-one',
3383 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003384 change_ids=['3,1'])
3385
James E. Blair78acec92014-02-06 07:11:32 -08003386 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003387 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003388 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003389 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003390 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003391 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003392 self.waitUntilSettled()
3393
3394 self.assertEqual(len(self.builds), 6)
3395 self.assertEqual(self.builds[0].name, 'project-test1')
3396 self.assertEqual(self.builds[1].name, 'project-test2')
3397 self.assertEqual(self.builds[2].name, 'project-test1')
3398 self.assertEqual(self.builds[3].name, 'project-test2')
3399 self.assertEqual(self.builds[4].name, 'project-test1')
3400 self.assertEqual(self.builds[5].name, 'project-test2')
3401
Paul Belangerbaca3132016-11-04 12:49:54 -04003402 self.assertTrue(self.builds[0].hasChanges(B))
3403 self.assertFalse(self.builds[0].hasChanges(A))
3404 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003405
Paul Belangerbaca3132016-11-04 12:49:54 -04003406 self.assertTrue(self.builds[2].hasChanges(B))
3407 self.assertTrue(self.builds[2].hasChanges(C))
3408 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003409
Paul Belangerbaca3132016-11-04 12:49:54 -04003410 self.assertTrue(self.builds[4].hasChanges(B))
3411 self.assertTrue(self.builds[4].hasChanges(C))
3412 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003413
Paul Belanger174a8272017-03-14 13:20:10 -04003414 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003415 self.waitUntilSettled()
3416
3417 self.assertEqual(A.data['status'], 'MERGED')
3418 self.assertEqual(A.reported, 2)
3419 self.assertEqual(B.data['status'], 'MERGED')
3420 self.assertEqual(B.reported, 2)
3421 self.assertEqual(C.data['status'], 'MERGED')
3422 self.assertEqual(C.reported, 2)
3423
James E. Blair36658cf2013-12-06 17:53:48 -08003424 self.assertEqual(r, True)
3425
3426 def test_client_promote_negative(self):
3427 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003428 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003429 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003430 A.addApproval('Code-Review', 2)
3431 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003432 self.waitUntilSettled()
3433
3434 client = zuul.rpcclient.RPCClient('127.0.0.1',
3435 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003436 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003437
3438 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003439 r = client.promote(tenant='tenant-one',
3440 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003441 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003442 self.assertEqual(r, False)
3443
3444 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003445 r = client.promote(tenant='tenant-one',
3446 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003447 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003448 self.assertEqual(r, False)
3449
Paul Belanger174a8272017-03-14 13:20:10 -04003450 self.executor_server.hold_jobs_in_build = False
3451 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003452 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003453
James E. Blaircc324192017-04-19 14:36:08 -07003454 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003455 def test_queue_rate_limiting(self):
3456 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003457 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003458 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3459 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3460 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3461
3462 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003463 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003464
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003465 A.addApproval('Code-Review', 2)
3466 B.addApproval('Code-Review', 2)
3467 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003468
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003469 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3470 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3471 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003472 self.waitUntilSettled()
3473
3474 # Only A and B will have their merge jobs queued because
3475 # window is 2.
3476 self.assertEqual(len(self.builds), 2)
3477 self.assertEqual(self.builds[0].name, 'project-merge')
3478 self.assertEqual(self.builds[1].name, 'project-merge')
3479
James E. Blair732ee862017-04-21 07:49:26 -07003480 # Release the merge jobs one at a time.
3481 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003482 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003483 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003484 self.waitUntilSettled()
3485
3486 # Only A and B will have their test jobs queued because
3487 # window is 2.
3488 self.assertEqual(len(self.builds), 4)
3489 self.assertEqual(self.builds[0].name, 'project-test1')
3490 self.assertEqual(self.builds[1].name, 'project-test2')
3491 self.assertEqual(self.builds[2].name, 'project-test1')
3492 self.assertEqual(self.builds[3].name, 'project-test2')
3493
Paul Belanger174a8272017-03-14 13:20:10 -04003494 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003495 self.waitUntilSettled()
3496
James E. Blaircc324192017-04-19 14:36:08 -07003497 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003498 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003499 # A failed so window is reduced by 1 to 1.
3500 self.assertEqual(queue.window, 1)
3501 self.assertEqual(queue.window_floor, 1)
3502 self.assertEqual(A.data['status'], 'NEW')
3503
3504 # Gate is reset and only B's merge job is queued because
3505 # window shrunk to 1.
3506 self.assertEqual(len(self.builds), 1)
3507 self.assertEqual(self.builds[0].name, 'project-merge')
3508
Paul Belanger174a8272017-03-14 13:20:10 -04003509 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003510 self.waitUntilSettled()
3511
3512 # Only B's test jobs are queued because window is still 1.
3513 self.assertEqual(len(self.builds), 2)
3514 self.assertEqual(self.builds[0].name, 'project-test1')
3515 self.assertEqual(self.builds[1].name, 'project-test2')
3516
Paul Belanger174a8272017-03-14 13:20:10 -04003517 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003518 self.waitUntilSettled()
3519
3520 # B was successfully merged so window is increased to 2.
3521 self.assertEqual(queue.window, 2)
3522 self.assertEqual(queue.window_floor, 1)
3523 self.assertEqual(B.data['status'], 'MERGED')
3524
3525 # Only C is left and its merge job is queued.
3526 self.assertEqual(len(self.builds), 1)
3527 self.assertEqual(self.builds[0].name, 'project-merge')
3528
Paul Belanger174a8272017-03-14 13:20:10 -04003529 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003530 self.waitUntilSettled()
3531
3532 # After successful merge job the test jobs for C are queued.
3533 self.assertEqual(len(self.builds), 2)
3534 self.assertEqual(self.builds[0].name, 'project-test1')
3535 self.assertEqual(self.builds[1].name, 'project-test2')
3536
Paul Belanger174a8272017-03-14 13:20:10 -04003537 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003538 self.waitUntilSettled()
3539
3540 # C successfully merged so window is bumped to 3.
3541 self.assertEqual(queue.window, 3)
3542 self.assertEqual(queue.window_floor, 1)
3543 self.assertEqual(C.data['status'], 'MERGED')
3544
James E. Blaircc324192017-04-19 14:36:08 -07003545 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003546 def test_queue_rate_limiting_dependent(self):
3547 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003548 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003549 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3550 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3551 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3552
3553 B.setDependsOn(A, 1)
3554
Paul Belanger174a8272017-03-14 13:20:10 -04003555 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003556
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003557 A.addApproval('Code-Review', 2)
3558 B.addApproval('Code-Review', 2)
3559 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003560
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003561 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3562 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3563 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003564 self.waitUntilSettled()
3565
3566 # Only A and B will have their merge jobs queued because
3567 # window is 2.
3568 self.assertEqual(len(self.builds), 2)
3569 self.assertEqual(self.builds[0].name, 'project-merge')
3570 self.assertEqual(self.builds[1].name, 'project-merge')
3571
James E. Blairf4a5f022017-04-18 14:01:10 -07003572 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003573
3574 # Only A and B will have their test jobs queued because
3575 # window is 2.
3576 self.assertEqual(len(self.builds), 4)
3577 self.assertEqual(self.builds[0].name, 'project-test1')
3578 self.assertEqual(self.builds[1].name, 'project-test2')
3579 self.assertEqual(self.builds[2].name, 'project-test1')
3580 self.assertEqual(self.builds[3].name, 'project-test2')
3581
Paul Belanger174a8272017-03-14 13:20:10 -04003582 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003583 self.waitUntilSettled()
3584
James E. Blaircc324192017-04-19 14:36:08 -07003585 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003586 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003587 # A failed so window is reduced by 1 to 1.
3588 self.assertEqual(queue.window, 1)
3589 self.assertEqual(queue.window_floor, 1)
3590 self.assertEqual(A.data['status'], 'NEW')
3591 self.assertEqual(B.data['status'], 'NEW')
3592
3593 # Gate is reset and only C's merge job is queued because
3594 # window shrunk to 1 and A and B were dequeued.
3595 self.assertEqual(len(self.builds), 1)
3596 self.assertEqual(self.builds[0].name, 'project-merge')
3597
James E. Blairf4a5f022017-04-18 14:01:10 -07003598 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003599
3600 # Only C's test jobs are queued because window is still 1.
3601 self.assertEqual(len(self.builds), 2)
James E. Blairdf25ddc2017-07-08 07:57:09 -07003602 builds = self.getSortedBuilds()
3603 self.assertEqual(builds[0].name, 'project-test1')
3604 self.assertEqual(builds[1].name, 'project-test2')
Clark Boylan7603a372014-01-21 11:43:20 -08003605
Paul Belanger174a8272017-03-14 13:20:10 -04003606 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003607 self.waitUntilSettled()
3608
3609 # C was successfully merged so window is increased to 2.
3610 self.assertEqual(queue.window, 2)
3611 self.assertEqual(queue.window_floor, 1)
3612 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003613
3614 def test_worker_update_metadata(self):
3615 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003616 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003617
3618 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003619 A.addApproval('Code-Review', 2)
3620 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003621 self.waitUntilSettled()
3622
Clint Byrumd4de79a2017-05-27 11:39:13 -07003623 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003624
3625 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07003626 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003627
3628 start = time.time()
3629 while True:
3630 if time.time() - start > 10:
3631 raise Exception("Timeout waiting for gearman server to report "
3632 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07003633 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003634 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08003635 break
3636 else:
3637 time.sleep(0)
3638
3639 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05003640 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003641
Paul Belanger174a8272017-03-14 13:20:10 -04003642 self.executor_server.hold_jobs_in_build = False
3643 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003644 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003645
James E. Blaircc324192017-04-19 14:36:08 -07003646 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003647 def test_footer_message(self):
3648 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003649 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003650 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003651 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003652 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003653 self.waitUntilSettled()
3654
3655 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003656 B.addApproval('Code-Review', 2)
3657 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003658 self.waitUntilSettled()
3659
3660 self.assertEqual(2, len(self.smtp_messages))
3661
Adam Gandelman82258fd2017-02-07 16:51:58 -08003662 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003663Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003664http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003665
Adam Gandelman82258fd2017-02-07 16:51:58 -08003666 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003667For CI problems and help debugging, contact ci@example.org"""
3668
Adam Gandelman82258fd2017-02-07 16:51:58 -08003669 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3670 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3671 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3672 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003673
Tobias Henkel9842bd72017-05-16 13:40:03 +02003674 @simple_layout('layouts/unmanaged-project.yaml')
3675 def test_unmanaged_project_start_message(self):
3676 "Test start reporting is not done for unmanaged projects."
3677 self.init_repo("org/project", tag='init')
3678 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3679 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3680 self.waitUntilSettled()
3681
3682 self.assertEqual(0, len(A.messages))
3683
Clint Byrumb9de8be2017-05-27 13:49:19 -07003684 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003685 def test_merge_failure_reporters(self):
3686 """Check that the config is set up correctly"""
3687
Clint Byrumb9de8be2017-05-27 13:49:19 -07003688 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11003689 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003690 "Merge Failed.\n\nThis change or one of its cross-repo "
3691 "dependencies was unable to be automatically merged with the "
3692 "current state of its repository. Please rebase the change and "
3693 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003694 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003695 self.assertEqual(
3696 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003697 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003698
3699 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003700 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11003701 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003702 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11003703
3704 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003705 tenant.layout.pipelines['check'].merge_failure_actions[0],
3706 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003707
3708 self.assertTrue(
3709 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003710 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003711 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003712 zuul.driver.smtp.smtpreporter.SMTPReporter) and
3713 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003714 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003715 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003716 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003717 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003718 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003719 gerritreporter.GerritReporter) and
3720 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003721 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003722 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003723 )
3724 )
3725
James E. Blairec056492016-07-22 09:45:56 -07003726 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003727 def test_merge_failure_reports(self):
3728 """Check that when a change fails to merge the correct message is sent
3729 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003730 self.updateConfigLayout(
3731 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003732 self.sched.reconfigure(self.config)
3733 self.registerJobs()
3734
3735 # Check a test failure isn't reported to SMTP
3736 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003737 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003738 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003739 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003740 self.waitUntilSettled()
3741
3742 self.assertEqual(3, len(self.history)) # 3 jobs
3743 self.assertEqual(0, len(self.smtp_messages))
3744
3745 # Check a merge failure is reported to SMTP
3746 # B should be merged, but C will conflict with B
3747 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3748 B.addPatchset(['conflict'])
3749 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3750 C.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003751 B.addApproval('Code-Review', 2)
3752 C.addApproval('Code-Review', 2)
3753 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3754 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003755 self.waitUntilSettled()
3756
3757 self.assertEqual(6, len(self.history)) # A and B jobs
3758 self.assertEqual(1, len(self.smtp_messages))
3759 self.assertEqual('The merge failed! For more information...',
3760 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003761
James E. Blairec056492016-07-22 09:45:56 -07003762 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003763 def test_default_merge_failure_reports(self):
3764 """Check that the default merge failure reports are correct."""
3765
3766 # A should report success, B should report merge failure.
3767 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3768 A.addPatchset(['conflict'])
3769 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3770 B.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003771 A.addApproval('Code-Review', 2)
3772 B.addApproval('Code-Review', 2)
3773 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3774 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003775 self.waitUntilSettled()
3776
3777 self.assertEqual(3, len(self.history)) # A jobs
3778 self.assertEqual(A.reported, 2)
3779 self.assertEqual(B.reported, 2)
3780 self.assertEqual(A.data['status'], 'MERGED')
3781 self.assertEqual(B.data['status'], 'NEW')
3782 self.assertIn('Build succeeded', A.messages[1])
3783 self.assertIn('Merge Failed', B.messages[1])
3784 self.assertIn('automatically merged', B.messages[1])
3785 self.assertNotIn('logs.example.com', B.messages[1])
3786 self.assertNotIn('SKIPPED', B.messages[1])
3787
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003788 def test_client_get_running_jobs(self):
3789 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003790 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003791 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003792 A.addApproval('Code-Review', 2)
3793 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003794 self.waitUntilSettled()
3795
3796 client = zuul.rpcclient.RPCClient('127.0.0.1',
3797 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003798 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003799
3800 # Wait for gearman server to send the initial workData back to zuul
3801 start = time.time()
3802 while True:
3803 if time.time() - start > 10:
3804 raise Exception("Timeout waiting for gearman server to report "
3805 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003806 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003807 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003808 break
3809 else:
3810 time.sleep(0)
3811
3812 running_items = client.get_running_jobs()
3813
3814 self.assertEqual(1, len(running_items))
3815 running_item = running_items[0]
3816 self.assertEqual([], running_item['failing_reasons'])
3817 self.assertEqual([], running_item['items_behind'])
3818 self.assertEqual('https://hostname/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003819 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003820 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003821 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003822 self.assertEqual(True, running_item['active'])
3823 self.assertEqual('1,1', running_item['id'])
3824
3825 self.assertEqual(3, len(running_item['jobs']))
3826 for job in running_item['jobs']:
3827 if job['name'] == 'project-merge':
3828 self.assertEqual('project-merge', job['name'])
3829 self.assertEqual('gate', job['pipeline'])
3830 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05003831 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003832 'static/stream.html?uuid={uuid}&logfile=console.log'
3833 .format(uuid=job['uuid']), job['url'])
3834 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05003835 'finger://{hostname}/{uuid}'.format(
3836 hostname=self.executor_server.hostname,
3837 uuid=job['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003838 job['finger_url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07003839 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003840 self.assertEqual(False, job['canceled'])
3841 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003842 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003843 self.assertEqual('gate', job['pipeline'])
3844 break
3845
Paul Belanger174a8272017-03-14 13:20:10 -04003846 self.executor_server.hold_jobs_in_build = False
3847 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003848 self.waitUntilSettled()
3849
3850 running_items = client.get_running_jobs()
3851 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003852
James E. Blairb70e55a2017-04-19 12:57:02 -07003853 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003854 def test_nonvoting_pipeline(self):
3855 "Test that a nonvoting pipeline (experimental) can still report"
3856
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003857 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3858 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003859 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3860 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003861
3862 self.assertEqual(self.getJobFromHistory('project-merge').result,
3863 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003864 self.assertEqual(
3865 self.getJobFromHistory('experimental-project-test').result,
3866 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003867 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003868
3869 def test_crd_gate(self):
3870 "Test cross-repo dependencies"
3871 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3872 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003873 A.addApproval('Code-Review', 2)
3874 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003875
3876 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3877 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3878 AM2.setMerged()
3879 AM1.setMerged()
3880
3881 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3882 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3883 BM2.setMerged()
3884 BM1.setMerged()
3885
3886 # A -> AM1 -> AM2
3887 # B -> BM1 -> BM2
3888 # A Depends-On: B
3889 # M2 is here to make sure it is never queried. If it is, it
3890 # means zuul is walking down the entire history of merged
3891 # changes.
3892
3893 B.setDependsOn(BM1, 1)
3894 BM1.setDependsOn(BM2, 1)
3895
3896 A.setDependsOn(AM1, 1)
3897 AM1.setDependsOn(AM2, 1)
3898
3899 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3900 A.subject, B.data['id'])
3901
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003902 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003903 self.waitUntilSettled()
3904
3905 self.assertEqual(A.data['status'], 'NEW')
3906 self.assertEqual(B.data['status'], 'NEW')
3907
Paul Belanger5dccbe72016-11-14 11:17:37 -05003908 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003909 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003910
Paul Belanger174a8272017-03-14 13:20:10 -04003911 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003912 B.addApproval('Approved', 1)
3913 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003914 self.waitUntilSettled()
3915
Paul Belanger174a8272017-03-14 13:20:10 -04003916 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003917 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003918 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003919 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003920 self.executor_server.hold_jobs_in_build = False
3921 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003922 self.waitUntilSettled()
3923
3924 self.assertEqual(AM2.queried, 0)
3925 self.assertEqual(BM2.queried, 0)
3926 self.assertEqual(A.data['status'], 'MERGED')
3927 self.assertEqual(B.data['status'], 'MERGED')
3928 self.assertEqual(A.reported, 2)
3929 self.assertEqual(B.reported, 2)
3930
Paul Belanger5dccbe72016-11-14 11:17:37 -05003931 changes = self.getJobFromHistory(
3932 'project-merge', 'org/project1').changes
3933 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003934
3935 def test_crd_branch(self):
3936 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003937
3938 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003939 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3940 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003941 C1 = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C1')
3942 C2 = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C2',
3943 status='ABANDONED')
3944 C1.data['id'] = B.data['id']
3945 C2.data['id'] = B.data['id']
3946
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003947 A.addApproval('Code-Review', 2)
3948 B.addApproval('Code-Review', 2)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003949 C1.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003950
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003951 # A Depends-On: B+C1
James E. Blair8f78d882015-02-05 08:51:37 -08003952 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3953 A.subject, B.data['id'])
3954
Paul Belanger174a8272017-03-14 13:20:10 -04003955 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003956 B.addApproval('Approved', 1)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003957 C1.addApproval('Approved', 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003958 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003959 self.waitUntilSettled()
3960
Paul Belanger174a8272017-03-14 13:20:10 -04003961 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003962 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003963 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003964 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003965 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003966 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003967 self.executor_server.hold_jobs_in_build = False
3968 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003969 self.waitUntilSettled()
3970
3971 self.assertEqual(A.data['status'], 'MERGED')
3972 self.assertEqual(B.data['status'], 'MERGED')
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003973 self.assertEqual(C1.data['status'], 'MERGED')
James E. Blair8f78d882015-02-05 08:51:37 -08003974 self.assertEqual(A.reported, 2)
3975 self.assertEqual(B.reported, 2)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003976 self.assertEqual(C1.reported, 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003977
Paul Belanger6379db12016-11-14 13:57:54 -05003978 changes = self.getJobFromHistory(
3979 'project-merge', 'org/project1').changes
3980 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003981
3982 def test_crd_multiline(self):
3983 "Test multiple depends-on lines in commit"
3984 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3985 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3986 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003987 A.addApproval('Code-Review', 2)
3988 B.addApproval('Code-Review', 2)
3989 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003990
3991 # A Depends-On: B+C
3992 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3993 A.subject, B.data['id'], C.data['id'])
3994
Paul Belanger174a8272017-03-14 13:20:10 -04003995 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003996 B.addApproval('Approved', 1)
3997 C.addApproval('Approved', 1)
3998 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003999 self.waitUntilSettled()
4000
Paul Belanger174a8272017-03-14 13:20:10 -04004001 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08004002 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004003 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08004004 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004005 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08004006 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004007 self.executor_server.hold_jobs_in_build = False
4008 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08004009 self.waitUntilSettled()
4010
4011 self.assertEqual(A.data['status'], 'MERGED')
4012 self.assertEqual(B.data['status'], 'MERGED')
4013 self.assertEqual(C.data['status'], 'MERGED')
4014 self.assertEqual(A.reported, 2)
4015 self.assertEqual(B.reported, 2)
4016 self.assertEqual(C.reported, 2)
4017
Paul Belanger5dccbe72016-11-14 11:17:37 -05004018 changes = self.getJobFromHistory(
4019 'project-merge', 'org/project1').changes
4020 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08004021
4022 def test_crd_unshared_gate(self):
4023 "Test cross-repo dependencies in unshared gate queues"
4024 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4025 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004026 A.addApproval('Code-Review', 2)
4027 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004028
4029 # A Depends-On: B
4030 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4031 A.subject, B.data['id'])
4032
4033 # A and B do not share a queue, make sure that A is unable to
4034 # enqueue B (and therefore, A is unable to be enqueued).
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004035 B.addApproval('Approved', 1)
4036 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004037 self.waitUntilSettled()
4038
4039 self.assertEqual(A.data['status'], 'NEW')
4040 self.assertEqual(B.data['status'], 'NEW')
4041 self.assertEqual(A.reported, 0)
4042 self.assertEqual(B.reported, 0)
4043 self.assertEqual(len(self.history), 0)
4044
4045 # Enqueue and merge B alone.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004046 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004047 self.waitUntilSettled()
4048
4049 self.assertEqual(B.data['status'], 'MERGED')
4050 self.assertEqual(B.reported, 2)
4051
4052 # Now that B is merged, A should be able to be enqueued and
4053 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004054 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004055 self.waitUntilSettled()
4056
4057 self.assertEqual(A.data['status'], 'MERGED')
4058 self.assertEqual(A.reported, 2)
4059
James E. Blair96698e22015-04-02 07:48:21 -07004060 def test_crd_gate_reverse(self):
4061 "Test reverse cross-repo dependencies"
4062 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4063 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004064 A.addApproval('Code-Review', 2)
4065 B.addApproval('Code-Review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07004066
4067 # A Depends-On: B
4068
4069 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4070 A.subject, B.data['id'])
4071
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004072 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004073 self.waitUntilSettled()
4074
4075 self.assertEqual(A.data['status'], 'NEW')
4076 self.assertEqual(B.data['status'], 'NEW')
4077
Paul Belanger174a8272017-03-14 13:20:10 -04004078 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004079 A.addApproval('Approved', 1)
4080 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004081 self.waitUntilSettled()
4082
Paul Belanger174a8272017-03-14 13:20:10 -04004083 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004084 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004085 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004086 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004087 self.executor_server.hold_jobs_in_build = False
4088 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07004089 self.waitUntilSettled()
4090
4091 self.assertEqual(A.data['status'], 'MERGED')
4092 self.assertEqual(B.data['status'], 'MERGED')
4093 self.assertEqual(A.reported, 2)
4094 self.assertEqual(B.reported, 2)
4095
Paul Belanger5dccbe72016-11-14 11:17:37 -05004096 changes = self.getJobFromHistory(
4097 'project-merge', 'org/project1').changes
4098 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07004099
James E. Blair5ee24252014-12-30 10:12:29 -08004100 def test_crd_cycle(self):
4101 "Test cross-repo dependency cycles"
4102 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4103 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004104 A.addApproval('Code-Review', 2)
4105 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004106
4107 # A -> B -> A (via commit-depends)
4108
4109 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4110 A.subject, B.data['id'])
4111 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4112 B.subject, A.data['id'])
4113
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004114 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004115 self.waitUntilSettled()
4116
4117 self.assertEqual(A.reported, 0)
4118 self.assertEqual(B.reported, 0)
4119 self.assertEqual(A.data['status'], 'NEW')
4120 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004121
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004122 def test_crd_gate_unknown(self):
4123 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004124 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004125 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4126 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004127 A.addApproval('Code-Review', 2)
4128 B.addApproval('Code-Review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004129
4130 # A Depends-On: B
4131 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4132 A.subject, B.data['id'])
4133
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004134 B.addApproval('Approved', 1)
4135 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004136 self.waitUntilSettled()
4137
4138 # Unknown projects cannot share a queue with any other
4139 # since they don't have common jobs with any other (they have no jobs).
4140 # Changes which depend on unknown project changes
4141 # should not be processed in dependent pipeline
4142 self.assertEqual(A.data['status'], 'NEW')
4143 self.assertEqual(B.data['status'], 'NEW')
4144 self.assertEqual(A.reported, 0)
4145 self.assertEqual(B.reported, 0)
4146 self.assertEqual(len(self.history), 0)
4147
James E. Blair7720b402017-06-07 08:51:49 -07004148 # Simulate change B being gated outside this layout Set the
4149 # change merged before submitting the event so that when the
4150 # event triggers a gerrit query to update the change, we get
4151 # the information that it was merged.
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004152 B.setMerged()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004153 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004154 self.waitUntilSettled()
4155 self.assertEqual(len(self.history), 0)
4156
4157 # Now that B is merged, A should be able to be enqueued and
4158 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004159 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004160 self.waitUntilSettled()
4161
4162 self.assertEqual(A.data['status'], 'MERGED')
4163 self.assertEqual(A.reported, 2)
4164 self.assertEqual(B.data['status'], 'MERGED')
4165 self.assertEqual(B.reported, 0)
4166
James E. Blairbfb8e042014-12-30 17:01:44 -08004167 def test_crd_check(self):
4168 "Test cross-repo dependencies in independent pipelines"
4169
Paul Belanger174a8272017-03-14 13:20:10 -04004170 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004171 self.gearman_server.hold_jobs_in_queue = True
4172 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4173 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4174
4175 # A Depends-On: B
4176 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4177 A.subject, B.data['id'])
4178
4179 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4180 self.waitUntilSettled()
4181
James E. Blairbfb8e042014-12-30 17:01:44 -08004182 self.gearman_server.hold_jobs_in_queue = False
4183 self.gearman_server.release()
4184 self.waitUntilSettled()
4185
Paul Belanger174a8272017-03-14 13:20:10 -04004186 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05004187 self.waitUntilSettled()
4188
James E. Blair2a535672017-04-27 12:03:15 -07004189 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08004190
Paul Belanger174a8272017-03-14 13:20:10 -04004191 self.executor_server.hold_jobs_in_build = False
4192 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05004193 self.waitUntilSettled()
4194
James E. Blairbfb8e042014-12-30 17:01:44 -08004195 self.assertEqual(A.data['status'], 'NEW')
4196 self.assertEqual(B.data['status'], 'NEW')
4197 self.assertEqual(A.reported, 1)
4198 self.assertEqual(B.reported, 0)
4199
4200 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004201 tenant = self.sched.abide.tenants.get('tenant-one')
4202 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004203
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004204 def test_crd_check_git_depends(self):
4205 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004206 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004207 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4208 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4209
4210 # Add two git-dependent changes and make sure they both report
4211 # success.
4212 B.setDependsOn(A, 1)
4213 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4214 self.waitUntilSettled()
4215 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4216 self.waitUntilSettled()
4217
James E. Blairb8c16472015-05-05 14:55:26 -07004218 self.orderedRelease()
4219 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004220 self.waitUntilSettled()
4221
4222 self.assertEqual(A.data['status'], 'NEW')
4223 self.assertEqual(B.data['status'], 'NEW')
4224 self.assertEqual(A.reported, 1)
4225 self.assertEqual(B.reported, 1)
4226
4227 self.assertEqual(self.history[0].changes, '1,1')
4228 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004229 tenant = self.sched.abide.tenants.get('tenant-one')
4230 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004231
4232 self.assertIn('Build succeeded', A.messages[0])
4233 self.assertIn('Build succeeded', B.messages[0])
4234
4235 def test_crd_check_duplicate(self):
4236 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004237 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004238 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4239 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004240 tenant = self.sched.abide.tenants.get('tenant-one')
4241 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004242
4243 # Add two git-dependent changes...
4244 B.setDependsOn(A, 1)
4245 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4246 self.waitUntilSettled()
4247 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4248
4249 # ...make sure the live one is not duplicated...
4250 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4251 self.waitUntilSettled()
4252 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4253
4254 # ...but the non-live one is able to be.
4255 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4256 self.waitUntilSettled()
4257 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4258
Clark Boylandd849822015-03-02 12:38:14 -08004259 # Release jobs in order to avoid races with change A jobs
4260 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004261 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004262 self.executor_server.hold_jobs_in_build = False
4263 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004264 self.waitUntilSettled()
4265
4266 self.assertEqual(A.data['status'], 'NEW')
4267 self.assertEqual(B.data['status'], 'NEW')
4268 self.assertEqual(A.reported, 1)
4269 self.assertEqual(B.reported, 1)
4270
4271 self.assertEqual(self.history[0].changes, '1,1 2,1')
4272 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004273 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004274
4275 self.assertIn('Build succeeded', A.messages[0])
4276 self.assertIn('Build succeeded', B.messages[0])
4277
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004278 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004279 "Test cross-repo dependencies re-enqueued in independent pipelines"
4280
4281 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004282 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4283 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004284
4285 # A Depends-On: B
4286 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4287 A.subject, B.data['id'])
4288
4289 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4290 self.waitUntilSettled()
4291
4292 self.sched.reconfigure(self.config)
4293
4294 # Make sure the items still share a change queue, and the
4295 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004296 tenant = self.sched.abide.tenants.get('tenant-one')
4297 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4298 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004299 first_item = queue.queue[0]
4300 for item in queue.queue:
4301 self.assertEqual(item.queue, first_item.queue)
4302 self.assertFalse(first_item.live)
4303 self.assertTrue(queue.queue[1].live)
4304
4305 self.gearman_server.hold_jobs_in_queue = False
4306 self.gearman_server.release()
4307 self.waitUntilSettled()
4308
4309 self.assertEqual(A.data['status'], 'NEW')
4310 self.assertEqual(B.data['status'], 'NEW')
4311 self.assertEqual(A.reported, 1)
4312 self.assertEqual(B.reported, 0)
4313
4314 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004315 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004316
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004317 def test_crd_check_reconfiguration(self):
4318 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4319
4320 def test_crd_undefined_project(self):
4321 """Test that undefined projects in dependencies are handled for
4322 independent pipelines"""
4323 # It's a hack for fake gerrit,
4324 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004325 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004326 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4327
Jesse Keatingea68ab42017-04-19 15:03:30 -07004328 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004329 def test_crd_check_ignore_dependencies(self):
4330 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004331
4332 self.gearman_server.hold_jobs_in_queue = True
4333 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4334 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4335 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4336
4337 # A Depends-On: B
4338 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4339 A.subject, B.data['id'])
4340 # C git-depends on B
4341 C.setDependsOn(B, 1)
4342 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4343 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4344 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4345 self.waitUntilSettled()
4346
4347 # Make sure none of the items share a change queue, and all
4348 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004349 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004350 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004351 self.assertEqual(len(check_pipeline.queues), 3)
4352 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4353 for item in check_pipeline.getAllItems():
4354 self.assertTrue(item.live)
4355
4356 self.gearman_server.hold_jobs_in_queue = False
4357 self.gearman_server.release()
4358 self.waitUntilSettled()
4359
4360 self.assertEqual(A.data['status'], 'NEW')
4361 self.assertEqual(B.data['status'], 'NEW')
4362 self.assertEqual(C.data['status'], 'NEW')
4363 self.assertEqual(A.reported, 1)
4364 self.assertEqual(B.reported, 1)
4365 self.assertEqual(C.reported, 1)
4366
4367 # Each job should have tested exactly one change
4368 for job in self.history:
4369 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004370
James E. Blair7cb84542017-04-19 13:35:05 -07004371 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004372 def test_crd_check_transitive(self):
4373 "Test transitive cross-repo dependencies"
4374 # Specifically, if A -> B -> C, and C gets a new patchset and
4375 # A gets a new patchset, ensure the test of A,2 includes B,1
4376 # and C,2 (not C,1 which would indicate stale data in the
4377 # cache for B).
4378 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4379 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4380 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4381
4382 # A Depends-On: B
4383 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4384 A.subject, B.data['id'])
4385
4386 # B Depends-On: C
4387 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4388 B.subject, C.data['id'])
4389
4390 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4391 self.waitUntilSettled()
4392 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4393
4394 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4395 self.waitUntilSettled()
4396 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4397
4398 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4399 self.waitUntilSettled()
4400 self.assertEqual(self.history[-1].changes, '3,1')
4401
4402 C.addPatchset()
4403 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4404 self.waitUntilSettled()
4405 self.assertEqual(self.history[-1].changes, '3,2')
4406
4407 A.addPatchset()
4408 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4409 self.waitUntilSettled()
4410 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004411
James E. Blaira8b90b32016-08-24 15:18:50 -07004412 def test_crd_check_unknown(self):
4413 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004414 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004415 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4416 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4417 # A Depends-On: B
4418 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4419 A.subject, B.data['id'])
4420
4421 # Make sure zuul has seen an event on B.
4422 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4423 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4424 self.waitUntilSettled()
4425
4426 self.assertEqual(A.data['status'], 'NEW')
4427 self.assertEqual(A.reported, 1)
4428 self.assertEqual(B.data['status'], 'NEW')
4429 self.assertEqual(B.reported, 0)
4430
James E. Blair92464a22016-04-05 10:21:26 -07004431 def test_crd_cycle_join(self):
4432 "Test an updated change creates a cycle"
4433 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4434
4435 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4436 self.waitUntilSettled()
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004437 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004438
4439 # Create B->A
4440 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4441 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4442 B.subject, A.data['id'])
4443 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4444 self.waitUntilSettled()
4445
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004446 # Dep is there so zuul should have reported on B
4447 self.assertEqual(B.reported, 1)
4448
James E. Blair92464a22016-04-05 10:21:26 -07004449 # Update A to add A->B (a cycle).
4450 A.addPatchset()
4451 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4452 A.subject, B.data['id'])
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004453 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4454 self.waitUntilSettled()
James E. Blair92464a22016-04-05 10:21:26 -07004455
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004456 # Dependency cycle injected so zuul should not have reported again on A
4457 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004458
4459 # Now if we update B to remove the depends-on, everything
4460 # should be okay. B; A->B
4461
4462 B.addPatchset()
4463 B.data['commitMessage'] = '%s\n' % (B.subject,)
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004464 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4465 self.waitUntilSettled()
Paul Belanger0e155e22016-11-14 14:12:23 -05004466
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004467 # Cycle was removed so now zuul should have reported again on A
4468 self.assertEqual(A.reported, 2)
4469
4470 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
4471 self.waitUntilSettled()
4472 self.assertEqual(B.reported, 2)
James E. Blair92464a22016-04-05 10:21:26 -07004473
Jesse Keatingcc67b122017-04-19 14:54:57 -07004474 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004475 def test_disable_at(self):
4476 "Test a pipeline will only report to the disabled trigger when failing"
4477
Jesse Keatingcc67b122017-04-19 14:54:57 -07004478 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004479 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004480 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004481 0, tenant.layout.pipelines['check']._consecutive_failures)
4482 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004483
4484 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4485 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4486 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4487 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4488 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4489 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4490 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4491 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4492 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4493 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4494 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4495
Paul Belanger174a8272017-03-14 13:20:10 -04004496 self.executor_server.failJob('project-test1', A)
4497 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004498 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004499 self.executor_server.failJob('project-test1', D)
4500 self.executor_server.failJob('project-test1', E)
4501 self.executor_server.failJob('project-test1', F)
4502 self.executor_server.failJob('project-test1', G)
4503 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004504 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004505 self.executor_server.failJob('project-test1', J)
4506 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004507
4508 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4509 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4510 self.waitUntilSettled()
4511
4512 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004513 2, tenant.layout.pipelines['check']._consecutive_failures)
4514 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004515
4516 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4517 self.waitUntilSettled()
4518
4519 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004520 0, tenant.layout.pipelines['check']._consecutive_failures)
4521 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004522
4523 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4524 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4525 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4526 self.waitUntilSettled()
4527
4528 # We should be disabled now
4529 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004530 3, tenant.layout.pipelines['check']._consecutive_failures)
4531 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004532
4533 # We need to wait between each of these patches to make sure the
4534 # smtp messages come back in an expected order
4535 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4536 self.waitUntilSettled()
4537 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4538 self.waitUntilSettled()
4539 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4540 self.waitUntilSettled()
4541
4542 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4543 # leaving a message on each change
4544 self.assertEqual(1, len(A.messages))
4545 self.assertIn('Build failed.', A.messages[0])
4546 self.assertEqual(1, len(B.messages))
4547 self.assertIn('Build failed.', B.messages[0])
4548 self.assertEqual(1, len(C.messages))
4549 self.assertIn('Build succeeded.', C.messages[0])
4550 self.assertEqual(1, len(D.messages))
4551 self.assertIn('Build failed.', D.messages[0])
4552 self.assertEqual(1, len(E.messages))
4553 self.assertIn('Build failed.', E.messages[0])
4554 self.assertEqual(1, len(F.messages))
4555 self.assertIn('Build failed.', F.messages[0])
4556
4557 # The last 3 (GHI) would have only reported via smtp.
4558 self.assertEqual(3, len(self.smtp_messages))
4559 self.assertEqual(0, len(G.messages))
4560 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004561 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004562 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004563 self.assertEqual(0, len(H.messages))
4564 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004565 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004566 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004567 self.assertEqual(0, len(I.messages))
4568 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004569 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004570 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004571
4572 # Now reload the configuration (simulate a HUP) to check the pipeline
4573 # comes out of disabled
4574 self.sched.reconfigure(self.config)
4575
Jesse Keatingcc67b122017-04-19 14:54:57 -07004576 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004577
4578 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004579 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004580 0, tenant.layout.pipelines['check']._consecutive_failures)
4581 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004582
4583 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4584 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4585 self.waitUntilSettled()
4586
4587 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004588 2, tenant.layout.pipelines['check']._consecutive_failures)
4589 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004590
4591 # J and K went back to gerrit
4592 self.assertEqual(1, len(J.messages))
4593 self.assertIn('Build failed.', J.messages[0])
4594 self.assertEqual(1, len(K.messages))
4595 self.assertIn('Build failed.', K.messages[0])
4596 # No more messages reported via smtp
4597 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004598
James E. Blaircc020532017-04-19 13:04:44 -07004599 @simple_layout('layouts/one-job-project.yaml')
4600 def test_one_job_project(self):
4601 "Test that queueing works with one job"
4602 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4603 'master', 'A')
4604 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4605 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004606 A.addApproval('Code-Review', 2)
4607 B.addApproval('Code-Review', 2)
4608 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4609 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blaircc020532017-04-19 13:04:44 -07004610 self.waitUntilSettled()
4611
4612 self.assertEqual(A.data['status'], 'MERGED')
4613 self.assertEqual(A.reported, 2)
4614 self.assertEqual(B.data['status'], 'MERGED')
4615 self.assertEqual(B.reported, 2)
4616
Paul Belanger32023542017-10-06 14:48:50 -04004617 def test_job_aborted(self):
4618 "Test that if a execute server aborts a job, it is run again"
4619 self.executor_server.hold_jobs_in_build = True
4620 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4621 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4622 self.waitUntilSettled()
4623
4624 self.executor_server.release('.*-merge')
4625 self.waitUntilSettled()
4626
4627 self.assertEqual(len(self.builds), 2)
Tobias Henkel26fa8a32017-10-07 07:08:36 +00004628
4629 # first abort
4630 self.builds[0].aborted = True
4631 self.executor_server.release('.*-test*')
4632 self.waitUntilSettled()
4633 self.assertEqual(len(self.builds), 1)
4634
4635 # second abort
4636 self.builds[0].aborted = True
4637 self.executor_server.release('.*-test*')
4638 self.waitUntilSettled()
4639 self.assertEqual(len(self.builds), 1)
4640
4641 # third abort
4642 self.builds[0].aborted = True
4643 self.executor_server.release('.*-test*')
4644 self.waitUntilSettled()
4645 self.assertEqual(len(self.builds), 1)
4646
4647 # fourth abort
Paul Belanger32023542017-10-06 14:48:50 -04004648 self.builds[0].aborted = True
4649 self.executor_server.release('.*-test*')
4650 self.waitUntilSettled()
4651 self.assertEqual(len(self.builds), 1)
4652
4653 self.executor_server.hold_jobs_in_build = False
4654 self.executor_server.release()
4655 self.waitUntilSettled()
4656
Tobias Henkel26fa8a32017-10-07 07:08:36 +00004657 self.assertEqual(len(self.history), 7)
4658 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
Paul Belanger32023542017-10-06 14:48:50 -04004659 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
4660
Paul Belanger71d98172016-11-08 10:56:31 -05004661 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004662 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004663
Paul Belanger174a8272017-03-14 13:20:10 -04004664 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004665 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4666 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4667 self.waitUntilSettled()
4668
Paul Belanger174a8272017-03-14 13:20:10 -04004669 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004670 self.waitUntilSettled()
4671
4672 self.assertEqual(len(self.builds), 2)
4673 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004674 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004675 self.waitUntilSettled()
4676
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004677 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004678 self.assertEqual(len(self.builds), 1,
4679 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004680 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004681 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004682 self.waitUntilSettled()
4683
Paul Belanger174a8272017-03-14 13:20:10 -04004684 self.executor_server.hold_jobs_in_build = False
4685 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004686 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004687 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004688 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4689 self.assertEqual(A.reported, 1)
4690 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004691
James E. Blair15be0e12017-01-03 13:45:20 -08004692 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004693 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004694
4695 self.fake_nodepool.paused = True
4696 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004697 A.addApproval('Code-Review', 2)
4698 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair15be0e12017-01-03 13:45:20 -08004699 self.waitUntilSettled()
4700
4701 self.zk.client.stop()
4702 self.zk.client.start()
4703 self.fake_nodepool.paused = False
4704 self.waitUntilSettled()
4705
4706 self.assertEqual(A.data['status'], 'MERGED')
4707 self.assertEqual(A.reported, 2)
4708
James E. Blair6ab79e02017-01-06 10:10:17 -08004709 def test_nodepool_failure(self):
4710 "Test that jobs are reported after a nodepool failure"
4711
4712 self.fake_nodepool.paused = True
4713 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004714 A.addApproval('Code-Review', 2)
4715 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6ab79e02017-01-06 10:10:17 -08004716 self.waitUntilSettled()
4717
4718 req = self.fake_nodepool.getNodeRequests()[0]
4719 self.fake_nodepool.addFailRequest(req)
4720
4721 self.fake_nodepool.paused = False
4722 self.waitUntilSettled()
4723
4724 self.assertEqual(A.data['status'], 'NEW')
4725 self.assertEqual(A.reported, 2)
4726 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4727 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4728 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4729
Monty Taylor6dc5bc12017-09-29 15:47:31 -05004730 def test_nodepool_priority(self):
4731 "Test that nodes are requested at the correct priority"
4732
4733 self.fake_nodepool.paused = True
4734
4735 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4736 self.fake_gerrit.addEvent(A.getRefUpdatedEvent())
4737
4738 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4739 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4740
4741 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4742 C.addApproval('Code-Review', 2)
4743 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
4744
4745 self.waitUntilSettled()
4746
4747 reqs = self.fake_nodepool.getNodeRequests()
4748
4749 # The requests come back sorted by oid. Since we have three requests
4750 # for the three changes each with a different priority.
4751 # Also they get a serial number based on order they were received
4752 # so the number on the endof the oid should map to order submitted.
4753
4754 # * gate first - high priority - change C
4755 self.assertEqual(reqs[0]['_oid'], '100-0000000002')
4756 self.assertEqual(reqs[0]['node_types'], ['label1'])
4757 # * check second - normal priority - change B
4758 self.assertEqual(reqs[1]['_oid'], '200-0000000001')
4759 self.assertEqual(reqs[1]['node_types'], ['label1'])
4760 # * post third - low priority - change A
4761 # additionally, the post job defined uses an ubuntu-xenial node,
4762 # so we include that check just as an extra verification
4763 self.assertEqual(reqs[2]['_oid'], '300-0000000000')
4764 self.assertEqual(reqs[2]['node_types'], ['ubuntu-xenial'])
4765
4766 self.fake_nodepool.paused = False
4767 self.waitUntilSettled()
4768
James E. Blairfceaf412017-09-29 13:44:57 -07004769 @simple_layout('layouts/multiple-templates.yaml')
4770 def test_multiple_project_templates(self):
4771 # Test that applying multiple project templates to a project
4772 # doesn't alter them when used for a second project.
4773 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4774 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4775 self.waitUntilSettled()
4776
4777 build = self.getJobFromHistory('py27')
4778 self.assertEqual(build.parameters['zuul']['jobtags'], [])
4779
James E. Blair29c77002017-10-05 14:56:35 -07004780 def test_pending_merge_in_reconfig(self):
4781 # Test that if we are waiting for an outstanding merge on
4782 # reconfiguration that we continue to do so.
4783 self.gearman_server.hold_merge_jobs_in_queue = True
4784 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4785 A.setMerged()
4786 self.fake_gerrit.addEvent(A.getRefUpdatedEvent())
4787 self.waitUntilSettled()
4788 # Reconfigure while we still have an outstanding merge job
4789 self.sched.reconfigureTenant(self.sched.abide.tenants['tenant-one'],
4790 None)
4791 self.waitUntilSettled()
4792 # Verify the merge job is still running and that the item is
4793 # in the pipeline
4794 self.assertEqual(len(self.sched.merger.jobs), 1)
4795 tenant = self.sched.abide.tenants.get('tenant-one')
4796 pipeline = tenant.layout.pipelines['post']
4797 self.assertEqual(len(pipeline.getAllItems()), 1)
4798 self.gearman_server.hold_merge_jobs_in_queue = False
4799 self.gearman_server.release()
4800 self.waitUntilSettled()
4801
Arieb6f068c2016-10-09 13:11:06 +03004802
James E. Blaira002b032017-04-18 10:35:48 -07004803class TestExecutor(ZuulTestCase):
4804 tenant_config_file = 'config/single-tenant/main.yaml'
4805
4806 def assertFinalState(self):
4807 # In this test, we expect to shut down in a non-final state,
4808 # so skip these checks.
4809 pass
4810
4811 def assertCleanShutdown(self):
4812 self.log.debug("Assert clean shutdown")
4813
4814 # After shutdown, make sure no jobs are running
4815 self.assertEqual({}, self.executor_server.job_workers)
4816
4817 # Make sure that git.Repo objects have been garbage collected.
4818 repos = []
4819 gc.collect()
4820 for obj in gc.get_objects():
4821 if isinstance(obj, git.Repo):
4822 self.log.debug("Leaked git repo object: %s" % repr(obj))
4823 repos.append(obj)
4824 self.assertEqual(len(repos), 0)
4825
4826 def test_executor_shutdown(self):
4827 "Test that the executor can shut down with jobs running"
4828
4829 self.executor_server.hold_jobs_in_build = True
4830 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004831 A.addApproval('Code-Review', 2)
4832 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaira002b032017-04-18 10:35:48 -07004833 self.waitUntilSettled()
4834
4835
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004836class TestDependencyGraph(ZuulTestCase):
4837 tenant_config_file = 'config/dependency-graph/main.yaml'
4838
4839 def test_dependeny_graph_dispatch_jobs_once(self):
4840 "Test a job in a dependency graph is queued only once"
4841 # Job dependencies, starting with A
4842 # A
4843 # / \
4844 # B C
4845 # / \ / \
4846 # D F E
4847 # |
4848 # G
4849
4850 self.executor_server.hold_jobs_in_build = True
4851 change = self.fake_gerrit.addFakeChange(
4852 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004853 change.addApproval('Code-Review', 2)
4854 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004855
4856 self.waitUntilSettled()
4857 self.assertEqual([b.name for b in self.builds], ['A'])
4858
4859 self.executor_server.release('A')
4860 self.waitUntilSettled()
4861 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4862
4863 self.executor_server.release('B')
4864 self.waitUntilSettled()
4865 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4866
4867 self.executor_server.release('D')
4868 self.waitUntilSettled()
4869 self.assertEqual([b.name for b in self.builds], ['C'])
4870
4871 self.executor_server.release('C')
4872 self.waitUntilSettled()
4873 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4874
4875 self.executor_server.release('F')
4876 self.waitUntilSettled()
4877 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4878
4879 self.executor_server.release('G')
4880 self.waitUntilSettled()
4881 self.assertEqual([b.name for b in self.builds], ['E'])
4882
4883 self.executor_server.release('E')
4884 self.waitUntilSettled()
4885 self.assertEqual(len(self.builds), 0)
4886
4887 self.executor_server.hold_jobs_in_build = False
4888 self.executor_server.release()
4889 self.waitUntilSettled()
4890
4891 self.assertEqual(len(self.builds), 0)
4892 self.assertEqual(len(self.history), 7)
4893
4894 self.assertEqual(change.data['status'], 'MERGED')
4895 self.assertEqual(change.reported, 2)
4896
4897 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4898 "Test that a job waits till all dependencies are successful"
4899 # Job dependencies, starting with A
4900 # A
4901 # / \
4902 # B C*
4903 # / \ / \
4904 # D F E
4905 # |
4906 # G
4907
4908 self.executor_server.hold_jobs_in_build = True
4909 change = self.fake_gerrit.addFakeChange(
4910 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004911 change.addApproval('Code-Review', 2)
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004912
4913 self.executor_server.failJob('C', change)
4914
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004915 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004916
4917 self.waitUntilSettled()
4918 self.assertEqual([b.name for b in self.builds], ['A'])
4919
4920 self.executor_server.release('A')
4921 self.waitUntilSettled()
4922 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4923
4924 self.executor_server.release('B')
4925 self.waitUntilSettled()
4926 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4927
4928 self.executor_server.release('D')
4929 self.waitUntilSettled()
4930 self.assertEqual([b.name for b in self.builds], ['C'])
4931
4932 self.executor_server.release('C')
4933 self.waitUntilSettled()
4934 self.assertEqual(len(self.builds), 0)
4935
4936 self.executor_server.hold_jobs_in_build = False
4937 self.executor_server.release()
4938 self.waitUntilSettled()
4939
4940 self.assertEqual(len(self.builds), 0)
4941 self.assertEqual(len(self.history), 4)
4942
4943 self.assertEqual(change.data['status'], 'NEW')
4944 self.assertEqual(change.reported, 2)
4945
4946
Arieb6f068c2016-10-09 13:11:06 +03004947class TestDuplicatePipeline(ZuulTestCase):
4948 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4949
4950 def test_duplicate_pipelines(self):
4951 "Test that a change matching multiple pipelines works"
4952
4953 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4954 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4955 self.waitUntilSettled()
4956
4957 self.assertHistory([
4958 dict(name='project-test1', result='SUCCESS', changes='1,1',
4959 pipeline='dup1'),
4960 dict(name='project-test1', result='SUCCESS', changes='1,1',
4961 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004962 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004963
4964 self.assertEqual(len(A.messages), 2)
4965
Paul Belangera46a3742016-11-09 14:23:03 -05004966 if 'dup1' in A.messages[0]:
4967 self.assertIn('dup1', A.messages[0])
4968 self.assertNotIn('dup2', A.messages[0])
4969 self.assertIn('project-test1', A.messages[0])
4970 self.assertIn('dup2', A.messages[1])
4971 self.assertNotIn('dup1', A.messages[1])
4972 self.assertIn('project-test1', A.messages[1])
4973 else:
4974 self.assertIn('dup1', A.messages[1])
4975 self.assertNotIn('dup2', A.messages[1])
4976 self.assertIn('project-test1', A.messages[1])
4977 self.assertIn('dup2', A.messages[0])
4978 self.assertNotIn('dup1', A.messages[0])
4979 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004980
4981
Paul Belanger86085b32016-11-03 12:48:57 -04004982class TestSchedulerTemplatedProject(ZuulTestCase):
4983 tenant_config_file = 'config/templated-project/main.yaml'
4984
Paul Belanger174a8272017-03-14 13:20:10 -04004985 def test_job_from_templates_executed(self):
4986 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004987
4988 A = self.fake_gerrit.addFakeChange(
4989 'org/templated-project', 'master', 'A')
4990 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4991 self.waitUntilSettled()
4992
4993 self.assertEqual(self.getJobFromHistory('project-test1').result,
4994 'SUCCESS')
4995 self.assertEqual(self.getJobFromHistory('project-test2').result,
4996 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004997
4998 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004999 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04005000
5001 A = self.fake_gerrit.addFakeChange(
5002 'org/layered-project', 'master', 'A')
5003 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5004 self.waitUntilSettled()
5005
5006 self.assertEqual(self.getJobFromHistory('project-test1').result,
5007 'SUCCESS')
5008 self.assertEqual(self.getJobFromHistory('project-test2').result,
5009 'SUCCESS')
5010 self.assertEqual(self.getJobFromHistory('layered-project-test3'
5011 ).result, 'SUCCESS')
5012 self.assertEqual(self.getJobFromHistory('layered-project-test4'
5013 ).result, 'SUCCESS')
5014 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
5015 ).result, 'SUCCESS')
5016 self.assertEqual(self.getJobFromHistory('project-test6').result,
5017 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08005018
James E. Blaire74f5712017-09-29 15:14:31 -07005019 def test_unimplied_branch_matchers(self):
5020 # This tests that there are no implied branch matchers added
5021 # by project templates.
5022 self.create_branch('org/layered-project', 'stable')
5023
5024 A = self.fake_gerrit.addFakeChange(
5025 'org/layered-project', 'stable', 'A')
5026
5027 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5028 self.waitUntilSettled()
5029
5030 self.assertEqual(self.getJobFromHistory('project-test1').result,
5031 'SUCCESS')
5032 print(self.getJobFromHistory('project-test1').
5033 parameters['zuul']['_inheritance_path'])
5034
5035 def test_implied_branch_matchers(self):
5036 # This tests that there is an implied branch matcher when a
5037 # template is used on an in-repo project pipeline definition.
5038 self.create_branch('untrusted-config', 'stable')
5039 self.fake_gerrit.addEvent(
5040 self.fake_gerrit.getFakeBranchCreatedEvent(
5041 'untrusted-config', 'stable'))
5042 self.waitUntilSettled()
5043
5044 A = self.fake_gerrit.addFakeChange(
5045 'untrusted-config', 'stable', 'A')
5046
5047 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5048 self.waitUntilSettled()
5049
5050 self.assertEqual(self.getJobFromHistory('project-test1').result,
5051 'SUCCESS')
5052 print(self.getJobFromHistory('project-test1').
5053 parameters['zuul']['_inheritance_path'])
5054
Adam Gandelman94a60062016-11-21 16:43:14 -08005055
5056class TestSchedulerSuccessURL(ZuulTestCase):
5057 tenant_config_file = 'config/success-url/main.yaml'
5058
5059 def test_success_url(self):
5060 "Ensure bad build params are ignored"
5061 self.sched.reconfigure(self.config)
5062 self.init_repo('org/docs')
5063
5064 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
5065 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5066 self.waitUntilSettled()
5067
5068 # Both builds ran: docs-draft-test + docs-draft-test2
5069 self.assertEqual(len(self.history), 2)
5070
5071 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08005072 for build in self.history:
5073 if build.name == 'docs-draft-test':
5074 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06005075 elif build.name == 'docs-draft-test2':
5076 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08005077
5078 # Two msgs: 'Starting...' + results
5079 self.assertEqual(len(self.smtp_messages), 2)
5080 body = self.smtp_messages[1]['body'].splitlines()
5081 self.assertEqual('Build succeeded.', body[0])
5082
5083 self.assertIn(
5084 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
5085 'docs-draft-test/%s/publish-docs/' % uuid,
5086 body[2])
5087
Paul Belanger174a8272017-03-14 13:20:10 -04005088 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08005089 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05005090 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
5091 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06005092 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08005093 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08005094
5095
Adam Gandelman4da00f62016-12-09 15:47:33 -08005096class TestSchedulerMerges(ZuulTestCase):
5097 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08005098
5099 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04005100 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08005101 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04005102 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08005103 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
5104 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
5105 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005106 A.addApproval('Code-Review', 2)
5107 B.addApproval('Code-Review', 2)
5108 C.addApproval('Code-Review', 2)
5109 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5110 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5111 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman8bd57102016-12-02 12:58:42 -08005112 self.waitUntilSettled()
5113
5114 build = self.builds[-1]
James E. Blair2a535672017-04-27 12:03:15 -07005115 path = os.path.join(build.jobdir.src_root, 'review.example.com',
5116 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08005117 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005118 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman8bd57102016-12-02 12:58:42 -08005119 repo_messages.reverse()
5120
Paul Belanger174a8272017-03-14 13:20:10 -04005121 self.executor_server.hold_jobs_in_build = False
5122 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08005123 self.waitUntilSettled()
5124
5125 return repo_messages
5126
5127 def _test_merge(self, mode):
5128 us_path = os.path.join(
5129 self.upstream_root, 'org/project-%s' % mode)
5130 expected_messages = [
5131 'initial commit',
5132 'add content from fixture',
5133 # the intermediate commits order is nondeterministic
5134 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
5135 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
5136 ]
5137 result = self._test_project_merge_mode(mode)
5138 self.assertEqual(result[:2], expected_messages[:2])
5139 self.assertEqual(result[-2:], expected_messages[-2:])
5140
5141 def test_project_merge_mode_merge(self):
5142 self._test_merge('merge')
5143
5144 def test_project_merge_mode_merge_resolve(self):
5145 self._test_merge('merge-resolve')
5146
5147 def test_project_merge_mode_cherrypick(self):
5148 expected_messages = [
5149 'initial commit',
5150 'add content from fixture',
5151 'A-1',
5152 'B-1',
5153 'C-1']
5154 result = self._test_project_merge_mode('cherry-pick')
5155 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08005156
5157 def test_merge_branch(self):
5158 "Test that the right commits are on alternate branches"
5159 self.create_branch('org/project-merge-branches', 'mp')
5160
Paul Belanger174a8272017-03-14 13:20:10 -04005161 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08005162 A = self.fake_gerrit.addFakeChange(
5163 'org/project-merge-branches', 'mp', 'A')
5164 B = self.fake_gerrit.addFakeChange(
5165 'org/project-merge-branches', 'mp', 'B')
5166 C = self.fake_gerrit.addFakeChange(
5167 'org/project-merge-branches', 'mp', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005168 A.addApproval('Code-Review', 2)
5169 B.addApproval('Code-Review', 2)
5170 C.addApproval('Code-Review', 2)
5171 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5172 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5173 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08005174 self.waitUntilSettled()
5175
Paul Belanger174a8272017-03-14 13:20:10 -04005176 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005177 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04005178 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005179 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04005180 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005181 self.waitUntilSettled()
5182
5183 build = self.builds[-1]
James E. Blair3b222492017-07-21 15:17:37 -07005184 self.assertEqual(build.parameters['zuul']['branch'], 'mp')
James E. Blair2a535672017-04-27 12:03:15 -07005185 path = os.path.join(build.jobdir.src_root, 'review.example.com',
5186 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005187 repo = git.Repo(path)
5188
James E. Blair247cab72017-07-20 16:52:36 -07005189 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005190 repo_messages.reverse()
5191 correct_messages = [
5192 'initial commit',
5193 'add content from fixture',
5194 'mp commit',
5195 'A-1', 'B-1', 'C-1']
5196 self.assertEqual(repo_messages, correct_messages)
5197
Paul Belanger174a8272017-03-14 13:20:10 -04005198 self.executor_server.hold_jobs_in_build = False
5199 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005200 self.waitUntilSettled()
5201
5202 def test_merge_multi_branch(self):
5203 "Test that dependent changes on multiple branches are merged"
5204 self.create_branch('org/project-merge-branches', 'mp')
5205
Paul Belanger174a8272017-03-14 13:20:10 -04005206 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08005207 A = self.fake_gerrit.addFakeChange(
5208 'org/project-merge-branches', 'master', 'A')
5209 B = self.fake_gerrit.addFakeChange(
5210 'org/project-merge-branches', 'mp', 'B')
5211 C = self.fake_gerrit.addFakeChange(
5212 'org/project-merge-branches', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005213 A.addApproval('Code-Review', 2)
5214 B.addApproval('Code-Review', 2)
5215 C.addApproval('Code-Review', 2)
5216 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5217 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5218 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08005219 self.waitUntilSettled()
5220
5221 job_A = None
5222 for job in self.builds:
5223 if 'project-merge' in job.name:
5224 job_A = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08005225
James E. Blair2a535672017-04-27 12:03:15 -07005226 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
5227 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005228 repo = git.Repo(path)
5229 repo_messages = [c.message.strip()
James E. Blair247cab72017-07-20 16:52:36 -07005230 for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005231 repo_messages.reverse()
5232 correct_messages = [
5233 'initial commit', 'add content from fixture', 'A-1']
5234 self.assertEqual(repo_messages, correct_messages)
5235
Paul Belanger174a8272017-03-14 13:20:10 -04005236 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005237 self.waitUntilSettled()
5238
5239 job_B = None
5240 for job in self.builds:
5241 if 'project-merge' in job.name:
5242 job_B = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08005243
James E. Blair2a535672017-04-27 12:03:15 -07005244 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
5245 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005246 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005247 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005248 repo_messages.reverse()
5249 correct_messages = [
5250 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
5251 self.assertEqual(repo_messages, correct_messages)
5252
Paul Belanger174a8272017-03-14 13:20:10 -04005253 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005254 self.waitUntilSettled()
5255
5256 job_C = None
5257 for job in self.builds:
5258 if 'project-merge' in job.name:
5259 job_C = job
James E. Blairb1345832017-07-21 14:51:35 -07005260
James E. Blair2a535672017-04-27 12:03:15 -07005261 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
5262 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005263 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005264 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005265
5266 repo_messages.reverse()
5267 correct_messages = [
5268 'initial commit', 'add content from fixture',
5269 'A-1', 'C-1']
5270 # Ensure the right commits are in the history for this ref
5271 self.assertEqual(repo_messages, correct_messages)
5272
Paul Belanger174a8272017-03-14 13:20:10 -04005273 self.executor_server.hold_jobs_in_build = False
5274 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005275 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005276
5277
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005278class TestSemaphore(ZuulTestCase):
5279 tenant_config_file = 'config/semaphore/main.yaml'
5280
5281 def test_semaphore_one(self):
5282 "Test semaphores with max=1 (mutex)"
5283 tenant = self.sched.abide.tenants.get('tenant-one')
5284
5285 self.executor_server.hold_jobs_in_build = True
5286
5287 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5288 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5289 self.assertFalse('test-semaphore' in
5290 tenant.semaphore_handler.semaphores)
5291
5292 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5293 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5294 self.waitUntilSettled()
5295
5296 self.assertEqual(len(self.builds), 3)
5297 self.assertEqual(self.builds[0].name, 'project-test1')
5298 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
5299 self.assertEqual(self.builds[2].name, 'project-test1')
5300
5301 self.executor_server.release('semaphore-one-test1')
5302 self.waitUntilSettled()
5303
5304 self.assertEqual(len(self.builds), 3)
5305 self.assertEqual(self.builds[0].name, 'project-test1')
5306 self.assertEqual(self.builds[1].name, 'project-test1')
5307 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5308 self.assertTrue('test-semaphore' in
5309 tenant.semaphore_handler.semaphores)
5310
5311 self.executor_server.release('semaphore-one-test2')
5312 self.waitUntilSettled()
5313
5314 self.assertEqual(len(self.builds), 3)
5315 self.assertEqual(self.builds[0].name, 'project-test1')
5316 self.assertEqual(self.builds[1].name, 'project-test1')
5317 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
5318 self.assertTrue('test-semaphore' in
5319 tenant.semaphore_handler.semaphores)
5320
5321 self.executor_server.release('semaphore-one-test1')
5322 self.waitUntilSettled()
5323
5324 self.assertEqual(len(self.builds), 3)
5325 self.assertEqual(self.builds[0].name, 'project-test1')
5326 self.assertEqual(self.builds[1].name, 'project-test1')
5327 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5328 self.assertTrue('test-semaphore' in
5329 tenant.semaphore_handler.semaphores)
5330
5331 self.executor_server.release('semaphore-one-test2')
5332 self.waitUntilSettled()
5333
5334 self.assertEqual(len(self.builds), 2)
5335 self.assertEqual(self.builds[0].name, 'project-test1')
5336 self.assertEqual(self.builds[1].name, 'project-test1')
5337 self.assertFalse('test-semaphore' in
5338 tenant.semaphore_handler.semaphores)
5339
5340 self.executor_server.hold_jobs_in_build = False
5341 self.executor_server.release()
5342
5343 self.waitUntilSettled()
5344 self.assertEqual(len(self.builds), 0)
5345
5346 self.assertEqual(A.reported, 1)
5347 self.assertEqual(B.reported, 1)
5348 self.assertFalse('test-semaphore' in
5349 tenant.semaphore_handler.semaphores)
5350
5351 def test_semaphore_two(self):
5352 "Test semaphores with max>1"
5353 tenant = self.sched.abide.tenants.get('tenant-one')
5354
5355 self.executor_server.hold_jobs_in_build = True
5356 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5357 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5358 self.assertFalse('test-semaphore-two' in
5359 tenant.semaphore_handler.semaphores)
5360
5361 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5362 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5363 self.waitUntilSettled()
5364
5365 self.assertEqual(len(self.builds), 4)
5366 self.assertEqual(self.builds[0].name, 'project-test1')
5367 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
5368 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5369 self.assertEqual(self.builds[3].name, 'project-test1')
5370 self.assertTrue('test-semaphore-two' in
5371 tenant.semaphore_handler.semaphores)
5372 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5373 'test-semaphore-two', [])), 2)
5374
5375 self.executor_server.release('semaphore-two-test1')
5376 self.waitUntilSettled()
5377
5378 self.assertEqual(len(self.builds), 4)
5379 self.assertEqual(self.builds[0].name, 'project-test1')
5380 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
5381 self.assertEqual(self.builds[2].name, 'project-test1')
5382 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
5383 self.assertTrue('test-semaphore-two' in
5384 tenant.semaphore_handler.semaphores)
5385 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5386 'test-semaphore-two', [])), 2)
5387
5388 self.executor_server.release('semaphore-two-test2')
5389 self.waitUntilSettled()
5390
5391 self.assertEqual(len(self.builds), 4)
5392 self.assertEqual(self.builds[0].name, 'project-test1')
5393 self.assertEqual(self.builds[1].name, 'project-test1')
5394 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5395 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
5396 self.assertTrue('test-semaphore-two' in
5397 tenant.semaphore_handler.semaphores)
5398 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5399 'test-semaphore-two', [])), 2)
5400
5401 self.executor_server.release('semaphore-two-test1')
5402 self.waitUntilSettled()
5403
5404 self.assertEqual(len(self.builds), 3)
5405 self.assertEqual(self.builds[0].name, 'project-test1')
5406 self.assertEqual(self.builds[1].name, 'project-test1')
5407 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5408 self.assertTrue('test-semaphore-two' in
5409 tenant.semaphore_handler.semaphores)
5410 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5411 'test-semaphore-two', [])), 1)
5412
5413 self.executor_server.release('semaphore-two-test2')
5414 self.waitUntilSettled()
5415
5416 self.assertEqual(len(self.builds), 2)
5417 self.assertEqual(self.builds[0].name, 'project-test1')
5418 self.assertEqual(self.builds[1].name, 'project-test1')
5419 self.assertFalse('test-semaphore-two' in
5420 tenant.semaphore_handler.semaphores)
5421
5422 self.executor_server.hold_jobs_in_build = False
5423 self.executor_server.release()
5424
5425 self.waitUntilSettled()
5426 self.assertEqual(len(self.builds), 0)
5427
5428 self.assertEqual(A.reported, 1)
5429 self.assertEqual(B.reported, 1)
5430
5431 def test_semaphore_abandon(self):
5432 "Test abandon with job semaphores"
5433 self.executor_server.hold_jobs_in_build = True
5434 tenant = self.sched.abide.tenants.get('tenant-one')
5435 check_pipeline = tenant.layout.pipelines['check']
5436
5437 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5438 self.assertFalse('test-semaphore' in
5439 tenant.semaphore_handler.semaphores)
5440
5441 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5442 self.waitUntilSettled()
5443
5444 self.assertTrue('test-semaphore' in
5445 tenant.semaphore_handler.semaphores)
5446
5447 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5448 self.waitUntilSettled()
5449
5450 # The check pipeline should be empty
5451 items = check_pipeline.getAllItems()
5452 self.assertEqual(len(items), 0)
5453
5454 # The semaphore should be released
5455 self.assertFalse('test-semaphore' in
5456 tenant.semaphore_handler.semaphores)
5457
5458 self.executor_server.hold_jobs_in_build = False
5459 self.executor_server.release()
5460 self.waitUntilSettled()
5461
Tobias Henkel0f714002017-06-30 23:30:52 +02005462 def test_semaphore_new_patchset(self):
5463 "Test new patchset with job semaphores"
5464 self.executor_server.hold_jobs_in_build = True
5465 tenant = self.sched.abide.tenants.get('tenant-one')
5466 check_pipeline = tenant.layout.pipelines['check']
5467
5468 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5469 self.assertFalse('test-semaphore' in
5470 tenant.semaphore_handler.semaphores)
5471
5472 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5473 self.waitUntilSettled()
5474
5475 self.assertTrue('test-semaphore' in
5476 tenant.semaphore_handler.semaphores)
5477 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5478 self.assertEqual(len(semaphore), 1)
5479
5480 A.addPatchset()
5481 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
5482 self.waitUntilSettled()
5483
5484 self.assertTrue('test-semaphore' in
5485 tenant.semaphore_handler.semaphores)
5486 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5487 self.assertEqual(len(semaphore), 1)
5488
5489 items = check_pipeline.getAllItems()
5490 self.assertEqual(items[0].change.number, '1')
5491 self.assertEqual(items[0].change.patchset, '2')
5492 self.assertTrue(items[0].live)
5493
5494 self.executor_server.hold_jobs_in_build = False
5495 self.executor_server.release()
5496 self.waitUntilSettled()
5497
5498 # The semaphore should be released
5499 self.assertFalse('test-semaphore' in
5500 tenant.semaphore_handler.semaphores)
5501
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005502 def test_semaphore_reconfigure(self):
5503 "Test reconfigure with job semaphores"
5504 self.executor_server.hold_jobs_in_build = True
5505 tenant = self.sched.abide.tenants.get('tenant-one')
5506 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5507 self.assertFalse('test-semaphore' in
5508 tenant.semaphore_handler.semaphores)
5509
5510 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5511 self.waitUntilSettled()
5512
5513 self.assertTrue('test-semaphore' in
5514 tenant.semaphore_handler.semaphores)
5515
5516 # reconfigure without layout change
5517 self.sched.reconfigure(self.config)
5518 self.waitUntilSettled()
5519 tenant = self.sched.abide.tenants.get('tenant-one')
5520
5521 # semaphore still must be held
5522 self.assertTrue('test-semaphore' in
5523 tenant.semaphore_handler.semaphores)
5524
5525 self.commitConfigUpdate(
5526 'common-config',
5527 'config/semaphore/zuul-reconfiguration.yaml')
5528 self.sched.reconfigure(self.config)
5529 self.waitUntilSettled()
5530 tenant = self.sched.abide.tenants.get('tenant-one')
5531
5532 self.executor_server.release('project-test1')
5533 self.waitUntilSettled()
5534
5535 # There should be no builds anymore
5536 self.assertEqual(len(self.builds), 0)
5537
5538 # The semaphore should be released
5539 self.assertFalse('test-semaphore' in
5540 tenant.semaphore_handler.semaphores)
5541
5542
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005543class TestSemaphoreMultiTenant(ZuulTestCase):
5544 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5545
5546 def test_semaphore_tenant_isolation(self):
5547 "Test semaphores in multiple tenants"
5548
5549 self.waitUntilSettled()
5550 tenant_one = self.sched.abide.tenants.get('tenant-one')
5551 tenant_two = self.sched.abide.tenants.get('tenant-two')
5552
5553 self.executor_server.hold_jobs_in_build = True
5554 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5555 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5556 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5557 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5558 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5559 self.assertFalse('test-semaphore' in
5560 tenant_one.semaphore_handler.semaphores)
5561 self.assertFalse('test-semaphore' in
5562 tenant_two.semaphore_handler.semaphores)
5563
5564 # add patches to project1 of tenant-one
5565 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5566 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5567 self.waitUntilSettled()
5568
5569 # one build of project1-test1 must run
5570 # semaphore of tenant-one must be acquired once
5571 # semaphore of tenant-two must not be acquired
5572 self.assertEqual(len(self.builds), 1)
5573 self.assertEqual(self.builds[0].name, 'project1-test1')
5574 self.assertTrue('test-semaphore' in
5575 tenant_one.semaphore_handler.semaphores)
5576 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5577 'test-semaphore', [])), 1)
5578 self.assertFalse('test-semaphore' in
5579 tenant_two.semaphore_handler.semaphores)
5580
5581 # add patches to project2 of tenant-two
5582 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5583 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5584 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5585 self.waitUntilSettled()
5586
5587 # one build of project1-test1 must run
5588 # two builds of project2-test1 must run
5589 # semaphore of tenant-one must be acquired once
5590 # semaphore of tenant-two must be acquired twice
5591 self.assertEqual(len(self.builds), 3)
5592 self.assertEqual(self.builds[0].name, 'project1-test1')
5593 self.assertEqual(self.builds[1].name, 'project2-test1')
5594 self.assertEqual(self.builds[2].name, 'project2-test1')
5595 self.assertTrue('test-semaphore' in
5596 tenant_one.semaphore_handler.semaphores)
5597 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5598 'test-semaphore', [])), 1)
5599 self.assertTrue('test-semaphore' in
5600 tenant_two.semaphore_handler.semaphores)
5601 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5602 'test-semaphore', [])), 2)
5603
5604 self.executor_server.release('project1-test1')
5605 self.waitUntilSettled()
5606
5607 # one build of project1-test1 must run
5608 # two builds of project2-test1 must run
5609 # semaphore of tenant-one must be acquired once
5610 # semaphore of tenant-two must be acquired twice
5611 self.assertEqual(len(self.builds), 3)
5612 self.assertEqual(self.builds[0].name, 'project2-test1')
5613 self.assertEqual(self.builds[1].name, 'project2-test1')
5614 self.assertEqual(self.builds[2].name, 'project1-test1')
5615 self.assertTrue('test-semaphore' in
5616 tenant_one.semaphore_handler.semaphores)
5617 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5618 'test-semaphore', [])), 1)
5619 self.assertTrue('test-semaphore' in
5620 tenant_two.semaphore_handler.semaphores)
5621 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5622 'test-semaphore', [])), 2)
5623
5624 self.executor_server.release('project2-test1')
5625 self.waitUntilSettled()
5626
5627 # one build of project1-test1 must run
5628 # one build of project2-test1 must run
5629 # semaphore of tenant-one must be acquired once
5630 # semaphore of tenant-two must be acquired once
5631 self.assertEqual(len(self.builds), 2)
5632 self.assertTrue('test-semaphore' in
5633 tenant_one.semaphore_handler.semaphores)
5634 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5635 'test-semaphore', [])), 1)
5636 self.assertTrue('test-semaphore' in
5637 tenant_two.semaphore_handler.semaphores)
5638 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5639 'test-semaphore', [])), 1)
5640
5641 self.executor_server.hold_jobs_in_build = False
5642 self.executor_server.release()
5643
5644 self.waitUntilSettled()
5645
5646 # no build must run
5647 # semaphore of tenant-one must not be acquired
5648 # semaphore of tenant-two must not be acquired
5649 self.assertEqual(len(self.builds), 0)
5650 self.assertFalse('test-semaphore' in
5651 tenant_one.semaphore_handler.semaphores)
5652 self.assertFalse('test-semaphore' in
5653 tenant_two.semaphore_handler.semaphores)
5654
5655 self.assertEqual(A.reported, 1)
5656 self.assertEqual(B.reported, 1)
5657
5658
5659class TestSemaphoreInRepo(ZuulTestCase):
Tobias Henkelabf973e2017-07-28 10:07:34 +02005660 config_file = 'zuul-connections-gerrit-and-github.conf'
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005661 tenant_config_file = 'config/in-repo/main.yaml'
5662
5663 def test_semaphore_in_repo(self):
5664 "Test semaphores in repo config"
5665
5666 # This tests dynamic semaphore handling in project repos. The semaphore
5667 # max value should not be evaluated dynamically but must be updated
5668 # after the change lands.
5669
5670 self.waitUntilSettled()
5671 tenant = self.sched.abide.tenants.get('tenant-one')
5672
5673 in_repo_conf = textwrap.dedent(
5674 """
5675 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +02005676 name: project-test1
5677
5678 - job:
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005679 name: project-test2
5680 semaphore: test-semaphore
5681
5682 - project:
5683 name: org/project
5684 tenant-one-gate:
5685 jobs:
5686 - project-test2
5687
5688 # the max value in dynamic layout must be ignored
5689 - semaphore:
5690 name: test-semaphore
5691 max: 2
5692 """)
5693
5694 in_repo_playbook = textwrap.dedent(
5695 """
5696 - hosts: all
5697 tasks: []
5698 """)
5699
5700 file_dict = {'.zuul.yaml': in_repo_conf,
5701 'playbooks/project-test2.yaml': in_repo_playbook}
5702 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5703 files=file_dict)
5704 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5705 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5706 B.setDependsOn(A, 1)
5707 C.setDependsOn(A, 1)
5708
5709 self.executor_server.hold_jobs_in_build = True
5710
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005711 A.addApproval('Code-Review', 2)
5712 B.addApproval('Code-Review', 2)
5713 C.addApproval('Code-Review', 2)
5714 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5715 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5716 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005717 self.waitUntilSettled()
5718
5719 # check that the layout in a queue item still has max value of 1
5720 # for test-semaphore
5721 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5722 queue = None
5723 for queue_candidate in pipeline.queues:
5724 if queue_candidate.name == 'org/project':
5725 queue = queue_candidate
5726 break
5727 queue_item = queue.queue[0]
James E. Blair29a24fd2017-10-02 15:04:56 -07005728 item_dynamic_layout = queue_item.layout
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005729 dynamic_test_semaphore = \
5730 item_dynamic_layout.semaphores.get('test-semaphore')
5731 self.assertEqual(dynamic_test_semaphore.max, 1)
5732
5733 # one build must be in queue, one semaphores acquired
5734 self.assertEqual(len(self.builds), 1)
5735 self.assertEqual(self.builds[0].name, 'project-test2')
5736 self.assertTrue('test-semaphore' in
5737 tenant.semaphore_handler.semaphores)
5738 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5739 'test-semaphore', [])), 1)
5740
5741 self.executor_server.release('project-test2')
5742 self.waitUntilSettled()
5743
5744 # change A must be merged
5745 self.assertEqual(A.data['status'], 'MERGED')
5746 self.assertEqual(A.reported, 2)
5747
5748 # send change-merged event as the gerrit mock doesn't send it
5749 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5750 self.waitUntilSettled()
5751
5752 # now that change A was merged, the new semaphore max must be effective
5753 tenant = self.sched.abide.tenants.get('tenant-one')
5754 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5755
5756 # two builds must be in queue, two semaphores acquired
5757 self.assertEqual(len(self.builds), 2)
5758 self.assertEqual(self.builds[0].name, 'project-test2')
5759 self.assertEqual(self.builds[1].name, 'project-test2')
5760 self.assertTrue('test-semaphore' in
5761 tenant.semaphore_handler.semaphores)
5762 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5763 'test-semaphore', [])), 2)
5764
5765 self.executor_server.release('project-test2')
5766 self.waitUntilSettled()
5767
5768 self.assertEqual(len(self.builds), 0)
5769 self.assertFalse('test-semaphore' in
5770 tenant.semaphore_handler.semaphores)
5771
5772 self.executor_server.hold_jobs_in_build = False
5773 self.executor_server.release()
5774
5775 self.waitUntilSettled()
5776 self.assertEqual(len(self.builds), 0)
5777
5778 self.assertEqual(A.reported, 2)
5779 self.assertEqual(B.reported, 2)
5780 self.assertEqual(C.reported, 2)
David Shrewsburyf6dc1762017-10-02 13:34:37 -04005781
5782
5783class TestSchedulerBranchMatcher(ZuulTestCase):
5784
5785 @simple_layout('layouts/matcher-test.yaml')
5786 def test_job_branch_ignored(self):
5787 '''
5788 Test that branch matching logic works.
5789
5790 The 'ignore-branch' job has a branch matcher that is supposed to
5791 match every branch except for the 'featureA' branch, so it should
5792 not be run on a change to that branch.
5793 '''
5794 self.create_branch('org/project', 'featureA')
5795 A = self.fake_gerrit.addFakeChange('org/project', 'featureA', 'A')
5796 A.addApproval('Code-Review', 2)
5797 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5798 self.waitUntilSettled()
5799 self.printHistory()
5800 self.assertEqual(self.getJobFromHistory('project-test1').result,
5801 'SUCCESS')
5802 self.assertJobNotInHistory('ignore-branch')
5803 self.assertEqual(A.data['status'], 'MERGED')
5804 self.assertEqual(A.reported, 2,
5805 "A should report start and success")
5806 self.assertIn('gate', A.messages[1],
5807 "A should transit gate")