blob: 32039605e8c93828a0ea0e1850a71b15b9cf0a43 [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. Blair412e5582013-04-22 15:50:12 -0700120
James E. Blair2ef29e92017-07-21 15:25:05 -0700121 for build in self.history:
122 self.assertTrue(build.parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -0700123
James E. Blair3cb10702013-08-24 08:56:03 -0700124 def test_initial_pipeline_gauges(self):
125 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -0700126 self.assertReportedStat('zuul.pipeline.gate.current_changes',
127 value='0|g')
128 self.assertReportedStat('zuul.pipeline.check.current_changes',
129 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -0700130
James E. Blair34776ee2016-08-25 13:53:54 -0700131 def test_job_branch(self):
132 "Test the correct variant of a job runs on a branch"
133 self.create_branch('org/project', 'stable')
134 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200135 A.addApproval('Code-Review', 2)
136 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair34776ee2016-08-25 13:53:54 -0700137 self.waitUntilSettled()
138 self.assertEqual(self.getJobFromHistory('project-test1').result,
139 'SUCCESS')
140 self.assertEqual(self.getJobFromHistory('project-test2').result,
141 'SUCCESS')
142 self.assertEqual(A.data['status'], 'MERGED')
143 self.assertEqual(A.reported, 2,
144 "A should report start and success")
145 self.assertIn('gate', A.messages[1],
146 "A should transit gate")
147 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -0700148 'label2')
James E. Blair34776ee2016-08-25 13:53:54 -0700149
James E. Blairb0fcae42012-07-17 11:12:10 -0700150 def test_parallel_changes(self):
151 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700152
Paul Belanger174a8272017-03-14 13:20:10 -0400153 self.executor_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700154 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
155 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
156 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200157 A.addApproval('Code-Review', 2)
158 B.addApproval('Code-Review', 2)
159 C.addApproval('Code-Review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700160
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200161 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
162 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
163 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700164
165 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400166 self.assertEqual(len(self.builds), 1)
167 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700168 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700169
Paul Belanger174a8272017-03-14 13:20:10 -0400170 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700171 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400172 self.assertEqual(len(self.builds), 3)
173 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700174 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400175 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700176 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400177 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700178 self.assertTrue(self.builds[2].hasChanges(A, B))
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), 5)
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))
James E. Blairb0fcae42012-07-17 11:12:10 -0700187
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400188 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700189 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400190 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700191 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700192
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400193 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700194 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700195
Paul Belanger174a8272017-03-14 13:20:10 -0400196 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700197 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400198 self.assertEqual(len(self.builds), 6)
199 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700200 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400201 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700202 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700203
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400204 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700205 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400206 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700207 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700208
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400209 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700210 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400211 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700212 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700213
Paul Belanger174a8272017-03-14 13:20:10 -0400214 self.executor_server.hold_jobs_in_build = False
215 self.executor_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700216 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400217 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700218
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400219 self.assertEqual(len(self.history), 9)
220 self.assertEqual(A.data['status'], 'MERGED')
221 self.assertEqual(B.data['status'], 'MERGED')
222 self.assertEqual(C.data['status'], 'MERGED')
223 self.assertEqual(A.reported, 2)
224 self.assertEqual(B.reported, 2)
225 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700226
227 def test_failed_changes(self):
228 "Test that a change behind a failed change is retested"
Paul Belanger174a8272017-03-14 13:20:10 -0400229 self.executor_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700230
James E. Blairb02a3bb2012-07-30 17:49:55 -0700231 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
232 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200233 A.addApproval('Code-Review', 2)
234 B.addApproval('Code-Review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700235
Paul Belanger174a8272017-03-14 13:20:10 -0400236 self.executor_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700237
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200238 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
239 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700240 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700241 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400242
Paul Belanger174a8272017-03-14 13:20:10 -0400243 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400244 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700245 # A/project-merge is complete
246 self.assertBuilds([
247 dict(name='project-test1', changes='1,1'),
248 dict(name='project-test2', changes='1,1'),
249 dict(name='project-merge', changes='1,1 2,1'),
250 ])
James E. Blaire2819012013-06-28 17:17:26 -0400251
Paul Belanger174a8272017-03-14 13:20:10 -0400252 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400253 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700254 # A/project-merge is complete
255 # B/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-test1', changes='1,1 2,1'),
260 dict(name='project-test2', changes='1,1 2,1'),
261 ])
262
263 # Release project-test1 for A which will fail. This will
Paul Belanger174a8272017-03-14 13:20:10 -0400264 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700265 self.builds[0].release()
266 self.waitUntilSettled()
267
268 self.orderedRelease()
269 self.assertHistory([
270 dict(name='project-merge', result='SUCCESS', changes='1,1'),
271 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
272 dict(name='project-test1', result='FAILURE', changes='1,1'),
273 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
274 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
275 dict(name='project-test2', result='SUCCESS', changes='1,1'),
276 dict(name='project-merge', result='SUCCESS', changes='2,1'),
277 dict(name='project-test1', result='SUCCESS', changes='2,1'),
278 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700279 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700280
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400281 self.assertEqual(A.data['status'], 'NEW')
282 self.assertEqual(B.data['status'], 'MERGED')
283 self.assertEqual(A.reported, 2)
284 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700285
286 def test_independent_queues(self):
287 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700288
Paul Belanger174a8272017-03-14 13:20:10 -0400289 self.executor_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900290 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700291 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
292 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200293 A.addApproval('Code-Review', 2)
294 B.addApproval('Code-Review', 2)
295 C.addApproval('Code-Review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700296
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200297 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800298 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700299
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200300 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800301 self.waitUntilSettled()
302
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200303 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700304 self.waitUntilSettled()
305
306 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700307 self.assertBuilds([
308 dict(name='project-merge', changes='1,1'),
309 dict(name='project-merge', changes='2,1'),
310 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700311
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700312 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700313 self.builds[0].release()
314 self.waitUntilSettled()
315 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700316 self.waitUntilSettled()
317 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400318 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700319 self.waitUntilSettled()
320
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700321 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700322 self.assertBuilds([
323 dict(name='project-test1', changes='1,1'),
324 dict(name='project-test2', changes='1,1'),
325 dict(name='project-test1', changes='2,1'),
326 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500327 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700328 dict(name='project-test1', changes='2,1 3,1'),
329 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800330 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700331 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700332
James E. Blair5f0d49e2016-08-19 09:36:50 -0700333 self.orderedRelease()
334 self.assertHistory([
335 dict(name='project-merge', result='SUCCESS', changes='1,1'),
336 dict(name='project-merge', result='SUCCESS', changes='2,1'),
337 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
338 dict(name='project-test1', result='SUCCESS', changes='1,1'),
339 dict(name='project-test2', result='SUCCESS', changes='1,1'),
340 dict(name='project-test1', result='SUCCESS', changes='2,1'),
341 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500342 dict(
343 name='project1-project2-integration',
344 result='SUCCESS',
345 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700346 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
347 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800348 dict(name='project1-project2-integration',
349 result='SUCCESS',
350 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700351 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700352
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400353 self.assertEqual(A.data['status'], 'MERGED')
354 self.assertEqual(B.data['status'], 'MERGED')
355 self.assertEqual(C.data['status'], 'MERGED')
356 self.assertEqual(A.reported, 2)
357 self.assertEqual(B.reported, 2)
358 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700359
360 def test_failed_change_at_head(self):
361 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700362
Paul Belanger174a8272017-03-14 13:20:10 -0400363 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700364 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
365 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
366 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200367 A.addApproval('Code-Review', 2)
368 B.addApproval('Code-Review', 2)
369 C.addApproval('Code-Review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700370
Paul Belanger174a8272017-03-14 13:20:10 -0400371 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700372
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200373 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
374 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
375 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700376
377 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700378
James E. Blairb536ecc2016-08-31 10:11:42 -0700379 self.assertBuilds([
380 dict(name='project-merge', changes='1,1'),
381 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700382
Paul Belanger174a8272017-03-14 13:20:10 -0400383 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700384 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400385 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700386 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400387 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700388 self.waitUntilSettled()
389
James E. Blairb536ecc2016-08-31 10:11:42 -0700390 self.assertBuilds([
391 dict(name='project-test1', changes='1,1'),
392 dict(name='project-test2', changes='1,1'),
393 dict(name='project-test1', changes='1,1 2,1'),
394 dict(name='project-test2', changes='1,1 2,1'),
395 dict(name='project-test1', changes='1,1 2,1 3,1'),
396 dict(name='project-test2', changes='1,1 2,1 3,1'),
397 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700398
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400399 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700400 self.waitUntilSettled()
401
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400402 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700403 self.assertBuilds([
404 dict(name='project-test2', changes='1,1'),
405 dict(name='project-merge', changes='2,1'),
406 ])
407 # Unordered history comparison because the aborts can finish
408 # in any order.
409 self.assertHistory([
410 dict(name='project-merge', result='SUCCESS',
411 changes='1,1'),
412 dict(name='project-merge', result='SUCCESS',
413 changes='1,1 2,1'),
414 dict(name='project-merge', result='SUCCESS',
415 changes='1,1 2,1 3,1'),
416 dict(name='project-test1', result='FAILURE',
417 changes='1,1'),
418 dict(name='project-test1', result='ABORTED',
419 changes='1,1 2,1'),
420 dict(name='project-test2', result='ABORTED',
421 changes='1,1 2,1'),
422 dict(name='project-test1', result='ABORTED',
423 changes='1,1 2,1 3,1'),
424 dict(name='project-test2', result='ABORTED',
425 changes='1,1 2,1 3,1'),
426 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700427
Paul Belanger174a8272017-03-14 13:20:10 -0400428 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700429 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400430 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700431 self.waitUntilSettled()
432 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700433
James E. Blairb536ecc2016-08-31 10:11:42 -0700434 self.assertBuilds([])
435 self.assertHistory([
436 dict(name='project-merge', result='SUCCESS',
437 changes='1,1'),
438 dict(name='project-merge', result='SUCCESS',
439 changes='1,1 2,1'),
440 dict(name='project-merge', result='SUCCESS',
441 changes='1,1 2,1 3,1'),
442 dict(name='project-test1', result='FAILURE',
443 changes='1,1'),
444 dict(name='project-test1', result='ABORTED',
445 changes='1,1 2,1'),
446 dict(name='project-test2', result='ABORTED',
447 changes='1,1 2,1'),
448 dict(name='project-test1', result='ABORTED',
449 changes='1,1 2,1 3,1'),
450 dict(name='project-test2', result='ABORTED',
451 changes='1,1 2,1 3,1'),
452 dict(name='project-merge', result='SUCCESS',
453 changes='2,1'),
454 dict(name='project-merge', result='SUCCESS',
455 changes='2,1 3,1'),
456 dict(name='project-test2', result='SUCCESS',
457 changes='1,1'),
458 dict(name='project-test1', result='SUCCESS',
459 changes='2,1'),
460 dict(name='project-test2', result='SUCCESS',
461 changes='2,1'),
462 dict(name='project-test1', result='SUCCESS',
463 changes='2,1 3,1'),
464 dict(name='project-test2', result='SUCCESS',
465 changes='2,1 3,1'),
466 ], ordered=False)
467
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400468 self.assertEqual(A.data['status'], 'NEW')
469 self.assertEqual(B.data['status'], 'MERGED')
470 self.assertEqual(C.data['status'], 'MERGED')
471 self.assertEqual(A.reported, 2)
472 self.assertEqual(B.reported, 2)
473 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700474
James E. Blair0aac4872013-08-23 14:02:38 -0700475 def test_failed_change_in_middle(self):
476 "Test a failed change in the middle of the queue"
477
Paul Belanger174a8272017-03-14 13:20:10 -0400478 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700479 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
480 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
481 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200482 A.addApproval('Code-Review', 2)
483 B.addApproval('Code-Review', 2)
484 C.addApproval('Code-Review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700485
Paul Belanger174a8272017-03-14 13:20:10 -0400486 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700487
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200488 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
489 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
490 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700491
492 self.waitUntilSettled()
493
Paul Belanger174a8272017-03-14 13:20:10 -0400494 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700495 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400496 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700497 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400498 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700499 self.waitUntilSettled()
500
501 self.assertEqual(len(self.builds), 6)
502 self.assertEqual(self.builds[0].name, 'project-test1')
503 self.assertEqual(self.builds[1].name, 'project-test2')
504 self.assertEqual(self.builds[2].name, 'project-test1')
505 self.assertEqual(self.builds[3].name, 'project-test2')
506 self.assertEqual(self.builds[4].name, 'project-test1')
507 self.assertEqual(self.builds[5].name, 'project-test2')
508
509 self.release(self.builds[2])
510 self.waitUntilSettled()
511
James E. Blair972e3c72013-08-29 12:04:55 -0700512 # project-test1 and project-test2 for A
513 # project-test2 for B
514 # project-merge for C (without B)
515 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700516 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
517
Paul Belanger174a8272017-03-14 13:20:10 -0400518 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700519 self.waitUntilSettled()
520
521 # project-test1 and project-test2 for A
522 # project-test2 for B
523 # project-test1 and project-test2 for C
524 self.assertEqual(len(self.builds), 5)
525
Paul Belanger2e2a0492016-10-30 18:33:37 -0400526 tenant = self.sched.abide.tenants.get('tenant-one')
527 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700528 builds = items[0].current_build_set.getBuilds()
529 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
530 self.assertEqual(self.countJobResults(builds, None), 2)
531 builds = items[1].current_build_set.getBuilds()
532 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
533 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
534 self.assertEqual(self.countJobResults(builds, None), 1)
535 builds = items[2].current_build_set.getBuilds()
536 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700537 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700538
Paul Belanger174a8272017-03-14 13:20:10 -0400539 self.executor_server.hold_jobs_in_build = False
540 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700541 self.waitUntilSettled()
542
543 self.assertEqual(len(self.builds), 0)
544 self.assertEqual(len(self.history), 12)
545 self.assertEqual(A.data['status'], 'MERGED')
546 self.assertEqual(B.data['status'], 'NEW')
547 self.assertEqual(C.data['status'], 'MERGED')
548 self.assertEqual(A.reported, 2)
549 self.assertEqual(B.reported, 2)
550 self.assertEqual(C.reported, 2)
551
James E. Blaird466dc42012-07-31 10:42:56 -0700552 def test_failed_change_at_head_with_queue(self):
553 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700554
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700555 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700556 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
557 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
558 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200559 A.addApproval('Code-Review', 2)
560 B.addApproval('Code-Review', 2)
561 C.addApproval('Code-Review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700562
Paul Belanger174a8272017-03-14 13:20:10 -0400563 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700564
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200565 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
566 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
567 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700568
569 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700570 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400571 self.assertEqual(len(self.builds), 0)
572 self.assertEqual(len(queue), 1)
Clint Byrumf322fe22017-05-10 20:53:12 -0700573 self.assertEqual(queue[0].name, b'executor:execute')
574 job_args = json.loads(queue[0].arguments.decode('utf8'))
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800575 self.assertEqual(job_args['job'], 'project-merge')
576 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700577
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700578 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700579 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700580 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700581 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700582 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700583 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700584 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700585
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400586 self.assertEqual(len(self.builds), 0)
587 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800588
589 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700590 json.loads(queue[0].arguments.decode('utf8'))['job'],
591 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800592 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700593 json.loads(queue[1].arguments.decode('utf8'))['job'],
594 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800595 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700596 json.loads(queue[2].arguments.decode('utf8'))['job'],
597 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800598 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700599 json.loads(queue[3].arguments.decode('utf8'))['job'],
600 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800601 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700602 json.loads(queue[4].arguments.decode('utf8'))['job'],
603 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800604 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700605 json.loads(queue[5].arguments.decode('utf8'))['job'],
606 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700607
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700608 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700609 self.waitUntilSettled()
610
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400611 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700612 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400613 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
614 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700615
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700616 self.gearman_server.hold_jobs_in_queue = False
617 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700618 self.waitUntilSettled()
619
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400620 self.assertEqual(len(self.builds), 0)
621 self.assertEqual(len(self.history), 11)
622 self.assertEqual(A.data['status'], 'NEW')
623 self.assertEqual(B.data['status'], 'MERGED')
624 self.assertEqual(C.data['status'], 'MERGED')
625 self.assertEqual(A.reported, 2)
626 self.assertEqual(B.reported, 2)
627 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700628
James E. Blairce8a2132016-05-19 15:21:52 -0700629 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400630 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700631 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200632 A.addApproval('Code-Review', 2)
633 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700634 self.waitUntilSettled()
635 time.sleep(2)
636
James E. Blairae0f23c2017-09-13 10:55:15 -0600637 data = json.loads(self.sched.formatStatusJSON('tenant-one'))
James E. Blairce8a2132016-05-19 15:21:52 -0700638 found_job = None
639 for pipeline in data['pipelines']:
640 if pipeline['name'] != 'gate':
641 continue
642 for queue in pipeline['change_queues']:
643 for head in queue['heads']:
644 for item in head:
645 for job in item['jobs']:
646 if job['name'] == 'project-merge':
647 found_job = job
648 break
649
650 self.assertIsNotNone(found_job)
651 if iteration == 1:
652 self.assertIsNotNone(found_job['estimated_time'])
653 self.assertIsNone(found_job['remaining_time'])
654 else:
655 self.assertIsNotNone(found_job['estimated_time'])
656 self.assertTrue(found_job['estimated_time'] >= 2)
657 self.assertIsNotNone(found_job['remaining_time'])
658
Paul Belanger174a8272017-03-14 13:20:10 -0400659 self.executor_server.hold_jobs_in_build = False
660 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700661 self.waitUntilSettled()
662
663 def test_time_database(self):
664 "Test the time database"
665
666 self._test_time_database(1)
667 self._test_time_database(2)
668
James E. Blairfef71632013-09-23 11:15:47 -0700669 def test_two_failed_changes_at_head(self):
670 "Test that changes are reparented correctly if 2 fail at head"
671
Paul Belanger174a8272017-03-14 13:20:10 -0400672 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700673 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
674 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
675 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200676 A.addApproval('Code-Review', 2)
677 B.addApproval('Code-Review', 2)
678 C.addApproval('Code-Review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700679
Paul Belanger174a8272017-03-14 13:20:10 -0400680 self.executor_server.failJob('project-test1', A)
681 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700682
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200683 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
684 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
685 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700686 self.waitUntilSettled()
687
Paul Belanger174a8272017-03-14 13:20:10 -0400688 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700689 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400690 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700691 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400692 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700693 self.waitUntilSettled()
694
695 self.assertEqual(len(self.builds), 6)
696 self.assertEqual(self.builds[0].name, 'project-test1')
697 self.assertEqual(self.builds[1].name, 'project-test2')
698 self.assertEqual(self.builds[2].name, 'project-test1')
699 self.assertEqual(self.builds[3].name, 'project-test2')
700 self.assertEqual(self.builds[4].name, 'project-test1')
701 self.assertEqual(self.builds[5].name, 'project-test2')
702
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400703 self.assertTrue(self.builds[0].hasChanges(A))
704 self.assertTrue(self.builds[2].hasChanges(A))
705 self.assertTrue(self.builds[2].hasChanges(B))
706 self.assertTrue(self.builds[4].hasChanges(A))
707 self.assertTrue(self.builds[4].hasChanges(B))
708 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700709
710 # Fail change B first
711 self.release(self.builds[2])
712 self.waitUntilSettled()
713
714 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400715 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700716 self.waitUntilSettled()
717
718 self.assertEqual(len(self.builds), 5)
719 self.assertEqual(self.builds[0].name, 'project-test1')
720 self.assertEqual(self.builds[1].name, 'project-test2')
721 self.assertEqual(self.builds[2].name, 'project-test2')
722 self.assertEqual(self.builds[3].name, 'project-test1')
723 self.assertEqual(self.builds[4].name, 'project-test2')
724
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400725 self.assertTrue(self.builds[1].hasChanges(A))
726 self.assertTrue(self.builds[2].hasChanges(A))
727 self.assertTrue(self.builds[2].hasChanges(B))
728 self.assertTrue(self.builds[4].hasChanges(A))
729 self.assertFalse(self.builds[4].hasChanges(B))
730 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700731
732 # Finish running all passing jobs for change A
733 self.release(self.builds[1])
734 self.waitUntilSettled()
735 # Fail and report change A
736 self.release(self.builds[0])
737 self.waitUntilSettled()
738
739 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400740 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700741 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400742 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700743 self.waitUntilSettled()
744
745 self.assertEqual(len(self.builds), 4)
746 self.assertEqual(self.builds[0].name, 'project-test1') # B
747 self.assertEqual(self.builds[1].name, 'project-test2') # B
748 self.assertEqual(self.builds[2].name, 'project-test1') # C
749 self.assertEqual(self.builds[3].name, 'project-test2') # C
750
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400751 self.assertFalse(self.builds[1].hasChanges(A))
752 self.assertTrue(self.builds[1].hasChanges(B))
753 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700754
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400755 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700756 # After A failed and B and C restarted, B should be back in
757 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400758 self.assertTrue(self.builds[2].hasChanges(B))
759 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700760
Paul Belanger174a8272017-03-14 13:20:10 -0400761 self.executor_server.hold_jobs_in_build = False
762 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700763 self.waitUntilSettled()
764
765 self.assertEqual(len(self.builds), 0)
766 self.assertEqual(len(self.history), 21)
767 self.assertEqual(A.data['status'], 'NEW')
768 self.assertEqual(B.data['status'], 'NEW')
769 self.assertEqual(C.data['status'], 'MERGED')
770 self.assertEqual(A.reported, 2)
771 self.assertEqual(B.reported, 2)
772 self.assertEqual(C.reported, 2)
773
James E. Blair8c803f82012-07-31 16:25:42 -0700774 def test_patch_order(self):
775 "Test that dependent patches are tested in the right order"
776 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
777 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
778 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200779 A.addApproval('Code-Review', 2)
780 B.addApproval('Code-Review', 2)
781 C.addApproval('Code-Review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700782
783 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
784 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
785 M2.setMerged()
786 M1.setMerged()
787
788 # C -> B -> A -> M1 -> M2
789 # M2 is here to make sure it is never queried. If it is, it
790 # means zuul is walking down the entire history of merged
791 # changes.
792
793 C.setDependsOn(B, 1)
794 B.setDependsOn(A, 1)
795 A.setDependsOn(M1, 1)
796 M1.setDependsOn(M2, 1)
797
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200798 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700799
800 self.waitUntilSettled()
801
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400802 self.assertEqual(A.data['status'], 'NEW')
803 self.assertEqual(B.data['status'], 'NEW')
804 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700805
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200806 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
807 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700808
809 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400810 self.assertEqual(M2.queried, 0)
811 self.assertEqual(A.data['status'], 'MERGED')
812 self.assertEqual(B.data['status'], 'MERGED')
813 self.assertEqual(C.data['status'], 'MERGED')
814 self.assertEqual(A.reported, 2)
815 self.assertEqual(B.reported, 2)
816 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700817
James E. Blair063672f2015-01-29 13:09:12 -0800818 def test_needed_changes_enqueue(self):
819 "Test that a needed change is enqueued ahead"
820 # A Given a git tree like this, if we enqueue
821 # / \ change C, we should walk up and down the tree
822 # B G and enqueue changes in the order ABCDEFG.
823 # /|\ This is also the order that you would get if
824 # *C E F you enqueued changes in the order ABCDEFG, so
825 # / the ordering is stable across re-enqueue events.
826 # D
827
828 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
829 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
830 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
831 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
832 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
833 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
834 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
835 B.setDependsOn(A, 1)
836 C.setDependsOn(B, 1)
837 D.setDependsOn(C, 1)
838 E.setDependsOn(B, 1)
839 F.setDependsOn(B, 1)
840 G.setDependsOn(A, 1)
841
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200842 A.addApproval('Code-Review', 2)
843 B.addApproval('Code-Review', 2)
844 C.addApproval('Code-Review', 2)
845 D.addApproval('Code-Review', 2)
846 E.addApproval('Code-Review', 2)
847 F.addApproval('Code-Review', 2)
848 G.addApproval('Code-Review', 2)
849 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800850
851 self.waitUntilSettled()
852
853 self.assertEqual(A.data['status'], 'NEW')
854 self.assertEqual(B.data['status'], 'NEW')
855 self.assertEqual(C.data['status'], 'NEW')
856 self.assertEqual(D.data['status'], 'NEW')
857 self.assertEqual(E.data['status'], 'NEW')
858 self.assertEqual(F.data['status'], 'NEW')
859 self.assertEqual(G.data['status'], 'NEW')
860
861 # We're about to add approvals to changes without adding the
862 # triggering events to Zuul, so that we can be sure that it is
863 # enqueing the changes based on dependencies, not because of
864 # triggering events. Since it will have the changes cached
865 # already (without approvals), we need to clear the cache
866 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400867 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100868 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800869
Paul Belanger174a8272017-03-14 13:20:10 -0400870 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200871 A.addApproval('Approved', 1)
872 B.addApproval('Approved', 1)
873 D.addApproval('Approved', 1)
874 E.addApproval('Approved', 1)
875 F.addApproval('Approved', 1)
876 G.addApproval('Approved', 1)
877 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800878
879 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400880 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800881 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400882 self.executor_server.hold_jobs_in_build = False
883 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800884 self.waitUntilSettled()
885
886 self.assertEqual(A.data['status'], 'MERGED')
887 self.assertEqual(B.data['status'], 'MERGED')
888 self.assertEqual(C.data['status'], 'MERGED')
889 self.assertEqual(D.data['status'], 'MERGED')
890 self.assertEqual(E.data['status'], 'MERGED')
891 self.assertEqual(F.data['status'], 'MERGED')
892 self.assertEqual(G.data['status'], 'MERGED')
893 self.assertEqual(A.reported, 2)
894 self.assertEqual(B.reported, 2)
895 self.assertEqual(C.reported, 2)
896 self.assertEqual(D.reported, 2)
897 self.assertEqual(E.reported, 2)
898 self.assertEqual(F.reported, 2)
899 self.assertEqual(G.reported, 2)
900 self.assertEqual(self.history[6].changes,
901 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
902
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100903 def test_source_cache(self):
904 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400905 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700906
907 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
908 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
909 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200910 A.addApproval('Code-Review', 2)
911 B.addApproval('Code-Review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700912
913 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
914 M1.setMerged()
915
916 B.setDependsOn(A, 1)
917 A.setDependsOn(M1, 1)
918
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200919 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700920 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
921
922 self.waitUntilSettled()
923
924 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700925 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700926 build.release()
927 self.waitUntilSettled()
928 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700929 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700930 build.release()
931 self.waitUntilSettled()
932
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200933 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700934 self.waitUntilSettled()
935
Joshua Hesketh352264b2015-08-11 23:42:08 +1000936 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700937 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000938 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700939
Paul Belanger174a8272017-03-14 13:20:10 -0400940 self.executor_server.hold_jobs_in_build = False
941 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700942 self.waitUntilSettled()
943
944 self.assertEqual(A.data['status'], 'MERGED')
945 self.assertEqual(B.data['status'], 'MERGED')
946 self.assertEqual(A.queried, 2) # Initial and isMerged
947 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
948
James E. Blair8c803f82012-07-31 16:25:42 -0700949 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700950 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700951 # TODO: move to test_gerrit (this is a unit test!)
952 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400953 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -0700954 (trusted, project) = tenant.getProject('org/project')
955 source = project.source
Paul Belanger9bba4902016-11-02 16:07:33 -0400956
957 # TODO(pabelanger): As we add more source / trigger APIs we should make
958 # it easier for users to create events for testing.
959 event = zuul.model.TriggerEvent()
960 event.trigger_name = 'gerrit'
961 event.change_number = '1'
962 event.patch_number = '2'
963
964 a = source.getChange(event)
965 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700966 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700967
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200968 A.addApproval('Code-Review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400969 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700970 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700971
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200972 A.addApproval('Approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400973 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700974 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700975
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700976 def test_project_merge_conflict(self):
977 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700978
979 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700980 A = self.fake_gerrit.addFakeChange('org/project',
981 'master', 'A',
982 files={'conflict': 'foo'})
983 B = self.fake_gerrit.addFakeChange('org/project',
984 'master', 'B',
985 files={'conflict': 'bar'})
986 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -0700987 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200988 A.addApproval('Code-Review', 2)
989 B.addApproval('Code-Review', 2)
990 C.addApproval('Code-Review', 2)
991 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
992 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
993 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700994 self.waitUntilSettled()
995
James E. Blair6736beb2013-07-11 15:18:15 -0700996 self.assertEqual(A.reported, 1)
James E. Blair6736beb2013-07-11 15:18:15 -0700997 self.assertEqual(C.reported, 1)
998
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700999 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001000 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001001 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001002 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001003 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001004 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001005
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001006 self.gearman_server.hold_jobs_in_queue = False
1007 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001008 self.waitUntilSettled()
1009
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001010 self.assertEqual(A.data['status'], 'MERGED')
1011 self.assertEqual(B.data['status'], 'NEW')
1012 self.assertEqual(C.data['status'], 'MERGED')
1013 self.assertEqual(A.reported, 2)
Tobias Henkel9842bd72017-05-16 13:40:03 +02001014 self.assertIn('Merge Failed', B.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001015 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001016
1017 self.assertHistory([
1018 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1019 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1020 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1021 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
1022 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
1023 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
1024 ], ordered=False)
1025
1026 def test_delayed_merge_conflict(self):
1027 "Test that delayed check merge conflicts are handled properly"
1028
1029 # Hold jobs in the gearman queue so that we can test whether
James E. Blair1960d682017-04-28 15:44:14 -07001030 # the executor sucesfully merges a change based on an old
1031 # repo state (frozen by the scheduler) which would otherwise
1032 # conflict.
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001033 self.gearman_server.hold_jobs_in_queue = True
1034 A = self.fake_gerrit.addFakeChange('org/project',
1035 'master', 'A',
1036 files={'conflict': 'foo'})
1037 B = self.fake_gerrit.addFakeChange('org/project',
1038 'master', 'B',
1039 files={'conflict': 'bar'})
1040 C = self.fake_gerrit.addFakeChange('org/project',
1041 'master', 'C')
1042 C.setDependsOn(B, 1)
1043
1044 # A enters the gate queue; B and C enter the check queue
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001045 A.addApproval('Code-Review', 2)
1046 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001047 self.waitUntilSettled()
1048 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
James E. Blair8d144dc2017-05-05 10:13:45 -07001049 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001050 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1051 self.waitUntilSettled()
1052
1053 self.assertEqual(A.reported, 1)
1054 self.assertEqual(B.reported, 0) # Check does not report start
1055 self.assertEqual(C.reported, 0) # Check does not report start
1056
1057 # A merges while B and C are queued in check
1058 # Release A project-merge
1059 queue = self.gearman_server.getQueue()
1060 self.release(queue[0])
1061 self.waitUntilSettled()
1062
1063 # Release A project-test*
1064 # gate has higher precedence, so A's test jobs are added in
1065 # front of the merge jobs for B and C
1066 queue = self.gearman_server.getQueue()
1067 self.release(queue[0])
1068 self.release(queue[1])
1069 self.waitUntilSettled()
1070
1071 self.assertEqual(A.data['status'], 'MERGED')
1072 self.assertEqual(B.data['status'], 'NEW')
1073 self.assertEqual(C.data['status'], 'NEW')
1074 self.assertEqual(A.reported, 2)
1075 self.assertEqual(B.reported, 0)
1076 self.assertEqual(C.reported, 0)
1077 self.assertHistory([
1078 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1079 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1080 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1081 ], ordered=False)
1082
1083 # B and C report merge conflicts
1084 # Release B project-merge
1085 queue = self.gearman_server.getQueue()
1086 self.release(queue[0])
1087 self.waitUntilSettled()
1088
1089 # Release C
1090 self.gearman_server.hold_jobs_in_queue = False
1091 self.gearman_server.release()
1092 self.waitUntilSettled()
1093
1094 self.assertEqual(A.data['status'], 'MERGED')
1095 self.assertEqual(B.data['status'], 'NEW')
1096 self.assertEqual(C.data['status'], 'NEW')
1097 self.assertEqual(A.reported, 2)
1098 self.assertEqual(B.reported, 1)
1099 self.assertEqual(C.reported, 1)
1100
1101 self.assertHistory([
1102 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1103 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1104 dict(name='project-test2', result='SUCCESS', changes='1,1'),
James E. Blair1960d682017-04-28 15:44:14 -07001105 dict(name='project-merge', result='SUCCESS', changes='2,1'),
1106 dict(name='project-test1', result='SUCCESS', changes='2,1'),
1107 dict(name='project-test2', result='SUCCESS', changes='2,1'),
1108 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
1109 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
1110 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001111 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001112
James E. Blairdaabed22012-08-15 15:38:57 -07001113 def test_post(self):
1114 "Test that post jobs run"
James E. Blair289f5932017-07-27 15:02:29 -07001115 p = "review.example.com/org/project"
1116 upstream = self.getUpstreamRepos([p])
1117 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1118 A.setMerged()
1119 A_commit = str(upstream[p].commit('master'))
1120 self.log.debug("A commit: %s" % A_commit)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001121
Zhongyue Luo5d556072012-09-21 02:00:47 +09001122 e = {
1123 "type": "ref-updated",
1124 "submitter": {
1125 "name": "User Name",
1126 },
1127 "refUpdate": {
1128 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
James E. Blair289f5932017-07-27 15:02:29 -07001129 "newRev": A_commit,
Zhongyue Luo5d556072012-09-21 02:00:47 +09001130 "refName": "master",
1131 "project": "org/project",
1132 }
1133 }
James E. Blairdaabed22012-08-15 15:38:57 -07001134 self.fake_gerrit.addEvent(e)
1135 self.waitUntilSettled()
1136
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001137 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001138 self.assertEqual(len(self.history), 1)
1139 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001140
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001141 def test_post_ignore_deletes(self):
1142 "Test that deleting refs does not trigger post jobs"
1143
1144 e = {
1145 "type": "ref-updated",
1146 "submitter": {
1147 "name": "User Name",
1148 },
1149 "refUpdate": {
1150 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1151 "newRev": "0000000000000000000000000000000000000000",
1152 "refName": "master",
1153 "project": "org/project",
1154 }
1155 }
1156 self.fake_gerrit.addEvent(e)
1157 self.waitUntilSettled()
1158
1159 job_names = [x.name for x in self.history]
1160 self.assertEqual(len(self.history), 0)
1161 self.assertNotIn('project-post', job_names)
1162
James E. Blaircc324192017-04-19 14:36:08 -07001163 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001164 def test_post_ignore_deletes_negative(self):
1165 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001166 e = {
1167 "type": "ref-updated",
1168 "submitter": {
1169 "name": "User Name",
1170 },
1171 "refUpdate": {
1172 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1173 "newRev": "0000000000000000000000000000000000000000",
James E. Blair289f5932017-07-27 15:02:29 -07001174 "refName": "testbranch",
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001175 "project": "org/project",
1176 }
1177 }
1178 self.fake_gerrit.addEvent(e)
1179 self.waitUntilSettled()
1180
1181 job_names = [x.name for x in self.history]
1182 self.assertEqual(len(self.history), 1)
1183 self.assertIn('project-post', job_names)
1184
James E. Blairec056492016-07-22 09:45:56 -07001185 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001186 def test_build_configuration_branch_interaction(self):
1187 "Test that switching between branches works"
1188 self.test_build_configuration()
1189 self.test_build_configuration_branch()
1190 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001191 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001192 repo = git.Repo(path)
1193 repo.heads.master.commit = repo.commit('init')
1194 self.test_build_configuration()
1195
James E. Blairdb93b302017-07-19 15:33:11 -07001196 def test_dependent_changes_rebase(self):
1197 # Test that no errors occur when we walk a dependency tree
1198 # with an unused leaf node due to a rebase.
1199 # Start by constructing: C -> B -> A
1200 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1201 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1202 B.setDependsOn(A, 1)
1203
1204 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1205 C.setDependsOn(B, 1)
1206
1207 # Then rebase to form: D -> C -> A
1208 C.addPatchset() # C,2
1209 C.setDependsOn(A, 1)
1210
1211 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1212 D.setDependsOn(C, 2)
1213
1214 # Walk the entire tree
1215 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1216 self.waitUntilSettled()
1217 self.assertEqual(len(self.history), 3)
1218
1219 # Verify that walking just part of the tree still works
1220 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
1221 self.waitUntilSettled()
1222 self.assertEqual(len(self.history), 6)
1223
James E. Blaircaec0c52012-08-22 14:52:22 -07001224 def test_dependent_changes_dequeue(self):
1225 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001226
James E. Blaircaec0c52012-08-22 14:52:22 -07001227 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1228 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1229 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001230 A.addApproval('Code-Review', 2)
1231 B.addApproval('Code-Review', 2)
1232 C.addApproval('Code-Review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001233
1234 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1235 M1.setMerged()
1236
1237 # C -> B -> A -> M1
1238
1239 C.setDependsOn(B, 1)
1240 B.setDependsOn(A, 1)
1241 A.setDependsOn(M1, 1)
1242
Paul Belanger174a8272017-03-14 13:20:10 -04001243 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001244
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001245 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1246 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1247 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001248
1249 self.waitUntilSettled()
1250
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001251 self.assertEqual(A.data['status'], 'NEW')
1252 self.assertEqual(A.reported, 2)
1253 self.assertEqual(B.data['status'], 'NEW')
1254 self.assertEqual(B.reported, 2)
1255 self.assertEqual(C.data['status'], 'NEW')
James E. Blair0d3e83b2017-06-05 13:51:57 -07001256 self.assertIn('This change depends on a change that failed to merge.',
1257 C.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001258 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001259
James E. Blair972e3c72013-08-29 12:04:55 -07001260 def test_failing_dependent_changes(self):
1261 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001262 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001263 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1264 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1265 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1266 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1267 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001268 A.addApproval('Code-Review', 2)
1269 B.addApproval('Code-Review', 2)
1270 C.addApproval('Code-Review', 2)
1271 D.addApproval('Code-Review', 2)
1272 E.addApproval('Code-Review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001273
1274 # E, D -> C -> B, A
1275
1276 D.setDependsOn(C, 1)
1277 C.setDependsOn(B, 1)
1278
Paul Belanger174a8272017-03-14 13:20:10 -04001279 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001280
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001281 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1282 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
1283 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1284 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1285 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001286
1287 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001288 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001289 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001290 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001291 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001292 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001293 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001294 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001295 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001296 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001297 self.waitUntilSettled()
1298
Paul Belanger174a8272017-03-14 13:20:10 -04001299 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001300 for build in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07001301 if build.parameters['zuul']['change'] != '1':
James E. Blair972e3c72013-08-29 12:04:55 -07001302 build.release()
1303 self.waitUntilSettled()
1304
Paul Belanger174a8272017-03-14 13:20:10 -04001305 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001306 self.waitUntilSettled()
1307
1308 self.assertEqual(A.data['status'], 'MERGED')
1309 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001310 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001311 self.assertEqual(B.data['status'], 'NEW')
1312 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001313 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001314 self.assertEqual(C.data['status'], 'NEW')
1315 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001316 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001317 self.assertEqual(D.data['status'], 'NEW')
1318 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001319 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001320 self.assertEqual(E.data['status'], 'MERGED')
1321 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001322 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001323 self.assertEqual(len(self.history), 18)
1324
James E. Blairec590122012-08-22 15:19:31 -07001325 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001326 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001327 # If it's dequeued more than once, we should see extra
1328 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001329
Paul Belanger174a8272017-03-14 13:20:10 -04001330 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001331 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1332 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1333 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001334 A.addApproval('Code-Review', 2)
1335 B.addApproval('Code-Review', 2)
1336 C.addApproval('Code-Review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001337
Paul Belanger174a8272017-03-14 13:20:10 -04001338 self.executor_server.failJob('project-test1', A)
1339 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001340
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001341 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1342 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1343 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001344
1345 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001346
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001347 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001348 self.assertEqual(self.builds[0].name, 'project-merge')
1349 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001350
Paul Belanger174a8272017-03-14 13:20:10 -04001351 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001352 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001353 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001354 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001355 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001356 self.waitUntilSettled()
1357
James E. Blair6df67132017-04-19 13:53:20 -07001358 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001359 self.assertEqual(self.builds[0].name, 'project-test1')
1360 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001361 self.assertEqual(self.builds[2].name, 'project-test1')
1362 self.assertEqual(self.builds[3].name, 'project-test2')
1363 self.assertEqual(self.builds[4].name, 'project-test1')
1364 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001365
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001366 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001367 self.waitUntilSettled()
1368
James E. Blair6df67132017-04-19 13:53:20 -07001369 self.assertEqual(len(self.builds), 2) # test2, merge for B
1370 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001371
Paul Belanger174a8272017-03-14 13:20:10 -04001372 self.executor_server.hold_jobs_in_build = False
1373 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001374 self.waitUntilSettled()
1375
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001376 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001377 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001378
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001379 self.assertEqual(A.data['status'], 'NEW')
1380 self.assertEqual(B.data['status'], 'MERGED')
1381 self.assertEqual(C.data['status'], 'MERGED')
1382 self.assertEqual(A.reported, 2)
1383 self.assertEqual(B.reported, 2)
1384 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001385
James E. Blaircc020532017-04-19 13:04:44 -07001386 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001387 def test_nonvoting_job(self):
1388 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001389
James E. Blair4ec821f2012-08-23 15:28:28 -07001390 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1391 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001392 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001393 self.executor_server.failJob('nonvoting-project-test2', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001394 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001395
1396 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001397
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001398 self.assertEqual(A.data['status'], 'MERGED')
1399 self.assertEqual(A.reported, 2)
1400 self.assertEqual(
1401 self.getJobFromHistory('nonvoting-project-merge').result,
1402 'SUCCESS')
1403 self.assertEqual(
1404 self.getJobFromHistory('nonvoting-project-test1').result,
1405 'SUCCESS')
1406 self.assertEqual(
1407 self.getJobFromHistory('nonvoting-project-test2').result,
1408 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001409
James E. Blair2ef29e92017-07-21 15:25:05 -07001410 self.assertTrue(self.getJobFromHistory('nonvoting-project-merge').
1411 parameters['zuul']['voting'])
1412 self.assertTrue(self.getJobFromHistory('nonvoting-project-test1').
1413 parameters['zuul']['voting'])
1414 self.assertFalse(self.getJobFromHistory('nonvoting-project-test2').
1415 parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -07001416
James E. Blaire0487072012-08-29 17:38:31 -07001417 def test_check_queue_success(self):
1418 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001419
James E. Blaire0487072012-08-29 17:38:31 -07001420 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1421 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1422
1423 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001424
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001425 self.assertEqual(A.data['status'], 'NEW')
1426 self.assertEqual(A.reported, 1)
1427 self.assertEqual(self.getJobFromHistory('project-merge').result,
1428 'SUCCESS')
1429 self.assertEqual(self.getJobFromHistory('project-test1').result,
1430 'SUCCESS')
1431 self.assertEqual(self.getJobFromHistory('project-test2').result,
1432 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001433
1434 def test_check_queue_failure(self):
1435 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001436
James E. Blaire0487072012-08-29 17:38:31 -07001437 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001438 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001439 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1440
1441 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001442
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001443 self.assertEqual(A.data['status'], 'NEW')
1444 self.assertEqual(A.reported, 1)
1445 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001446 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001447 self.assertEqual(self.getJobFromHistory('project-test1').result,
1448 'SUCCESS')
1449 self.assertEqual(self.getJobFromHistory('project-test2').result,
1450 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001451
David Shrewsburyffab07a2017-07-24 12:45:07 -04001452 @simple_layout('layouts/autohold.yaml')
1453 def test_autohold(self):
1454 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1455
1456 client = zuul.rpcclient.RPCClient('127.0.0.1',
1457 self.gearman_server.port)
1458 self.addCleanup(client.shutdown)
David Shrewsbury36b2adf2017-07-31 15:40:13 -04001459 r = client.autohold('tenant-one', 'org/project', 'project-test2',
1460 "reason text", 1)
David Shrewsburyffab07a2017-07-24 12:45:07 -04001461 self.assertTrue(r)
1462
1463 self.executor_server.failJob('project-test2', A)
1464 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1465
1466 self.waitUntilSettled()
1467
1468 self.assertEqual(A.data['status'], 'NEW')
1469 self.assertEqual(A.reported, 1)
1470 self.assertEqual(self.getJobFromHistory('project-test2').result,
1471 'FAILURE')
1472
1473 # Check nodepool for a held node
1474 held_node = None
1475 for node in self.fake_nodepool.getNodes():
1476 if node['state'] == zuul.model.STATE_HOLD:
1477 held_node = node
1478 break
1479 self.assertIsNotNone(held_node)
1480
1481 # Validate node has recorded the failed job
1482 self.assertEqual(
1483 held_node['hold_job'],
1484 " ".join(['tenant-one',
1485 'review.example.com/org/project',
1486 'project-test2'])
1487 )
David Shrewsburyf9af9df2017-08-01 15:19:26 -04001488 self.assertEqual(held_node['comment'], "reason text")
David Shrewsburyffab07a2017-07-24 12:45:07 -04001489
1490 # Another failed change should not hold any more nodes
1491 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1492 self.executor_server.failJob('project-test2', B)
1493 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1494 self.waitUntilSettled()
1495 self.assertEqual(B.data['status'], 'NEW')
1496 self.assertEqual(B.reported, 1)
1497 self.assertEqual(self.getJobFromHistory('project-test2').result,
1498 'FAILURE')
1499
1500 held_nodes = 0
1501 for node in self.fake_nodepool.getNodes():
1502 if node['state'] == zuul.model.STATE_HOLD:
1503 held_nodes += 1
1504 self.assertEqual(held_nodes, 1)
1505
James E. Blair7cb84542017-04-19 13:35:05 -07001506 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001507 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001508 # This particular test does a large amount of merges and needs a little
1509 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001510 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001511 "test that dependent changes behind dequeued changes work"
1512 # This complicated test is a reproduction of a real life bug
1513 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001514
Paul Belanger174a8272017-03-14 13:20:10 -04001515 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001516 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1517 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1518 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1519 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1520 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1521 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1522 D.setDependsOn(C, 1)
1523 E.setDependsOn(D, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001524 A.addApproval('Code-Review', 2)
1525 B.addApproval('Code-Review', 2)
1526 C.addApproval('Code-Review', 2)
1527 D.addApproval('Code-Review', 2)
1528 E.addApproval('Code-Review', 2)
1529 F.addApproval('Code-Review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001530
1531 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001532
1533 # Change object re-use in the gerrit trigger is hidden if
1534 # changes are added in quick succession; waiting makes it more
1535 # like real life.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001536 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001537 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001538 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001539 self.waitUntilSettled()
1540
Paul Belanger174a8272017-03-14 13:20:10 -04001541 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001542 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001543 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001544 self.waitUntilSettled()
1545
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001546 self.fake_gerrit.addEvent(C.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(D.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001549 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001550 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001551 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001552 self.fake_gerrit.addEvent(F.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001553 self.waitUntilSettled()
1554
Paul Belanger174a8272017-03-14 13:20:10 -04001555 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001556 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001557 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001558 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001559 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001560 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001561 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001562 self.waitUntilSettled()
1563
1564 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001565
1566 # Grab pointers to the jobs we want to release before
1567 # releasing any, because list indexes may change as
1568 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001569 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001570 a.release()
1571 b.release()
1572 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001573 self.waitUntilSettled()
1574
Paul Belanger174a8272017-03-14 13:20:10 -04001575 self.executor_server.hold_jobs_in_build = False
1576 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001577 self.waitUntilSettled()
1578
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001579 self.assertEqual(A.data['status'], 'NEW')
1580 self.assertEqual(B.data['status'], 'MERGED')
1581 self.assertEqual(C.data['status'], 'MERGED')
1582 self.assertEqual(D.data['status'], 'MERGED')
1583 self.assertEqual(E.data['status'], 'MERGED')
1584 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001585
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001586 self.assertEqual(A.reported, 2)
1587 self.assertEqual(B.reported, 2)
1588 self.assertEqual(C.reported, 2)
1589 self.assertEqual(D.reported, 2)
1590 self.assertEqual(E.reported, 2)
1591 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001592
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001593 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1594 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001595
1596 def test_merger_repack(self):
1597 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001598
James E. Blair05fed602012-09-07 12:45:24 -07001599 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001600 A.addApproval('Code-Review', 2)
1601 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001602 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001603 self.assertEqual(self.getJobFromHistory('project-merge').result,
1604 'SUCCESS')
1605 self.assertEqual(self.getJobFromHistory('project-test1').result,
1606 'SUCCESS')
1607 self.assertEqual(self.getJobFromHistory('project-test2').result,
1608 'SUCCESS')
1609 self.assertEqual(A.data['status'], 'MERGED')
1610 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001611 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001612 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001613
James E. Blair2a535672017-04-27 12:03:15 -07001614 path = os.path.join(self.merger_src_root, "review.example.com",
1615 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001616 if os.path.exists(path):
1617 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001618 path = os.path.join(self.executor_src_root, "review.example.com",
1619 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001620 if os.path.exists(path):
1621 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001622
1623 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001624 A.addApproval('Code-Review', 2)
1625 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001626 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001627 self.assertEqual(self.getJobFromHistory('project-merge').result,
1628 'SUCCESS')
1629 self.assertEqual(self.getJobFromHistory('project-test1').result,
1630 'SUCCESS')
1631 self.assertEqual(self.getJobFromHistory('project-test2').result,
1632 'SUCCESS')
1633 self.assertEqual(A.data['status'], 'MERGED')
1634 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001635
James E. Blair4886f282012-11-15 09:27:33 -08001636 def test_merger_repack_large_change(self):
1637 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001638 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001639 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001640 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001641 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001642 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001643 self.executor_server.merger._addProject('review.example.com',
James E. Blair197e8202017-06-09 12:54:28 -07001644 'org/project', url, None)
James E. Blair6df67132017-04-19 13:53:20 -07001645 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001646 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001647 # TODOv3(jeblair): add hostname to upstream root
1648 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001649 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001650 path = os.path.join(self.merger_src_root, 'review.example.com',
1651 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001652 if os.path.exists(path):
1653 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001654 path = os.path.join(self.executor_src_root, 'review.example.com',
1655 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001656 if os.path.exists(path):
1657 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001658
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001659 A.addApproval('Code-Review', 2)
1660 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001661 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001662 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001663 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001664 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001665 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001666 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001667 'SUCCESS')
1668 self.assertEqual(A.data['status'], 'MERGED')
1669 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001670
James E. Blair2fa50962013-01-30 21:50:41 -08001671 def test_new_patchset_dequeues_old(self):
1672 "Test that a new patchset causes the old to be dequeued"
1673 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001674 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001675 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1676 M.setMerged()
1677
1678 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1679 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1680 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1681 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001682 A.addApproval('Code-Review', 2)
1683 B.addApproval('Code-Review', 2)
1684 C.addApproval('Code-Review', 2)
1685 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001686
1687 C.setDependsOn(B, 1)
1688 B.setDependsOn(A, 1)
1689 A.setDependsOn(M, 1)
1690
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001691 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1692 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1693 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1694 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001695 self.waitUntilSettled()
1696
1697 B.addPatchset()
1698 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1699 self.waitUntilSettled()
1700
Paul Belanger174a8272017-03-14 13:20:10 -04001701 self.executor_server.hold_jobs_in_build = False
1702 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001703 self.waitUntilSettled()
1704
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001705 self.assertEqual(A.data['status'], 'MERGED')
1706 self.assertEqual(A.reported, 2)
1707 self.assertEqual(B.data['status'], 'NEW')
1708 self.assertEqual(B.reported, 2)
1709 self.assertEqual(C.data['status'], 'NEW')
1710 self.assertEqual(C.reported, 2)
1711 self.assertEqual(D.data['status'], 'MERGED')
1712 self.assertEqual(D.reported, 2)
1713 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001714
James E. Blairba437362015-02-07 11:41:52 -08001715 def test_new_patchset_check(self):
1716 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001717
Paul Belanger174a8272017-03-14 13:20:10 -04001718 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001719
1720 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001721 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001722 tenant = self.sched.abide.tenants.get('tenant-one')
1723 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001724
1725 # Add two git-dependent changes
1726 B.setDependsOn(A, 1)
1727 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1728 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001729 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1730 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001731
1732 # A live item, and a non-live/live pair
1733 items = check_pipeline.getAllItems()
1734 self.assertEqual(len(items), 3)
1735
1736 self.assertEqual(items[0].change.number, '1')
1737 self.assertEqual(items[0].change.patchset, '1')
1738 self.assertFalse(items[0].live)
1739
1740 self.assertEqual(items[1].change.number, '2')
1741 self.assertEqual(items[1].change.patchset, '1')
1742 self.assertTrue(items[1].live)
1743
1744 self.assertEqual(items[2].change.number, '1')
1745 self.assertEqual(items[2].change.patchset, '1')
1746 self.assertTrue(items[2].live)
1747
1748 # Add a new patchset to A
1749 A.addPatchset()
1750 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1751 self.waitUntilSettled()
1752
1753 # The live copy of A,1 should be gone, but the non-live and B
1754 # should continue, and we should have a new A,2
1755 items = check_pipeline.getAllItems()
1756 self.assertEqual(len(items), 3)
1757
1758 self.assertEqual(items[0].change.number, '1')
1759 self.assertEqual(items[0].change.patchset, '1')
1760 self.assertFalse(items[0].live)
1761
1762 self.assertEqual(items[1].change.number, '2')
1763 self.assertEqual(items[1].change.patchset, '1')
1764 self.assertTrue(items[1].live)
1765
1766 self.assertEqual(items[2].change.number, '1')
1767 self.assertEqual(items[2].change.patchset, '2')
1768 self.assertTrue(items[2].live)
1769
1770 # Add a new patchset to B
1771 B.addPatchset()
1772 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1773 self.waitUntilSettled()
1774
1775 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1776 # but we should have a new B,2 (still based on A,1)
1777 items = check_pipeline.getAllItems()
1778 self.assertEqual(len(items), 3)
1779
1780 self.assertEqual(items[0].change.number, '1')
1781 self.assertEqual(items[0].change.patchset, '2')
1782 self.assertTrue(items[0].live)
1783
1784 self.assertEqual(items[1].change.number, '1')
1785 self.assertEqual(items[1].change.patchset, '1')
1786 self.assertFalse(items[1].live)
1787
1788 self.assertEqual(items[2].change.number, '2')
1789 self.assertEqual(items[2].change.patchset, '2')
1790 self.assertTrue(items[2].live)
1791
1792 self.builds[0].release()
1793 self.waitUntilSettled()
1794 self.builds[0].release()
1795 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001796 self.executor_server.hold_jobs_in_build = False
1797 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001798 self.waitUntilSettled()
1799
1800 self.assertEqual(A.reported, 1)
1801 self.assertEqual(B.reported, 1)
1802 self.assertEqual(self.history[0].result, 'ABORTED')
1803 self.assertEqual(self.history[0].changes, '1,1')
1804 self.assertEqual(self.history[1].result, 'ABORTED')
1805 self.assertEqual(self.history[1].changes, '1,1 2,1')
1806 self.assertEqual(self.history[2].result, 'SUCCESS')
1807 self.assertEqual(self.history[2].changes, '1,2')
1808 self.assertEqual(self.history[3].result, 'SUCCESS')
1809 self.assertEqual(self.history[3].changes, '1,1 2,2')
1810
1811 def test_abandoned_gate(self):
1812 "Test that an abandoned change is dequeued from gate"
1813
Paul Belanger174a8272017-03-14 13:20:10 -04001814 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001815
1816 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001817 A.addApproval('Code-Review', 2)
1818 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001819 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001820 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1821 self.assertEqual(self.builds[0].name, 'project-merge')
1822
1823 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1824 self.waitUntilSettled()
1825
Paul Belanger174a8272017-03-14 13:20:10 -04001826 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001827 self.waitUntilSettled()
1828
Arie5b4048c2016-10-05 16:12:06 +03001829 self.assertBuilds([])
1830 self.assertHistory([
1831 dict(name='project-merge', result='ABORTED', changes='1,1')],
1832 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001833 self.assertEqual(A.reported, 1,
1834 "Abandoned gate change should report only start")
1835
1836 def test_abandoned_check(self):
1837 "Test that an abandoned change is dequeued from check"
1838
Paul Belanger174a8272017-03-14 13:20:10 -04001839 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001840
1841 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1842 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001843 tenant = self.sched.abide.tenants.get('tenant-one')
1844 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001845
1846 # Add two git-dependent changes
1847 B.setDependsOn(A, 1)
1848 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1849 self.waitUntilSettled()
1850 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1851 self.waitUntilSettled()
1852 # A live item, and a non-live/live pair
1853 items = check_pipeline.getAllItems()
1854 self.assertEqual(len(items), 3)
1855
1856 self.assertEqual(items[0].change.number, '1')
1857 self.assertFalse(items[0].live)
1858
1859 self.assertEqual(items[1].change.number, '2')
1860 self.assertTrue(items[1].live)
1861
1862 self.assertEqual(items[2].change.number, '1')
1863 self.assertTrue(items[2].live)
1864
1865 # Abandon A
1866 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1867 self.waitUntilSettled()
1868
1869 # The live copy of A should be gone, but the non-live and B
1870 # should continue
1871 items = check_pipeline.getAllItems()
1872 self.assertEqual(len(items), 2)
1873
1874 self.assertEqual(items[0].change.number, '1')
1875 self.assertFalse(items[0].live)
1876
1877 self.assertEqual(items[1].change.number, '2')
1878 self.assertTrue(items[1].live)
1879
Paul Belanger174a8272017-03-14 13:20:10 -04001880 self.executor_server.hold_jobs_in_build = False
1881 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001882 self.waitUntilSettled()
1883
1884 self.assertEqual(len(self.history), 4)
1885 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001886 'Build should have been aborted')
1887 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001888 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001889
Steve Varnau7b78b312015-04-03 14:49:46 -07001890 def test_abandoned_not_timer(self):
1891 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001892 # This test can not use simple_layout because it must start
1893 # with a configuration which does not include a
1894 # timer-triggered job so that we have an opportunity to set
1895 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001896 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001897 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001898 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001899 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001900 # The pipeline triggers every second, so we should have seen
1901 # several by now.
1902 time.sleep(5)
1903 self.waitUntilSettled()
1904 # Stop queuing timer triggered jobs so that the assertions
1905 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001906 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001907 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001908 self.sched.reconfigure(self.config)
James E. Blair0b137b42017-07-27 08:51:25 -07001909 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07001910 # If APScheduler is in mid-event when we remove the job, we
1911 # can end up with one more event firing, so give it an extra
1912 # second to settle.
1913 time.sleep(1)
1914 self.waitUntilSettled()
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001915
James E. Blair21037782017-07-19 11:56:55 -07001916 self.assertEqual(len(self.builds), 1, "One timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001917
1918 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1919 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1920 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07001921 self.assertEqual(len(self.builds), 2, "One change plus one timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001922
1923 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1924 self.waitUntilSettled()
1925
James E. Blair21037782017-07-19 11:56:55 -07001926 self.assertEqual(len(self.builds), 1, "One timer job remains")
Steve Varnau7b78b312015-04-03 14:49:46 -07001927
Paul Belanger174a8272017-03-14 13:20:10 -04001928 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001929 self.waitUntilSettled()
1930
James E. Blair2fa50962013-01-30 21:50:41 -08001931 def test_new_patchset_dequeues_old_on_head(self):
1932 "Test that a new patchset causes the old to be dequeued (at head)"
1933 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001934 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001935 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1936 M.setMerged()
1937 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1938 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1939 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1940 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001941 A.addApproval('Code-Review', 2)
1942 B.addApproval('Code-Review', 2)
1943 C.addApproval('Code-Review', 2)
1944 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001945
1946 C.setDependsOn(B, 1)
1947 B.setDependsOn(A, 1)
1948 A.setDependsOn(M, 1)
1949
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001950 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1951 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1952 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1953 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001954 self.waitUntilSettled()
1955
1956 A.addPatchset()
1957 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1958 self.waitUntilSettled()
1959
Paul Belanger174a8272017-03-14 13:20:10 -04001960 self.executor_server.hold_jobs_in_build = False
1961 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001962 self.waitUntilSettled()
1963
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001964 self.assertEqual(A.data['status'], 'NEW')
1965 self.assertEqual(A.reported, 2)
1966 self.assertEqual(B.data['status'], 'NEW')
1967 self.assertEqual(B.reported, 2)
1968 self.assertEqual(C.data['status'], 'NEW')
1969 self.assertEqual(C.reported, 2)
1970 self.assertEqual(D.data['status'], 'MERGED')
1971 self.assertEqual(D.reported, 2)
1972 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001973
1974 def test_new_patchset_dequeues_old_without_dependents(self):
1975 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001976 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001977 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1978 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1979 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001980 A.addApproval('Code-Review', 2)
1981 B.addApproval('Code-Review', 2)
1982 C.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001983
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001984 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1985 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1986 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001987 self.waitUntilSettled()
1988
1989 B.addPatchset()
1990 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1991 self.waitUntilSettled()
1992
Paul Belanger174a8272017-03-14 13:20:10 -04001993 self.executor_server.hold_jobs_in_build = False
1994 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001995 self.waitUntilSettled()
1996
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001997 self.assertEqual(A.data['status'], 'MERGED')
1998 self.assertEqual(A.reported, 2)
1999 self.assertEqual(B.data['status'], 'NEW')
2000 self.assertEqual(B.reported, 2)
2001 self.assertEqual(C.data['status'], 'MERGED')
2002 self.assertEqual(C.reported, 2)
2003 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002004
2005 def test_new_patchset_dequeues_old_independent_queue(self):
2006 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04002007 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002008 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2009 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2010 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2011 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2012 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2013 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2014 self.waitUntilSettled()
2015
2016 B.addPatchset()
2017 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2018 self.waitUntilSettled()
2019
Paul Belanger174a8272017-03-14 13:20:10 -04002020 self.executor_server.hold_jobs_in_build = False
2021 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002022 self.waitUntilSettled()
2023
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002024 self.assertEqual(A.data['status'], 'NEW')
2025 self.assertEqual(A.reported, 1)
2026 self.assertEqual(B.data['status'], 'NEW')
2027 self.assertEqual(B.reported, 1)
2028 self.assertEqual(C.data['status'], 'NEW')
2029 self.assertEqual(C.reported, 1)
2030 self.assertEqual(len(self.history), 10)
2031 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002032
James E. Blaircc020532017-04-19 13:04:44 -07002033 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07002034 def test_noop_job(self):
2035 "Test that the internal noop job works"
2036 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002037 A.addApproval('Code-Review', 2)
2038 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07002039 self.waitUntilSettled()
2040
2041 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2042 self.assertTrue(self.sched._areAllBuildsComplete())
2043 self.assertEqual(len(self.history), 0)
2044 self.assertEqual(A.data['status'], 'MERGED')
2045 self.assertEqual(A.reported, 2)
2046
James E. Blair06cc3922017-04-19 10:08:10 -07002047 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002048 def test_no_job_project(self):
2049 "Test that reports with no jobs don't get sent"
2050 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
2051 'master', 'A')
2052 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2053 self.waitUntilSettled()
2054
2055 # Change wasn't reported to
2056 self.assertEqual(A.reported, False)
2057
2058 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08002059 tenant = self.sched.abide.tenants.get('tenant-one')
2060 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002061 items = check_pipeline.getAllItems()
2062 self.assertEqual(len(items), 0)
2063
2064 self.assertEqual(len(self.history), 0)
2065
James E. Blair7d0dedc2013-02-21 17:26:09 -08002066 def test_zuul_refs(self):
2067 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04002068 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002069 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2070 M1.setMerged()
2071 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2072 M2.setMerged()
2073
2074 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2075 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2076 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2077 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002078 A.addApproval('Code-Review', 2)
2079 B.addApproval('Code-Review', 2)
2080 C.addApproval('Code-Review', 2)
2081 D.addApproval('Code-Review', 2)
2082 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2083 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2084 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2085 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002086
2087 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002088 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002089 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002090 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002091 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002092 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002093 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002094 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002095 self.waitUntilSettled()
2096
Clint Byrum3343e3e2016-11-15 16:05:03 -08002097 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002098 for x in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07002099 if x.parameters['zuul']['change'] == '3':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002100 a_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002101 elif x.parameters['zuul']['change'] == '4':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002102 b_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002103 elif x.parameters['zuul']['change'] == '5':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002104 c_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002105 elif x.parameters['zuul']['change'] == '6':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002106 d_build = x
2107 if a_build and b_build and c_build and d_build:
2108 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002109
Clint Byrum3343e3e2016-11-15 16:05:03 -08002110 # should have a, not b, and should not be in project2
2111 self.assertTrue(a_build.hasChanges(A))
2112 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002113
Clint Byrum3343e3e2016-11-15 16:05:03 -08002114 # should have a and b, and should not be in project2
2115 self.assertTrue(b_build.hasChanges(A, B))
2116 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002117
Clint Byrum3343e3e2016-11-15 16:05:03 -08002118 # should have a and b in 1, c in 2
2119 self.assertTrue(c_build.hasChanges(A, B, C))
2120 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002121
Clint Byrum3343e3e2016-11-15 16:05:03 -08002122 # should have a and b in 1, c and d in 2
2123 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002124
Paul Belanger174a8272017-03-14 13:20:10 -04002125 self.executor_server.hold_jobs_in_build = False
2126 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002127 self.waitUntilSettled()
2128
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002129 self.assertEqual(A.data['status'], 'MERGED')
2130 self.assertEqual(A.reported, 2)
2131 self.assertEqual(B.data['status'], 'MERGED')
2132 self.assertEqual(B.reported, 2)
2133 self.assertEqual(C.data['status'], 'MERGED')
2134 self.assertEqual(C.reported, 2)
2135 self.assertEqual(D.data['status'], 'MERGED')
2136 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002137
James E. Blair4a28a882013-08-23 15:17:33 -07002138 def test_rerun_on_error(self):
2139 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002140 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002141 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002142 A.addApproval('Code-Review', 2)
2143 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002144 self.waitUntilSettled()
2145
James E. Blair412fba82017-01-26 15:00:50 -08002146 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002147 self.executor_server.hold_jobs_in_build = False
2148 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002149 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002150 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002151 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2152
James E. Blair412e5582013-04-22 15:50:12 -07002153 def test_statsd(self):
2154 "Test each of the statsd methods used in the scheduler"
2155 import extras
2156 statsd = extras.try_import('statsd.statsd')
2157 statsd.incr('test-incr')
2158 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002159 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002160 self.assertReportedStat('test-incr', '1|c')
2161 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002162 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002163
James E. Blairdad52252014-02-07 16:59:17 -08002164 def test_stuck_job_cleanup(self):
2165 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002166
2167 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002168 self.gearman_server.hold_jobs_in_queue = True
2169 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002170 A.addApproval('Code-Review', 2)
2171 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002172 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002173 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002174 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2175
James E. Blair31cb3e82017-04-20 13:08:56 -07002176 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002177 self.sched.reconfigure(self.config)
2178 self.waitUntilSettled()
2179
James E. Blair18c64442014-03-18 10:14:45 -07002180 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002181 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002182 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002183 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2184 self.assertTrue(self.sched._areAllBuildsComplete())
2185
2186 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002187 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002188 self.assertEqual(self.history[0].result, 'SUCCESS')
2189
James E. Blair879dafb2015-07-17 14:04:49 -07002190 def test_file_head(self):
2191 # This is a regression test for an observed bug. A change
2192 # with a file named "HEAD" in the root directory of the repo
2193 # was processed by a merger. It then was unable to reset the
2194 # repo because of:
2195 # GitCommandError: 'git reset --hard HEAD' returned
2196 # with exit code 128
2197 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2198 # and filename
2199 # Use '--' to separate filenames from revisions'
2200
2201 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002202 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002203 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2204
2205 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2206 self.waitUntilSettled()
2207
2208 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2209 self.waitUntilSettled()
2210
2211 self.assertIn('Build succeeded', A.messages[0])
2212 self.assertIn('Build succeeded', B.messages[0])
2213
James E. Blair70c71582013-03-06 08:50:50 -08002214 def test_file_jobs(self):
2215 "Test that file jobs run only when appropriate"
2216 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002217 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002218 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002219 A.addApproval('Code-Review', 2)
2220 B.addApproval('Code-Review', 2)
2221 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2222 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002223 self.waitUntilSettled()
2224
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002225 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002226 if x.name == 'project-testfile']
2227
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002228 self.assertEqual(len(testfile_jobs), 1)
2229 self.assertEqual(testfile_jobs[0].changes, '1,2')
2230 self.assertEqual(A.data['status'], 'MERGED')
2231 self.assertEqual(A.reported, 2)
2232 self.assertEqual(B.data['status'], 'MERGED')
2233 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002234
Clint Byrum3ee39f32016-11-17 23:45:07 -08002235 def _test_irrelevant_files_jobs(self, should_skip):
2236 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002237 if should_skip:
2238 files = {'ignoreme': 'ignored\n'}
2239 else:
2240 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002241
2242 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002243 'master',
2244 'test irrelevant-files',
2245 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002246 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2247 self.waitUntilSettled()
2248
2249 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002250 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002251
2252 if should_skip:
2253 self.assertEqual([], tested_change_ids)
2254 else:
2255 self.assertIn(change.data['number'], tested_change_ids)
2256
James E. Blaircc324192017-04-19 14:36:08 -07002257 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002258 def test_irrelevant_files_match_skips_job(self):
2259 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002260
James E. Blaircc324192017-04-19 14:36:08 -07002261 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002262 def test_irrelevant_files_no_match_runs_job(self):
2263 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002264
James E. Blaircc324192017-04-19 14:36:08 -07002265 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002266 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002267 files = {'ignoreme': 'ignored\n'}
2268
2269 change = self.fake_gerrit.addFakeChange('org/project',
2270 'master',
2271 'test irrelevant-files',
2272 files=files)
2273 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2274 self.waitUntilSettled()
2275
2276 run_jobs = set([build.name for build in self.history])
2277
2278 self.assertEqual(set(['project-test-nomatch-starts-empty',
2279 'project-test-nomatch-starts-full']), run_jobs)
2280
James E. Blairaf8b2082017-10-03 15:38:27 -07002281 @simple_layout('layouts/job-vars.yaml')
2282 def test_inherited_job_variables(self):
2283 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2284 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2285 self.waitUntilSettled()
2286 self.assertHistory([
2287 dict(name='parentjob', result='SUCCESS'),
2288 dict(name='child1', result='SUCCESS'),
2289 dict(name='child2', result='SUCCESS'),
2290 dict(name='child3', result='SUCCESS'),
2291 ], ordered=False)
2292 j = self.getJobFromHistory('parentjob')
2293 rp = set([p['name'] for p in j.parameters['projects']])
2294 self.assertEqual(j.parameters['vars']['override'], 0)
2295 self.assertEqual(j.parameters['vars']['child1override'], 0)
2296 self.assertEqual(j.parameters['vars']['parent'], 0)
2297 self.assertFalse('child1' in j.parameters['vars'])
2298 self.assertFalse('child2' in j.parameters['vars'])
2299 self.assertFalse('child3' in j.parameters['vars'])
2300 self.assertEqual(rp, set(['org/project', 'org/project0',
2301 'org/project0']))
2302 j = self.getJobFromHistory('child1')
2303 rp = set([p['name'] for p in j.parameters['projects']])
2304 self.assertEqual(j.parameters['vars']['override'], 1)
2305 self.assertEqual(j.parameters['vars']['child1override'], 1)
2306 self.assertEqual(j.parameters['vars']['parent'], 0)
2307 self.assertEqual(j.parameters['vars']['child1'], 1)
2308 self.assertFalse('child2' in j.parameters['vars'])
2309 self.assertFalse('child3' in j.parameters['vars'])
2310 self.assertEqual(rp, set(['org/project', 'org/project0',
2311 'org/project1']))
2312 j = self.getJobFromHistory('child2')
2313 rp = set([p['name'] for p in j.parameters['projects']])
2314 self.assertEqual(j.parameters['vars']['override'], 2)
2315 self.assertEqual(j.parameters['vars']['child1override'], 0)
2316 self.assertEqual(j.parameters['vars']['parent'], 0)
2317 self.assertFalse('child1' in j.parameters['vars'])
2318 self.assertEqual(j.parameters['vars']['child2'], 2)
2319 self.assertFalse('child3' in j.parameters['vars'])
2320 self.assertEqual(rp, set(['org/project', 'org/project0',
2321 'org/project2']))
2322 j = self.getJobFromHistory('child3')
2323 rp = set([p['name'] for p in j.parameters['projects']])
2324 self.assertEqual(j.parameters['vars']['override'], 3)
2325 self.assertEqual(j.parameters['vars']['child1override'], 0)
2326 self.assertEqual(j.parameters['vars']['parent'], 0)
2327 self.assertFalse('child1' in j.parameters['vars'])
2328 self.assertFalse('child2' in j.parameters['vars'])
2329 self.assertEqual(j.parameters['vars']['child3'], 3)
2330 self.assertEqual(rp, set(['org/project', 'org/project0',
2331 'org/project3']))
2332
James E. Blairc8a1e052014-02-25 09:29:26 -08002333 def test_queue_names(self):
2334 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002335 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002336 (trusted, project1) = tenant.getProject('org/project1')
2337 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002338 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2339 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2340 self.assertEqual(q1.name, 'integrated')
2341 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002342
James E. Blair64ed6f22013-07-10 14:07:23 -07002343 def test_queue_precedence(self):
2344 "Test that queue precedence works"
2345
2346 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002347 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002348 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2349 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002350 A.addApproval('Code-Review', 2)
2351 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002352
2353 self.waitUntilSettled()
2354 self.gearman_server.hold_jobs_in_queue = False
2355 self.gearman_server.release()
2356 self.waitUntilSettled()
2357
James E. Blair8de58bd2013-07-18 16:23:33 -07002358 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002359 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002360 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002361 self.waitUntilSettled()
2362
James E. Blair64ed6f22013-07-10 14:07:23 -07002363 self.log.debug(self.history)
2364 self.assertEqual(self.history[0].pipeline, 'gate')
2365 self.assertEqual(self.history[1].pipeline, 'check')
2366 self.assertEqual(self.history[2].pipeline, 'gate')
2367 self.assertEqual(self.history[3].pipeline, 'gate')
2368 self.assertEqual(self.history[4].pipeline, 'check')
2369 self.assertEqual(self.history[5].pipeline, 'check')
2370
Clark Boylana5edbe42014-06-03 16:39:10 -07002371 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002372 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002373 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002374 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002375 A.addApproval('Code-Review', 2)
2376 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002377 self.waitUntilSettled()
2378
Paul Belanger174a8272017-03-14 13:20:10 -04002379 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002380 self.waitUntilSettled()
2381
James E. Blair1843a552013-07-03 14:19:52 -07002382 port = self.webapp.server.socket.getsockname()[1]
2383
Adam Gandelman77a12c72017-02-03 17:43:43 -08002384 req = urllib.request.Request(
2385 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002386 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002387 headers = f.info()
2388 self.assertIn('Content-Length', headers)
2389 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002390 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2391 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002392 self.assertIn('Access-Control-Allow-Origin', headers)
2393 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002394 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002395 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002396 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002397
Paul Belanger174a8272017-03-14 13:20:10 -04002398 self.executor_server.hold_jobs_in_build = False
2399 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002400 self.waitUntilSettled()
2401
2402 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002403 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002404 for p in data['pipelines']:
2405 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002406 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002407 self.assertEqual(q['window'], 20)
2408 else:
2409 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002410 for head in q['heads']:
2411 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002412 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002413 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002414 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002415 status_jobs.append(job)
2416 self.assertEqual('project-merge', status_jobs[0]['name'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002417 # TODO(mordred) pull uuids from self.builds
Monty Taylor51139a02016-05-24 11:28:10 -05002418 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002419 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2420 uuid=status_jobs[0]['uuid']),
2421 status_jobs[0]['url'])
2422 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002423 'finger://{hostname}/{uuid}'.format(
2424 hostname=self.executor_server.hostname,
2425 uuid=status_jobs[0]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002426 status_jobs[0]['finger_url'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002427 # TOOD(mordred) configure a success-url on the base job
Monty Taylor51139a02016-05-24 11:28:10 -05002428 self.assertEqual(
2429 'finger://{hostname}/{uuid}'.format(
2430 hostname=self.executor_server.hostname,
2431 uuid=status_jobs[0]['uuid']),
2432 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002433 self.assertEqual('project-test1', status_jobs[1]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002434 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002435 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2436 uuid=status_jobs[1]['uuid']),
2437 status_jobs[1]['url'])
2438 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002439 'finger://{hostname}/{uuid}'.format(
2440 hostname=self.executor_server.hostname,
2441 uuid=status_jobs[1]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002442 status_jobs[1]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002443 self.assertEqual(
2444 'finger://{hostname}/{uuid}'.format(
2445 hostname=self.executor_server.hostname,
2446 uuid=status_jobs[1]['uuid']),
2447 status_jobs[1]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002448
2449 self.assertEqual('project-test2', status_jobs[2]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002450 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002451 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2452 uuid=status_jobs[2]['uuid']),
2453 status_jobs[2]['url'])
2454 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002455 'finger://{hostname}/{uuid}'.format(
2456 hostname=self.executor_server.hostname,
2457 uuid=status_jobs[2]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002458 status_jobs[2]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002459 self.assertEqual(
2460 'finger://{hostname}/{uuid}'.format(
2461 hostname=self.executor_server.hostname,
2462 uuid=status_jobs[2]['uuid']),
2463 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002464
Tobias Henkel65639f82017-07-10 10:25:42 +02002465 # check job dependencies
2466 self.assertIsNotNone(status_jobs[0]['dependencies'])
2467 self.assertIsNotNone(status_jobs[1]['dependencies'])
2468 self.assertIsNotNone(status_jobs[2]['dependencies'])
2469 self.assertEqual(len(status_jobs[0]['dependencies']), 0)
2470 self.assertEqual(len(status_jobs[1]['dependencies']), 1)
2471 self.assertEqual(len(status_jobs[2]['dependencies']), 1)
2472 self.assertIn('project-merge', status_jobs[1]['dependencies'])
2473 self.assertIn('project-merge', status_jobs[2]['dependencies'])
2474
James E. Blaircdccd972013-07-01 12:10:22 -07002475 def test_live_reconfiguration(self):
2476 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002477 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002478 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002479 A.addApproval('Code-Review', 2)
2480 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002481 self.waitUntilSettled()
2482
2483 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002484 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002485
Paul Belanger174a8272017-03-14 13:20:10 -04002486 self.executor_server.hold_jobs_in_build = False
2487 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002488 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002489 self.assertEqual(self.getJobFromHistory('project-merge').result,
2490 'SUCCESS')
2491 self.assertEqual(self.getJobFromHistory('project-test1').result,
2492 'SUCCESS')
2493 self.assertEqual(self.getJobFromHistory('project-test2').result,
2494 'SUCCESS')
2495 self.assertEqual(A.data['status'], 'MERGED')
2496 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002497
James E. Blair027ba992017-09-20 13:48:32 -07002498 def test_live_reconfiguration_abort(self):
2499 # Raise an exception during reconfiguration and verify we
2500 # still function.
2501 self.executor_server.hold_jobs_in_build = True
2502 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2503 A.addApproval('Code-Review', 2)
2504 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2505 self.waitUntilSettled()
2506
2507 tenant = self.sched.abide.tenants.get('tenant-one')
2508 pipeline = tenant.layout.pipelines['gate']
2509 change = pipeline.getAllItems()[0].change
2510 # Set this to an invalid value to cause an exception during
2511 # reconfiguration.
2512 change.branch = None
2513
2514 self.sched.reconfigure(self.config)
2515 self.waitUntilSettled()
2516
2517 self.executor_server.hold_jobs_in_build = False
2518 self.executor_server.release()
2519
2520 self.waitUntilSettled()
2521 self.assertEqual(self.getJobFromHistory('project-merge').result,
2522 'ABORTED')
2523 self.assertEqual(A.data['status'], 'NEW')
2524 # The final report fails because of the invalid value set above.
2525 self.assertEqual(A.reported, 1)
2526
James E. Blair6bc782d2015-07-17 16:20:21 -07002527 def test_live_reconfiguration_merge_conflict(self):
2528 # A real-world bug: a change in a gate queue has a merge
2529 # conflict and a job is added to its project while it's
2530 # sitting in the queue. The job gets added to the change and
2531 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002532 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002533
2534 # This change is fine. It's here to stop the queue long
2535 # enough for the next change to be subject to the
2536 # reconfiguration, as well as to provide a conflict for the
2537 # next change. This change will succeed and merge.
2538 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002539 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002540 A.addApproval('Code-Review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002541
2542 # This change will be in merge conflict. During the
2543 # reconfiguration, we will add a job. We want to make sure
2544 # that doesn't cause it to get stuck.
2545 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002546 B.addPatchset({'conflict': 'B'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002547 B.addApproval('Code-Review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002548
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002549 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2550 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002551
2552 self.waitUntilSettled()
2553
2554 # No jobs have run yet
2555 self.assertEqual(A.data['status'], 'NEW')
2556 self.assertEqual(A.reported, 1)
2557 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002558 self.assertEqual(len(self.history), 0)
2559
2560 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002561 self.commitConfigUpdate('common-config',
2562 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002563 self.sched.reconfigure(self.config)
2564 self.waitUntilSettled()
2565
Paul Belanger174a8272017-03-14 13:20:10 -04002566 self.executor_server.hold_jobs_in_build = False
2567 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002568 self.waitUntilSettled()
2569
2570 self.assertEqual(A.data['status'], 'MERGED')
2571 self.assertEqual(A.reported, 2)
2572 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002573 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002574 self.assertEqual(self.getJobFromHistory('project-merge').result,
2575 'SUCCESS')
2576 self.assertEqual(self.getJobFromHistory('project-test1').result,
2577 'SUCCESS')
2578 self.assertEqual(self.getJobFromHistory('project-test2').result,
2579 'SUCCESS')
2580 self.assertEqual(self.getJobFromHistory('project-test3').result,
2581 'SUCCESS')
2582 self.assertEqual(len(self.history), 4)
2583
James E. Blair400e8fd2015-07-30 17:44:45 -07002584 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002585 # An extrapolation of test_live_reconfiguration_merge_conflict
2586 # that tests a job added to a job tree with a failed root does
2587 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002588 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002589
2590 # This change is fine. It's here to stop the queue long
2591 # enough for the next change to be subject to the
2592 # reconfiguration. This change will succeed and merge.
2593 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002594 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002595 A.addApproval('Code-Review', 2)
2596 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002597 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002598 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002599 self.waitUntilSettled()
2600
2601 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002602 self.executor_server.failJob('project-merge', B)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002603 B.addApproval('Code-Review', 2)
2604 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002605 self.waitUntilSettled()
2606
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 # Both -merge jobs have run, but no others.
2611 self.assertEqual(A.data['status'], 'NEW')
2612 self.assertEqual(A.reported, 1)
2613 self.assertEqual(B.data['status'], 'NEW')
2614 self.assertEqual(B.reported, 1)
2615 self.assertEqual(self.history[0].result, 'SUCCESS')
2616 self.assertEqual(self.history[0].name, 'project-merge')
2617 self.assertEqual(self.history[1].result, 'FAILURE')
2618 self.assertEqual(self.history[1].name, 'project-merge')
2619 self.assertEqual(len(self.history), 2)
2620
2621 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002622 self.commitConfigUpdate('common-config',
2623 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002624 self.sched.reconfigure(self.config)
2625 self.waitUntilSettled()
2626
Paul Belanger174a8272017-03-14 13:20:10 -04002627 self.executor_server.hold_jobs_in_build = False
2628 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002629 self.waitUntilSettled()
2630
2631 self.assertEqual(A.data['status'], 'MERGED')
2632 self.assertEqual(A.reported, 2)
2633 self.assertEqual(B.data['status'], 'NEW')
2634 self.assertEqual(B.reported, 2)
2635 self.assertEqual(self.history[0].result, 'SUCCESS')
2636 self.assertEqual(self.history[0].name, 'project-merge')
2637 self.assertEqual(self.history[1].result, 'FAILURE')
2638 self.assertEqual(self.history[1].name, 'project-merge')
2639 self.assertEqual(self.history[2].result, 'SUCCESS')
2640 self.assertEqual(self.history[3].result, 'SUCCESS')
2641 self.assertEqual(self.history[4].result, 'SUCCESS')
2642 self.assertEqual(len(self.history), 5)
2643
James E. Blair400e8fd2015-07-30 17:44:45 -07002644 def test_live_reconfiguration_failed_job(self):
2645 # Test that a change with a removed failing job does not
2646 # disrupt reconfiguration. If a change has a failed job and
2647 # that job is removed during a reconfiguration, we observed a
2648 # bug where the code to re-set build statuses would run on
2649 # that build and raise an exception because the job no longer
2650 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002651 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002652
2653 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2654
2655 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002656 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002657
2658 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2659 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002660 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002661 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002662 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002663 self.waitUntilSettled()
2664
2665 self.assertEqual(A.data['status'], 'NEW')
2666 self.assertEqual(A.reported, 0)
2667
2668 self.assertEqual(self.getJobFromHistory('project-merge').result,
2669 'SUCCESS')
2670 self.assertEqual(self.getJobFromHistory('project-test1').result,
2671 'FAILURE')
2672 self.assertEqual(len(self.history), 2)
2673
2674 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002675 self.commitConfigUpdate('common-config',
2676 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002677 self.sched.reconfigure(self.config)
2678 self.waitUntilSettled()
2679
Paul Belanger174a8272017-03-14 13:20:10 -04002680 self.executor_server.hold_jobs_in_build = False
2681 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002682 self.waitUntilSettled()
2683
2684 self.assertEqual(self.getJobFromHistory('project-test2').result,
2685 'SUCCESS')
2686 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2687 'SUCCESS')
2688 self.assertEqual(len(self.history), 4)
2689
2690 self.assertEqual(A.data['status'], 'NEW')
2691 self.assertEqual(A.reported, 1)
2692 self.assertIn('Build succeeded', A.messages[0])
2693 # Ensure the removed job was not included in the report.
2694 self.assertNotIn('project-test1', A.messages[0])
2695
James E. Blairfe707d12015-08-05 15:18:15 -07002696 def test_live_reconfiguration_shared_queue(self):
2697 # Test that a change with a failing job which was removed from
2698 # this project but otherwise still exists in the system does
2699 # not disrupt reconfiguration.
2700
Paul Belanger174a8272017-03-14 13:20:10 -04002701 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002702
2703 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2704
Paul Belanger174a8272017-03-14 13:20:10 -04002705 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002706
2707 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2708 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002709 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002710 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002711 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002712 self.waitUntilSettled()
2713
2714 self.assertEqual(A.data['status'], 'NEW')
2715 self.assertEqual(A.reported, 0)
2716
Clint Byrum69663d42017-05-01 14:51:04 -07002717 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002718 'SUCCESS')
2719 self.assertEqual(self.getJobFromHistory(
2720 'project1-project2-integration').result, 'FAILURE')
2721 self.assertEqual(len(self.history), 2)
2722
2723 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002724 self.commitConfigUpdate(
2725 'common-config',
2726 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002727 self.sched.reconfigure(self.config)
2728 self.waitUntilSettled()
2729
Paul Belanger174a8272017-03-14 13:20:10 -04002730 self.executor_server.hold_jobs_in_build = False
2731 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002732 self.waitUntilSettled()
2733
Clint Byrum69663d42017-05-01 14:51:04 -07002734 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002735 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002736 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002737 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002738 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002739 'SUCCESS')
2740 self.assertEqual(self.getJobFromHistory(
2741 'project1-project2-integration').result, 'FAILURE')
2742 self.assertEqual(len(self.history), 4)
2743
2744 self.assertEqual(A.data['status'], 'NEW')
2745 self.assertEqual(A.reported, 1)
2746 self.assertIn('Build succeeded', A.messages[0])
2747 # Ensure the removed job was not included in the report.
2748 self.assertNotIn('project1-project2-integration', A.messages[0])
2749
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002750 def test_double_live_reconfiguration_shared_queue(self):
2751 # This was a real-world regression. A change is added to
2752 # gate; a reconfigure happens, a second change which depends
2753 # on the first is added, and a second reconfiguration happens.
2754 # Ensure that both changes merge.
2755
2756 # A failure may indicate incorrect caching or cleaning up of
2757 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002758 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002759
2760 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2761 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2762 B.setDependsOn(A, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002763 A.addApproval('Code-Review', 2)
2764 B.addApproval('Code-Review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002765
2766 # Add the parent change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002767 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002768 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002769 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002770 self.waitUntilSettled()
2771
2772 # Reconfigure (with only one change in the pipeline).
2773 self.sched.reconfigure(self.config)
2774 self.waitUntilSettled()
2775
2776 # Add the child change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002777 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002778 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002779 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002780 self.waitUntilSettled()
2781
2782 # Reconfigure (with both in the pipeline).
2783 self.sched.reconfigure(self.config)
2784 self.waitUntilSettled()
2785
Paul Belanger174a8272017-03-14 13:20:10 -04002786 self.executor_server.hold_jobs_in_build = False
2787 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002788 self.waitUntilSettled()
2789
2790 self.assertEqual(len(self.history), 8)
2791
2792 self.assertEqual(A.data['status'], 'MERGED')
2793 self.assertEqual(A.reported, 2)
2794 self.assertEqual(B.data['status'], 'MERGED')
2795 self.assertEqual(B.reported, 2)
2796
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002797 def test_live_reconfiguration_del_project(self):
2798 # Test project deletion from layout
2799 # while changes are enqueued
2800
Paul Belanger174a8272017-03-14 13:20:10 -04002801 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002802 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2803 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2804 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2805
2806 # A Depends-On: B
2807 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2808 A.subject, B.data['id'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002809 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002810
2811 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2812 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2813 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002814 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002815 self.waitUntilSettled()
2816 self.assertEqual(len(self.builds), 5)
2817
2818 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002819 self.commitConfigUpdate(
2820 'common-config',
2821 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002822 self.sched.reconfigure(self.config)
2823 self.waitUntilSettled()
2824
2825 # Builds for C aborted, builds for A succeed,
2826 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002827 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002828 self.assertEqual(job_c.changes, '3,1')
2829 self.assertEqual(job_c.result, 'ABORTED')
2830
Paul Belanger174a8272017-03-14 13:20:10 -04002831 self.executor_server.hold_jobs_in_build = False
2832 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002833 self.waitUntilSettled()
2834
Clint Byrum0149ada2017-04-04 12:22:01 -07002835 self.assertEqual(
2836 self.getJobFromHistory('project-test1', 'org/project').changes,
2837 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002838
2839 self.assertEqual(A.data['status'], 'NEW')
2840 self.assertEqual(B.data['status'], 'NEW')
2841 self.assertEqual(C.data['status'], 'NEW')
2842 self.assertEqual(A.reported, 1)
2843 self.assertEqual(B.reported, 0)
2844 self.assertEqual(C.reported, 0)
2845
Clint Byrum0149ada2017-04-04 12:22:01 -07002846 tenant = self.sched.abide.tenants.get('tenant-one')
2847 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002848 self.assertIn('Build succeeded', A.messages[0])
2849
James E. Blair287c06d2013-07-24 10:39:30 -07002850 def test_delayed_repo_init(self):
James E. Blair287c06d2013-07-24 10:39:30 -07002851 self.init_repo("org/new-project")
Clint Byrum627ba362017-08-14 13:20:40 -07002852 files = {'README': ''}
2853 self.addCommitToRepo("org/new-project", 'Initial commit',
2854 files=files, tag='init')
2855 self.newTenantConfig('tenants/delayed-repo-init.yaml')
2856 self.commitConfigUpdate(
2857 'common-config',
2858 'layouts/delayed-repo-init.yaml')
2859 self.sched.reconfigure(self.config)
2860 self.waitUntilSettled()
2861
James E. Blair287c06d2013-07-24 10:39:30 -07002862 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2863
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002864 A.addApproval('Code-Review', 2)
2865 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002866 self.waitUntilSettled()
2867 self.assertEqual(self.getJobFromHistory('project-merge').result,
2868 'SUCCESS')
2869 self.assertEqual(self.getJobFromHistory('project-test1').result,
2870 'SUCCESS')
2871 self.assertEqual(self.getJobFromHistory('project-test2').result,
2872 'SUCCESS')
2873 self.assertEqual(A.data['status'], 'MERGED')
2874 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002875
James E. Blair9ea70072017-04-19 16:05:30 -07002876 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002877 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002878 self.init_repo("org/delete-project")
2879 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2880
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002881 A.addApproval('Code-Review', 2)
2882 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002883 self.waitUntilSettled()
2884 self.assertEqual(self.getJobFromHistory('project-merge').result,
2885 'SUCCESS')
2886 self.assertEqual(self.getJobFromHistory('project-test1').result,
2887 'SUCCESS')
2888 self.assertEqual(self.getJobFromHistory('project-test2').result,
2889 'SUCCESS')
2890 self.assertEqual(A.data['status'], 'MERGED')
2891 self.assertEqual(A.reported, 2)
2892
2893 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002894 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002895 if os.path.exists(os.path.join(self.merger_src_root, p)):
2896 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002897 if os.path.exists(os.path.join(self.executor_src_root, p)):
2898 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002899
2900 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2901
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002902 B.addApproval('Code-Review', 2)
2903 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002904 self.waitUntilSettled()
2905 self.assertEqual(self.getJobFromHistory('project-merge').result,
2906 'SUCCESS')
2907 self.assertEqual(self.getJobFromHistory('project-test1').result,
2908 'SUCCESS')
2909 self.assertEqual(self.getJobFromHistory('project-test2').result,
2910 'SUCCESS')
2911 self.assertEqual(B.data['status'], 'MERGED')
2912 self.assertEqual(B.reported, 2)
2913
James E. Blair892cca62017-08-09 11:36:58 -07002914 @simple_layout('layouts/untrusted-secrets.yaml')
2915 def test_untrusted_secrets(self):
2916 "Test untrusted secrets"
2917 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2918 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2919 self.waitUntilSettled()
2920
2921 self.assertHistory([])
2922 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
James E. Blair8eb564a2017-08-10 09:21:41 -07002923 self.assertIn('does not allow post-review job',
James E. Blair892cca62017-08-09 11:36:58 -07002924 A.messages[0])
2925
James E. Blaire68d43f2017-04-19 14:03:30 -07002926 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002927 def test_tags(self):
2928 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002929 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2930 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2931 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2932 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2933 self.waitUntilSettled()
2934
James E. Blaire68d43f2017-04-19 14:03:30 -07002935 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002936
2937 results = {self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002938 project='org/project1').uuid: ['extratag', 'merge'],
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002939 self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002940 project='org/project2').uuid: ['merge']}
James E. Blair456f2fb2016-02-09 09:29:33 -08002941
2942 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002943 self.assertEqual(results.get(build.uuid, ''),
James E. Blair9d46f092017-07-20 16:06:20 -07002944 build.parameters['zuul'].get('jobtags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002945
James E. Blair63bb0ef2013-07-29 17:14:51 -07002946 def test_timer(self):
2947 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002948 # This test can not use simple_layout because it must start
2949 # with a configuration which does not include a
2950 # timer-triggered job so that we have an opportunity to set
2951 # the hold flag before the first job.
James E. Blair21037782017-07-19 11:56:55 -07002952 self.create_branch('org/project', 'stable')
Paul Belanger174a8272017-03-14 13:20:10 -04002953 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002954 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002955 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002956
Clark Boylan3ee090a2014-04-03 20:55:09 -07002957 # The pipeline triggers every second, so we should have seen
2958 # several by now.
2959 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002960 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002961
2962 self.assertEqual(len(self.builds), 2)
2963
James E. Blair63bb0ef2013-07-29 17:14:51 -07002964 port = self.webapp.server.socket.getsockname()[1]
2965
Clint Byrum5e729e92017-02-08 06:34:57 -08002966 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002967 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002968 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002969 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002970
Paul Belanger174a8272017-03-14 13:20:10 -04002971 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002972 # Stop queuing timer triggered jobs so that the assertions
2973 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002974 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002975 self.sched.reconfigure(self.config)
James E. Blair78ae4782017-08-02 14:19:10 -07002976 self.waitUntilSettled()
2977 # If APScheduler is in mid-event when we remove the job, we
2978 # can end up with one more event firing, so give it an extra
2979 # second to settle.
2980 time.sleep(1)
2981 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002982 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002983 self.waitUntilSettled()
2984
James E. Blair21037782017-07-19 11:56:55 -07002985 self.assertHistory([
2986 dict(name='project-bitrot', result='SUCCESS',
2987 ref='refs/heads/master'),
2988 dict(name='project-bitrot', result='SUCCESS',
2989 ref='refs/heads/stable'),
2990 ], ordered=False)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002991
2992 data = json.loads(data)
2993 status_jobs = set()
2994 for p in data['pipelines']:
2995 for q in p['change_queues']:
2996 for head in q['heads']:
2997 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002998 for job in change['jobs']:
2999 status_jobs.add(job['name'])
James E. Blair21037782017-07-19 11:56:55 -07003000 self.assertIn('project-bitrot', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003001
James E. Blair4f6033c2014-03-27 15:49:09 -07003002 def test_idle(self):
3003 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003004 # This test can not use simple_layout because it must start
3005 # with a configuration which does not include a
3006 # timer-triggered job so that we have an opportunity to set
3007 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04003008 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07003009
Clark Boylan3ee090a2014-04-03 20:55:09 -07003010 for x in range(1, 3):
3011 # Test that timer triggers periodic jobs even across
3012 # layout config reloads.
3013 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003014 self.commitConfigUpdate('common-config',
3015 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003016 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003017 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07003018
Clark Boylan3ee090a2014-04-03 20:55:09 -07003019 # The pipeline triggers every second, so we should have seen
3020 # several by now.
3021 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07003022
3023 # Stop queuing timer triggered jobs so that the assertions
3024 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003025 self.commitConfigUpdate('common-config',
3026 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003027 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003028 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003029 # If APScheduler is in mid-event when we remove the job,
3030 # we can end up with one more event firing, so give it an
3031 # extra second to settle.
3032 time.sleep(1)
3033 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07003034 self.assertEqual(len(self.builds), 1,
Clint Byrum58264dc2017-02-07 21:21:22 -08003035 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04003036 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003037 self.waitUntilSettled()
3038 self.assertEqual(len(self.builds), 0)
James E. Blair21037782017-07-19 11:56:55 -07003039 self.assertEqual(len(self.history), x)
James E. Blair4f6033c2014-03-27 15:49:09 -07003040
James E. Blaircc324192017-04-19 14:36:08 -07003041 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003042 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003043 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3044 self.waitUntilSettled()
3045
3046 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3047 self.waitUntilSettled()
3048
James E. Blairff80a2f2013-12-27 13:24:06 -08003049 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003050
3051 # A.messages only holds what FakeGerrit places in it. Thus we
3052 # work on the knowledge of what the first message should be as
3053 # it is only configured to go to SMTP.
3054
3055 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003056 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003057 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003058 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003059 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003060 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003061
3062 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003063 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003064 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003065 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003066 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003067 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003068
James E. Blaire5910202013-12-27 09:50:31 -08003069 def test_timer_smtp(self):
3070 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003071 # This test can not use simple_layout because it must start
3072 # with a configuration which does not include a
3073 # timer-triggered job so that we have an opportunity to set
3074 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04003075 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003076 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08003077 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08003078
Clark Boylan3ee090a2014-04-03 20:55:09 -07003079 # The pipeline triggers every second, so we should have seen
3080 # several by now.
3081 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003082 self.waitUntilSettled()
3083
Clark Boylan3ee090a2014-04-03 20:55:09 -07003084 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003085 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003086 self.waitUntilSettled()
3087 self.assertEqual(len(self.history), 2)
3088
James E. Blaire5910202013-12-27 09:50:31 -08003089 self.assertEqual(self.getJobFromHistory(
3090 'project-bitrot-stable-old').result, 'SUCCESS')
3091 self.assertEqual(self.getJobFromHistory(
3092 'project-bitrot-stable-older').result, 'SUCCESS')
3093
James E. Blairff80a2f2013-12-27 13:24:06 -08003094 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003095
3096 # A.messages only holds what FakeGerrit places in it. Thus we
3097 # work on the knowledge of what the first message should be as
3098 # it is only configured to go to SMTP.
3099
3100 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003101 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003102 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003103 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003104 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003105 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003106
Clark Boylan3ee090a2014-04-03 20:55:09 -07003107 # Stop queuing timer triggered jobs and let any that may have
3108 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003109 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003110 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07003111 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003112 # If APScheduler is in mid-event when we remove the job, we
3113 # can end up with one more event firing, so give it an extra
3114 # second to settle.
3115 time.sleep(1)
3116 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003117 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003118 self.waitUntilSettled()
3119
Joshua Hesketh25695cb2017-03-06 12:50:04 +11003120 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003121 def test_timer_sshkey(self):
3122 "Test that a periodic job can setup SSH key authentication"
3123 self.worker.hold_jobs_in_build = True
3124 self.config.set('zuul', 'layout_config',
3125 'tests/fixtures/layout-timer.yaml')
3126 self.sched.reconfigure(self.config)
3127 self.registerJobs()
3128
3129 # The pipeline triggers every second, so we should have seen
3130 # several by now.
3131 time.sleep(5)
3132 self.waitUntilSettled()
3133
3134 self.assertEqual(len(self.builds), 2)
3135
3136 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3137 self.assertTrue(os.path.isfile(ssh_wrapper))
3138 with open(ssh_wrapper) as f:
3139 ssh_wrapper_content = f.read()
3140 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3141 # In the unit tests Merger runs in the same process,
3142 # so we see its' environment variables
3143 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3144
3145 self.worker.release('.*')
3146 self.waitUntilSettled()
3147 self.assertEqual(len(self.history), 2)
3148
3149 self.assertEqual(self.getJobFromHistory(
3150 'project-bitrot-stable-old').result, 'SUCCESS')
3151 self.assertEqual(self.getJobFromHistory(
3152 'project-bitrot-stable-older').result, 'SUCCESS')
3153
3154 # Stop queuing timer triggered jobs and let any that may have
3155 # queued through so that end of test assertions pass.
3156 self.config.set('zuul', 'layout_config',
3157 'tests/fixtures/layout-no-timer.yaml')
3158 self.sched.reconfigure(self.config)
3159 self.registerJobs()
3160 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003161 # If APScheduler is in mid-event when we remove the job, we
3162 # can end up with one more event firing, so give it an extra
3163 # second to settle.
3164 time.sleep(1)
3165 self.waitUntilSettled()
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003166 self.worker.release('.*')
3167 self.waitUntilSettled()
3168
James E. Blair91e34592015-07-31 16:45:59 -07003169 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003170 "Test that the RPC client can enqueue a change"
3171 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003172 A.addApproval('Code-Review', 2)
3173 A.addApproval('Approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003174
3175 client = zuul.rpcclient.RPCClient('127.0.0.1',
3176 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003177 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003178 r = client.enqueue(tenant='tenant-one',
3179 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003180 project='org/project',
3181 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003182 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003183 self.waitUntilSettled()
3184 self.assertEqual(self.getJobFromHistory('project-merge').result,
3185 'SUCCESS')
3186 self.assertEqual(self.getJobFromHistory('project-test1').result,
3187 'SUCCESS')
3188 self.assertEqual(self.getJobFromHistory('project-test2').result,
3189 'SUCCESS')
3190 self.assertEqual(A.data['status'], 'MERGED')
3191 self.assertEqual(A.reported, 2)
3192 self.assertEqual(r, True)
3193
James E. Blair91e34592015-07-31 16:45:59 -07003194 def test_client_enqueue_ref(self):
3195 "Test that the RPC client can enqueue a ref"
James E. Blair289f5932017-07-27 15:02:29 -07003196 p = "review.example.com/org/project"
3197 upstream = self.getUpstreamRepos([p])
3198 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3199 A.setMerged()
3200 A_commit = str(upstream[p].commit('master'))
3201 self.log.debug("A commit: %s" % A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003202
3203 client = zuul.rpcclient.RPCClient('127.0.0.1',
3204 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003205 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07003206 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003207 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003208 pipeline='post',
3209 project='org/project',
3210 trigger='gerrit',
3211 ref='master',
3212 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
James E. Blair289f5932017-07-27 15:02:29 -07003213 newrev=A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003214 self.waitUntilSettled()
3215 job_names = [x.name for x in self.history]
3216 self.assertEqual(len(self.history), 1)
3217 self.assertIn('project-post', job_names)
3218 self.assertEqual(r, True)
3219
James E. Blairad28e912013-11-27 10:43:22 -08003220 def test_client_enqueue_negative(self):
3221 "Test that the RPC client returns errors"
3222 client = zuul.rpcclient.RPCClient('127.0.0.1',
3223 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003224 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003225 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003226 "Invalid tenant"):
3227 r = client.enqueue(tenant='tenant-foo',
3228 pipeline='gate',
3229 project='org/project',
3230 trigger='gerrit',
3231 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003232 self.assertEqual(r, False)
3233
3234 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003235 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003236 r = client.enqueue(tenant='tenant-one',
3237 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003238 project='project-does-not-exist',
3239 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003240 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003241 self.assertEqual(r, False)
3242
3243 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3244 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003245 r = client.enqueue(tenant='tenant-one',
3246 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003247 project='org/project',
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 trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003254 r = client.enqueue(tenant='tenant-one',
3255 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003256 project='org/project',
3257 trigger='trigger-does-not-exist',
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 change"):
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='gerrit',
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 self.waitUntilSettled()
3271 self.assertEqual(len(self.history), 0)
3272 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003273
3274 def test_client_promote(self):
3275 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003276 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003277 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3278 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3279 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003280 A.addApproval('Code-Review', 2)
3281 B.addApproval('Code-Review', 2)
3282 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003283
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003284 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3285 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3286 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003287
3288 self.waitUntilSettled()
3289
Paul Belangerbaca3132016-11-04 12:49:54 -04003290 tenant = self.sched.abide.tenants.get('tenant-one')
3291 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003292 enqueue_times = {}
3293 for item in items:
3294 enqueue_times[str(item.change)] = item.enqueue_time
3295
James E. Blair36658cf2013-12-06 17:53:48 -08003296 client = zuul.rpcclient.RPCClient('127.0.0.1',
3297 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003298 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003299 r = client.promote(tenant='tenant-one',
3300 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003301 change_ids=['2,1', '3,1'])
3302
Sean Daguef39b9ca2014-01-10 21:34:35 -05003303 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003304 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003305 for item in items:
3306 self.assertEqual(
3307 enqueue_times[str(item.change)], item.enqueue_time)
3308
James E. Blair78acec92014-02-06 07:11:32 -08003309 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003310 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003311 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003312 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003313 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003314 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003315 self.waitUntilSettled()
3316
3317 self.assertEqual(len(self.builds), 6)
3318 self.assertEqual(self.builds[0].name, 'project-test1')
3319 self.assertEqual(self.builds[1].name, 'project-test2')
3320 self.assertEqual(self.builds[2].name, 'project-test1')
3321 self.assertEqual(self.builds[3].name, 'project-test2')
3322 self.assertEqual(self.builds[4].name, 'project-test1')
3323 self.assertEqual(self.builds[5].name, 'project-test2')
3324
Paul Belangerbaca3132016-11-04 12:49:54 -04003325 self.assertTrue(self.builds[0].hasChanges(B))
3326 self.assertFalse(self.builds[0].hasChanges(A))
3327 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003328
Paul Belangerbaca3132016-11-04 12:49:54 -04003329 self.assertTrue(self.builds[2].hasChanges(B))
3330 self.assertTrue(self.builds[2].hasChanges(C))
3331 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003332
Paul Belangerbaca3132016-11-04 12:49:54 -04003333 self.assertTrue(self.builds[4].hasChanges(B))
3334 self.assertTrue(self.builds[4].hasChanges(C))
3335 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003336
Paul Belanger174a8272017-03-14 13:20:10 -04003337 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003338 self.waitUntilSettled()
3339
3340 self.assertEqual(A.data['status'], 'MERGED')
3341 self.assertEqual(A.reported, 2)
3342 self.assertEqual(B.data['status'], 'MERGED')
3343 self.assertEqual(B.reported, 2)
3344 self.assertEqual(C.data['status'], 'MERGED')
3345 self.assertEqual(C.reported, 2)
3346
James E. Blair36658cf2013-12-06 17:53:48 -08003347 self.assertEqual(r, True)
3348
3349 def test_client_promote_dependent(self):
3350 "Test that the RPC client can promote a dependent change"
3351 # C (depends on B) -> B -> A ; then promote C to get:
3352 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003353 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003354 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3355 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3356 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3357
3358 C.setDependsOn(B, 1)
3359
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003360 A.addApproval('Code-Review', 2)
3361 B.addApproval('Code-Review', 2)
3362 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003363
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003364 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3365 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3366 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003367
3368 self.waitUntilSettled()
3369
3370 client = zuul.rpcclient.RPCClient('127.0.0.1',
3371 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003372 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003373 r = client.promote(tenant='tenant-one',
3374 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003375 change_ids=['3,1'])
3376
James E. Blair78acec92014-02-06 07:11:32 -08003377 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003378 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003379 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003380 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003381 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003382 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003383 self.waitUntilSettled()
3384
3385 self.assertEqual(len(self.builds), 6)
3386 self.assertEqual(self.builds[0].name, 'project-test1')
3387 self.assertEqual(self.builds[1].name, 'project-test2')
3388 self.assertEqual(self.builds[2].name, 'project-test1')
3389 self.assertEqual(self.builds[3].name, 'project-test2')
3390 self.assertEqual(self.builds[4].name, 'project-test1')
3391 self.assertEqual(self.builds[5].name, 'project-test2')
3392
Paul Belangerbaca3132016-11-04 12:49:54 -04003393 self.assertTrue(self.builds[0].hasChanges(B))
3394 self.assertFalse(self.builds[0].hasChanges(A))
3395 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003396
Paul Belangerbaca3132016-11-04 12:49:54 -04003397 self.assertTrue(self.builds[2].hasChanges(B))
3398 self.assertTrue(self.builds[2].hasChanges(C))
3399 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003400
Paul Belangerbaca3132016-11-04 12:49:54 -04003401 self.assertTrue(self.builds[4].hasChanges(B))
3402 self.assertTrue(self.builds[4].hasChanges(C))
3403 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003404
Paul Belanger174a8272017-03-14 13:20:10 -04003405 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003406 self.waitUntilSettled()
3407
3408 self.assertEqual(A.data['status'], 'MERGED')
3409 self.assertEqual(A.reported, 2)
3410 self.assertEqual(B.data['status'], 'MERGED')
3411 self.assertEqual(B.reported, 2)
3412 self.assertEqual(C.data['status'], 'MERGED')
3413 self.assertEqual(C.reported, 2)
3414
James E. Blair36658cf2013-12-06 17:53:48 -08003415 self.assertEqual(r, True)
3416
3417 def test_client_promote_negative(self):
3418 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003419 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003420 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003421 A.addApproval('Code-Review', 2)
3422 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003423 self.waitUntilSettled()
3424
3425 client = zuul.rpcclient.RPCClient('127.0.0.1',
3426 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003427 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003428
3429 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003430 r = client.promote(tenant='tenant-one',
3431 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003432 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003433 self.assertEqual(r, False)
3434
3435 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003436 r = client.promote(tenant='tenant-one',
3437 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003438 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003439 self.assertEqual(r, False)
3440
Paul Belanger174a8272017-03-14 13:20:10 -04003441 self.executor_server.hold_jobs_in_build = False
3442 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003443 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003444
James E. Blaircc324192017-04-19 14:36:08 -07003445 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003446 def test_queue_rate_limiting(self):
3447 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003448 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003449 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3450 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3451 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3452
3453 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003454 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003455
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003456 A.addApproval('Code-Review', 2)
3457 B.addApproval('Code-Review', 2)
3458 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003459
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003460 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3461 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3462 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003463 self.waitUntilSettled()
3464
3465 # Only A and B will have their merge jobs queued because
3466 # window is 2.
3467 self.assertEqual(len(self.builds), 2)
3468 self.assertEqual(self.builds[0].name, 'project-merge')
3469 self.assertEqual(self.builds[1].name, 'project-merge')
3470
James E. Blair732ee862017-04-21 07:49:26 -07003471 # Release the merge jobs one at a time.
3472 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003473 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003474 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003475 self.waitUntilSettled()
3476
3477 # Only A and B will have their test jobs queued because
3478 # window is 2.
3479 self.assertEqual(len(self.builds), 4)
3480 self.assertEqual(self.builds[0].name, 'project-test1')
3481 self.assertEqual(self.builds[1].name, 'project-test2')
3482 self.assertEqual(self.builds[2].name, 'project-test1')
3483 self.assertEqual(self.builds[3].name, 'project-test2')
3484
Paul Belanger174a8272017-03-14 13:20:10 -04003485 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003486 self.waitUntilSettled()
3487
James E. Blaircc324192017-04-19 14:36:08 -07003488 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003489 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003490 # A failed so window is reduced by 1 to 1.
3491 self.assertEqual(queue.window, 1)
3492 self.assertEqual(queue.window_floor, 1)
3493 self.assertEqual(A.data['status'], 'NEW')
3494
3495 # Gate is reset and only B's merge job is queued because
3496 # window shrunk to 1.
3497 self.assertEqual(len(self.builds), 1)
3498 self.assertEqual(self.builds[0].name, 'project-merge')
3499
Paul Belanger174a8272017-03-14 13:20:10 -04003500 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003501 self.waitUntilSettled()
3502
3503 # Only B's test jobs are queued because window is still 1.
3504 self.assertEqual(len(self.builds), 2)
3505 self.assertEqual(self.builds[0].name, 'project-test1')
3506 self.assertEqual(self.builds[1].name, 'project-test2')
3507
Paul Belanger174a8272017-03-14 13:20:10 -04003508 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003509 self.waitUntilSettled()
3510
3511 # B was successfully merged so window is increased to 2.
3512 self.assertEqual(queue.window, 2)
3513 self.assertEqual(queue.window_floor, 1)
3514 self.assertEqual(B.data['status'], 'MERGED')
3515
3516 # Only C is left and its merge job is queued.
3517 self.assertEqual(len(self.builds), 1)
3518 self.assertEqual(self.builds[0].name, 'project-merge')
3519
Paul Belanger174a8272017-03-14 13:20:10 -04003520 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003521 self.waitUntilSettled()
3522
3523 # After successful merge job the test jobs for C are queued.
3524 self.assertEqual(len(self.builds), 2)
3525 self.assertEqual(self.builds[0].name, 'project-test1')
3526 self.assertEqual(self.builds[1].name, 'project-test2')
3527
Paul Belanger174a8272017-03-14 13:20:10 -04003528 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003529 self.waitUntilSettled()
3530
3531 # C successfully merged so window is bumped to 3.
3532 self.assertEqual(queue.window, 3)
3533 self.assertEqual(queue.window_floor, 1)
3534 self.assertEqual(C.data['status'], 'MERGED')
3535
James E. Blaircc324192017-04-19 14:36:08 -07003536 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003537 def test_queue_rate_limiting_dependent(self):
3538 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003539 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003540 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3541 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3542 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3543
3544 B.setDependsOn(A, 1)
3545
Paul Belanger174a8272017-03-14 13:20:10 -04003546 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003547
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003548 A.addApproval('Code-Review', 2)
3549 B.addApproval('Code-Review', 2)
3550 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003551
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003552 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3553 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3554 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003555 self.waitUntilSettled()
3556
3557 # Only A and B will have their merge jobs queued because
3558 # window is 2.
3559 self.assertEqual(len(self.builds), 2)
3560 self.assertEqual(self.builds[0].name, 'project-merge')
3561 self.assertEqual(self.builds[1].name, 'project-merge')
3562
James E. Blairf4a5f022017-04-18 14:01:10 -07003563 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003564
3565 # Only A and B will have their test jobs queued because
3566 # window is 2.
3567 self.assertEqual(len(self.builds), 4)
3568 self.assertEqual(self.builds[0].name, 'project-test1')
3569 self.assertEqual(self.builds[1].name, 'project-test2')
3570 self.assertEqual(self.builds[2].name, 'project-test1')
3571 self.assertEqual(self.builds[3].name, 'project-test2')
3572
Paul Belanger174a8272017-03-14 13:20:10 -04003573 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003574 self.waitUntilSettled()
3575
James E. Blaircc324192017-04-19 14:36:08 -07003576 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003577 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003578 # A failed so window is reduced by 1 to 1.
3579 self.assertEqual(queue.window, 1)
3580 self.assertEqual(queue.window_floor, 1)
3581 self.assertEqual(A.data['status'], 'NEW')
3582 self.assertEqual(B.data['status'], 'NEW')
3583
3584 # Gate is reset and only C's merge job is queued because
3585 # window shrunk to 1 and A and B were dequeued.
3586 self.assertEqual(len(self.builds), 1)
3587 self.assertEqual(self.builds[0].name, 'project-merge')
3588
James E. Blairf4a5f022017-04-18 14:01:10 -07003589 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003590
3591 # Only C's test jobs are queued because window is still 1.
3592 self.assertEqual(len(self.builds), 2)
James E. Blairdf25ddc2017-07-08 07:57:09 -07003593 builds = self.getSortedBuilds()
3594 self.assertEqual(builds[0].name, 'project-test1')
3595 self.assertEqual(builds[1].name, 'project-test2')
Clark Boylan7603a372014-01-21 11:43:20 -08003596
Paul Belanger174a8272017-03-14 13:20:10 -04003597 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003598 self.waitUntilSettled()
3599
3600 # C was successfully merged so window is increased to 2.
3601 self.assertEqual(queue.window, 2)
3602 self.assertEqual(queue.window_floor, 1)
3603 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003604
3605 def test_worker_update_metadata(self):
3606 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003607 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003608
3609 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003610 A.addApproval('Code-Review', 2)
3611 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003612 self.waitUntilSettled()
3613
Clint Byrumd4de79a2017-05-27 11:39:13 -07003614 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003615
3616 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07003617 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003618
3619 start = time.time()
3620 while True:
3621 if time.time() - start > 10:
3622 raise Exception("Timeout waiting for gearman server to report "
3623 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07003624 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003625 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08003626 break
3627 else:
3628 time.sleep(0)
3629
3630 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05003631 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003632
Paul Belanger174a8272017-03-14 13:20:10 -04003633 self.executor_server.hold_jobs_in_build = False
3634 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003635 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003636
James E. Blaircc324192017-04-19 14:36:08 -07003637 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003638 def test_footer_message(self):
3639 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003640 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003641 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003642 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003643 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003644 self.waitUntilSettled()
3645
3646 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003647 B.addApproval('Code-Review', 2)
3648 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003649 self.waitUntilSettled()
3650
3651 self.assertEqual(2, len(self.smtp_messages))
3652
Adam Gandelman82258fd2017-02-07 16:51:58 -08003653 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003654Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003655http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003656
Adam Gandelman82258fd2017-02-07 16:51:58 -08003657 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003658For CI problems and help debugging, contact ci@example.org"""
3659
Adam Gandelman82258fd2017-02-07 16:51:58 -08003660 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3661 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3662 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3663 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003664
Tobias Henkel9842bd72017-05-16 13:40:03 +02003665 @simple_layout('layouts/unmanaged-project.yaml')
3666 def test_unmanaged_project_start_message(self):
3667 "Test start reporting is not done for unmanaged projects."
3668 self.init_repo("org/project", tag='init')
3669 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3670 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3671 self.waitUntilSettled()
3672
3673 self.assertEqual(0, len(A.messages))
3674
Clint Byrumb9de8be2017-05-27 13:49:19 -07003675 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003676 def test_merge_failure_reporters(self):
3677 """Check that the config is set up correctly"""
3678
Clint Byrumb9de8be2017-05-27 13:49:19 -07003679 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11003680 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003681 "Merge Failed.\n\nThis change or one of its cross-repo "
3682 "dependencies was unable to be automatically merged with the "
3683 "current state of its repository. Please rebase the change and "
3684 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003685 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003686 self.assertEqual(
3687 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003688 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003689
3690 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003691 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11003692 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003693 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11003694
3695 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003696 tenant.layout.pipelines['check'].merge_failure_actions[0],
3697 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003698
3699 self.assertTrue(
3700 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003701 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003702 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003703 zuul.driver.smtp.smtpreporter.SMTPReporter) and
3704 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003705 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003706 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003707 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003708 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003709 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003710 gerritreporter.GerritReporter) and
3711 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003712 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003713 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003714 )
3715 )
3716
James E. Blairec056492016-07-22 09:45:56 -07003717 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003718 def test_merge_failure_reports(self):
3719 """Check that when a change fails to merge the correct message is sent
3720 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003721 self.updateConfigLayout(
3722 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003723 self.sched.reconfigure(self.config)
3724 self.registerJobs()
3725
3726 # Check a test failure isn't reported to SMTP
3727 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003728 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003729 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003730 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003731 self.waitUntilSettled()
3732
3733 self.assertEqual(3, len(self.history)) # 3 jobs
3734 self.assertEqual(0, len(self.smtp_messages))
3735
3736 # Check a merge failure is reported to SMTP
3737 # B should be merged, but C will conflict with B
3738 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3739 B.addPatchset(['conflict'])
3740 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3741 C.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003742 B.addApproval('Code-Review', 2)
3743 C.addApproval('Code-Review', 2)
3744 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3745 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003746 self.waitUntilSettled()
3747
3748 self.assertEqual(6, len(self.history)) # A and B jobs
3749 self.assertEqual(1, len(self.smtp_messages))
3750 self.assertEqual('The merge failed! For more information...',
3751 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003752
James E. Blairec056492016-07-22 09:45:56 -07003753 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003754 def test_default_merge_failure_reports(self):
3755 """Check that the default merge failure reports are correct."""
3756
3757 # A should report success, B should report merge failure.
3758 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3759 A.addPatchset(['conflict'])
3760 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3761 B.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003762 A.addApproval('Code-Review', 2)
3763 B.addApproval('Code-Review', 2)
3764 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3765 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003766 self.waitUntilSettled()
3767
3768 self.assertEqual(3, len(self.history)) # A jobs
3769 self.assertEqual(A.reported, 2)
3770 self.assertEqual(B.reported, 2)
3771 self.assertEqual(A.data['status'], 'MERGED')
3772 self.assertEqual(B.data['status'], 'NEW')
3773 self.assertIn('Build succeeded', A.messages[1])
3774 self.assertIn('Merge Failed', B.messages[1])
3775 self.assertIn('automatically merged', B.messages[1])
3776 self.assertNotIn('logs.example.com', B.messages[1])
3777 self.assertNotIn('SKIPPED', B.messages[1])
3778
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003779 def test_client_get_running_jobs(self):
3780 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003781 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003782 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003783 A.addApproval('Code-Review', 2)
3784 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003785 self.waitUntilSettled()
3786
3787 client = zuul.rpcclient.RPCClient('127.0.0.1',
3788 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003789 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003790
3791 # Wait for gearman server to send the initial workData back to zuul
3792 start = time.time()
3793 while True:
3794 if time.time() - start > 10:
3795 raise Exception("Timeout waiting for gearman server to report "
3796 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003797 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003798 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003799 break
3800 else:
3801 time.sleep(0)
3802
3803 running_items = client.get_running_jobs()
3804
3805 self.assertEqual(1, len(running_items))
3806 running_item = running_items[0]
3807 self.assertEqual([], running_item['failing_reasons'])
3808 self.assertEqual([], running_item['items_behind'])
3809 self.assertEqual('https://hostname/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003810 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003811 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003812 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003813 self.assertEqual(True, running_item['active'])
3814 self.assertEqual('1,1', running_item['id'])
3815
3816 self.assertEqual(3, len(running_item['jobs']))
3817 for job in running_item['jobs']:
3818 if job['name'] == 'project-merge':
3819 self.assertEqual('project-merge', job['name'])
3820 self.assertEqual('gate', job['pipeline'])
3821 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05003822 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003823 'static/stream.html?uuid={uuid}&logfile=console.log'
3824 .format(uuid=job['uuid']), job['url'])
3825 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05003826 'finger://{hostname}/{uuid}'.format(
3827 hostname=self.executor_server.hostname,
3828 uuid=job['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003829 job['finger_url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07003830 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003831 self.assertEqual(False, job['canceled'])
3832 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003833 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003834 self.assertEqual('gate', job['pipeline'])
3835 break
3836
Paul Belanger174a8272017-03-14 13:20:10 -04003837 self.executor_server.hold_jobs_in_build = False
3838 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003839 self.waitUntilSettled()
3840
3841 running_items = client.get_running_jobs()
3842 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003843
James E. Blairb70e55a2017-04-19 12:57:02 -07003844 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003845 def test_nonvoting_pipeline(self):
3846 "Test that a nonvoting pipeline (experimental) can still report"
3847
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003848 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3849 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003850 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3851 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003852
3853 self.assertEqual(self.getJobFromHistory('project-merge').result,
3854 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003855 self.assertEqual(
3856 self.getJobFromHistory('experimental-project-test').result,
3857 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003858 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003859
3860 def test_crd_gate(self):
3861 "Test cross-repo dependencies"
3862 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3863 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003864 A.addApproval('Code-Review', 2)
3865 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003866
3867 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3868 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3869 AM2.setMerged()
3870 AM1.setMerged()
3871
3872 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3873 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3874 BM2.setMerged()
3875 BM1.setMerged()
3876
3877 # A -> AM1 -> AM2
3878 # B -> BM1 -> BM2
3879 # A Depends-On: B
3880 # M2 is here to make sure it is never queried. If it is, it
3881 # means zuul is walking down the entire history of merged
3882 # changes.
3883
3884 B.setDependsOn(BM1, 1)
3885 BM1.setDependsOn(BM2, 1)
3886
3887 A.setDependsOn(AM1, 1)
3888 AM1.setDependsOn(AM2, 1)
3889
3890 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3891 A.subject, B.data['id'])
3892
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003893 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003894 self.waitUntilSettled()
3895
3896 self.assertEqual(A.data['status'], 'NEW')
3897 self.assertEqual(B.data['status'], 'NEW')
3898
Paul Belanger5dccbe72016-11-14 11:17:37 -05003899 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003900 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003901
Paul Belanger174a8272017-03-14 13:20:10 -04003902 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003903 B.addApproval('Approved', 1)
3904 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003905 self.waitUntilSettled()
3906
Paul Belanger174a8272017-03-14 13:20:10 -04003907 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003908 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003909 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003910 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003911 self.executor_server.hold_jobs_in_build = False
3912 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003913 self.waitUntilSettled()
3914
3915 self.assertEqual(AM2.queried, 0)
3916 self.assertEqual(BM2.queried, 0)
3917 self.assertEqual(A.data['status'], 'MERGED')
3918 self.assertEqual(B.data['status'], 'MERGED')
3919 self.assertEqual(A.reported, 2)
3920 self.assertEqual(B.reported, 2)
3921
Paul Belanger5dccbe72016-11-14 11:17:37 -05003922 changes = self.getJobFromHistory(
3923 'project-merge', 'org/project1').changes
3924 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003925
3926 def test_crd_branch(self):
3927 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003928
3929 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003930 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3931 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003932 C1 = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C1')
3933 C2 = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C2',
3934 status='ABANDONED')
3935 C1.data['id'] = B.data['id']
3936 C2.data['id'] = B.data['id']
3937
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003938 A.addApproval('Code-Review', 2)
3939 B.addApproval('Code-Review', 2)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003940 C1.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003941
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003942 # A Depends-On: B+C1
James E. Blair8f78d882015-02-05 08:51:37 -08003943 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3944 A.subject, B.data['id'])
3945
Paul Belanger174a8272017-03-14 13:20:10 -04003946 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003947 B.addApproval('Approved', 1)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003948 C1.addApproval('Approved', 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003949 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003950 self.waitUntilSettled()
3951
Paul Belanger174a8272017-03-14 13:20:10 -04003952 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003953 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003954 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003955 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003956 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003957 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003958 self.executor_server.hold_jobs_in_build = False
3959 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003960 self.waitUntilSettled()
3961
3962 self.assertEqual(A.data['status'], 'MERGED')
3963 self.assertEqual(B.data['status'], 'MERGED')
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003964 self.assertEqual(C1.data['status'], 'MERGED')
James E. Blair8f78d882015-02-05 08:51:37 -08003965 self.assertEqual(A.reported, 2)
3966 self.assertEqual(B.reported, 2)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003967 self.assertEqual(C1.reported, 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003968
Paul Belanger6379db12016-11-14 13:57:54 -05003969 changes = self.getJobFromHistory(
3970 'project-merge', 'org/project1').changes
3971 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003972
3973 def test_crd_multiline(self):
3974 "Test multiple depends-on lines in commit"
3975 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3976 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3977 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003978 A.addApproval('Code-Review', 2)
3979 B.addApproval('Code-Review', 2)
3980 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003981
3982 # A Depends-On: B+C
3983 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3984 A.subject, B.data['id'], C.data['id'])
3985
Paul Belanger174a8272017-03-14 13:20:10 -04003986 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003987 B.addApproval('Approved', 1)
3988 C.addApproval('Approved', 1)
3989 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003990 self.waitUntilSettled()
3991
Paul Belanger174a8272017-03-14 13:20:10 -04003992 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003993 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003994 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003995 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003996 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003997 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003998 self.executor_server.hold_jobs_in_build = False
3999 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08004000 self.waitUntilSettled()
4001
4002 self.assertEqual(A.data['status'], 'MERGED')
4003 self.assertEqual(B.data['status'], 'MERGED')
4004 self.assertEqual(C.data['status'], 'MERGED')
4005 self.assertEqual(A.reported, 2)
4006 self.assertEqual(B.reported, 2)
4007 self.assertEqual(C.reported, 2)
4008
Paul Belanger5dccbe72016-11-14 11:17:37 -05004009 changes = self.getJobFromHistory(
4010 'project-merge', 'org/project1').changes
4011 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08004012
4013 def test_crd_unshared_gate(self):
4014 "Test cross-repo dependencies in unshared gate queues"
4015 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4016 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004017 A.addApproval('Code-Review', 2)
4018 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004019
4020 # A Depends-On: B
4021 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4022 A.subject, B.data['id'])
4023
4024 # A and B do not share a queue, make sure that A is unable to
4025 # enqueue B (and therefore, A is unable to be enqueued).
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004026 B.addApproval('Approved', 1)
4027 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004028 self.waitUntilSettled()
4029
4030 self.assertEqual(A.data['status'], 'NEW')
4031 self.assertEqual(B.data['status'], 'NEW')
4032 self.assertEqual(A.reported, 0)
4033 self.assertEqual(B.reported, 0)
4034 self.assertEqual(len(self.history), 0)
4035
4036 # Enqueue and merge B alone.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004037 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004038 self.waitUntilSettled()
4039
4040 self.assertEqual(B.data['status'], 'MERGED')
4041 self.assertEqual(B.reported, 2)
4042
4043 # Now that B is merged, A should be able to be enqueued and
4044 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004045 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004046 self.waitUntilSettled()
4047
4048 self.assertEqual(A.data['status'], 'MERGED')
4049 self.assertEqual(A.reported, 2)
4050
James E. Blair96698e22015-04-02 07:48:21 -07004051 def test_crd_gate_reverse(self):
4052 "Test reverse cross-repo dependencies"
4053 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4054 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004055 A.addApproval('Code-Review', 2)
4056 B.addApproval('Code-Review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07004057
4058 # A Depends-On: B
4059
4060 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4061 A.subject, B.data['id'])
4062
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004063 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004064 self.waitUntilSettled()
4065
4066 self.assertEqual(A.data['status'], 'NEW')
4067 self.assertEqual(B.data['status'], 'NEW')
4068
Paul Belanger174a8272017-03-14 13:20:10 -04004069 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004070 A.addApproval('Approved', 1)
4071 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004072 self.waitUntilSettled()
4073
Paul Belanger174a8272017-03-14 13:20:10 -04004074 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004075 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004076 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004077 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004078 self.executor_server.hold_jobs_in_build = False
4079 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07004080 self.waitUntilSettled()
4081
4082 self.assertEqual(A.data['status'], 'MERGED')
4083 self.assertEqual(B.data['status'], 'MERGED')
4084 self.assertEqual(A.reported, 2)
4085 self.assertEqual(B.reported, 2)
4086
Paul Belanger5dccbe72016-11-14 11:17:37 -05004087 changes = self.getJobFromHistory(
4088 'project-merge', 'org/project1').changes
4089 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07004090
James E. Blair5ee24252014-12-30 10:12:29 -08004091 def test_crd_cycle(self):
4092 "Test cross-repo dependency cycles"
4093 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4094 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004095 A.addApproval('Code-Review', 2)
4096 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004097
4098 # A -> B -> A (via commit-depends)
4099
4100 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4101 A.subject, B.data['id'])
4102 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4103 B.subject, A.data['id'])
4104
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004105 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004106 self.waitUntilSettled()
4107
4108 self.assertEqual(A.reported, 0)
4109 self.assertEqual(B.reported, 0)
4110 self.assertEqual(A.data['status'], 'NEW')
4111 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004112
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004113 def test_crd_gate_unknown(self):
4114 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004115 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004116 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4117 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004118 A.addApproval('Code-Review', 2)
4119 B.addApproval('Code-Review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004120
4121 # A Depends-On: B
4122 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4123 A.subject, B.data['id'])
4124
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004125 B.addApproval('Approved', 1)
4126 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004127 self.waitUntilSettled()
4128
4129 # Unknown projects cannot share a queue with any other
4130 # since they don't have common jobs with any other (they have no jobs).
4131 # Changes which depend on unknown project changes
4132 # should not be processed in dependent pipeline
4133 self.assertEqual(A.data['status'], 'NEW')
4134 self.assertEqual(B.data['status'], 'NEW')
4135 self.assertEqual(A.reported, 0)
4136 self.assertEqual(B.reported, 0)
4137 self.assertEqual(len(self.history), 0)
4138
James E. Blair7720b402017-06-07 08:51:49 -07004139 # Simulate change B being gated outside this layout Set the
4140 # change merged before submitting the event so that when the
4141 # event triggers a gerrit query to update the change, we get
4142 # the information that it was merged.
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004143 B.setMerged()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004144 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004145 self.waitUntilSettled()
4146 self.assertEqual(len(self.history), 0)
4147
4148 # Now that B is merged, A should be able to be enqueued and
4149 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004150 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004151 self.waitUntilSettled()
4152
4153 self.assertEqual(A.data['status'], 'MERGED')
4154 self.assertEqual(A.reported, 2)
4155 self.assertEqual(B.data['status'], 'MERGED')
4156 self.assertEqual(B.reported, 0)
4157
James E. Blairbfb8e042014-12-30 17:01:44 -08004158 def test_crd_check(self):
4159 "Test cross-repo dependencies in independent pipelines"
4160
Paul Belanger174a8272017-03-14 13:20:10 -04004161 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004162 self.gearman_server.hold_jobs_in_queue = True
4163 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4164 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4165
4166 # A Depends-On: B
4167 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4168 A.subject, B.data['id'])
4169
4170 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4171 self.waitUntilSettled()
4172
James E. Blairbfb8e042014-12-30 17:01:44 -08004173 self.gearman_server.hold_jobs_in_queue = False
4174 self.gearman_server.release()
4175 self.waitUntilSettled()
4176
Paul Belanger174a8272017-03-14 13:20:10 -04004177 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05004178 self.waitUntilSettled()
4179
James E. Blair2a535672017-04-27 12:03:15 -07004180 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08004181
Paul Belanger174a8272017-03-14 13:20:10 -04004182 self.executor_server.hold_jobs_in_build = False
4183 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05004184 self.waitUntilSettled()
4185
James E. Blairbfb8e042014-12-30 17:01:44 -08004186 self.assertEqual(A.data['status'], 'NEW')
4187 self.assertEqual(B.data['status'], 'NEW')
4188 self.assertEqual(A.reported, 1)
4189 self.assertEqual(B.reported, 0)
4190
4191 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004192 tenant = self.sched.abide.tenants.get('tenant-one')
4193 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004194
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004195 def test_crd_check_git_depends(self):
4196 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004197 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004198 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4199 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4200
4201 # Add two git-dependent changes and make sure they both report
4202 # success.
4203 B.setDependsOn(A, 1)
4204 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4205 self.waitUntilSettled()
4206 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4207 self.waitUntilSettled()
4208
James E. Blairb8c16472015-05-05 14:55:26 -07004209 self.orderedRelease()
4210 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004211 self.waitUntilSettled()
4212
4213 self.assertEqual(A.data['status'], 'NEW')
4214 self.assertEqual(B.data['status'], 'NEW')
4215 self.assertEqual(A.reported, 1)
4216 self.assertEqual(B.reported, 1)
4217
4218 self.assertEqual(self.history[0].changes, '1,1')
4219 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004220 tenant = self.sched.abide.tenants.get('tenant-one')
4221 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004222
4223 self.assertIn('Build succeeded', A.messages[0])
4224 self.assertIn('Build succeeded', B.messages[0])
4225
4226 def test_crd_check_duplicate(self):
4227 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004228 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004229 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4230 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004231 tenant = self.sched.abide.tenants.get('tenant-one')
4232 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004233
4234 # Add two git-dependent changes...
4235 B.setDependsOn(A, 1)
4236 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4237 self.waitUntilSettled()
4238 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4239
4240 # ...make sure the live one is not duplicated...
4241 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4242 self.waitUntilSettled()
4243 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4244
4245 # ...but the non-live one is able to be.
4246 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4247 self.waitUntilSettled()
4248 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4249
Clark Boylandd849822015-03-02 12:38:14 -08004250 # Release jobs in order to avoid races with change A jobs
4251 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004252 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004253 self.executor_server.hold_jobs_in_build = False
4254 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004255 self.waitUntilSettled()
4256
4257 self.assertEqual(A.data['status'], 'NEW')
4258 self.assertEqual(B.data['status'], 'NEW')
4259 self.assertEqual(A.reported, 1)
4260 self.assertEqual(B.reported, 1)
4261
4262 self.assertEqual(self.history[0].changes, '1,1 2,1')
4263 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004264 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004265
4266 self.assertIn('Build succeeded', A.messages[0])
4267 self.assertIn('Build succeeded', B.messages[0])
4268
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004269 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004270 "Test cross-repo dependencies re-enqueued in independent pipelines"
4271
4272 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004273 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4274 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004275
4276 # A Depends-On: B
4277 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4278 A.subject, B.data['id'])
4279
4280 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4281 self.waitUntilSettled()
4282
4283 self.sched.reconfigure(self.config)
4284
4285 # Make sure the items still share a change queue, and the
4286 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004287 tenant = self.sched.abide.tenants.get('tenant-one')
4288 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4289 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004290 first_item = queue.queue[0]
4291 for item in queue.queue:
4292 self.assertEqual(item.queue, first_item.queue)
4293 self.assertFalse(first_item.live)
4294 self.assertTrue(queue.queue[1].live)
4295
4296 self.gearman_server.hold_jobs_in_queue = False
4297 self.gearman_server.release()
4298 self.waitUntilSettled()
4299
4300 self.assertEqual(A.data['status'], 'NEW')
4301 self.assertEqual(B.data['status'], 'NEW')
4302 self.assertEqual(A.reported, 1)
4303 self.assertEqual(B.reported, 0)
4304
4305 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004306 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004307
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004308 def test_crd_check_reconfiguration(self):
4309 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4310
4311 def test_crd_undefined_project(self):
4312 """Test that undefined projects in dependencies are handled for
4313 independent pipelines"""
4314 # It's a hack for fake gerrit,
4315 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004316 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004317 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4318
Jesse Keatingea68ab42017-04-19 15:03:30 -07004319 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004320 def test_crd_check_ignore_dependencies(self):
4321 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004322
4323 self.gearman_server.hold_jobs_in_queue = True
4324 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4325 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4326 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4327
4328 # A Depends-On: B
4329 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4330 A.subject, B.data['id'])
4331 # C git-depends on B
4332 C.setDependsOn(B, 1)
4333 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4334 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4335 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4336 self.waitUntilSettled()
4337
4338 # Make sure none of the items share a change queue, and all
4339 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004340 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004341 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004342 self.assertEqual(len(check_pipeline.queues), 3)
4343 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4344 for item in check_pipeline.getAllItems():
4345 self.assertTrue(item.live)
4346
4347 self.gearman_server.hold_jobs_in_queue = False
4348 self.gearman_server.release()
4349 self.waitUntilSettled()
4350
4351 self.assertEqual(A.data['status'], 'NEW')
4352 self.assertEqual(B.data['status'], 'NEW')
4353 self.assertEqual(C.data['status'], 'NEW')
4354 self.assertEqual(A.reported, 1)
4355 self.assertEqual(B.reported, 1)
4356 self.assertEqual(C.reported, 1)
4357
4358 # Each job should have tested exactly one change
4359 for job in self.history:
4360 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004361
James E. Blair7cb84542017-04-19 13:35:05 -07004362 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004363 def test_crd_check_transitive(self):
4364 "Test transitive cross-repo dependencies"
4365 # Specifically, if A -> B -> C, and C gets a new patchset and
4366 # A gets a new patchset, ensure the test of A,2 includes B,1
4367 # and C,2 (not C,1 which would indicate stale data in the
4368 # cache for B).
4369 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4370 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4371 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4372
4373 # A Depends-On: B
4374 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4375 A.subject, B.data['id'])
4376
4377 # B Depends-On: C
4378 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4379 B.subject, C.data['id'])
4380
4381 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4382 self.waitUntilSettled()
4383 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4384
4385 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4386 self.waitUntilSettled()
4387 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4388
4389 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4390 self.waitUntilSettled()
4391 self.assertEqual(self.history[-1].changes, '3,1')
4392
4393 C.addPatchset()
4394 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4395 self.waitUntilSettled()
4396 self.assertEqual(self.history[-1].changes, '3,2')
4397
4398 A.addPatchset()
4399 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4400 self.waitUntilSettled()
4401 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004402
James E. Blaira8b90b32016-08-24 15:18:50 -07004403 def test_crd_check_unknown(self):
4404 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004405 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004406 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4407 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4408 # A Depends-On: B
4409 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4410 A.subject, B.data['id'])
4411
4412 # Make sure zuul has seen an event on B.
4413 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4414 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4415 self.waitUntilSettled()
4416
4417 self.assertEqual(A.data['status'], 'NEW')
4418 self.assertEqual(A.reported, 1)
4419 self.assertEqual(B.data['status'], 'NEW')
4420 self.assertEqual(B.reported, 0)
4421
James E. Blair92464a22016-04-05 10:21:26 -07004422 def test_crd_cycle_join(self):
4423 "Test an updated change creates a cycle"
4424 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4425
4426 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4427 self.waitUntilSettled()
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004428 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004429
4430 # Create B->A
4431 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4432 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4433 B.subject, A.data['id'])
4434 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4435 self.waitUntilSettled()
4436
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004437 # Dep is there so zuul should have reported on B
4438 self.assertEqual(B.reported, 1)
4439
James E. Blair92464a22016-04-05 10:21:26 -07004440 # Update A to add A->B (a cycle).
4441 A.addPatchset()
4442 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4443 A.subject, B.data['id'])
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004444 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4445 self.waitUntilSettled()
James E. Blair92464a22016-04-05 10:21:26 -07004446
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004447 # Dependency cycle injected so zuul should not have reported again on A
4448 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004449
4450 # Now if we update B to remove the depends-on, everything
4451 # should be okay. B; A->B
4452
4453 B.addPatchset()
4454 B.data['commitMessage'] = '%s\n' % (B.subject,)
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004455 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4456 self.waitUntilSettled()
Paul Belanger0e155e22016-11-14 14:12:23 -05004457
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004458 # Cycle was removed so now zuul should have reported again on A
4459 self.assertEqual(A.reported, 2)
4460
4461 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
4462 self.waitUntilSettled()
4463 self.assertEqual(B.reported, 2)
James E. Blair92464a22016-04-05 10:21:26 -07004464
Jesse Keatingcc67b122017-04-19 14:54:57 -07004465 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004466 def test_disable_at(self):
4467 "Test a pipeline will only report to the disabled trigger when failing"
4468
Jesse Keatingcc67b122017-04-19 14:54:57 -07004469 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004470 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004471 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004472 0, tenant.layout.pipelines['check']._consecutive_failures)
4473 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004474
4475 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4476 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4477 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4478 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4479 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4480 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4481 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4482 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4483 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4484 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4485 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4486
Paul Belanger174a8272017-03-14 13:20:10 -04004487 self.executor_server.failJob('project-test1', A)
4488 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004489 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004490 self.executor_server.failJob('project-test1', D)
4491 self.executor_server.failJob('project-test1', E)
4492 self.executor_server.failJob('project-test1', F)
4493 self.executor_server.failJob('project-test1', G)
4494 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004495 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004496 self.executor_server.failJob('project-test1', J)
4497 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004498
4499 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4500 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4501 self.waitUntilSettled()
4502
4503 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004504 2, tenant.layout.pipelines['check']._consecutive_failures)
4505 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004506
4507 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4508 self.waitUntilSettled()
4509
4510 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004511 0, tenant.layout.pipelines['check']._consecutive_failures)
4512 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004513
4514 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4515 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4516 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4517 self.waitUntilSettled()
4518
4519 # We should be disabled now
4520 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004521 3, tenant.layout.pipelines['check']._consecutive_failures)
4522 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004523
4524 # We need to wait between each of these patches to make sure the
4525 # smtp messages come back in an expected order
4526 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4527 self.waitUntilSettled()
4528 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4529 self.waitUntilSettled()
4530 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4531 self.waitUntilSettled()
4532
4533 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4534 # leaving a message on each change
4535 self.assertEqual(1, len(A.messages))
4536 self.assertIn('Build failed.', A.messages[0])
4537 self.assertEqual(1, len(B.messages))
4538 self.assertIn('Build failed.', B.messages[0])
4539 self.assertEqual(1, len(C.messages))
4540 self.assertIn('Build succeeded.', C.messages[0])
4541 self.assertEqual(1, len(D.messages))
4542 self.assertIn('Build failed.', D.messages[0])
4543 self.assertEqual(1, len(E.messages))
4544 self.assertIn('Build failed.', E.messages[0])
4545 self.assertEqual(1, len(F.messages))
4546 self.assertIn('Build failed.', F.messages[0])
4547
4548 # The last 3 (GHI) would have only reported via smtp.
4549 self.assertEqual(3, len(self.smtp_messages))
4550 self.assertEqual(0, len(G.messages))
4551 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004552 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004553 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004554 self.assertEqual(0, len(H.messages))
4555 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004556 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004557 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004558 self.assertEqual(0, len(I.messages))
4559 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004560 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004561 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004562
4563 # Now reload the configuration (simulate a HUP) to check the pipeline
4564 # comes out of disabled
4565 self.sched.reconfigure(self.config)
4566
Jesse Keatingcc67b122017-04-19 14:54:57 -07004567 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004568
4569 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004570 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004571 0, tenant.layout.pipelines['check']._consecutive_failures)
4572 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004573
4574 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4575 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4576 self.waitUntilSettled()
4577
4578 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004579 2, tenant.layout.pipelines['check']._consecutive_failures)
4580 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004581
4582 # J and K went back to gerrit
4583 self.assertEqual(1, len(J.messages))
4584 self.assertIn('Build failed.', J.messages[0])
4585 self.assertEqual(1, len(K.messages))
4586 self.assertIn('Build failed.', K.messages[0])
4587 # No more messages reported via smtp
4588 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004589
James E. Blaircc020532017-04-19 13:04:44 -07004590 @simple_layout('layouts/one-job-project.yaml')
4591 def test_one_job_project(self):
4592 "Test that queueing works with one job"
4593 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4594 'master', 'A')
4595 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4596 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004597 A.addApproval('Code-Review', 2)
4598 B.addApproval('Code-Review', 2)
4599 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4600 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blaircc020532017-04-19 13:04:44 -07004601 self.waitUntilSettled()
4602
4603 self.assertEqual(A.data['status'], 'MERGED')
4604 self.assertEqual(A.reported, 2)
4605 self.assertEqual(B.data['status'], 'MERGED')
4606 self.assertEqual(B.reported, 2)
4607
Paul Belanger32023542017-10-06 14:48:50 -04004608 def test_job_aborted(self):
4609 "Test that if a execute server aborts a job, it is run again"
4610 self.executor_server.hold_jobs_in_build = True
4611 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4612 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4613 self.waitUntilSettled()
4614
4615 self.executor_server.release('.*-merge')
4616 self.waitUntilSettled()
4617
4618 self.assertEqual(len(self.builds), 2)
Tobias Henkel26fa8a32017-10-07 07:08:36 +00004619
4620 # first abort
4621 self.builds[0].aborted = True
4622 self.executor_server.release('.*-test*')
4623 self.waitUntilSettled()
4624 self.assertEqual(len(self.builds), 1)
4625
4626 # second abort
4627 self.builds[0].aborted = True
4628 self.executor_server.release('.*-test*')
4629 self.waitUntilSettled()
4630 self.assertEqual(len(self.builds), 1)
4631
4632 # third abort
4633 self.builds[0].aborted = True
4634 self.executor_server.release('.*-test*')
4635 self.waitUntilSettled()
4636 self.assertEqual(len(self.builds), 1)
4637
4638 # fourth abort
Paul Belanger32023542017-10-06 14:48:50 -04004639 self.builds[0].aborted = True
4640 self.executor_server.release('.*-test*')
4641 self.waitUntilSettled()
4642 self.assertEqual(len(self.builds), 1)
4643
4644 self.executor_server.hold_jobs_in_build = False
4645 self.executor_server.release()
4646 self.waitUntilSettled()
4647
Tobias Henkel26fa8a32017-10-07 07:08:36 +00004648 self.assertEqual(len(self.history), 7)
4649 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
Paul Belanger32023542017-10-06 14:48:50 -04004650 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
4651
Paul Belanger71d98172016-11-08 10:56:31 -05004652 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004653 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004654
Paul Belanger174a8272017-03-14 13:20:10 -04004655 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004656 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4657 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4658 self.waitUntilSettled()
4659
Paul Belanger174a8272017-03-14 13:20:10 -04004660 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004661 self.waitUntilSettled()
4662
4663 self.assertEqual(len(self.builds), 2)
4664 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004665 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004666 self.waitUntilSettled()
4667
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004668 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004669 self.assertEqual(len(self.builds), 1,
4670 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004671 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004672 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004673 self.waitUntilSettled()
4674
Paul Belanger174a8272017-03-14 13:20:10 -04004675 self.executor_server.hold_jobs_in_build = False
4676 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004677 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004678 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004679 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4680 self.assertEqual(A.reported, 1)
4681 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004682
James E. Blair15be0e12017-01-03 13:45:20 -08004683 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004684 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004685
4686 self.fake_nodepool.paused = True
4687 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004688 A.addApproval('Code-Review', 2)
4689 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair15be0e12017-01-03 13:45:20 -08004690 self.waitUntilSettled()
4691
4692 self.zk.client.stop()
4693 self.zk.client.start()
4694 self.fake_nodepool.paused = False
4695 self.waitUntilSettled()
4696
4697 self.assertEqual(A.data['status'], 'MERGED')
4698 self.assertEqual(A.reported, 2)
4699
James E. Blair6ab79e02017-01-06 10:10:17 -08004700 def test_nodepool_failure(self):
4701 "Test that jobs are reported after a nodepool failure"
4702
4703 self.fake_nodepool.paused = True
4704 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004705 A.addApproval('Code-Review', 2)
4706 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6ab79e02017-01-06 10:10:17 -08004707 self.waitUntilSettled()
4708
4709 req = self.fake_nodepool.getNodeRequests()[0]
4710 self.fake_nodepool.addFailRequest(req)
4711
4712 self.fake_nodepool.paused = False
4713 self.waitUntilSettled()
4714
4715 self.assertEqual(A.data['status'], 'NEW')
4716 self.assertEqual(A.reported, 2)
4717 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4718 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4719 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4720
Monty Taylor6dc5bc12017-09-29 15:47:31 -05004721 def test_nodepool_priority(self):
4722 "Test that nodes are requested at the correct priority"
4723
4724 self.fake_nodepool.paused = True
4725
4726 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4727 self.fake_gerrit.addEvent(A.getRefUpdatedEvent())
4728
4729 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4730 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4731
4732 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4733 C.addApproval('Code-Review', 2)
4734 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
4735
4736 self.waitUntilSettled()
4737
4738 reqs = self.fake_nodepool.getNodeRequests()
4739
4740 # The requests come back sorted by oid. Since we have three requests
4741 # for the three changes each with a different priority.
4742 # Also they get a serial number based on order they were received
4743 # so the number on the endof the oid should map to order submitted.
4744
4745 # * gate first - high priority - change C
4746 self.assertEqual(reqs[0]['_oid'], '100-0000000002')
4747 self.assertEqual(reqs[0]['node_types'], ['label1'])
4748 # * check second - normal priority - change B
4749 self.assertEqual(reqs[1]['_oid'], '200-0000000001')
4750 self.assertEqual(reqs[1]['node_types'], ['label1'])
4751 # * post third - low priority - change A
4752 # additionally, the post job defined uses an ubuntu-xenial node,
4753 # so we include that check just as an extra verification
4754 self.assertEqual(reqs[2]['_oid'], '300-0000000000')
4755 self.assertEqual(reqs[2]['node_types'], ['ubuntu-xenial'])
4756
4757 self.fake_nodepool.paused = False
4758 self.waitUntilSettled()
4759
James E. Blairfceaf412017-09-29 13:44:57 -07004760 @simple_layout('layouts/multiple-templates.yaml')
4761 def test_multiple_project_templates(self):
4762 # Test that applying multiple project templates to a project
4763 # doesn't alter them when used for a second project.
4764 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4765 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4766 self.waitUntilSettled()
4767
4768 build = self.getJobFromHistory('py27')
4769 self.assertEqual(build.parameters['zuul']['jobtags'], [])
4770
James E. Blair29c77002017-10-05 14:56:35 -07004771 def test_pending_merge_in_reconfig(self):
4772 # Test that if we are waiting for an outstanding merge on
4773 # reconfiguration that we continue to do so.
4774 self.gearman_server.hold_merge_jobs_in_queue = True
4775 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4776 A.setMerged()
4777 self.fake_gerrit.addEvent(A.getRefUpdatedEvent())
4778 self.waitUntilSettled()
4779 # Reconfigure while we still have an outstanding merge job
4780 self.sched.reconfigureTenant(self.sched.abide.tenants['tenant-one'],
4781 None)
4782 self.waitUntilSettled()
4783 # Verify the merge job is still running and that the item is
4784 # in the pipeline
4785 self.assertEqual(len(self.sched.merger.jobs), 1)
4786 tenant = self.sched.abide.tenants.get('tenant-one')
4787 pipeline = tenant.layout.pipelines['post']
4788 self.assertEqual(len(pipeline.getAllItems()), 1)
4789 self.gearman_server.hold_merge_jobs_in_queue = False
4790 self.gearman_server.release()
4791 self.waitUntilSettled()
4792
Arieb6f068c2016-10-09 13:11:06 +03004793
James E. Blaira002b032017-04-18 10:35:48 -07004794class TestExecutor(ZuulTestCase):
4795 tenant_config_file = 'config/single-tenant/main.yaml'
4796
4797 def assertFinalState(self):
4798 # In this test, we expect to shut down in a non-final state,
4799 # so skip these checks.
4800 pass
4801
4802 def assertCleanShutdown(self):
4803 self.log.debug("Assert clean shutdown")
4804
4805 # After shutdown, make sure no jobs are running
4806 self.assertEqual({}, self.executor_server.job_workers)
4807
4808 # Make sure that git.Repo objects have been garbage collected.
4809 repos = []
4810 gc.collect()
4811 for obj in gc.get_objects():
4812 if isinstance(obj, git.Repo):
4813 self.log.debug("Leaked git repo object: %s" % repr(obj))
4814 repos.append(obj)
4815 self.assertEqual(len(repos), 0)
4816
4817 def test_executor_shutdown(self):
4818 "Test that the executor can shut down with jobs running"
4819
4820 self.executor_server.hold_jobs_in_build = True
4821 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004822 A.addApproval('Code-Review', 2)
4823 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaira002b032017-04-18 10:35:48 -07004824 self.waitUntilSettled()
4825
4826
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004827class TestDependencyGraph(ZuulTestCase):
4828 tenant_config_file = 'config/dependency-graph/main.yaml'
4829
4830 def test_dependeny_graph_dispatch_jobs_once(self):
4831 "Test a job in a dependency graph is queued only once"
4832 # Job dependencies, starting with A
4833 # A
4834 # / \
4835 # B C
4836 # / \ / \
4837 # D F E
4838 # |
4839 # G
4840
4841 self.executor_server.hold_jobs_in_build = True
4842 change = self.fake_gerrit.addFakeChange(
4843 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004844 change.addApproval('Code-Review', 2)
4845 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004846
4847 self.waitUntilSettled()
4848 self.assertEqual([b.name for b in self.builds], ['A'])
4849
4850 self.executor_server.release('A')
4851 self.waitUntilSettled()
4852 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4853
4854 self.executor_server.release('B')
4855 self.waitUntilSettled()
4856 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4857
4858 self.executor_server.release('D')
4859 self.waitUntilSettled()
4860 self.assertEqual([b.name for b in self.builds], ['C'])
4861
4862 self.executor_server.release('C')
4863 self.waitUntilSettled()
4864 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4865
4866 self.executor_server.release('F')
4867 self.waitUntilSettled()
4868 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4869
4870 self.executor_server.release('G')
4871 self.waitUntilSettled()
4872 self.assertEqual([b.name for b in self.builds], ['E'])
4873
4874 self.executor_server.release('E')
4875 self.waitUntilSettled()
4876 self.assertEqual(len(self.builds), 0)
4877
4878 self.executor_server.hold_jobs_in_build = False
4879 self.executor_server.release()
4880 self.waitUntilSettled()
4881
4882 self.assertEqual(len(self.builds), 0)
4883 self.assertEqual(len(self.history), 7)
4884
4885 self.assertEqual(change.data['status'], 'MERGED')
4886 self.assertEqual(change.reported, 2)
4887
4888 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4889 "Test that a job waits till all dependencies are successful"
4890 # Job dependencies, starting with A
4891 # A
4892 # / \
4893 # B C*
4894 # / \ / \
4895 # D F E
4896 # |
4897 # G
4898
4899 self.executor_server.hold_jobs_in_build = True
4900 change = self.fake_gerrit.addFakeChange(
4901 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004902 change.addApproval('Code-Review', 2)
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004903
4904 self.executor_server.failJob('C', change)
4905
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004906 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004907
4908 self.waitUntilSettled()
4909 self.assertEqual([b.name for b in self.builds], ['A'])
4910
4911 self.executor_server.release('A')
4912 self.waitUntilSettled()
4913 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4914
4915 self.executor_server.release('B')
4916 self.waitUntilSettled()
4917 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4918
4919 self.executor_server.release('D')
4920 self.waitUntilSettled()
4921 self.assertEqual([b.name for b in self.builds], ['C'])
4922
4923 self.executor_server.release('C')
4924 self.waitUntilSettled()
4925 self.assertEqual(len(self.builds), 0)
4926
4927 self.executor_server.hold_jobs_in_build = False
4928 self.executor_server.release()
4929 self.waitUntilSettled()
4930
4931 self.assertEqual(len(self.builds), 0)
4932 self.assertEqual(len(self.history), 4)
4933
4934 self.assertEqual(change.data['status'], 'NEW')
4935 self.assertEqual(change.reported, 2)
4936
4937
Arieb6f068c2016-10-09 13:11:06 +03004938class TestDuplicatePipeline(ZuulTestCase):
4939 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4940
4941 def test_duplicate_pipelines(self):
4942 "Test that a change matching multiple pipelines works"
4943
4944 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4945 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4946 self.waitUntilSettled()
4947
4948 self.assertHistory([
4949 dict(name='project-test1', result='SUCCESS', changes='1,1',
4950 pipeline='dup1'),
4951 dict(name='project-test1', result='SUCCESS', changes='1,1',
4952 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004953 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004954
4955 self.assertEqual(len(A.messages), 2)
4956
Paul Belangera46a3742016-11-09 14:23:03 -05004957 if 'dup1' in A.messages[0]:
4958 self.assertIn('dup1', A.messages[0])
4959 self.assertNotIn('dup2', A.messages[0])
4960 self.assertIn('project-test1', A.messages[0])
4961 self.assertIn('dup2', A.messages[1])
4962 self.assertNotIn('dup1', A.messages[1])
4963 self.assertIn('project-test1', A.messages[1])
4964 else:
4965 self.assertIn('dup1', A.messages[1])
4966 self.assertNotIn('dup2', A.messages[1])
4967 self.assertIn('project-test1', A.messages[1])
4968 self.assertIn('dup2', A.messages[0])
4969 self.assertNotIn('dup1', A.messages[0])
4970 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004971
4972
Paul Belanger86085b32016-11-03 12:48:57 -04004973class TestSchedulerTemplatedProject(ZuulTestCase):
4974 tenant_config_file = 'config/templated-project/main.yaml'
4975
Paul Belanger174a8272017-03-14 13:20:10 -04004976 def test_job_from_templates_executed(self):
4977 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004978
4979 A = self.fake_gerrit.addFakeChange(
4980 'org/templated-project', 'master', 'A')
4981 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4982 self.waitUntilSettled()
4983
4984 self.assertEqual(self.getJobFromHistory('project-test1').result,
4985 'SUCCESS')
4986 self.assertEqual(self.getJobFromHistory('project-test2').result,
4987 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004988
4989 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004990 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004991
4992 A = self.fake_gerrit.addFakeChange(
4993 'org/layered-project', 'master', 'A')
4994 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4995 self.waitUntilSettled()
4996
4997 self.assertEqual(self.getJobFromHistory('project-test1').result,
4998 'SUCCESS')
4999 self.assertEqual(self.getJobFromHistory('project-test2').result,
5000 'SUCCESS')
5001 self.assertEqual(self.getJobFromHistory('layered-project-test3'
5002 ).result, 'SUCCESS')
5003 self.assertEqual(self.getJobFromHistory('layered-project-test4'
5004 ).result, 'SUCCESS')
5005 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
5006 ).result, 'SUCCESS')
5007 self.assertEqual(self.getJobFromHistory('project-test6').result,
5008 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08005009
James E. Blaire74f5712017-09-29 15:14:31 -07005010 def test_unimplied_branch_matchers(self):
5011 # This tests that there are no implied branch matchers added
5012 # by project templates.
5013 self.create_branch('org/layered-project', 'stable')
5014
5015 A = self.fake_gerrit.addFakeChange(
5016 'org/layered-project', 'stable', 'A')
5017
5018 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5019 self.waitUntilSettled()
5020
5021 self.assertEqual(self.getJobFromHistory('project-test1').result,
5022 'SUCCESS')
5023 print(self.getJobFromHistory('project-test1').
5024 parameters['zuul']['_inheritance_path'])
5025
5026 def test_implied_branch_matchers(self):
5027 # This tests that there is an implied branch matcher when a
5028 # template is used on an in-repo project pipeline definition.
5029 self.create_branch('untrusted-config', 'stable')
5030 self.fake_gerrit.addEvent(
5031 self.fake_gerrit.getFakeBranchCreatedEvent(
5032 'untrusted-config', 'stable'))
5033 self.waitUntilSettled()
5034
5035 A = self.fake_gerrit.addFakeChange(
5036 'untrusted-config', 'stable', 'A')
5037
5038 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5039 self.waitUntilSettled()
5040
5041 self.assertEqual(self.getJobFromHistory('project-test1').result,
5042 'SUCCESS')
5043 print(self.getJobFromHistory('project-test1').
5044 parameters['zuul']['_inheritance_path'])
5045
Adam Gandelman94a60062016-11-21 16:43:14 -08005046
5047class TestSchedulerSuccessURL(ZuulTestCase):
5048 tenant_config_file = 'config/success-url/main.yaml'
5049
5050 def test_success_url(self):
5051 "Ensure bad build params are ignored"
5052 self.sched.reconfigure(self.config)
5053 self.init_repo('org/docs')
5054
5055 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
5056 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5057 self.waitUntilSettled()
5058
5059 # Both builds ran: docs-draft-test + docs-draft-test2
5060 self.assertEqual(len(self.history), 2)
5061
5062 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08005063 for build in self.history:
5064 if build.name == 'docs-draft-test':
5065 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06005066 elif build.name == 'docs-draft-test2':
5067 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08005068
5069 # Two msgs: 'Starting...' + results
5070 self.assertEqual(len(self.smtp_messages), 2)
5071 body = self.smtp_messages[1]['body'].splitlines()
5072 self.assertEqual('Build succeeded.', body[0])
5073
5074 self.assertIn(
5075 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
5076 'docs-draft-test/%s/publish-docs/' % uuid,
5077 body[2])
5078
Paul Belanger174a8272017-03-14 13:20:10 -04005079 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08005080 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05005081 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
5082 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06005083 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08005084 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08005085
5086
Adam Gandelman4da00f62016-12-09 15:47:33 -08005087class TestSchedulerMerges(ZuulTestCase):
5088 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08005089
5090 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04005091 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08005092 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04005093 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08005094 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
5095 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
5096 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005097 A.addApproval('Code-Review', 2)
5098 B.addApproval('Code-Review', 2)
5099 C.addApproval('Code-Review', 2)
5100 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5101 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5102 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman8bd57102016-12-02 12:58:42 -08005103 self.waitUntilSettled()
5104
5105 build = self.builds[-1]
James E. Blair2a535672017-04-27 12:03:15 -07005106 path = os.path.join(build.jobdir.src_root, 'review.example.com',
5107 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08005108 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005109 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman8bd57102016-12-02 12:58:42 -08005110 repo_messages.reverse()
5111
Paul Belanger174a8272017-03-14 13:20:10 -04005112 self.executor_server.hold_jobs_in_build = False
5113 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08005114 self.waitUntilSettled()
5115
5116 return repo_messages
5117
5118 def _test_merge(self, mode):
5119 us_path = os.path.join(
5120 self.upstream_root, 'org/project-%s' % mode)
5121 expected_messages = [
5122 'initial commit',
5123 'add content from fixture',
5124 # the intermediate commits order is nondeterministic
5125 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
5126 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
5127 ]
5128 result = self._test_project_merge_mode(mode)
5129 self.assertEqual(result[:2], expected_messages[:2])
5130 self.assertEqual(result[-2:], expected_messages[-2:])
5131
5132 def test_project_merge_mode_merge(self):
5133 self._test_merge('merge')
5134
5135 def test_project_merge_mode_merge_resolve(self):
5136 self._test_merge('merge-resolve')
5137
5138 def test_project_merge_mode_cherrypick(self):
5139 expected_messages = [
5140 'initial commit',
5141 'add content from fixture',
5142 'A-1',
5143 'B-1',
5144 'C-1']
5145 result = self._test_project_merge_mode('cherry-pick')
5146 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08005147
5148 def test_merge_branch(self):
5149 "Test that the right commits are on alternate branches"
5150 self.create_branch('org/project-merge-branches', 'mp')
5151
Paul Belanger174a8272017-03-14 13:20:10 -04005152 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08005153 A = self.fake_gerrit.addFakeChange(
5154 'org/project-merge-branches', 'mp', 'A')
5155 B = self.fake_gerrit.addFakeChange(
5156 'org/project-merge-branches', 'mp', 'B')
5157 C = self.fake_gerrit.addFakeChange(
5158 'org/project-merge-branches', 'mp', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005159 A.addApproval('Code-Review', 2)
5160 B.addApproval('Code-Review', 2)
5161 C.addApproval('Code-Review', 2)
5162 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5163 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5164 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08005165 self.waitUntilSettled()
5166
Paul Belanger174a8272017-03-14 13:20:10 -04005167 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005168 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04005169 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005170 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04005171 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005172 self.waitUntilSettled()
5173
5174 build = self.builds[-1]
James E. Blair3b222492017-07-21 15:17:37 -07005175 self.assertEqual(build.parameters['zuul']['branch'], 'mp')
James E. Blair2a535672017-04-27 12:03:15 -07005176 path = os.path.join(build.jobdir.src_root, 'review.example.com',
5177 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005178 repo = git.Repo(path)
5179
James E. Blair247cab72017-07-20 16:52:36 -07005180 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005181 repo_messages.reverse()
5182 correct_messages = [
5183 'initial commit',
5184 'add content from fixture',
5185 'mp commit',
5186 'A-1', 'B-1', 'C-1']
5187 self.assertEqual(repo_messages, correct_messages)
5188
Paul Belanger174a8272017-03-14 13:20:10 -04005189 self.executor_server.hold_jobs_in_build = False
5190 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005191 self.waitUntilSettled()
5192
5193 def test_merge_multi_branch(self):
5194 "Test that dependent changes on multiple branches are merged"
5195 self.create_branch('org/project-merge-branches', 'mp')
5196
Paul Belanger174a8272017-03-14 13:20:10 -04005197 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08005198 A = self.fake_gerrit.addFakeChange(
5199 'org/project-merge-branches', 'master', 'A')
5200 B = self.fake_gerrit.addFakeChange(
5201 'org/project-merge-branches', 'mp', 'B')
5202 C = self.fake_gerrit.addFakeChange(
5203 'org/project-merge-branches', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005204 A.addApproval('Code-Review', 2)
5205 B.addApproval('Code-Review', 2)
5206 C.addApproval('Code-Review', 2)
5207 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5208 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5209 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08005210 self.waitUntilSettled()
5211
5212 job_A = None
5213 for job in self.builds:
5214 if 'project-merge' in job.name:
5215 job_A = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08005216
James E. Blair2a535672017-04-27 12:03:15 -07005217 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
5218 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005219 repo = git.Repo(path)
5220 repo_messages = [c.message.strip()
James E. Blair247cab72017-07-20 16:52:36 -07005221 for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005222 repo_messages.reverse()
5223 correct_messages = [
5224 'initial commit', 'add content from fixture', 'A-1']
5225 self.assertEqual(repo_messages, correct_messages)
5226
Paul Belanger174a8272017-03-14 13:20:10 -04005227 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005228 self.waitUntilSettled()
5229
5230 job_B = None
5231 for job in self.builds:
5232 if 'project-merge' in job.name:
5233 job_B = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08005234
James E. Blair2a535672017-04-27 12:03:15 -07005235 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
5236 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005237 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005238 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005239 repo_messages.reverse()
5240 correct_messages = [
5241 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
5242 self.assertEqual(repo_messages, correct_messages)
5243
Paul Belanger174a8272017-03-14 13:20:10 -04005244 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005245 self.waitUntilSettled()
5246
5247 job_C = None
5248 for job in self.builds:
5249 if 'project-merge' in job.name:
5250 job_C = job
James E. Blairb1345832017-07-21 14:51:35 -07005251
James E. Blair2a535672017-04-27 12:03:15 -07005252 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
5253 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005254 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005255 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005256
5257 repo_messages.reverse()
5258 correct_messages = [
5259 'initial commit', 'add content from fixture',
5260 'A-1', 'C-1']
5261 # Ensure the right commits are in the history for this ref
5262 self.assertEqual(repo_messages, correct_messages)
5263
Paul Belanger174a8272017-03-14 13:20:10 -04005264 self.executor_server.hold_jobs_in_build = False
5265 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005266 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005267
5268
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005269class TestSemaphore(ZuulTestCase):
5270 tenant_config_file = 'config/semaphore/main.yaml'
5271
5272 def test_semaphore_one(self):
5273 "Test semaphores with max=1 (mutex)"
5274 tenant = self.sched.abide.tenants.get('tenant-one')
5275
5276 self.executor_server.hold_jobs_in_build = True
5277
5278 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5279 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5280 self.assertFalse('test-semaphore' in
5281 tenant.semaphore_handler.semaphores)
5282
5283 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5284 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5285 self.waitUntilSettled()
5286
5287 self.assertEqual(len(self.builds), 3)
5288 self.assertEqual(self.builds[0].name, 'project-test1')
5289 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
5290 self.assertEqual(self.builds[2].name, 'project-test1')
5291
5292 self.executor_server.release('semaphore-one-test1')
5293 self.waitUntilSettled()
5294
5295 self.assertEqual(len(self.builds), 3)
5296 self.assertEqual(self.builds[0].name, 'project-test1')
5297 self.assertEqual(self.builds[1].name, 'project-test1')
5298 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5299 self.assertTrue('test-semaphore' in
5300 tenant.semaphore_handler.semaphores)
5301
5302 self.executor_server.release('semaphore-one-test2')
5303 self.waitUntilSettled()
5304
5305 self.assertEqual(len(self.builds), 3)
5306 self.assertEqual(self.builds[0].name, 'project-test1')
5307 self.assertEqual(self.builds[1].name, 'project-test1')
5308 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
5309 self.assertTrue('test-semaphore' in
5310 tenant.semaphore_handler.semaphores)
5311
5312 self.executor_server.release('semaphore-one-test1')
5313 self.waitUntilSettled()
5314
5315 self.assertEqual(len(self.builds), 3)
5316 self.assertEqual(self.builds[0].name, 'project-test1')
5317 self.assertEqual(self.builds[1].name, 'project-test1')
5318 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5319 self.assertTrue('test-semaphore' in
5320 tenant.semaphore_handler.semaphores)
5321
5322 self.executor_server.release('semaphore-one-test2')
5323 self.waitUntilSettled()
5324
5325 self.assertEqual(len(self.builds), 2)
5326 self.assertEqual(self.builds[0].name, 'project-test1')
5327 self.assertEqual(self.builds[1].name, 'project-test1')
5328 self.assertFalse('test-semaphore' in
5329 tenant.semaphore_handler.semaphores)
5330
5331 self.executor_server.hold_jobs_in_build = False
5332 self.executor_server.release()
5333
5334 self.waitUntilSettled()
5335 self.assertEqual(len(self.builds), 0)
5336
5337 self.assertEqual(A.reported, 1)
5338 self.assertEqual(B.reported, 1)
5339 self.assertFalse('test-semaphore' in
5340 tenant.semaphore_handler.semaphores)
5341
5342 def test_semaphore_two(self):
5343 "Test semaphores with max>1"
5344 tenant = self.sched.abide.tenants.get('tenant-one')
5345
5346 self.executor_server.hold_jobs_in_build = True
5347 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5348 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5349 self.assertFalse('test-semaphore-two' in
5350 tenant.semaphore_handler.semaphores)
5351
5352 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5353 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5354 self.waitUntilSettled()
5355
5356 self.assertEqual(len(self.builds), 4)
5357 self.assertEqual(self.builds[0].name, 'project-test1')
5358 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
5359 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5360 self.assertEqual(self.builds[3].name, 'project-test1')
5361 self.assertTrue('test-semaphore-two' in
5362 tenant.semaphore_handler.semaphores)
5363 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5364 'test-semaphore-two', [])), 2)
5365
5366 self.executor_server.release('semaphore-two-test1')
5367 self.waitUntilSettled()
5368
5369 self.assertEqual(len(self.builds), 4)
5370 self.assertEqual(self.builds[0].name, 'project-test1')
5371 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
5372 self.assertEqual(self.builds[2].name, 'project-test1')
5373 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
5374 self.assertTrue('test-semaphore-two' in
5375 tenant.semaphore_handler.semaphores)
5376 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5377 'test-semaphore-two', [])), 2)
5378
5379 self.executor_server.release('semaphore-two-test2')
5380 self.waitUntilSettled()
5381
5382 self.assertEqual(len(self.builds), 4)
5383 self.assertEqual(self.builds[0].name, 'project-test1')
5384 self.assertEqual(self.builds[1].name, 'project-test1')
5385 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5386 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
5387 self.assertTrue('test-semaphore-two' in
5388 tenant.semaphore_handler.semaphores)
5389 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5390 'test-semaphore-two', [])), 2)
5391
5392 self.executor_server.release('semaphore-two-test1')
5393 self.waitUntilSettled()
5394
5395 self.assertEqual(len(self.builds), 3)
5396 self.assertEqual(self.builds[0].name, 'project-test1')
5397 self.assertEqual(self.builds[1].name, 'project-test1')
5398 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5399 self.assertTrue('test-semaphore-two' in
5400 tenant.semaphore_handler.semaphores)
5401 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5402 'test-semaphore-two', [])), 1)
5403
5404 self.executor_server.release('semaphore-two-test2')
5405 self.waitUntilSettled()
5406
5407 self.assertEqual(len(self.builds), 2)
5408 self.assertEqual(self.builds[0].name, 'project-test1')
5409 self.assertEqual(self.builds[1].name, 'project-test1')
5410 self.assertFalse('test-semaphore-two' in
5411 tenant.semaphore_handler.semaphores)
5412
5413 self.executor_server.hold_jobs_in_build = False
5414 self.executor_server.release()
5415
5416 self.waitUntilSettled()
5417 self.assertEqual(len(self.builds), 0)
5418
5419 self.assertEqual(A.reported, 1)
5420 self.assertEqual(B.reported, 1)
5421
5422 def test_semaphore_abandon(self):
5423 "Test abandon with job semaphores"
5424 self.executor_server.hold_jobs_in_build = True
5425 tenant = self.sched.abide.tenants.get('tenant-one')
5426 check_pipeline = tenant.layout.pipelines['check']
5427
5428 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5429 self.assertFalse('test-semaphore' in
5430 tenant.semaphore_handler.semaphores)
5431
5432 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5433 self.waitUntilSettled()
5434
5435 self.assertTrue('test-semaphore' in
5436 tenant.semaphore_handler.semaphores)
5437
5438 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5439 self.waitUntilSettled()
5440
5441 # The check pipeline should be empty
5442 items = check_pipeline.getAllItems()
5443 self.assertEqual(len(items), 0)
5444
5445 # The semaphore should be released
5446 self.assertFalse('test-semaphore' in
5447 tenant.semaphore_handler.semaphores)
5448
5449 self.executor_server.hold_jobs_in_build = False
5450 self.executor_server.release()
5451 self.waitUntilSettled()
5452
Tobias Henkel0f714002017-06-30 23:30:52 +02005453 def test_semaphore_new_patchset(self):
5454 "Test new patchset with job semaphores"
5455 self.executor_server.hold_jobs_in_build = True
5456 tenant = self.sched.abide.tenants.get('tenant-one')
5457 check_pipeline = tenant.layout.pipelines['check']
5458
5459 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5460 self.assertFalse('test-semaphore' in
5461 tenant.semaphore_handler.semaphores)
5462
5463 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5464 self.waitUntilSettled()
5465
5466 self.assertTrue('test-semaphore' in
5467 tenant.semaphore_handler.semaphores)
5468 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5469 self.assertEqual(len(semaphore), 1)
5470
5471 A.addPatchset()
5472 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
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 items = check_pipeline.getAllItems()
5481 self.assertEqual(items[0].change.number, '1')
5482 self.assertEqual(items[0].change.patchset, '2')
5483 self.assertTrue(items[0].live)
5484
5485 self.executor_server.hold_jobs_in_build = False
5486 self.executor_server.release()
5487 self.waitUntilSettled()
5488
5489 # The semaphore should be released
5490 self.assertFalse('test-semaphore' in
5491 tenant.semaphore_handler.semaphores)
5492
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005493 def test_semaphore_reconfigure(self):
5494 "Test reconfigure with job semaphores"
5495 self.executor_server.hold_jobs_in_build = True
5496 tenant = self.sched.abide.tenants.get('tenant-one')
5497 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5498 self.assertFalse('test-semaphore' in
5499 tenant.semaphore_handler.semaphores)
5500
5501 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5502 self.waitUntilSettled()
5503
5504 self.assertTrue('test-semaphore' in
5505 tenant.semaphore_handler.semaphores)
5506
5507 # reconfigure without layout change
5508 self.sched.reconfigure(self.config)
5509 self.waitUntilSettled()
5510 tenant = self.sched.abide.tenants.get('tenant-one')
5511
5512 # semaphore still must be held
5513 self.assertTrue('test-semaphore' in
5514 tenant.semaphore_handler.semaphores)
5515
5516 self.commitConfigUpdate(
5517 'common-config',
5518 'config/semaphore/zuul-reconfiguration.yaml')
5519 self.sched.reconfigure(self.config)
5520 self.waitUntilSettled()
5521 tenant = self.sched.abide.tenants.get('tenant-one')
5522
5523 self.executor_server.release('project-test1')
5524 self.waitUntilSettled()
5525
5526 # There should be no builds anymore
5527 self.assertEqual(len(self.builds), 0)
5528
5529 # The semaphore should be released
5530 self.assertFalse('test-semaphore' in
5531 tenant.semaphore_handler.semaphores)
5532
5533
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005534class TestSemaphoreMultiTenant(ZuulTestCase):
5535 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5536
5537 def test_semaphore_tenant_isolation(self):
5538 "Test semaphores in multiple tenants"
5539
5540 self.waitUntilSettled()
5541 tenant_one = self.sched.abide.tenants.get('tenant-one')
5542 tenant_two = self.sched.abide.tenants.get('tenant-two')
5543
5544 self.executor_server.hold_jobs_in_build = True
5545 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5546 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5547 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5548 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5549 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5550 self.assertFalse('test-semaphore' in
5551 tenant_one.semaphore_handler.semaphores)
5552 self.assertFalse('test-semaphore' in
5553 tenant_two.semaphore_handler.semaphores)
5554
5555 # add patches to project1 of tenant-one
5556 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5557 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5558 self.waitUntilSettled()
5559
5560 # one build of project1-test1 must run
5561 # semaphore of tenant-one must be acquired once
5562 # semaphore of tenant-two must not be acquired
5563 self.assertEqual(len(self.builds), 1)
5564 self.assertEqual(self.builds[0].name, 'project1-test1')
5565 self.assertTrue('test-semaphore' in
5566 tenant_one.semaphore_handler.semaphores)
5567 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5568 'test-semaphore', [])), 1)
5569 self.assertFalse('test-semaphore' in
5570 tenant_two.semaphore_handler.semaphores)
5571
5572 # add patches to project2 of tenant-two
5573 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5574 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5575 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5576 self.waitUntilSettled()
5577
5578 # one build of project1-test1 must run
5579 # two builds of project2-test1 must run
5580 # semaphore of tenant-one must be acquired once
5581 # semaphore of tenant-two must be acquired twice
5582 self.assertEqual(len(self.builds), 3)
5583 self.assertEqual(self.builds[0].name, 'project1-test1')
5584 self.assertEqual(self.builds[1].name, 'project2-test1')
5585 self.assertEqual(self.builds[2].name, 'project2-test1')
5586 self.assertTrue('test-semaphore' in
5587 tenant_one.semaphore_handler.semaphores)
5588 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5589 'test-semaphore', [])), 1)
5590 self.assertTrue('test-semaphore' in
5591 tenant_two.semaphore_handler.semaphores)
5592 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5593 'test-semaphore', [])), 2)
5594
5595 self.executor_server.release('project1-test1')
5596 self.waitUntilSettled()
5597
5598 # one build of project1-test1 must run
5599 # two builds of project2-test1 must run
5600 # semaphore of tenant-one must be acquired once
5601 # semaphore of tenant-two must be acquired twice
5602 self.assertEqual(len(self.builds), 3)
5603 self.assertEqual(self.builds[0].name, 'project2-test1')
5604 self.assertEqual(self.builds[1].name, 'project2-test1')
5605 self.assertEqual(self.builds[2].name, 'project1-test1')
5606 self.assertTrue('test-semaphore' in
5607 tenant_one.semaphore_handler.semaphores)
5608 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5609 'test-semaphore', [])), 1)
5610 self.assertTrue('test-semaphore' in
5611 tenant_two.semaphore_handler.semaphores)
5612 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5613 'test-semaphore', [])), 2)
5614
5615 self.executor_server.release('project2-test1')
5616 self.waitUntilSettled()
5617
5618 # one build of project1-test1 must run
5619 # one build of project2-test1 must run
5620 # semaphore of tenant-one must be acquired once
5621 # semaphore of tenant-two must be acquired once
5622 self.assertEqual(len(self.builds), 2)
5623 self.assertTrue('test-semaphore' in
5624 tenant_one.semaphore_handler.semaphores)
5625 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5626 'test-semaphore', [])), 1)
5627 self.assertTrue('test-semaphore' in
5628 tenant_two.semaphore_handler.semaphores)
5629 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5630 'test-semaphore', [])), 1)
5631
5632 self.executor_server.hold_jobs_in_build = False
5633 self.executor_server.release()
5634
5635 self.waitUntilSettled()
5636
5637 # no build must run
5638 # semaphore of tenant-one must not be acquired
5639 # semaphore of tenant-two must not be acquired
5640 self.assertEqual(len(self.builds), 0)
5641 self.assertFalse('test-semaphore' in
5642 tenant_one.semaphore_handler.semaphores)
5643 self.assertFalse('test-semaphore' in
5644 tenant_two.semaphore_handler.semaphores)
5645
5646 self.assertEqual(A.reported, 1)
5647 self.assertEqual(B.reported, 1)
5648
5649
5650class TestSemaphoreInRepo(ZuulTestCase):
Tobias Henkelabf973e2017-07-28 10:07:34 +02005651 config_file = 'zuul-connections-gerrit-and-github.conf'
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005652 tenant_config_file = 'config/in-repo/main.yaml'
5653
5654 def test_semaphore_in_repo(self):
5655 "Test semaphores in repo config"
5656
5657 # This tests dynamic semaphore handling in project repos. The semaphore
5658 # max value should not be evaluated dynamically but must be updated
5659 # after the change lands.
5660
5661 self.waitUntilSettled()
5662 tenant = self.sched.abide.tenants.get('tenant-one')
5663
5664 in_repo_conf = textwrap.dedent(
5665 """
5666 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +02005667 name: project-test1
5668
5669 - job:
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005670 name: project-test2
5671 semaphore: test-semaphore
5672
5673 - project:
5674 name: org/project
5675 tenant-one-gate:
5676 jobs:
5677 - project-test2
5678
5679 # the max value in dynamic layout must be ignored
5680 - semaphore:
5681 name: test-semaphore
5682 max: 2
5683 """)
5684
5685 in_repo_playbook = textwrap.dedent(
5686 """
5687 - hosts: all
5688 tasks: []
5689 """)
5690
5691 file_dict = {'.zuul.yaml': in_repo_conf,
5692 'playbooks/project-test2.yaml': in_repo_playbook}
5693 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5694 files=file_dict)
5695 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5696 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5697 B.setDependsOn(A, 1)
5698 C.setDependsOn(A, 1)
5699
5700 self.executor_server.hold_jobs_in_build = True
5701
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005702 A.addApproval('Code-Review', 2)
5703 B.addApproval('Code-Review', 2)
5704 C.addApproval('Code-Review', 2)
5705 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5706 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5707 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005708 self.waitUntilSettled()
5709
5710 # check that the layout in a queue item still has max value of 1
5711 # for test-semaphore
5712 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5713 queue = None
5714 for queue_candidate in pipeline.queues:
5715 if queue_candidate.name == 'org/project':
5716 queue = queue_candidate
5717 break
5718 queue_item = queue.queue[0]
James E. Blair29a24fd2017-10-02 15:04:56 -07005719 item_dynamic_layout = queue_item.layout
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005720 dynamic_test_semaphore = \
5721 item_dynamic_layout.semaphores.get('test-semaphore')
5722 self.assertEqual(dynamic_test_semaphore.max, 1)
5723
5724 # one build must be in queue, one semaphores acquired
5725 self.assertEqual(len(self.builds), 1)
5726 self.assertEqual(self.builds[0].name, 'project-test2')
5727 self.assertTrue('test-semaphore' in
5728 tenant.semaphore_handler.semaphores)
5729 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5730 'test-semaphore', [])), 1)
5731
5732 self.executor_server.release('project-test2')
5733 self.waitUntilSettled()
5734
5735 # change A must be merged
5736 self.assertEqual(A.data['status'], 'MERGED')
5737 self.assertEqual(A.reported, 2)
5738
5739 # send change-merged event as the gerrit mock doesn't send it
5740 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5741 self.waitUntilSettled()
5742
5743 # now that change A was merged, the new semaphore max must be effective
5744 tenant = self.sched.abide.tenants.get('tenant-one')
5745 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5746
5747 # two builds must be in queue, two semaphores acquired
5748 self.assertEqual(len(self.builds), 2)
5749 self.assertEqual(self.builds[0].name, 'project-test2')
5750 self.assertEqual(self.builds[1].name, 'project-test2')
5751 self.assertTrue('test-semaphore' in
5752 tenant.semaphore_handler.semaphores)
5753 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5754 'test-semaphore', [])), 2)
5755
5756 self.executor_server.release('project-test2')
5757 self.waitUntilSettled()
5758
5759 self.assertEqual(len(self.builds), 0)
5760 self.assertFalse('test-semaphore' in
5761 tenant.semaphore_handler.semaphores)
5762
5763 self.executor_server.hold_jobs_in_build = False
5764 self.executor_server.release()
5765
5766 self.waitUntilSettled()
5767 self.assertEqual(len(self.builds), 0)
5768
5769 self.assertEqual(A.reported, 2)
5770 self.assertEqual(B.reported, 2)
5771 self.assertEqual(C.reported, 2)
David Shrewsburyf6dc1762017-10-02 13:34:37 -04005772
5773
5774class TestSchedulerBranchMatcher(ZuulTestCase):
5775
5776 @simple_layout('layouts/matcher-test.yaml')
5777 def test_job_branch_ignored(self):
5778 '''
5779 Test that branch matching logic works.
5780
5781 The 'ignore-branch' job has a branch matcher that is supposed to
5782 match every branch except for the 'featureA' branch, so it should
5783 not be run on a change to that branch.
5784 '''
5785 self.create_branch('org/project', 'featureA')
5786 A = self.fake_gerrit.addFakeChange('org/project', 'featureA', 'A')
5787 A.addApproval('Code-Review', 2)
5788 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5789 self.waitUntilSettled()
5790 self.printHistory()
5791 self.assertEqual(self.getJobFromHistory('project-test1').result,
5792 'SUCCESS')
5793 self.assertJobNotInHistory('ignore-branch')
5794 self.assertEqual(A.data['status'], 'MERGED')
5795 self.assertEqual(A.reported, 2,
5796 "A should report start and success")
5797 self.assertIn('gate', A.messages[1],
5798 "A should transit gate")