blob: 06ec70d0b9de99d832105b062438b5a83cbe475d [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. Blairb0fcae42012-07-17 11:12:10 -070017import json
Monty Taylorbc758832013-06-17 17:22:42 -040018import os
James E. Blairb0fcae42012-07-17 11:12:10 -070019import re
James E. Blair4886cc12012-07-18 15:39:41 -070020import shutil
Monty Taylorbc758832013-06-17 17:22:42 -040021import time
James E. Blairec056492016-07-22 09:45:56 -070022from unittest import skip
Monty Taylorbc758832013-06-17 17:22:42 -040023
James E. Blair4886cc12012-07-18 15:39:41 -070024import git
Morgan Fainberg293f7f82016-05-30 14:01:22 -070025from six.moves import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040026import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070027
Maru Newby3fe5f852015-01-13 04:22:14 +000028import zuul.change_matcher
James E. Blairb0fcae42012-07-17 11:12:10 -070029import zuul.scheduler
James E. Blairad28e912013-11-27 10:43:22 -080030import zuul.rpcclient
Paul Belanger9bba4902016-11-02 16:07:33 -040031import zuul.model
James E. Blairb0fcae42012-07-17 11:12:10 -070032
Maru Newby3fe5f852015-01-13 04:22:14 +000033from tests.base import (
Maru Newby3fe5f852015-01-13 04:22:14 +000034 ZuulTestCase,
35 repack_repo,
36)
James E. Blairb0fcae42012-07-17 11:12:10 -070037
James E. Blairb0fcae42012-07-17 11:12:10 -070038
Clark Boylanb640e052014-04-03 16:41:46 -070039class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070040 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010041
James E. Blairb0fcae42012-07-17 11:12:10 -070042 def test_jobs_launched(self):
43 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070044
James E. Blairb0fcae42012-07-17 11:12:10 -070045 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070046 A.addApproval('code-review', 2)
47 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070048 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040049 self.assertEqual(self.getJobFromHistory('project-merge').result,
50 'SUCCESS')
51 self.assertEqual(self.getJobFromHistory('project-test1').result,
52 'SUCCESS')
53 self.assertEqual(self.getJobFromHistory('project-test2').result,
54 'SUCCESS')
55 self.assertEqual(A.data['status'], 'MERGED')
56 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070057 self.assertEqual(self.getJobFromHistory('project-test1').node,
58 'image1')
James E. Blairb0fcae42012-07-17 11:12:10 -070059
James E. Blair552b54f2016-07-22 13:55:32 -070060 # TODOv3(jeblair): we may want to report stats by tenant (also?).
James E. Blair66eeebf2013-07-27 17:44:32 -070061 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
62 self.assertReportedStat('zuul.pipeline.gate.current_changes',
63 value='1|g')
64 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
65 kind='ms')
66 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
67 value='1|c')
68 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
69 self.assertReportedStat('zuul.pipeline.gate.total_changes',
70 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070071 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070072 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -070073 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070074 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070075
James E. Blair5821bd92015-09-16 08:48:15 -070076 for build in self.builds:
77 self.assertEqual(build.parameters['ZUUL_VOTING'], '1')
78
James E. Blair3cb10702013-08-24 08:56:03 -070079 def test_initial_pipeline_gauges(self):
80 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -070081 self.assertReportedStat('zuul.pipeline.gate.current_changes',
82 value='0|g')
83 self.assertReportedStat('zuul.pipeline.check.current_changes',
84 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -070085
James E. Blair34776ee2016-08-25 13:53:54 -070086 def test_job_branch(self):
87 "Test the correct variant of a job runs on a branch"
88 self.create_branch('org/project', 'stable')
89 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
90 A.addApproval('code-review', 2)
91 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
92 self.waitUntilSettled()
93 self.assertEqual(self.getJobFromHistory('project-test1').result,
94 'SUCCESS')
95 self.assertEqual(self.getJobFromHistory('project-test2').result,
96 'SUCCESS')
97 self.assertEqual(A.data['status'], 'MERGED')
98 self.assertEqual(A.reported, 2,
99 "A should report start and success")
100 self.assertIn('gate', A.messages[1],
101 "A should transit gate")
102 self.assertEqual(self.getJobFromHistory('project-test1').node,
103 'image2')
104
James E. Blairb0fcae42012-07-17 11:12:10 -0700105 def test_parallel_changes(self):
106 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700107
James E. Blair791b5392016-08-03 11:25:56 -0700108 self.launch_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700109 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
110 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
111 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700112 A.addApproval('code-review', 2)
113 B.addApproval('code-review', 2)
114 C.addApproval('code-review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700115
James E. Blair8b5408c2016-08-08 15:37:46 -0700116 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
117 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
118 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700119
120 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400121 self.assertEqual(len(self.builds), 1)
122 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700123 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700124
James E. Blair791b5392016-08-03 11:25:56 -0700125 self.launch_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700126 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400127 self.assertEqual(len(self.builds), 3)
128 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700129 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400130 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700131 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400132 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700133 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700134
James E. Blair791b5392016-08-03 11:25:56 -0700135 self.launch_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700136 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400137 self.assertEqual(len(self.builds), 5)
138 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700139 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400140 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700141 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700142
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400143 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700144 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400145 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700146 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700147
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400148 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700149 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700150
James E. Blair791b5392016-08-03 11:25:56 -0700151 self.launch_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700152 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400153 self.assertEqual(len(self.builds), 6)
154 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700155 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400156 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700157 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700158
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400159 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700160 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400161 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700162 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700163
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400164 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700165 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400166 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700167 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700168
James E. Blair791b5392016-08-03 11:25:56 -0700169 self.launch_server.hold_jobs_in_build = False
170 self.launch_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700171 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400172 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700173
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400174 self.assertEqual(len(self.history), 9)
175 self.assertEqual(A.data['status'], 'MERGED')
176 self.assertEqual(B.data['status'], 'MERGED')
177 self.assertEqual(C.data['status'], 'MERGED')
178 self.assertEqual(A.reported, 2)
179 self.assertEqual(B.reported, 2)
180 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700181
182 def test_failed_changes(self):
183 "Test that a change behind a failed change is retested"
James E. Blair08d19992016-08-10 15:25:31 -0700184 self.launch_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700185
James E. Blairb02a3bb2012-07-30 17:49:55 -0700186 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
187 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -0700188 A.addApproval('code-review', 2)
189 B.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700190
James E. Blair08d19992016-08-10 15:25:31 -0700191 self.launch_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700192
James E. Blair8b5408c2016-08-08 15:37:46 -0700193 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
194 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700195 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700196 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400197
James E. Blair08d19992016-08-10 15:25:31 -0700198 self.launch_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400199 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700200 # A/project-merge is complete
201 self.assertBuilds([
202 dict(name='project-test1', changes='1,1'),
203 dict(name='project-test2', changes='1,1'),
204 dict(name='project-merge', changes='1,1 2,1'),
205 ])
James E. Blaire2819012013-06-28 17:17:26 -0400206
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700207 self.launch_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400208 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700209 # A/project-merge is complete
210 # B/project-merge is complete
211 self.assertBuilds([
212 dict(name='project-test1', changes='1,1'),
213 dict(name='project-test2', changes='1,1'),
214 dict(name='project-test1', changes='1,1 2,1'),
215 dict(name='project-test2', changes='1,1 2,1'),
216 ])
217
218 # Release project-test1 for A which will fail. This will
219 # abort both running B jobs and relaunch project-merge for B.
220 self.builds[0].release()
221 self.waitUntilSettled()
222
223 self.orderedRelease()
224 self.assertHistory([
225 dict(name='project-merge', result='SUCCESS', changes='1,1'),
226 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
227 dict(name='project-test1', result='FAILURE', changes='1,1'),
228 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
229 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
230 dict(name='project-test2', result='SUCCESS', changes='1,1'),
231 dict(name='project-merge', result='SUCCESS', changes='2,1'),
232 dict(name='project-test1', result='SUCCESS', changes='2,1'),
233 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700234 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700235
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400236 self.assertEqual(A.data['status'], 'NEW')
237 self.assertEqual(B.data['status'], 'MERGED')
238 self.assertEqual(A.reported, 2)
239 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700240
241 def test_independent_queues(self):
242 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700243
James E. Blair08d19992016-08-10 15:25:31 -0700244 self.launch_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900245 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700246 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
247 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700248 A.addApproval('code-review', 2)
249 B.addApproval('code-review', 2)
250 C.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700251
James E. Blair8b5408c2016-08-08 15:37:46 -0700252 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
253 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
254 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700255
James E. Blairb02a3bb2012-07-30 17:49:55 -0700256 self.waitUntilSettled()
257
258 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700259 self.assertBuilds([
260 dict(name='project-merge', changes='1,1'),
261 dict(name='project-merge', changes='2,1'),
262 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700263
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700264 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700265 self.builds[0].release()
266 self.waitUntilSettled()
267 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700268 self.waitUntilSettled()
269 # Release the merge job for project2 which is behind project1
James E. Blair08d19992016-08-10 15:25:31 -0700270 self.launch_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700271 self.waitUntilSettled()
272
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700273 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700274 self.assertBuilds([
275 dict(name='project-test1', changes='1,1'),
276 dict(name='project-test2', changes='1,1'),
277 dict(name='project-test1', changes='2,1'),
278 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500279 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700280 dict(name='project-test1', changes='2,1 3,1'),
281 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800282 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700283 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700284
James E. Blair5f0d49e2016-08-19 09:36:50 -0700285 self.orderedRelease()
286 self.assertHistory([
287 dict(name='project-merge', result='SUCCESS', changes='1,1'),
288 dict(name='project-merge', result='SUCCESS', changes='2,1'),
289 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
290 dict(name='project-test1', result='SUCCESS', changes='1,1'),
291 dict(name='project-test2', result='SUCCESS', changes='1,1'),
292 dict(name='project-test1', result='SUCCESS', changes='2,1'),
293 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500294 dict(
295 name='project1-project2-integration',
296 result='SUCCESS',
297 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700298 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
299 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800300 dict(name='project1-project2-integration',
301 result='SUCCESS',
302 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700303 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700304
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400305 self.assertEqual(A.data['status'], 'MERGED')
306 self.assertEqual(B.data['status'], 'MERGED')
307 self.assertEqual(C.data['status'], 'MERGED')
308 self.assertEqual(A.reported, 2)
309 self.assertEqual(B.reported, 2)
310 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700311
312 def test_failed_change_at_head(self):
313 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700314
James E. Blair08d19992016-08-10 15:25:31 -0700315 self.launch_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700316 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
317 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
318 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700319 A.addApproval('code-review', 2)
320 B.addApproval('code-review', 2)
321 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700322
James E. Blair08d19992016-08-10 15:25:31 -0700323 self.launch_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700324
James E. Blair8b5408c2016-08-08 15:37:46 -0700325 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
326 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
327 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700328
329 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700330
James E. Blairb536ecc2016-08-31 10:11:42 -0700331 self.assertBuilds([
332 dict(name='project-merge', changes='1,1'),
333 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700334
James E. Blair08d19992016-08-10 15:25:31 -0700335 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700336 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700337 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700338 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700339 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700340 self.waitUntilSettled()
341
James E. Blairb536ecc2016-08-31 10:11:42 -0700342 self.assertBuilds([
343 dict(name='project-test1', changes='1,1'),
344 dict(name='project-test2', changes='1,1'),
345 dict(name='project-test1', changes='1,1 2,1'),
346 dict(name='project-test2', changes='1,1 2,1'),
347 dict(name='project-test1', changes='1,1 2,1 3,1'),
348 dict(name='project-test2', changes='1,1 2,1 3,1'),
349 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700350
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400351 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700352 self.waitUntilSettled()
353
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400354 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700355 self.assertBuilds([
356 dict(name='project-test2', changes='1,1'),
357 dict(name='project-merge', changes='2,1'),
358 ])
359 # Unordered history comparison because the aborts can finish
360 # in any order.
361 self.assertHistory([
362 dict(name='project-merge', result='SUCCESS',
363 changes='1,1'),
364 dict(name='project-merge', result='SUCCESS',
365 changes='1,1 2,1'),
366 dict(name='project-merge', result='SUCCESS',
367 changes='1,1 2,1 3,1'),
368 dict(name='project-test1', result='FAILURE',
369 changes='1,1'),
370 dict(name='project-test1', result='ABORTED',
371 changes='1,1 2,1'),
372 dict(name='project-test2', result='ABORTED',
373 changes='1,1 2,1'),
374 dict(name='project-test1', result='ABORTED',
375 changes='1,1 2,1 3,1'),
376 dict(name='project-test2', result='ABORTED',
377 changes='1,1 2,1 3,1'),
378 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700379
James E. Blairb536ecc2016-08-31 10:11:42 -0700380 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700381 self.waitUntilSettled()
James E. Blairb536ecc2016-08-31 10:11:42 -0700382 self.launch_server.release('.*-merge')
383 self.waitUntilSettled()
384 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700385
James E. Blairb536ecc2016-08-31 10:11:42 -0700386 self.assertBuilds([])
387 self.assertHistory([
388 dict(name='project-merge', result='SUCCESS',
389 changes='1,1'),
390 dict(name='project-merge', result='SUCCESS',
391 changes='1,1 2,1'),
392 dict(name='project-merge', result='SUCCESS',
393 changes='1,1 2,1 3,1'),
394 dict(name='project-test1', result='FAILURE',
395 changes='1,1'),
396 dict(name='project-test1', result='ABORTED',
397 changes='1,1 2,1'),
398 dict(name='project-test2', result='ABORTED',
399 changes='1,1 2,1'),
400 dict(name='project-test1', result='ABORTED',
401 changes='1,1 2,1 3,1'),
402 dict(name='project-test2', result='ABORTED',
403 changes='1,1 2,1 3,1'),
404 dict(name='project-merge', result='SUCCESS',
405 changes='2,1'),
406 dict(name='project-merge', result='SUCCESS',
407 changes='2,1 3,1'),
408 dict(name='project-test2', result='SUCCESS',
409 changes='1,1'),
410 dict(name='project-test1', result='SUCCESS',
411 changes='2,1'),
412 dict(name='project-test2', result='SUCCESS',
413 changes='2,1'),
414 dict(name='project-test1', result='SUCCESS',
415 changes='2,1 3,1'),
416 dict(name='project-test2', result='SUCCESS',
417 changes='2,1 3,1'),
418 ], ordered=False)
419
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400420 self.assertEqual(A.data['status'], 'NEW')
421 self.assertEqual(B.data['status'], 'MERGED')
422 self.assertEqual(C.data['status'], 'MERGED')
423 self.assertEqual(A.reported, 2)
424 self.assertEqual(B.reported, 2)
425 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700426
James E. Blair0aac4872013-08-23 14:02:38 -0700427 def test_failed_change_in_middle(self):
428 "Test a failed change in the middle of the queue"
429
James E. Blair08d19992016-08-10 15:25:31 -0700430 self.launch_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700431 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
432 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
433 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700434 A.addApproval('code-review', 2)
435 B.addApproval('code-review', 2)
436 C.addApproval('code-review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700437
James E. Blair08d19992016-08-10 15:25:31 -0700438 self.launch_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700439
James E. Blair8b5408c2016-08-08 15:37:46 -0700440 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
441 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
442 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700443
444 self.waitUntilSettled()
445
James E. Blair08d19992016-08-10 15:25:31 -0700446 self.launch_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700447 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700448 self.launch_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700449 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700450 self.launch_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700451 self.waitUntilSettled()
452
453 self.assertEqual(len(self.builds), 6)
454 self.assertEqual(self.builds[0].name, 'project-test1')
455 self.assertEqual(self.builds[1].name, 'project-test2')
456 self.assertEqual(self.builds[2].name, 'project-test1')
457 self.assertEqual(self.builds[3].name, 'project-test2')
458 self.assertEqual(self.builds[4].name, 'project-test1')
459 self.assertEqual(self.builds[5].name, 'project-test2')
460
461 self.release(self.builds[2])
462 self.waitUntilSettled()
463
James E. Blair972e3c72013-08-29 12:04:55 -0700464 # project-test1 and project-test2 for A
465 # project-test2 for B
466 # project-merge for C (without B)
467 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700468 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
469
James E. Blair08d19992016-08-10 15:25:31 -0700470 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700471 self.waitUntilSettled()
472
473 # project-test1 and project-test2 for A
474 # project-test2 for B
475 # project-test1 and project-test2 for C
476 self.assertEqual(len(self.builds), 5)
477
Paul Belanger2e2a0492016-10-30 18:33:37 -0400478 tenant = self.sched.abide.tenants.get('tenant-one')
479 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700480 builds = items[0].current_build_set.getBuilds()
481 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
482 self.assertEqual(self.countJobResults(builds, None), 2)
483 builds = items[1].current_build_set.getBuilds()
484 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
485 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
486 self.assertEqual(self.countJobResults(builds, None), 1)
487 builds = items[2].current_build_set.getBuilds()
488 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700489 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700490
James E. Blair08d19992016-08-10 15:25:31 -0700491 self.launch_server.hold_jobs_in_build = False
492 self.launch_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700493 self.waitUntilSettled()
494
495 self.assertEqual(len(self.builds), 0)
496 self.assertEqual(len(self.history), 12)
497 self.assertEqual(A.data['status'], 'MERGED')
498 self.assertEqual(B.data['status'], 'NEW')
499 self.assertEqual(C.data['status'], 'MERGED')
500 self.assertEqual(A.reported, 2)
501 self.assertEqual(B.reported, 2)
502 self.assertEqual(C.reported, 2)
503
James E. Blaird466dc42012-07-31 10:42:56 -0700504 def test_failed_change_at_head_with_queue(self):
505 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700506
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700507 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700508 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
509 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
510 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700511 A.addApproval('code-review', 2)
512 B.addApproval('code-review', 2)
513 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700514
James E. Blair08d19992016-08-10 15:25:31 -0700515 self.launch_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700516
James E. Blair8b5408c2016-08-08 15:37:46 -0700517 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
518 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
519 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700520
521 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700522 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400523 self.assertEqual(len(self.builds), 0)
524 self.assertEqual(len(queue), 1)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800525 self.assertEqual(queue[0].name, 'launcher:launch')
526 job_args = json.loads(queue[0].arguments)
527 self.assertEqual(job_args['job'], 'project-merge')
528 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700529
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700530 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700531 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700532 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700533 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700534 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700535 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700536 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700537
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400538 self.assertEqual(len(self.builds), 0)
539 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800540
541 self.assertEqual(
542 json.loads(queue[0].arguments)['job'], 'project-test1')
543 self.assertEqual(
544 json.loads(queue[1].arguments)['job'], 'project-test2')
545 self.assertEqual(
546 json.loads(queue[2].arguments)['job'], 'project-test1')
547 self.assertEqual(
548 json.loads(queue[3].arguments)['job'], 'project-test2')
549 self.assertEqual(
550 json.loads(queue[4].arguments)['job'], 'project-test1')
551 self.assertEqual(
552 json.loads(queue[5].arguments)['job'], 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700553
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700554 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700555 self.waitUntilSettled()
556
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400557 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700558 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400559 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
560 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700561
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700562 self.gearman_server.hold_jobs_in_queue = False
563 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700564 self.waitUntilSettled()
565
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400566 self.assertEqual(len(self.builds), 0)
567 self.assertEqual(len(self.history), 11)
568 self.assertEqual(A.data['status'], 'NEW')
569 self.assertEqual(B.data['status'], 'MERGED')
570 self.assertEqual(C.data['status'], 'MERGED')
571 self.assertEqual(A.reported, 2)
572 self.assertEqual(B.reported, 2)
573 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700574
James E. Blairec056492016-07-22 09:45:56 -0700575 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700576 def _test_time_database(self, iteration):
James E. Blair08d19992016-08-10 15:25:31 -0700577 self.launch_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700578 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -0700579 A.addApproval('code-review', 2)
580 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700581 self.waitUntilSettled()
582 time.sleep(2)
583
584 data = json.loads(self.sched.formatStatusJSON())
585 found_job = None
586 for pipeline in data['pipelines']:
587 if pipeline['name'] != 'gate':
588 continue
589 for queue in pipeline['change_queues']:
590 for head in queue['heads']:
591 for item in head:
592 for job in item['jobs']:
593 if job['name'] == 'project-merge':
594 found_job = job
595 break
596
597 self.assertIsNotNone(found_job)
598 if iteration == 1:
599 self.assertIsNotNone(found_job['estimated_time'])
600 self.assertIsNone(found_job['remaining_time'])
601 else:
602 self.assertIsNotNone(found_job['estimated_time'])
603 self.assertTrue(found_job['estimated_time'] >= 2)
604 self.assertIsNotNone(found_job['remaining_time'])
605
James E. Blair08d19992016-08-10 15:25:31 -0700606 self.launch_server.hold_jobs_in_build = False
607 self.launch_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700608 self.waitUntilSettled()
609
James E. Blairec056492016-07-22 09:45:56 -0700610 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700611 def test_time_database(self):
612 "Test the time database"
613
614 self._test_time_database(1)
615 self._test_time_database(2)
616
James E. Blairfef71632013-09-23 11:15:47 -0700617 def test_two_failed_changes_at_head(self):
618 "Test that changes are reparented correctly if 2 fail at head"
619
James E. Blair08d19992016-08-10 15:25:31 -0700620 self.launch_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700621 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
622 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
623 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700624 A.addApproval('code-review', 2)
625 B.addApproval('code-review', 2)
626 C.addApproval('code-review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700627
James E. Blair08d19992016-08-10 15:25:31 -0700628 self.launch_server.failJob('project-test1', A)
629 self.launch_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700630
James E. Blair8b5408c2016-08-08 15:37:46 -0700631 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
632 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
633 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700634 self.waitUntilSettled()
635
James E. Blair08d19992016-08-10 15:25:31 -0700636 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700637 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700638 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700639 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700640 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700641 self.waitUntilSettled()
642
643 self.assertEqual(len(self.builds), 6)
644 self.assertEqual(self.builds[0].name, 'project-test1')
645 self.assertEqual(self.builds[1].name, 'project-test2')
646 self.assertEqual(self.builds[2].name, 'project-test1')
647 self.assertEqual(self.builds[3].name, 'project-test2')
648 self.assertEqual(self.builds[4].name, 'project-test1')
649 self.assertEqual(self.builds[5].name, 'project-test2')
650
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400651 self.assertTrue(self.builds[0].hasChanges(A))
652 self.assertTrue(self.builds[2].hasChanges(A))
653 self.assertTrue(self.builds[2].hasChanges(B))
654 self.assertTrue(self.builds[4].hasChanges(A))
655 self.assertTrue(self.builds[4].hasChanges(B))
656 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700657
658 # Fail change B first
659 self.release(self.builds[2])
660 self.waitUntilSettled()
661
662 # restart of C after B failure
James E. Blair08d19992016-08-10 15:25:31 -0700663 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700664 self.waitUntilSettled()
665
666 self.assertEqual(len(self.builds), 5)
667 self.assertEqual(self.builds[0].name, 'project-test1')
668 self.assertEqual(self.builds[1].name, 'project-test2')
669 self.assertEqual(self.builds[2].name, 'project-test2')
670 self.assertEqual(self.builds[3].name, 'project-test1')
671 self.assertEqual(self.builds[4].name, 'project-test2')
672
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400673 self.assertTrue(self.builds[1].hasChanges(A))
674 self.assertTrue(self.builds[2].hasChanges(A))
675 self.assertTrue(self.builds[2].hasChanges(B))
676 self.assertTrue(self.builds[4].hasChanges(A))
677 self.assertFalse(self.builds[4].hasChanges(B))
678 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700679
680 # Finish running all passing jobs for change A
681 self.release(self.builds[1])
682 self.waitUntilSettled()
683 # Fail and report change A
684 self.release(self.builds[0])
685 self.waitUntilSettled()
686
687 # restart of B,C after A failure
James E. Blair08d19992016-08-10 15:25:31 -0700688 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700689 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700690 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700691 self.waitUntilSettled()
692
693 self.assertEqual(len(self.builds), 4)
694 self.assertEqual(self.builds[0].name, 'project-test1') # B
695 self.assertEqual(self.builds[1].name, 'project-test2') # B
696 self.assertEqual(self.builds[2].name, 'project-test1') # C
697 self.assertEqual(self.builds[3].name, 'project-test2') # C
698
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400699 self.assertFalse(self.builds[1].hasChanges(A))
700 self.assertTrue(self.builds[1].hasChanges(B))
701 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700702
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400703 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700704 # After A failed and B and C restarted, B should be back in
705 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400706 self.assertTrue(self.builds[2].hasChanges(B))
707 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700708
James E. Blair08d19992016-08-10 15:25:31 -0700709 self.launch_server.hold_jobs_in_build = False
710 self.launch_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700711 self.waitUntilSettled()
712
713 self.assertEqual(len(self.builds), 0)
714 self.assertEqual(len(self.history), 21)
715 self.assertEqual(A.data['status'], 'NEW')
716 self.assertEqual(B.data['status'], 'NEW')
717 self.assertEqual(C.data['status'], 'MERGED')
718 self.assertEqual(A.reported, 2)
719 self.assertEqual(B.reported, 2)
720 self.assertEqual(C.reported, 2)
721
James E. Blair8c803f82012-07-31 16:25:42 -0700722 def test_patch_order(self):
723 "Test that dependent patches are tested in the right order"
724 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
725 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
726 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700727 A.addApproval('code-review', 2)
728 B.addApproval('code-review', 2)
729 C.addApproval('code-review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700730
731 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
732 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
733 M2.setMerged()
734 M1.setMerged()
735
736 # C -> B -> A -> M1 -> M2
737 # M2 is here to make sure it is never queried. If it is, it
738 # means zuul is walking down the entire history of merged
739 # changes.
740
741 C.setDependsOn(B, 1)
742 B.setDependsOn(A, 1)
743 A.setDependsOn(M1, 1)
744 M1.setDependsOn(M2, 1)
745
James E. Blair8b5408c2016-08-08 15:37:46 -0700746 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700747
748 self.waitUntilSettled()
749
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400750 self.assertEqual(A.data['status'], 'NEW')
751 self.assertEqual(B.data['status'], 'NEW')
752 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700753
James E. Blair8b5408c2016-08-08 15:37:46 -0700754 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
755 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700756
757 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400758 self.assertEqual(M2.queried, 0)
759 self.assertEqual(A.data['status'], 'MERGED')
760 self.assertEqual(B.data['status'], 'MERGED')
761 self.assertEqual(C.data['status'], 'MERGED')
762 self.assertEqual(A.reported, 2)
763 self.assertEqual(B.reported, 2)
764 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700765
James E. Blair063672f2015-01-29 13:09:12 -0800766 def test_needed_changes_enqueue(self):
767 "Test that a needed change is enqueued ahead"
768 # A Given a git tree like this, if we enqueue
769 # / \ change C, we should walk up and down the tree
770 # B G and enqueue changes in the order ABCDEFG.
771 # /|\ This is also the order that you would get if
772 # *C E F you enqueued changes in the order ABCDEFG, so
773 # / the ordering is stable across re-enqueue events.
774 # D
775
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')
779 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
780 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
781 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
782 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
783 B.setDependsOn(A, 1)
784 C.setDependsOn(B, 1)
785 D.setDependsOn(C, 1)
786 E.setDependsOn(B, 1)
787 F.setDependsOn(B, 1)
788 G.setDependsOn(A, 1)
789
James E. Blair8b5408c2016-08-08 15:37:46 -0700790 A.addApproval('code-review', 2)
791 B.addApproval('code-review', 2)
792 C.addApproval('code-review', 2)
793 D.addApproval('code-review', 2)
794 E.addApproval('code-review', 2)
795 F.addApproval('code-review', 2)
796 G.addApproval('code-review', 2)
797 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800798
799 self.waitUntilSettled()
800
801 self.assertEqual(A.data['status'], 'NEW')
802 self.assertEqual(B.data['status'], 'NEW')
803 self.assertEqual(C.data['status'], 'NEW')
804 self.assertEqual(D.data['status'], 'NEW')
805 self.assertEqual(E.data['status'], 'NEW')
806 self.assertEqual(F.data['status'], 'NEW')
807 self.assertEqual(G.data['status'], 'NEW')
808
809 # We're about to add approvals to changes without adding the
810 # triggering events to Zuul, so that we can be sure that it is
811 # enqueing the changes based on dependencies, not because of
812 # triggering events. Since it will have the changes cached
813 # already (without approvals), we need to clear the cache
814 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400815 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100816 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800817
James E. Blair08d19992016-08-10 15:25:31 -0700818 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -0700819 A.addApproval('approved', 1)
820 B.addApproval('approved', 1)
821 D.addApproval('approved', 1)
822 E.addApproval('approved', 1)
823 F.addApproval('approved', 1)
824 G.addApproval('approved', 1)
825 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800826
827 for x in range(8):
James E. Blair08d19992016-08-10 15:25:31 -0700828 self.launch_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800829 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700830 self.launch_server.hold_jobs_in_build = False
831 self.launch_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800832 self.waitUntilSettled()
833
834 self.assertEqual(A.data['status'], 'MERGED')
835 self.assertEqual(B.data['status'], 'MERGED')
836 self.assertEqual(C.data['status'], 'MERGED')
837 self.assertEqual(D.data['status'], 'MERGED')
838 self.assertEqual(E.data['status'], 'MERGED')
839 self.assertEqual(F.data['status'], 'MERGED')
840 self.assertEqual(G.data['status'], 'MERGED')
841 self.assertEqual(A.reported, 2)
842 self.assertEqual(B.reported, 2)
843 self.assertEqual(C.reported, 2)
844 self.assertEqual(D.reported, 2)
845 self.assertEqual(E.reported, 2)
846 self.assertEqual(F.reported, 2)
847 self.assertEqual(G.reported, 2)
848 self.assertEqual(self.history[6].changes,
849 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
850
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100851 def test_source_cache(self):
852 "Test that the source cache operates correctly"
James E. Blair08d19992016-08-10 15:25:31 -0700853 self.launch_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700854
855 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
856 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
857 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
James E. Blair8b5408c2016-08-08 15:37:46 -0700858 A.addApproval('code-review', 2)
859 B.addApproval('code-review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700860
861 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
862 M1.setMerged()
863
864 B.setDependsOn(A, 1)
865 A.setDependsOn(M1, 1)
866
James E. Blair8b5408c2016-08-08 15:37:46 -0700867 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700868 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
869
870 self.waitUntilSettled()
871
872 for build in self.builds:
873 if build.parameters['ZUUL_PIPELINE'] == 'check':
874 build.release()
875 self.waitUntilSettled()
876 for build in self.builds:
877 if build.parameters['ZUUL_PIPELINE'] == 'check':
878 build.release()
879 self.waitUntilSettled()
880
James E. Blair8b5408c2016-08-08 15:37:46 -0700881 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700882 self.waitUntilSettled()
883
Joshua Hesketh352264b2015-08-11 23:42:08 +1000884 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700885 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000886 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700887
James E. Blair08d19992016-08-10 15:25:31 -0700888 self.launch_server.hold_jobs_in_build = False
889 self.launch_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700890 self.waitUntilSettled()
891
892 self.assertEqual(A.data['status'], 'MERGED')
893 self.assertEqual(B.data['status'], 'MERGED')
894 self.assertEqual(A.queried, 2) # Initial and isMerged
895 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
896
James E. Blair8c803f82012-07-31 16:25:42 -0700897 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700898 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700899 # TODO: move to test_gerrit (this is a unit test!)
900 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400901 tenant = self.sched.abide.tenants.get('tenant-one')
902 source = tenant.layout.pipelines['gate'].source
903
904 # TODO(pabelanger): As we add more source / trigger APIs we should make
905 # it easier for users to create events for testing.
906 event = zuul.model.TriggerEvent()
907 event.trigger_name = 'gerrit'
908 event.change_number = '1'
909 event.patch_number = '2'
910
911 a = source.getChange(event)
912 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700913 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700914
James E. Blair8b5408c2016-08-08 15:37:46 -0700915 A.addApproval('code-review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400916 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700917 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700918
James E. Blair8b5408c2016-08-08 15:37:46 -0700919 A.addApproval('approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400920 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700921 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700922
James E. Blairec056492016-07-22 09:45:56 -0700923 @skip("Disabled for early v3 development")
James E. Blair973721f2012-08-15 10:19:43 -0700924 def test_build_configuration_conflict(self):
925 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700926
927 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -0700928 A = self.fake_gerrit.addFakeChange('org/conflict-project',
929 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -0700930 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700931 B = self.fake_gerrit.addFakeChange('org/conflict-project',
932 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -0700933 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700934 C = self.fake_gerrit.addFakeChange('org/conflict-project',
935 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700936 A.addApproval('code-review', 2)
937 B.addApproval('code-review', 2)
938 C.addApproval('code-review', 2)
939 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
940 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
941 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700942 self.waitUntilSettled()
943
James E. Blair6736beb2013-07-11 15:18:15 -0700944 self.assertEqual(A.reported, 1)
945 self.assertEqual(B.reported, 1)
946 self.assertEqual(C.reported, 1)
947
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700948 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700949 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700950 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700951 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700952 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700953 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700954
955 self.assertEqual(len(self.history), 2) # A and C merge jobs
956
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700957 self.gearman_server.hold_jobs_in_queue = False
958 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700959 self.waitUntilSettled()
960
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400961 self.assertEqual(A.data['status'], 'MERGED')
962 self.assertEqual(B.data['status'], 'NEW')
963 self.assertEqual(C.data['status'], 'MERGED')
964 self.assertEqual(A.reported, 2)
965 self.assertEqual(B.reported, 2)
966 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -0700967 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -0700968
James E. Blairdaabed22012-08-15 15:38:57 -0700969 def test_post(self):
970 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700971
Zhongyue Luo5d556072012-09-21 02:00:47 +0900972 e = {
973 "type": "ref-updated",
974 "submitter": {
975 "name": "User Name",
976 },
977 "refUpdate": {
978 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
979 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
980 "refName": "master",
981 "project": "org/project",
982 }
983 }
James E. Blairdaabed22012-08-15 15:38:57 -0700984 self.fake_gerrit.addEvent(e)
985 self.waitUntilSettled()
986
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400987 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400988 self.assertEqual(len(self.history), 1)
989 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -0700990
K Jonathan Harkerf95e7232015-04-29 13:33:16 -0700991 def test_post_ignore_deletes(self):
992 "Test that deleting refs does not trigger post jobs"
993
994 e = {
995 "type": "ref-updated",
996 "submitter": {
997 "name": "User Name",
998 },
999 "refUpdate": {
1000 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1001 "newRev": "0000000000000000000000000000000000000000",
1002 "refName": "master",
1003 "project": "org/project",
1004 }
1005 }
1006 self.fake_gerrit.addEvent(e)
1007 self.waitUntilSettled()
1008
1009 job_names = [x.name for x in self.history]
1010 self.assertEqual(len(self.history), 0)
1011 self.assertNotIn('project-post', job_names)
1012
1013 def test_post_ignore_deletes_negative(self):
1014 "Test that deleting refs does trigger post jobs"
1015
Adam Gandelmanc5e4f1d2016-11-29 14:27:17 -08001016 self.updateConfigLayout('layout-dont-ignore-ref-deletes')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001017 self.sched.reconfigure(self.config)
1018
1019 e = {
1020 "type": "ref-updated",
1021 "submitter": {
1022 "name": "User Name",
1023 },
1024 "refUpdate": {
1025 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1026 "newRev": "0000000000000000000000000000000000000000",
1027 "refName": "master",
1028 "project": "org/project",
1029 }
1030 }
1031 self.fake_gerrit.addEvent(e)
1032 self.waitUntilSettled()
1033
1034 job_names = [x.name for x in self.history]
1035 self.assertEqual(len(self.history), 1)
1036 self.assertIn('project-post', job_names)
1037
James E. Blairec056492016-07-22 09:45:56 -07001038 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001039 def test_build_configuration_branch_interaction(self):
1040 "Test that switching between branches works"
1041 self.test_build_configuration()
1042 self.test_build_configuration_branch()
1043 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001044 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001045 repo = git.Repo(path)
1046 repo.heads.master.commit = repo.commit('init')
1047 self.test_build_configuration()
1048
James E. Blaircaec0c52012-08-22 14:52:22 -07001049 def test_dependent_changes_dequeue(self):
1050 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001051
James E. Blaircaec0c52012-08-22 14:52:22 -07001052 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1053 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1054 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001055 A.addApproval('code-review', 2)
1056 B.addApproval('code-review', 2)
1057 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001058
1059 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1060 M1.setMerged()
1061
1062 # C -> B -> A -> M1
1063
1064 C.setDependsOn(B, 1)
1065 B.setDependsOn(A, 1)
1066 A.setDependsOn(M1, 1)
1067
James E. Blair08d19992016-08-10 15:25:31 -07001068 self.launch_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001069
James E. Blair8b5408c2016-08-08 15:37:46 -07001070 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1071 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1072 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001073
1074 self.waitUntilSettled()
1075
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001076 self.assertEqual(A.data['status'], 'NEW')
1077 self.assertEqual(A.reported, 2)
1078 self.assertEqual(B.data['status'], 'NEW')
1079 self.assertEqual(B.reported, 2)
1080 self.assertEqual(C.data['status'], 'NEW')
1081 self.assertEqual(C.reported, 2)
1082 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001083
James E. Blair972e3c72013-08-29 12:04:55 -07001084 def test_failing_dependent_changes(self):
1085 "Test that failing dependent patches are taken out of stream"
James E. Blair08d19992016-08-10 15:25:31 -07001086 self.launch_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001087 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1088 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1089 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1090 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1091 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001092 A.addApproval('code-review', 2)
1093 B.addApproval('code-review', 2)
1094 C.addApproval('code-review', 2)
1095 D.addApproval('code-review', 2)
1096 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001097
1098 # E, D -> C -> B, A
1099
1100 D.setDependsOn(C, 1)
1101 C.setDependsOn(B, 1)
1102
James E. Blair08d19992016-08-10 15:25:31 -07001103 self.launch_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001104
James E. Blair8b5408c2016-08-08 15:37:46 -07001105 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1106 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1107 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1108 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1109 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001110
1111 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001112 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001113 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001114 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001115 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001116 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001117 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001118 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001119 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001120 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001121 self.waitUntilSettled()
1122
James E. Blair08d19992016-08-10 15:25:31 -07001123 self.launch_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001124 for build in self.builds:
1125 if build.parameters['ZUUL_CHANGE'] != '1':
1126 build.release()
1127 self.waitUntilSettled()
1128
James E. Blair08d19992016-08-10 15:25:31 -07001129 self.launch_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001130 self.waitUntilSettled()
1131
1132 self.assertEqual(A.data['status'], 'MERGED')
1133 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001134 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001135 self.assertEqual(B.data['status'], 'NEW')
1136 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001137 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001138 self.assertEqual(C.data['status'], 'NEW')
1139 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001140 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001141 self.assertEqual(D.data['status'], 'NEW')
1142 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001143 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001144 self.assertEqual(E.data['status'], 'MERGED')
1145 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001146 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001147 self.assertEqual(len(self.history), 18)
1148
James E. Blairec590122012-08-22 15:19:31 -07001149 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001150 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001151 # If it's dequeued more than once, we should see extra
1152 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001153
James E. Blair08d19992016-08-10 15:25:31 -07001154 self.launch_server.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001155 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1156 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1157 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001158 A.addApproval('code-review', 2)
1159 B.addApproval('code-review', 2)
1160 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001161
Paul Belanger58bf6912016-11-11 19:36:01 -05001162 self.launch_server.failJob('project-test1', A)
1163 self.launch_server.failJob('project-test2', A)
James E. Blair08d19992016-08-10 15:25:31 -07001164 self.launch_server.failJob('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001165
James E. Blair8b5408c2016-08-08 15:37:46 -07001166 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1167 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1168 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001169
1170 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001171
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001172 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001173 self.assertEqual(self.builds[0].name, 'project-merge')
1174 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001175
James E. Blair08d19992016-08-10 15:25:31 -07001176 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001177 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001178 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001179 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001180 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001181 self.waitUntilSettled()
1182
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001183 self.assertEqual(len(self.builds), 9)
Paul Belanger58bf6912016-11-11 19:36:01 -05001184 self.assertEqual(self.builds[0].name, 'project-test1')
1185 self.assertEqual(self.builds[1].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001186 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001187 self.assertEqual(self.builds[3].name, 'project-test1')
1188 self.assertEqual(self.builds[4].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001189 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001190 self.assertEqual(self.builds[6].name, 'project-test1')
1191 self.assertEqual(self.builds[7].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001192 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001193
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001194 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001195 self.waitUntilSettled()
1196
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001197 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1198 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001199
James E. Blair08d19992016-08-10 15:25:31 -07001200 self.launch_server.hold_jobs_in_build = False
1201 self.launch_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001202 self.waitUntilSettled()
1203
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001204 self.assertEqual(len(self.builds), 0)
1205 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001206
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001207 self.assertEqual(A.data['status'], 'NEW')
1208 self.assertEqual(B.data['status'], 'MERGED')
1209 self.assertEqual(C.data['status'], 'MERGED')
1210 self.assertEqual(A.reported, 2)
1211 self.assertEqual(B.reported, 2)
1212 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001213
1214 def test_nonvoting_job(self):
1215 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001216
James E. Blair4ec821f2012-08-23 15:28:28 -07001217 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1218 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001219 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07001220 self.launch_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001221 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001222
1223 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001224
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001225 self.assertEqual(A.data['status'], 'MERGED')
1226 self.assertEqual(A.reported, 2)
1227 self.assertEqual(
1228 self.getJobFromHistory('nonvoting-project-merge').result,
1229 'SUCCESS')
1230 self.assertEqual(
1231 self.getJobFromHistory('nonvoting-project-test1').result,
1232 'SUCCESS')
1233 self.assertEqual(
1234 self.getJobFromHistory('nonvoting-project-test2').result,
1235 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001236
James E. Blair5821bd92015-09-16 08:48:15 -07001237 for build in self.builds:
1238 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1239
James E. Blaire0487072012-08-29 17:38:31 -07001240 def test_check_queue_success(self):
1241 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001242
James E. Blaire0487072012-08-29 17:38:31 -07001243 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1244 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1245
1246 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001247
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001248 self.assertEqual(A.data['status'], 'NEW')
1249 self.assertEqual(A.reported, 1)
1250 self.assertEqual(self.getJobFromHistory('project-merge').result,
1251 'SUCCESS')
1252 self.assertEqual(self.getJobFromHistory('project-test1').result,
1253 'SUCCESS')
1254 self.assertEqual(self.getJobFromHistory('project-test2').result,
1255 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001256
1257 def test_check_queue_failure(self):
1258 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001259
James E. Blaire0487072012-08-29 17:38:31 -07001260 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair08d19992016-08-10 15:25:31 -07001261 self.launch_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001262 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1263
1264 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001265
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001266 self.assertEqual(A.data['status'], 'NEW')
1267 self.assertEqual(A.reported, 1)
1268 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001269 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001270 self.assertEqual(self.getJobFromHistory('project-test1').result,
1271 'SUCCESS')
1272 self.assertEqual(self.getJobFromHistory('project-test2').result,
1273 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001274
1275 def test_dependent_behind_dequeue(self):
1276 "test that dependent changes behind dequeued changes work"
1277 # This complicated test is a reproduction of a real life bug
1278 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001279
James E. Blair08d19992016-08-10 15:25:31 -07001280 self.launch_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001281 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1282 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1283 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1284 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1285 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1286 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1287 D.setDependsOn(C, 1)
1288 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001289 A.addApproval('code-review', 2)
1290 B.addApproval('code-review', 2)
1291 C.addApproval('code-review', 2)
1292 D.addApproval('code-review', 2)
1293 E.addApproval('code-review', 2)
1294 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001295
1296 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001297
1298 # Change object re-use in the gerrit trigger is hidden if
1299 # changes are added in quick succession; waiting makes it more
1300 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001301 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001302 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001303 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001304 self.waitUntilSettled()
1305
James E. Blair08d19992016-08-10 15:25:31 -07001306 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001307 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001308 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001309 self.waitUntilSettled()
1310
James E. Blair8b5408c2016-08-08 15:37:46 -07001311 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001312 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001313 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001314 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001315 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001316 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001317 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001318 self.waitUntilSettled()
1319
James E. Blair08d19992016-08-10 15:25:31 -07001320 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001321 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001322 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001323 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001324 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001325 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001326 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001327 self.waitUntilSettled()
1328
1329 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001330
1331 # Grab pointers to the jobs we want to release before
1332 # releasing any, because list indexes may change as
1333 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001334 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001335 a.release()
1336 b.release()
1337 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001338 self.waitUntilSettled()
1339
James E. Blair08d19992016-08-10 15:25:31 -07001340 self.launch_server.hold_jobs_in_build = False
1341 self.launch_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001342 self.waitUntilSettled()
1343
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001344 self.assertEqual(A.data['status'], 'NEW')
1345 self.assertEqual(B.data['status'], 'MERGED')
1346 self.assertEqual(C.data['status'], 'MERGED')
1347 self.assertEqual(D.data['status'], 'MERGED')
1348 self.assertEqual(E.data['status'], 'MERGED')
1349 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001350
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001351 self.assertEqual(A.reported, 2)
1352 self.assertEqual(B.reported, 2)
1353 self.assertEqual(C.reported, 2)
1354 self.assertEqual(D.reported, 2)
1355 self.assertEqual(E.reported, 2)
1356 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001357
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001358 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1359 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001360
1361 def test_merger_repack(self):
1362 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001363
James E. Blair05fed602012-09-07 12:45:24 -07001364 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001365 A.addApproval('code-review', 2)
1366 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001367 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001368 self.assertEqual(self.getJobFromHistory('project-merge').result,
1369 'SUCCESS')
1370 self.assertEqual(self.getJobFromHistory('project-test1').result,
1371 'SUCCESS')
1372 self.assertEqual(self.getJobFromHistory('project-test2').result,
1373 'SUCCESS')
1374 self.assertEqual(A.data['status'], 'MERGED')
1375 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001376 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001377 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001378
Monty Taylorbc758832013-06-17 17:22:42 -04001379 path = os.path.join(self.git_root, "org/project")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001380 print(repack_repo(path))
James E. Blair05fed602012-09-07 12:45:24 -07001381
1382 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001383 A.addApproval('code-review', 2)
1384 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001385 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001386 self.assertEqual(self.getJobFromHistory('project-merge').result,
1387 'SUCCESS')
1388 self.assertEqual(self.getJobFromHistory('project-test1').result,
1389 'SUCCESS')
1390 self.assertEqual(self.getJobFromHistory('project-test2').result,
1391 'SUCCESS')
1392 self.assertEqual(A.data['status'], 'MERGED')
1393 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001394
James E. Blair4886f282012-11-15 09:27:33 -08001395 def test_merger_repack_large_change(self):
1396 "Test that the merger works with large changes after a repack"
1397 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001398 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001399 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Hesketh352264b2015-08-11 23:42:08 +10001400 url = self.fake_gerrit.getGitUrl(
Paul Belanger503bded2016-11-11 19:43:38 -05001401 tenant.layout.project_configs.get('org/project1'))
James E. Blair4076e2b2014-01-28 12:42:20 -08001402 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08001403 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1404 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001405 path = os.path.join(self.upstream_root, "org/project1")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001406 print(repack_repo(path))
Monty Taylorbc758832013-06-17 17:22:42 -04001407 path = os.path.join(self.git_root, "org/project1")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001408 print(repack_repo(path))
James E. Blair4886f282012-11-15 09:27:33 -08001409
James E. Blair8b5408c2016-08-08 15:37:46 -07001410 A.addApproval('code-review', 2)
1411 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001412 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001413 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001414 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001415 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001416 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001417 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001418 'SUCCESS')
1419 self.assertEqual(A.data['status'], 'MERGED')
1420 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001421
James E. Blair2fa50962013-01-30 21:50:41 -08001422 def test_new_patchset_dequeues_old(self):
1423 "Test that a new patchset causes the old to be dequeued"
1424 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair08d19992016-08-10 15:25:31 -07001425 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001426 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1427 M.setMerged()
1428
1429 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1430 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1431 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1432 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001433 A.addApproval('code-review', 2)
1434 B.addApproval('code-review', 2)
1435 C.addApproval('code-review', 2)
1436 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001437
1438 C.setDependsOn(B, 1)
1439 B.setDependsOn(A, 1)
1440 A.setDependsOn(M, 1)
1441
James E. Blair8b5408c2016-08-08 15:37:46 -07001442 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1443 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1444 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1445 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001446 self.waitUntilSettled()
1447
1448 B.addPatchset()
1449 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1450 self.waitUntilSettled()
1451
James E. Blair08d19992016-08-10 15:25:31 -07001452 self.launch_server.hold_jobs_in_build = False
1453 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001454 self.waitUntilSettled()
1455
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001456 self.assertEqual(A.data['status'], 'MERGED')
1457 self.assertEqual(A.reported, 2)
1458 self.assertEqual(B.data['status'], 'NEW')
1459 self.assertEqual(B.reported, 2)
1460 self.assertEqual(C.data['status'], 'NEW')
1461 self.assertEqual(C.reported, 2)
1462 self.assertEqual(D.data['status'], 'MERGED')
1463 self.assertEqual(D.reported, 2)
1464 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001465
James E. Blairba437362015-02-07 11:41:52 -08001466 def test_new_patchset_check(self):
1467 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001468
James E. Blair08d19992016-08-10 15:25:31 -07001469 self.launch_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001470
1471 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001472 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001473 tenant = self.sched.abide.tenants.get('tenant-one')
1474 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001475
1476 # Add two git-dependent changes
1477 B.setDependsOn(A, 1)
1478 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1479 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001480 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1481 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001482
1483 # A live item, and a non-live/live pair
1484 items = check_pipeline.getAllItems()
1485 self.assertEqual(len(items), 3)
1486
1487 self.assertEqual(items[0].change.number, '1')
1488 self.assertEqual(items[0].change.patchset, '1')
1489 self.assertFalse(items[0].live)
1490
1491 self.assertEqual(items[1].change.number, '2')
1492 self.assertEqual(items[1].change.patchset, '1')
1493 self.assertTrue(items[1].live)
1494
1495 self.assertEqual(items[2].change.number, '1')
1496 self.assertEqual(items[2].change.patchset, '1')
1497 self.assertTrue(items[2].live)
1498
1499 # Add a new patchset to A
1500 A.addPatchset()
1501 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1502 self.waitUntilSettled()
1503
1504 # The live copy of A,1 should be gone, but the non-live and B
1505 # should continue, and we should have a new A,2
1506 items = check_pipeline.getAllItems()
1507 self.assertEqual(len(items), 3)
1508
1509 self.assertEqual(items[0].change.number, '1')
1510 self.assertEqual(items[0].change.patchset, '1')
1511 self.assertFalse(items[0].live)
1512
1513 self.assertEqual(items[1].change.number, '2')
1514 self.assertEqual(items[1].change.patchset, '1')
1515 self.assertTrue(items[1].live)
1516
1517 self.assertEqual(items[2].change.number, '1')
1518 self.assertEqual(items[2].change.patchset, '2')
1519 self.assertTrue(items[2].live)
1520
1521 # Add a new patchset to B
1522 B.addPatchset()
1523 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1524 self.waitUntilSettled()
1525
1526 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1527 # but we should have a new B,2 (still based on A,1)
1528 items = check_pipeline.getAllItems()
1529 self.assertEqual(len(items), 3)
1530
1531 self.assertEqual(items[0].change.number, '1')
1532 self.assertEqual(items[0].change.patchset, '2')
1533 self.assertTrue(items[0].live)
1534
1535 self.assertEqual(items[1].change.number, '1')
1536 self.assertEqual(items[1].change.patchset, '1')
1537 self.assertFalse(items[1].live)
1538
1539 self.assertEqual(items[2].change.number, '2')
1540 self.assertEqual(items[2].change.patchset, '2')
1541 self.assertTrue(items[2].live)
1542
1543 self.builds[0].release()
1544 self.waitUntilSettled()
1545 self.builds[0].release()
1546 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001547 self.launch_server.hold_jobs_in_build = False
1548 self.launch_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001549 self.waitUntilSettled()
1550
1551 self.assertEqual(A.reported, 1)
1552 self.assertEqual(B.reported, 1)
1553 self.assertEqual(self.history[0].result, 'ABORTED')
1554 self.assertEqual(self.history[0].changes, '1,1')
1555 self.assertEqual(self.history[1].result, 'ABORTED')
1556 self.assertEqual(self.history[1].changes, '1,1 2,1')
1557 self.assertEqual(self.history[2].result, 'SUCCESS')
1558 self.assertEqual(self.history[2].changes, '1,2')
1559 self.assertEqual(self.history[3].result, 'SUCCESS')
1560 self.assertEqual(self.history[3].changes, '1,1 2,2')
1561
1562 def test_abandoned_gate(self):
1563 "Test that an abandoned change is dequeued from gate"
1564
James E. Blair08d19992016-08-10 15:25:31 -07001565 self.launch_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001566
1567 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001568 A.addApproval('code-review', 2)
1569 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001570 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001571 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1572 self.assertEqual(self.builds[0].name, 'project-merge')
1573
1574 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1575 self.waitUntilSettled()
1576
James E. Blair08d19992016-08-10 15:25:31 -07001577 self.launch_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001578 self.waitUntilSettled()
1579
Arie5b4048c2016-10-05 16:12:06 +03001580 self.assertBuilds([])
1581 self.assertHistory([
1582 dict(name='project-merge', result='ABORTED', changes='1,1')],
1583 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001584 self.assertEqual(A.reported, 1,
1585 "Abandoned gate change should report only start")
1586
1587 def test_abandoned_check(self):
1588 "Test that an abandoned change is dequeued from check"
1589
James E. Blair08d19992016-08-10 15:25:31 -07001590 self.launch_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001591
1592 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1593 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001594 tenant = self.sched.abide.tenants.get('tenant-one')
1595 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001596
1597 # Add two git-dependent changes
1598 B.setDependsOn(A, 1)
1599 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1600 self.waitUntilSettled()
1601 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1602 self.waitUntilSettled()
1603 # A live item, and a non-live/live pair
1604 items = check_pipeline.getAllItems()
1605 self.assertEqual(len(items), 3)
1606
1607 self.assertEqual(items[0].change.number, '1')
1608 self.assertFalse(items[0].live)
1609
1610 self.assertEqual(items[1].change.number, '2')
1611 self.assertTrue(items[1].live)
1612
1613 self.assertEqual(items[2].change.number, '1')
1614 self.assertTrue(items[2].live)
1615
1616 # Abandon A
1617 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1618 self.waitUntilSettled()
1619
1620 # The live copy of A should be gone, but the non-live and B
1621 # should continue
1622 items = check_pipeline.getAllItems()
1623 self.assertEqual(len(items), 2)
1624
1625 self.assertEqual(items[0].change.number, '1')
1626 self.assertFalse(items[0].live)
1627
1628 self.assertEqual(items[1].change.number, '2')
1629 self.assertTrue(items[1].live)
1630
James E. Blair08d19992016-08-10 15:25:31 -07001631 self.launch_server.hold_jobs_in_build = False
1632 self.launch_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001633 self.waitUntilSettled()
1634
1635 self.assertEqual(len(self.history), 4)
1636 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001637 'Build should have been aborted')
1638 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001639 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001640
Steve Varnau7b78b312015-04-03 14:49:46 -07001641 def test_abandoned_not_timer(self):
1642 "Test that an abandoned change does not cancel timer jobs"
1643
James E. Blair08d19992016-08-10 15:25:31 -07001644 self.launch_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001645
1646 # Start timer trigger - also org/project
Clint Byrumead6c562017-02-01 16:34:04 -08001647 self.updateConfigLayout('layout-idle')
Steve Varnau7b78b312015-04-03 14:49:46 -07001648 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001649 # The pipeline triggers every second, so we should have seen
1650 # several by now.
1651 time.sleep(5)
1652 self.waitUntilSettled()
1653 # Stop queuing timer triggered jobs so that the assertions
1654 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001655 # Must be in same repo, so overwrite config with another one
1656 no_timer_path = os.path.join(self.test_root, 'upstream',
1657 'layout-no-timer', 'zuul.yaml')
1658 with open(no_timer_path, 'r') as nt:
1659 self.addCommitToRepo('layout-idle', 'Removing timer jobs',
1660 {'zuul.yaml': nt.read()})
1661
Steve Varnau7b78b312015-04-03 14:49:46 -07001662 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001663 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1664
1665 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1666 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1667 self.waitUntilSettled()
1668 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1669
1670 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1671 self.waitUntilSettled()
1672
1673 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1674
James E. Blair08d19992016-08-10 15:25:31 -07001675 self.launch_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001676 self.waitUntilSettled()
1677
Arx Cruzb1b010d2013-10-28 19:49:59 -02001678 def test_zuul_url_return(self):
1679 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001680 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
James E. Blair08d19992016-08-10 15:25:31 -07001681 self.launch_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001682
1683 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001684 A.addApproval('code-review', 2)
1685 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001686 self.waitUntilSettled()
1687
1688 self.assertEqual(len(self.builds), 1)
1689 for build in self.builds:
1690 self.assertTrue('ZUUL_URL' in build.parameters)
1691
James E. Blair08d19992016-08-10 15:25:31 -07001692 self.launch_server.hold_jobs_in_build = False
1693 self.launch_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001694 self.waitUntilSettled()
1695
James E. Blair2fa50962013-01-30 21:50:41 -08001696 def test_new_patchset_dequeues_old_on_head(self):
1697 "Test that a new patchset causes the old to be dequeued (at head)"
1698 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair08d19992016-08-10 15:25:31 -07001699 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001700 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1701 M.setMerged()
1702 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1703 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1704 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1705 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001706 A.addApproval('code-review', 2)
1707 B.addApproval('code-review', 2)
1708 C.addApproval('code-review', 2)
1709 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001710
1711 C.setDependsOn(B, 1)
1712 B.setDependsOn(A, 1)
1713 A.setDependsOn(M, 1)
1714
James E. Blair8b5408c2016-08-08 15:37:46 -07001715 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1716 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1717 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1718 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001719 self.waitUntilSettled()
1720
1721 A.addPatchset()
1722 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1723 self.waitUntilSettled()
1724
James E. Blair08d19992016-08-10 15:25:31 -07001725 self.launch_server.hold_jobs_in_build = False
1726 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001727 self.waitUntilSettled()
1728
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001729 self.assertEqual(A.data['status'], 'NEW')
1730 self.assertEqual(A.reported, 2)
1731 self.assertEqual(B.data['status'], 'NEW')
1732 self.assertEqual(B.reported, 2)
1733 self.assertEqual(C.data['status'], 'NEW')
1734 self.assertEqual(C.reported, 2)
1735 self.assertEqual(D.data['status'], 'MERGED')
1736 self.assertEqual(D.reported, 2)
1737 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001738
1739 def test_new_patchset_dequeues_old_without_dependents(self):
1740 "Test that a new patchset causes only the old to be dequeued"
James E. Blair08d19992016-08-10 15:25:31 -07001741 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001742 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1743 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1744 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001745 A.addApproval('code-review', 2)
1746 B.addApproval('code-review', 2)
1747 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001748
James E. Blair8b5408c2016-08-08 15:37:46 -07001749 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1750 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1751 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001752 self.waitUntilSettled()
1753
1754 B.addPatchset()
1755 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1756 self.waitUntilSettled()
1757
James E. Blair08d19992016-08-10 15:25:31 -07001758 self.launch_server.hold_jobs_in_build = False
1759 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001760 self.waitUntilSettled()
1761
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001762 self.assertEqual(A.data['status'], 'MERGED')
1763 self.assertEqual(A.reported, 2)
1764 self.assertEqual(B.data['status'], 'NEW')
1765 self.assertEqual(B.reported, 2)
1766 self.assertEqual(C.data['status'], 'MERGED')
1767 self.assertEqual(C.reported, 2)
1768 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001769
1770 def test_new_patchset_dequeues_old_independent_queue(self):
1771 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair08d19992016-08-10 15:25:31 -07001772 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001773 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1774 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1775 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1776 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1777 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1778 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1779 self.waitUntilSettled()
1780
1781 B.addPatchset()
1782 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1783 self.waitUntilSettled()
1784
James E. Blair08d19992016-08-10 15:25:31 -07001785 self.launch_server.hold_jobs_in_build = False
1786 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001787 self.waitUntilSettled()
1788
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001789 self.assertEqual(A.data['status'], 'NEW')
1790 self.assertEqual(A.reported, 1)
1791 self.assertEqual(B.data['status'], 'NEW')
1792 self.assertEqual(B.reported, 1)
1793 self.assertEqual(C.data['status'], 'NEW')
1794 self.assertEqual(C.reported, 1)
1795 self.assertEqual(len(self.history), 10)
1796 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001797
James E. Blair18c64442014-03-18 10:14:45 -07001798 def test_noop_job(self):
1799 "Test that the internal noop job works"
1800 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001801 A.addApproval('code-review', 2)
1802 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001803 self.waitUntilSettled()
1804
1805 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1806 self.assertTrue(self.sched._areAllBuildsComplete())
1807 self.assertEqual(len(self.history), 0)
1808 self.assertEqual(A.data['status'], 'MERGED')
1809 self.assertEqual(A.reported, 2)
1810
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001811 def test_no_job_project(self):
1812 "Test that reports with no jobs don't get sent"
1813 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1814 'master', 'A')
1815 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1816 self.waitUntilSettled()
1817
1818 # Change wasn't reported to
1819 self.assertEqual(A.reported, False)
1820
1821 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001822 tenant = self.sched.abide.tenants.get('tenant-one')
1823 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001824 items = check_pipeline.getAllItems()
1825 self.assertEqual(len(items), 0)
1826
1827 self.assertEqual(len(self.history), 0)
1828
James E. Blair7d0dedc2013-02-21 17:26:09 -08001829 def test_zuul_refs(self):
1830 "Test that zuul refs exist and have the right changes"
James E. Blair08d19992016-08-10 15:25:31 -07001831 self.launch_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001832 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1833 M1.setMerged()
1834 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1835 M2.setMerged()
1836
1837 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1838 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1839 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1840 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001841 A.addApproval('code-review', 2)
1842 B.addApproval('code-review', 2)
1843 C.addApproval('code-review', 2)
1844 D.addApproval('code-review', 2)
1845 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1846 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1847 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1848 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001849
1850 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001851 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001852 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001853 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001854 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001855 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001856 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001857 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001858 self.waitUntilSettled()
1859
James E. Blair7d0dedc2013-02-21 17:26:09 -08001860 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08001861 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001862 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001863 if x.parameters['ZUUL_CHANGE'] == '3':
1864 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001865 a_build = x
1866 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001867 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001868 b_build = x
1869 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001870 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001871 c_build = x
1872 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001873 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001874 d_build = x
1875 if a_build and b_build and c_build and d_build:
1876 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08001877
1878 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001879 self.assertIsNotNone(a_zref)
1880 self.assertIsNotNone(b_zref)
1881 self.assertIsNotNone(c_zref)
1882 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001883
1884 # And they should all be different
1885 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001886 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001887
Clint Byrum3343e3e2016-11-15 16:05:03 -08001888 # should have a, not b, and should not be in project2
1889 self.assertTrue(a_build.hasChanges(A))
1890 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001891
Clint Byrum3343e3e2016-11-15 16:05:03 -08001892 # should have a and b, and should not be in project2
1893 self.assertTrue(b_build.hasChanges(A, B))
1894 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001895
Clint Byrum3343e3e2016-11-15 16:05:03 -08001896 # should have a and b in 1, c in 2
1897 self.assertTrue(c_build.hasChanges(A, B, C))
1898 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001899
Clint Byrum3343e3e2016-11-15 16:05:03 -08001900 # should have a and b in 1, c and d in 2
1901 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001902
James E. Blair08d19992016-08-10 15:25:31 -07001903 self.launch_server.hold_jobs_in_build = False
1904 self.launch_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08001905 self.waitUntilSettled()
1906
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001907 self.assertEqual(A.data['status'], 'MERGED')
1908 self.assertEqual(A.reported, 2)
1909 self.assertEqual(B.data['status'], 'MERGED')
1910 self.assertEqual(B.reported, 2)
1911 self.assertEqual(C.data['status'], 'MERGED')
1912 self.assertEqual(C.reported, 2)
1913 self.assertEqual(D.data['status'], 'MERGED')
1914 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08001915
James E. Blair4a28a882013-08-23 15:17:33 -07001916 def test_rerun_on_error(self):
1917 "Test that if a worker fails to run a job, it is run again"
James E. Blair08d19992016-08-10 15:25:31 -07001918 self.launch_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07001919 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001920 A.addApproval('code-review', 2)
1921 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07001922 self.waitUntilSettled()
1923
James E. Blair412fba82017-01-26 15:00:50 -08001924 self.builds[0].requeue = True
James E. Blair08d19992016-08-10 15:25:31 -07001925 self.launch_server.hold_jobs_in_build = False
1926 self.launch_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07001927 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08001928 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07001929 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
1930
James E. Blair412e5582013-04-22 15:50:12 -07001931 def test_statsd(self):
1932 "Test each of the statsd methods used in the scheduler"
1933 import extras
1934 statsd = extras.try_import('statsd.statsd')
1935 statsd.incr('test-incr')
1936 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07001937 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07001938 self.assertReportedStat('test-incr', '1|c')
1939 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07001940 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07001941
James E. Blairec056492016-07-22 09:45:56 -07001942 @skip("Disabled for early v3 development")
James E. Blairdad52252014-02-07 16:59:17 -08001943 def test_stuck_job_cleanup(self):
1944 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07001945 # This job won't be registered at startup because it is not in
1946 # the standard layout, but we need it to already be registerd
1947 # for when we reconfigure, as that is when Zuul will attempt
1948 # to run the new job.
1949 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001950 self.gearman_server.hold_jobs_in_queue = True
1951 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001952 A.addApproval('code-review', 2)
1953 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08001954 self.waitUntilSettled()
1955 self.assertEqual(len(self.gearman_server.getQueue()), 1)
1956
James E. Blairf84026c2015-12-08 16:11:46 -08001957 self.updateConfigLayout(
1958 'tests/fixtures/layout-no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08001959 self.sched.reconfigure(self.config)
1960 self.waitUntilSettled()
1961
James E. Blair18c64442014-03-18 10:14:45 -07001962 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001963 self.waitUntilSettled()
1964 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1965 self.assertTrue(self.sched._areAllBuildsComplete())
1966
1967 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07001968 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001969 self.assertEqual(self.history[0].result, 'SUCCESS')
1970
James E. Blair879dafb2015-07-17 14:04:49 -07001971 def test_file_head(self):
1972 # This is a regression test for an observed bug. A change
1973 # with a file named "HEAD" in the root directory of the repo
1974 # was processed by a merger. It then was unable to reset the
1975 # repo because of:
1976 # GitCommandError: 'git reset --hard HEAD' returned
1977 # with exit code 128
1978 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
1979 # and filename
1980 # Use '--' to separate filenames from revisions'
1981
1982 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04001983 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07001984 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1985
1986 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1987 self.waitUntilSettled()
1988
1989 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1990 self.waitUntilSettled()
1991
1992 self.assertIn('Build succeeded', A.messages[0])
1993 self.assertIn('Build succeeded', B.messages[0])
1994
James E. Blair70c71582013-03-06 08:50:50 -08001995 def test_file_jobs(self):
1996 "Test that file jobs run only when appropriate"
1997 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08001998 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08001999 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002000 A.addApproval('code-review', 2)
2001 B.addApproval('code-review', 2)
2002 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2003 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002004 self.waitUntilSettled()
2005
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002006 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002007 if x.name == 'project-testfile']
2008
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002009 self.assertEqual(len(testfile_jobs), 1)
2010 self.assertEqual(testfile_jobs[0].changes, '1,2')
2011 self.assertEqual(A.data['status'], 'MERGED')
2012 self.assertEqual(A.reported, 2)
2013 self.assertEqual(B.data['status'], 'MERGED')
2014 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002015
Clint Byrum3ee39f32016-11-17 23:45:07 -08002016 def _test_irrelevant_files_jobs(self, should_skip):
2017 "Test that jobs with irrelevant-files filter run only when appropriate"
2018 self.updateConfigLayout('layout-irrelevant-files')
Maru Newby3fe5f852015-01-13 04:22:14 +00002019 self.sched.reconfigure(self.config)
Clint Byrum3ee39f32016-11-17 23:45:07 -08002020
2021 if should_skip:
2022 files = {'ignoreme': 'ignored\n'}
2023 else:
2024 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002025
2026 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002027 'master',
2028 'test irrelevant-files',
2029 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002030 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2031 self.waitUntilSettled()
2032
2033 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002034 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002035
2036 if should_skip:
2037 self.assertEqual([], tested_change_ids)
2038 else:
2039 self.assertIn(change.data['number'], tested_change_ids)
2040
Clint Byrum3ee39f32016-11-17 23:45:07 -08002041 def test_irrelevant_files_match_skips_job(self):
2042 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002043
Clint Byrum3ee39f32016-11-17 23:45:07 -08002044 def test_irrelevant_files_no_match_runs_job(self):
2045 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002046
Clint Byrum85493602016-11-18 11:59:47 -08002047 def test_inherited_jobs_keep_matchers(self):
2048 self.updateConfigLayout('layout-inheritance')
2049 self.sched.reconfigure(self.config)
2050
2051 files = {'ignoreme': 'ignored\n'}
2052
2053 change = self.fake_gerrit.addFakeChange('org/project',
2054 'master',
2055 'test irrelevant-files',
2056 files=files)
2057 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2058 self.waitUntilSettled()
2059
2060 run_jobs = set([build.name for build in self.history])
2061
2062 self.assertEqual(set(['project-test-nomatch-starts-empty',
2063 'project-test-nomatch-starts-full']), run_jobs)
2064
James E. Blairec056492016-07-22 09:45:56 -07002065 @skip("Disabled for early v3 development")
James E. Blair3c5e5b52013-04-26 11:17:03 -07002066 def test_test_config(self):
2067 "Test that we can test the config"
James E. Blairf84026c2015-12-08 16:11:46 -08002068 self.sched.testConfig(self.config.get('zuul', 'tenant_config'),
Joshua Hesketh352264b2015-08-11 23:42:08 +10002069 self.connections)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002070
James E. Blairec056492016-07-22 09:45:56 -07002071 @skip("Disabled for early v3 development")
James E. Blairc8a1e052014-02-25 09:29:26 -08002072 def test_queue_names(self):
2073 "Test shared change queue names"
2074 project1 = self.sched.layout.projects['org/project1']
2075 project2 = self.sched.layout.projects['org/project2']
2076 q1 = self.sched.layout.pipelines['gate'].getQueue(project1)
2077 q2 = self.sched.layout.pipelines['gate'].getQueue(project2)
2078 self.assertEqual(q1.name, 'integration')
2079 self.assertEqual(q2.name, 'integration')
2080
James E. Blairf84026c2015-12-08 16:11:46 -08002081 self.updateConfigLayout(
2082 'tests/fixtures/layout-bad-queue.yaml')
James E. Blairc8a1e052014-02-25 09:29:26 -08002083 with testtools.ExpectedException(
2084 Exception, "More than one name assigned to change queue"):
2085 self.sched.reconfigure(self.config)
2086
James E. Blair64ed6f22013-07-10 14:07:23 -07002087 def test_queue_precedence(self):
2088 "Test that queue precedence works"
2089
2090 self.gearman_server.hold_jobs_in_queue = True
James E. Blair08d19992016-08-10 15:25:31 -07002091 self.launch_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002092 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2093 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002094 A.addApproval('code-review', 2)
2095 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002096
2097 self.waitUntilSettled()
2098 self.gearman_server.hold_jobs_in_queue = False
2099 self.gearman_server.release()
2100 self.waitUntilSettled()
2101
James E. Blair8de58bd2013-07-18 16:23:33 -07002102 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002103 self.orderedRelease()
James E. Blair08d19992016-08-10 15:25:31 -07002104 self.launch_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002105 self.waitUntilSettled()
2106
James E. Blair64ed6f22013-07-10 14:07:23 -07002107 self.log.debug(self.history)
2108 self.assertEqual(self.history[0].pipeline, 'gate')
2109 self.assertEqual(self.history[1].pipeline, 'check')
2110 self.assertEqual(self.history[2].pipeline, 'gate')
2111 self.assertEqual(self.history[3].pipeline, 'gate')
2112 self.assertEqual(self.history[4].pipeline, 'check')
2113 self.assertEqual(self.history[5].pipeline, 'check')
2114
James E. Blairec056492016-07-22 09:45:56 -07002115 @skip("Disabled for early v3 development")
Clark Boylana5edbe42014-06-03 16:39:10 -07002116 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002117 "Test that we can retrieve JSON status info"
James E. Blair08d19992016-08-10 15:25:31 -07002118 self.launch_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002119 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002120 A.addApproval('code-review', 2)
2121 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002122 self.waitUntilSettled()
2123
James E. Blair08d19992016-08-10 15:25:31 -07002124 self.launch_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002125 self.waitUntilSettled()
2126
James E. Blair1843a552013-07-03 14:19:52 -07002127 port = self.webapp.server.socket.getsockname()[1]
2128
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002129 req = urllib.request.Request("http://localhost:%s/status.json" % port)
2130 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002131 headers = f.info()
2132 self.assertIn('Content-Length', headers)
2133 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002134 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2135 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002136 self.assertIn('Access-Control-Allow-Origin', headers)
2137 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002138 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002139 self.assertIn('Expires', headers)
James E. Blair1843a552013-07-03 14:19:52 -07002140 data = f.read()
2141
James E. Blair08d19992016-08-10 15:25:31 -07002142 self.launch_server.hold_jobs_in_build = False
2143 self.launch_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002144 self.waitUntilSettled()
2145
2146 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002147 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002148 for p in data['pipelines']:
2149 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002150 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002151 self.assertEqual(q['window'], 20)
2152 else:
2153 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002154 for head in q['heads']:
2155 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002156 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002157 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002158 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002159 status_jobs.append(job)
2160 self.assertEqual('project-merge', status_jobs[0]['name'])
2161 self.assertEqual('https://server/job/project-merge/0/',
2162 status_jobs[0]['url'])
2163 self.assertEqual('http://logs.example.com/1/1/gate/project-merge/0',
2164 status_jobs[0]['report_url'])
2165
2166 self.assertEqual('project-test1', status_jobs[1]['name'])
2167 self.assertEqual('https://server/job/project-test1/1/',
2168 status_jobs[1]['url'])
2169 self.assertEqual('http://logs.example.com/1/1/gate/project-test1/1',
2170 status_jobs[1]['report_url'])
2171
2172 self.assertEqual('project-test2', status_jobs[2]['name'])
2173 self.assertEqual('https://server/job/project-test2/2/',
2174 status_jobs[2]['url'])
2175 self.assertEqual('http://logs.example.com/1/1/gate/project-test2/2',
2176 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002177
James E. Blairec056492016-07-22 09:45:56 -07002178 @skip("Disabled for early v3 development")
James E. Blairc3d428e2013-12-03 15:06:48 -08002179 def test_merging_queues(self):
2180 "Test that transitively-connected change queues are merged"
James E. Blairf84026c2015-12-08 16:11:46 -08002181 self.updateConfigLayout(
2182 'tests/fixtures/layout-merge-queues.yaml')
James E. Blairc3d428e2013-12-03 15:06:48 -08002183 self.sched.reconfigure(self.config)
2184 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
2185
James E. Blairec056492016-07-22 09:45:56 -07002186 @skip("Disabled for early v3 development")
James E. Blairaf17a972016-02-03 15:07:18 -08002187 def test_mutex(self):
2188 "Test job mutexes"
2189 self.config.set('zuul', 'layout_config',
2190 'tests/fixtures/layout-mutex.yaml')
2191 self.sched.reconfigure(self.config)
2192
James E. Blair08d19992016-08-10 15:25:31 -07002193 self.launch_server.hold_jobs_in_build = True
James E. Blairaf17a972016-02-03 15:07:18 -08002194 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2195 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2196 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2197
2198 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2199 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2200 self.waitUntilSettled()
2201 self.assertEqual(len(self.builds), 3)
2202 self.assertEqual(self.builds[0].name, 'project-test1')
2203 self.assertEqual(self.builds[1].name, 'mutex-one')
2204 self.assertEqual(self.builds[2].name, 'project-test1')
2205
James E. Blair08d19992016-08-10 15:25:31 -07002206 self.launch_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002207 self.waitUntilSettled()
2208
2209 self.assertEqual(len(self.builds), 3)
2210 self.assertEqual(self.builds[0].name, 'project-test1')
2211 self.assertEqual(self.builds[1].name, 'project-test1')
2212 self.assertEqual(self.builds[2].name, 'mutex-two')
2213 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2214
James E. Blair08d19992016-08-10 15:25:31 -07002215 self.launch_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002216 self.waitUntilSettled()
2217
2218 self.assertEqual(len(self.builds), 3)
2219 self.assertEqual(self.builds[0].name, 'project-test1')
2220 self.assertEqual(self.builds[1].name, 'project-test1')
2221 self.assertEqual(self.builds[2].name, 'mutex-one')
2222 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2223
James E. Blair08d19992016-08-10 15:25:31 -07002224 self.launch_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002225 self.waitUntilSettled()
2226
2227 self.assertEqual(len(self.builds), 3)
2228 self.assertEqual(self.builds[0].name, 'project-test1')
2229 self.assertEqual(self.builds[1].name, 'project-test1')
2230 self.assertEqual(self.builds[2].name, 'mutex-two')
2231 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2232
James E. Blair08d19992016-08-10 15:25:31 -07002233 self.launch_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002234 self.waitUntilSettled()
2235
2236 self.assertEqual(len(self.builds), 2)
2237 self.assertEqual(self.builds[0].name, 'project-test1')
2238 self.assertEqual(self.builds[1].name, 'project-test1')
2239 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2240
James E. Blair08d19992016-08-10 15:25:31 -07002241 self.launch_server.hold_jobs_in_build = False
2242 self.launch_server.release()
James E. Blairaf17a972016-02-03 15:07:18 -08002243
2244 self.waitUntilSettled()
2245 self.assertEqual(len(self.builds), 0)
2246
2247 self.assertEqual(A.reported, 1)
2248 self.assertEqual(B.reported, 1)
2249 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2250
James E. Blairec056492016-07-22 09:45:56 -07002251 @skip("Disabled for early v3 development")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002252 def test_node_label(self):
2253 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002254 self.worker.registerFunction('build:node-project-test1:debian')
2255
2256 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002257 A.addApproval('code-review', 2)
2258 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002259 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002260
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002261 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2262 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2263 'debian')
2264 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002265
2266 def test_live_reconfiguration(self):
2267 "Test that live reconfiguration works"
James E. Blair08d19992016-08-10 15:25:31 -07002268 self.launch_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002269 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002270 A.addApproval('code-review', 2)
2271 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002272 self.waitUntilSettled()
2273
2274 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002275 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002276
James E. Blair08d19992016-08-10 15:25:31 -07002277 self.launch_server.hold_jobs_in_build = False
2278 self.launch_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002279 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002280 self.assertEqual(self.getJobFromHistory('project-merge').result,
2281 'SUCCESS')
2282 self.assertEqual(self.getJobFromHistory('project-test1').result,
2283 'SUCCESS')
2284 self.assertEqual(self.getJobFromHistory('project-test2').result,
2285 'SUCCESS')
2286 self.assertEqual(A.data['status'], 'MERGED')
2287 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002288
James E. Blairec056492016-07-22 09:45:56 -07002289 @skip("Disabled for early v3 development")
James E. Blair6bc782d2015-07-17 16:20:21 -07002290 def test_live_reconfiguration_merge_conflict(self):
2291 # A real-world bug: a change in a gate queue has a merge
2292 # conflict and a job is added to its project while it's
2293 # sitting in the queue. The job gets added to the change and
2294 # enqueued and the change gets stuck.
2295 self.worker.registerFunction('build:project-test3')
James E. Blair08d19992016-08-10 15:25:31 -07002296 self.launch_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002297
2298 # This change is fine. It's here to stop the queue long
2299 # enough for the next change to be subject to the
2300 # reconfiguration, as well as to provide a conflict for the
2301 # next change. This change will succeed and merge.
2302 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2303 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002304 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002305
2306 # This change will be in merge conflict. During the
2307 # reconfiguration, we will add a job. We want to make sure
2308 # that doesn't cause it to get stuck.
2309 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2310 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002311 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002312
James E. Blair8b5408c2016-08-08 15:37:46 -07002313 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2314 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002315
2316 self.waitUntilSettled()
2317
2318 # No jobs have run yet
2319 self.assertEqual(A.data['status'], 'NEW')
2320 self.assertEqual(A.reported, 1)
2321 self.assertEqual(B.data['status'], 'NEW')
2322 self.assertEqual(B.reported, 1)
2323 self.assertEqual(len(self.history), 0)
2324
2325 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002326 self.updateConfigLayout(
2327 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002328 self.sched.reconfigure(self.config)
2329 self.waitUntilSettled()
2330
James E. Blair08d19992016-08-10 15:25:31 -07002331 self.launch_server.hold_jobs_in_build = False
2332 self.launch_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002333 self.waitUntilSettled()
2334
2335 self.assertEqual(A.data['status'], 'MERGED')
2336 self.assertEqual(A.reported, 2)
2337 self.assertEqual(B.data['status'], 'NEW')
2338 self.assertEqual(B.reported, 2)
2339 self.assertEqual(self.getJobFromHistory('project-merge').result,
2340 'SUCCESS')
2341 self.assertEqual(self.getJobFromHistory('project-test1').result,
2342 'SUCCESS')
2343 self.assertEqual(self.getJobFromHistory('project-test2').result,
2344 'SUCCESS')
2345 self.assertEqual(self.getJobFromHistory('project-test3').result,
2346 'SUCCESS')
2347 self.assertEqual(len(self.history), 4)
2348
James E. Blairec056492016-07-22 09:45:56 -07002349 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002350 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002351 # An extrapolation of test_live_reconfiguration_merge_conflict
2352 # that tests a job added to a job tree with a failed root does
2353 # not run.
2354 self.worker.registerFunction('build:project-test3')
James E. Blair08d19992016-08-10 15:25:31 -07002355 self.launch_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002356
2357 # This change is fine. It's here to stop the queue long
2358 # enough for the next change to be subject to the
2359 # reconfiguration. This change will succeed and merge.
2360 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2361 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002362 A.addApproval('code-review', 2)
2363 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002364 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002365 self.launch_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002366 self.waitUntilSettled()
2367
2368 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair08d19992016-08-10 15:25:31 -07002369 self.launch_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002370 B.addApproval('code-review', 2)
2371 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002372 self.waitUntilSettled()
2373
James E. Blair08d19992016-08-10 15:25:31 -07002374 self.launch_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002375 self.waitUntilSettled()
2376
2377 # Both -merge jobs have run, but no others.
2378 self.assertEqual(A.data['status'], 'NEW')
2379 self.assertEqual(A.reported, 1)
2380 self.assertEqual(B.data['status'], 'NEW')
2381 self.assertEqual(B.reported, 1)
2382 self.assertEqual(self.history[0].result, 'SUCCESS')
2383 self.assertEqual(self.history[0].name, 'project-merge')
2384 self.assertEqual(self.history[1].result, 'FAILURE')
2385 self.assertEqual(self.history[1].name, 'project-merge')
2386 self.assertEqual(len(self.history), 2)
2387
2388 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002389 self.updateConfigLayout(
2390 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002391 self.sched.reconfigure(self.config)
2392 self.waitUntilSettled()
2393
James E. Blair08d19992016-08-10 15:25:31 -07002394 self.launch_server.hold_jobs_in_build = False
2395 self.launch_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002396 self.waitUntilSettled()
2397
2398 self.assertEqual(A.data['status'], 'MERGED')
2399 self.assertEqual(A.reported, 2)
2400 self.assertEqual(B.data['status'], 'NEW')
2401 self.assertEqual(B.reported, 2)
2402 self.assertEqual(self.history[0].result, 'SUCCESS')
2403 self.assertEqual(self.history[0].name, 'project-merge')
2404 self.assertEqual(self.history[1].result, 'FAILURE')
2405 self.assertEqual(self.history[1].name, 'project-merge')
2406 self.assertEqual(self.history[2].result, 'SUCCESS')
2407 self.assertEqual(self.history[3].result, 'SUCCESS')
2408 self.assertEqual(self.history[4].result, 'SUCCESS')
2409 self.assertEqual(len(self.history), 5)
2410
James E. Blairec056492016-07-22 09:45:56 -07002411 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002412 def test_live_reconfiguration_failed_job(self):
2413 # Test that a change with a removed failing job does not
2414 # disrupt reconfiguration. If a change has a failed job and
2415 # that job is removed during a reconfiguration, we observed a
2416 # bug where the code to re-set build statuses would run on
2417 # that build and raise an exception because the job no longer
2418 # existed.
James E. Blair08d19992016-08-10 15:25:31 -07002419 self.launch_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002420
2421 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2422
2423 # This change will fail and later be removed by the reconfiguration.
James E. Blair08d19992016-08-10 15:25:31 -07002424 self.launch_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002425
2426 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2427 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002428 self.launch_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002429 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002430 self.launch_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002431 self.waitUntilSettled()
2432
2433 self.assertEqual(A.data['status'], 'NEW')
2434 self.assertEqual(A.reported, 0)
2435
2436 self.assertEqual(self.getJobFromHistory('project-merge').result,
2437 'SUCCESS')
2438 self.assertEqual(self.getJobFromHistory('project-test1').result,
2439 'FAILURE')
2440 self.assertEqual(len(self.history), 2)
2441
2442 # Remove the test1 job.
James E. Blairf84026c2015-12-08 16:11:46 -08002443 self.updateConfigLayout(
2444 'tests/fixtures/layout-live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002445 self.sched.reconfigure(self.config)
2446 self.waitUntilSettled()
2447
James E. Blair08d19992016-08-10 15:25:31 -07002448 self.launch_server.hold_jobs_in_build = False
2449 self.launch_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002450 self.waitUntilSettled()
2451
2452 self.assertEqual(self.getJobFromHistory('project-test2').result,
2453 'SUCCESS')
2454 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2455 'SUCCESS')
2456 self.assertEqual(len(self.history), 4)
2457
2458 self.assertEqual(A.data['status'], 'NEW')
2459 self.assertEqual(A.reported, 1)
2460 self.assertIn('Build succeeded', A.messages[0])
2461 # Ensure the removed job was not included in the report.
2462 self.assertNotIn('project-test1', A.messages[0])
2463
James E. Blairec056492016-07-22 09:45:56 -07002464 @skip("Disabled for early v3 development")
James E. Blairfe707d12015-08-05 15:18:15 -07002465 def test_live_reconfiguration_shared_queue(self):
2466 # Test that a change with a failing job which was removed from
2467 # this project but otherwise still exists in the system does
2468 # not disrupt reconfiguration.
2469
James E. Blair08d19992016-08-10 15:25:31 -07002470 self.launch_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002471
2472 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2473
James E. Blair08d19992016-08-10 15:25:31 -07002474 self.launch_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002475
2476 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2477 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002478 self.launch_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002479 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002480 self.launch_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002481 self.waitUntilSettled()
2482
2483 self.assertEqual(A.data['status'], 'NEW')
2484 self.assertEqual(A.reported, 0)
2485
2486 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2487 'SUCCESS')
2488 self.assertEqual(self.getJobFromHistory(
2489 'project1-project2-integration').result, 'FAILURE')
2490 self.assertEqual(len(self.history), 2)
2491
2492 # Remove the integration job.
James E. Blairf84026c2015-12-08 16:11:46 -08002493 self.updateConfigLayout(
2494 'tests/fixtures/layout-live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002495 self.sched.reconfigure(self.config)
2496 self.waitUntilSettled()
2497
James E. Blair08d19992016-08-10 15:25:31 -07002498 self.launch_server.hold_jobs_in_build = False
2499 self.launch_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002500 self.waitUntilSettled()
2501
2502 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2503 'SUCCESS')
2504 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2505 'SUCCESS')
2506 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2507 'SUCCESS')
2508 self.assertEqual(self.getJobFromHistory(
2509 'project1-project2-integration').result, 'FAILURE')
2510 self.assertEqual(len(self.history), 4)
2511
2512 self.assertEqual(A.data['status'], 'NEW')
2513 self.assertEqual(A.reported, 1)
2514 self.assertIn('Build succeeded', A.messages[0])
2515 # Ensure the removed job was not included in the report.
2516 self.assertNotIn('project1-project2-integration', A.messages[0])
2517
James E. Blairec056492016-07-22 09:45:56 -07002518 @skip("Disabled for early v3 development")
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002519 def test_double_live_reconfiguration_shared_queue(self):
2520 # This was a real-world regression. A change is added to
2521 # gate; a reconfigure happens, a second change which depends
2522 # on the first is added, and a second reconfiguration happens.
2523 # Ensure that both changes merge.
2524
2525 # A failure may indicate incorrect caching or cleaning up of
2526 # references during a reconfiguration.
James E. Blair08d19992016-08-10 15:25:31 -07002527 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002528
2529 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2530 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2531 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002532 A.addApproval('code-review', 2)
2533 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002534
2535 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002536 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002537 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002538 self.launch_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002539 self.waitUntilSettled()
2540
2541 # Reconfigure (with only one change in the pipeline).
2542 self.sched.reconfigure(self.config)
2543 self.waitUntilSettled()
2544
2545 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002546 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002547 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002548 self.launch_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002549 self.waitUntilSettled()
2550
2551 # Reconfigure (with both in the pipeline).
2552 self.sched.reconfigure(self.config)
2553 self.waitUntilSettled()
2554
James E. Blair08d19992016-08-10 15:25:31 -07002555 self.launch_server.hold_jobs_in_build = False
2556 self.launch_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002557 self.waitUntilSettled()
2558
2559 self.assertEqual(len(self.history), 8)
2560
2561 self.assertEqual(A.data['status'], 'MERGED')
2562 self.assertEqual(A.reported, 2)
2563 self.assertEqual(B.data['status'], 'MERGED')
2564 self.assertEqual(B.reported, 2)
2565
James E. Blairec056492016-07-22 09:45:56 -07002566 @skip("Disabled for early v3 development")
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002567 def test_live_reconfiguration_del_project(self):
2568 # Test project deletion from layout
2569 # while changes are enqueued
2570
James E. Blair08d19992016-08-10 15:25:31 -07002571 self.launch_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002572 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2573 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2574 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2575
2576 # A Depends-On: B
2577 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2578 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002579 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002580
2581 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2582 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2583 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002584 self.launch_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002585 self.waitUntilSettled()
2586 self.assertEqual(len(self.builds), 5)
2587
2588 # This layout defines only org/project, not org/project1
James E. Blairf84026c2015-12-08 16:11:46 -08002589 self.updateConfigLayout(
2590 'tests/fixtures/layout-live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002591 self.sched.reconfigure(self.config)
2592 self.waitUntilSettled()
2593
2594 # Builds for C aborted, builds for A succeed,
2595 # and have change B applied ahead
2596 job_c = self.getJobFromHistory('project1-test1')
2597 self.assertEqual(job_c.changes, '3,1')
2598 self.assertEqual(job_c.result, 'ABORTED')
2599
James E. Blair08d19992016-08-10 15:25:31 -07002600 self.launch_server.hold_jobs_in_build = False
2601 self.launch_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002602 self.waitUntilSettled()
2603
2604 self.assertEqual(self.getJobFromHistory('project-test1').changes,
2605 '2,1 1,1')
2606
2607 self.assertEqual(A.data['status'], 'NEW')
2608 self.assertEqual(B.data['status'], 'NEW')
2609 self.assertEqual(C.data['status'], 'NEW')
2610 self.assertEqual(A.reported, 1)
2611 self.assertEqual(B.reported, 0)
2612 self.assertEqual(C.reported, 0)
2613
2614 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
2615 self.assertIn('Build succeeded', A.messages[0])
2616
James E. Blairec056492016-07-22 09:45:56 -07002617 @skip("Disabled for early v3 development")
James E. Blaire712d9f2013-07-31 11:40:11 -07002618 def test_live_reconfiguration_functions(self):
2619 "Test live reconfiguration with a custom function"
2620 self.worker.registerFunction('build:node-project-test1:debian')
2621 self.worker.registerFunction('build:node-project-test1:wheezy')
2622 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002623 A.addApproval('code-review', 2)
2624 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002625 self.waitUntilSettled()
2626
2627 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2628 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2629 'debian')
2630 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2631
James E. Blairf84026c2015-12-08 16:11:46 -08002632 self.updateConfigLayout(
2633 'tests/fixtures/layout-live-reconfiguration-functions.yaml')
James E. Blaire712d9f2013-07-31 11:40:11 -07002634 self.sched.reconfigure(self.config)
2635 self.worker.build_history = []
2636
2637 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002638 B.addApproval('code-review', 2)
2639 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002640 self.waitUntilSettled()
2641
2642 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2643 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2644 'wheezy')
2645 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2646
James E. Blairec056492016-07-22 09:45:56 -07002647 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002648 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002649 self.updateConfigLayout(
2650 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002651 self.sched.reconfigure(self.config)
2652
2653 self.init_repo("org/new-project")
2654 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2655
James E. Blair8b5408c2016-08-08 15:37:46 -07002656 A.addApproval('code-review', 2)
2657 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002658 self.waitUntilSettled()
2659 self.assertEqual(self.getJobFromHistory('project-merge').result,
2660 'SUCCESS')
2661 self.assertEqual(self.getJobFromHistory('project-test1').result,
2662 'SUCCESS')
2663 self.assertEqual(self.getJobFromHistory('project-test2').result,
2664 'SUCCESS')
2665 self.assertEqual(A.data['status'], 'MERGED')
2666 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002667
Clark Boylan6dbbc482013-10-18 10:57:31 -07002668 def test_repo_deleted(self):
Paul Belanger66e95962016-11-11 12:11:06 -05002669 self.updateConfigLayout('layout-repo-deleted')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002670 self.sched.reconfigure(self.config)
2671
2672 self.init_repo("org/delete-project")
2673 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2674
James E. Blair8b5408c2016-08-08 15:37:46 -07002675 A.addApproval('code-review', 2)
2676 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002677 self.waitUntilSettled()
2678 self.assertEqual(self.getJobFromHistory('project-merge').result,
2679 'SUCCESS')
2680 self.assertEqual(self.getJobFromHistory('project-test1').result,
2681 'SUCCESS')
2682 self.assertEqual(self.getJobFromHistory('project-test2').result,
2683 'SUCCESS')
2684 self.assertEqual(A.data['status'], 'MERGED')
2685 self.assertEqual(A.reported, 2)
2686
2687 # Delete org/new-project zuul repo. Should be recloned.
2688 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
2689
2690 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2691
James E. Blair8b5408c2016-08-08 15:37:46 -07002692 B.addApproval('code-review', 2)
2693 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002694 self.waitUntilSettled()
2695 self.assertEqual(self.getJobFromHistory('project-merge').result,
2696 'SUCCESS')
2697 self.assertEqual(self.getJobFromHistory('project-test1').result,
2698 'SUCCESS')
2699 self.assertEqual(self.getJobFromHistory('project-test2').result,
2700 'SUCCESS')
2701 self.assertEqual(B.data['status'], 'MERGED')
2702 self.assertEqual(B.reported, 2)
2703
James E. Blairec056492016-07-22 09:45:56 -07002704 @skip("Disabled for early v3 development")
James E. Blair456f2fb2016-02-09 09:29:33 -08002705 def test_tags(self):
2706 "Test job tags"
2707 self.config.set('zuul', 'layout_config',
2708 'tests/fixtures/layout-tags.yaml')
2709 self.sched.reconfigure(self.config)
2710
2711 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2712 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2713 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2714 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2715 self.waitUntilSettled()
2716
2717 results = {'project1-merge': 'extratag merge project1',
2718 'project2-merge': 'merge'}
2719
2720 for build in self.history:
2721 self.assertEqual(results.get(build.name, ''),
2722 build.parameters.get('BUILD_TAGS'))
2723
James E. Blairec056492016-07-22 09:45:56 -07002724 @skip("Disabled for early v3 development")
James E. Blair63bb0ef2013-07-29 17:14:51 -07002725 def test_timer(self):
2726 "Test that a periodic job is triggered"
James E. Blair08d19992016-08-10 15:25:31 -07002727 self.launch_server.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08002728 self.updateConfigLayout(
2729 'tests/fixtures/layout-timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002730 self.sched.reconfigure(self.config)
2731 self.registerJobs()
2732
Clark Boylan3ee090a2014-04-03 20:55:09 -07002733 # The pipeline triggers every second, so we should have seen
2734 # several by now.
2735 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002736 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002737
2738 self.assertEqual(len(self.builds), 2)
2739
James E. Blair63bb0ef2013-07-29 17:14:51 -07002740 port = self.webapp.server.socket.getsockname()[1]
2741
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002742 req = urllib.request.Request("http://localhost:%s/status.json" % port)
2743 f = urllib.request.urlopen(req)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002744 data = f.read()
2745
James E. Blair08d19992016-08-10 15:25:31 -07002746 self.launch_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002747 # Stop queuing timer triggered jobs so that the assertions
2748 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002749 self.updateConfigLayout(
2750 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002751 self.sched.reconfigure(self.config)
2752 self.registerJobs()
James E. Blair08d19992016-08-10 15:25:31 -07002753 self.launch_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002754 self.waitUntilSettled()
2755
2756 self.assertEqual(self.getJobFromHistory(
2757 'project-bitrot-stable-old').result, 'SUCCESS')
2758 self.assertEqual(self.getJobFromHistory(
2759 'project-bitrot-stable-older').result, 'SUCCESS')
2760
2761 data = json.loads(data)
2762 status_jobs = set()
2763 for p in data['pipelines']:
2764 for q in p['change_queues']:
2765 for head in q['heads']:
2766 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07002767 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002768 for job in change['jobs']:
2769 status_jobs.add(job['name'])
2770 self.assertIn('project-bitrot-stable-old', status_jobs)
2771 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002772
James E. Blairec056492016-07-22 09:45:56 -07002773 @skip("Disabled for early v3 development")
James E. Blair4f6033c2014-03-27 15:49:09 -07002774 def test_idle(self):
2775 "Test that frequent periodic jobs work"
James E. Blair08d19992016-08-10 15:25:31 -07002776 self.launch_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002777
Clark Boylan3ee090a2014-04-03 20:55:09 -07002778 for x in range(1, 3):
2779 # Test that timer triggers periodic jobs even across
2780 # layout config reloads.
2781 # Start timer trigger
James E. Blairf84026c2015-12-08 16:11:46 -08002782 self.updateConfigLayout(
2783 'tests/fixtures/layout-idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002784 self.sched.reconfigure(self.config)
2785 self.registerJobs()
James E. Blair995fc0f2016-02-04 16:48:31 -08002786 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002787
Clark Boylan3ee090a2014-04-03 20:55:09 -07002788 # The pipeline triggers every second, so we should have seen
2789 # several by now.
2790 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002791
2792 # Stop queuing timer triggered jobs so that the assertions
2793 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002794 self.updateConfigLayout(
2795 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002796 self.sched.reconfigure(self.config)
2797 self.registerJobs()
James E. Blair995fc0f2016-02-04 16:48:31 -08002798 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002799
2800 self.assertEqual(len(self.builds), 2)
James E. Blair08d19992016-08-10 15:25:31 -07002801 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002802 self.waitUntilSettled()
2803 self.assertEqual(len(self.builds), 0)
2804 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002805
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002806 def test_check_smtp_pool(self):
Paul Belanger01be8ca2016-11-11 12:21:06 -05002807 self.updateConfigLayout('layout-smtp')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002808 self.sched.reconfigure(self.config)
2809
2810 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2811 self.waitUntilSettled()
2812
2813 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2814 self.waitUntilSettled()
2815
James E. Blairff80a2f2013-12-27 13:24:06 -08002816 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002817
2818 # A.messages only holds what FakeGerrit places in it. Thus we
2819 # work on the knowledge of what the first message should be as
2820 # it is only configured to go to SMTP.
2821
2822 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002823 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002824 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002825 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002826 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002827 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002828
2829 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002830 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002831 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002832 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002833 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002834 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002835
James E. Blairec056492016-07-22 09:45:56 -07002836 @skip("Disabled for early v3 development")
James E. Blaire5910202013-12-27 09:50:31 -08002837 def test_timer_smtp(self):
2838 "Test that a periodic job is triggered"
James E. Blair08d19992016-08-10 15:25:31 -07002839 self.launch_server.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08002840 self.updateConfigLayout(
2841 'tests/fixtures/layout-timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002842 self.sched.reconfigure(self.config)
2843 self.registerJobs()
2844
Clark Boylan3ee090a2014-04-03 20:55:09 -07002845 # The pipeline triggers every second, so we should have seen
2846 # several by now.
2847 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002848 self.waitUntilSettled()
2849
Clark Boylan3ee090a2014-04-03 20:55:09 -07002850 self.assertEqual(len(self.builds), 2)
James E. Blair08d19992016-08-10 15:25:31 -07002851 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002852 self.waitUntilSettled()
2853 self.assertEqual(len(self.history), 2)
2854
James E. Blaire5910202013-12-27 09:50:31 -08002855 self.assertEqual(self.getJobFromHistory(
2856 'project-bitrot-stable-old').result, 'SUCCESS')
2857 self.assertEqual(self.getJobFromHistory(
2858 'project-bitrot-stable-older').result, 'SUCCESS')
2859
James E. Blairff80a2f2013-12-27 13:24:06 -08002860 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002861
2862 # A.messages only holds what FakeGerrit places in it. Thus we
2863 # work on the knowledge of what the first message should be as
2864 # it is only configured to go to SMTP.
2865
2866 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002867 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002868 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002869 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002870 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002871 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002872
Clark Boylan3ee090a2014-04-03 20:55:09 -07002873 # Stop queuing timer triggered jobs and let any that may have
2874 # queued through so that end of test assertions pass.
James E. Blairf84026c2015-12-08 16:11:46 -08002875 self.updateConfigLayout(
2876 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002877 self.sched.reconfigure(self.config)
2878 self.registerJobs()
James E. Blairf8058972014-08-15 16:09:16 -07002879 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002880 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002881 self.waitUntilSettled()
2882
James E. Blair91e34592015-07-31 16:45:59 -07002883 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08002884 "Test that the RPC client can enqueue a change"
2885 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002886 A.addApproval('code-review', 2)
2887 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08002888
2889 client = zuul.rpcclient.RPCClient('127.0.0.1',
2890 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04002891 r = client.enqueue(tenant='tenant-one',
2892 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002893 project='org/project',
2894 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002895 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002896 self.waitUntilSettled()
2897 self.assertEqual(self.getJobFromHistory('project-merge').result,
2898 'SUCCESS')
2899 self.assertEqual(self.getJobFromHistory('project-test1').result,
2900 'SUCCESS')
2901 self.assertEqual(self.getJobFromHistory('project-test2').result,
2902 'SUCCESS')
2903 self.assertEqual(A.data['status'], 'MERGED')
2904 self.assertEqual(A.reported, 2)
2905 self.assertEqual(r, True)
2906
James E. Blairec056492016-07-22 09:45:56 -07002907 @skip("Disabled for early v3 development")
James E. Blair91e34592015-07-31 16:45:59 -07002908 def test_client_enqueue_ref(self):
2909 "Test that the RPC client can enqueue a ref"
2910
2911 client = zuul.rpcclient.RPCClient('127.0.0.1',
2912 self.gearman_server.port)
2913 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04002914 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07002915 pipeline='post',
2916 project='org/project',
2917 trigger='gerrit',
2918 ref='master',
2919 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
2920 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
2921 self.waitUntilSettled()
2922 job_names = [x.name for x in self.history]
2923 self.assertEqual(len(self.history), 1)
2924 self.assertIn('project-post', job_names)
2925 self.assertEqual(r, True)
2926
James E. Blairad28e912013-11-27 10:43:22 -08002927 def test_client_enqueue_negative(self):
2928 "Test that the RPC client returns errors"
2929 client = zuul.rpcclient.RPCClient('127.0.0.1',
2930 self.gearman_server.port)
2931 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04002932 "Invalid tenant"):
2933 r = client.enqueue(tenant='tenant-foo',
2934 pipeline='gate',
2935 project='org/project',
2936 trigger='gerrit',
2937 change='1,1')
2938 client.shutdown()
2939 self.assertEqual(r, False)
2940
2941 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08002942 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002943 r = client.enqueue(tenant='tenant-one',
2944 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002945 project='project-does-not-exist',
2946 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002947 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002948 client.shutdown()
2949 self.assertEqual(r, False)
2950
2951 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2952 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002953 r = client.enqueue(tenant='tenant-one',
2954 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08002955 project='org/project',
2956 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002957 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002958 client.shutdown()
2959 self.assertEqual(r, False)
2960
2961 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2962 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002963 r = client.enqueue(tenant='tenant-one',
2964 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002965 project='org/project',
2966 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08002967 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002968 client.shutdown()
2969 self.assertEqual(r, False)
2970
2971 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2972 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002973 r = client.enqueue(tenant='tenant-one',
2974 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002975 project='org/project',
2976 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002977 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002978 client.shutdown()
2979 self.assertEqual(r, False)
2980
2981 self.waitUntilSettled()
2982 self.assertEqual(len(self.history), 0)
2983 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08002984
2985 def test_client_promote(self):
2986 "Test that the RPC client can promote a change"
James E. Blair08d19992016-08-10 15:25:31 -07002987 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08002988 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2989 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2990 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07002991 A.addApproval('code-review', 2)
2992 B.addApproval('code-review', 2)
2993 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08002994
James E. Blair8b5408c2016-08-08 15:37:46 -07002995 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2996 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
2997 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08002998
2999 self.waitUntilSettled()
3000
Paul Belangerbaca3132016-11-04 12:49:54 -04003001 tenant = self.sched.abide.tenants.get('tenant-one')
3002 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003003 enqueue_times = {}
3004 for item in items:
3005 enqueue_times[str(item.change)] = item.enqueue_time
3006
James E. Blair36658cf2013-12-06 17:53:48 -08003007 client = zuul.rpcclient.RPCClient('127.0.0.1',
3008 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003009 r = client.promote(tenant='tenant-one',
3010 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003011 change_ids=['2,1', '3,1'])
3012
Sean Daguef39b9ca2014-01-10 21:34:35 -05003013 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003014 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003015 for item in items:
3016 self.assertEqual(
3017 enqueue_times[str(item.change)], item.enqueue_time)
3018
James E. Blair78acec92014-02-06 07:11:32 -08003019 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003020 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003021 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003022 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003023 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003024 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003025 self.waitUntilSettled()
3026
3027 self.assertEqual(len(self.builds), 6)
3028 self.assertEqual(self.builds[0].name, 'project-test1')
3029 self.assertEqual(self.builds[1].name, 'project-test2')
3030 self.assertEqual(self.builds[2].name, 'project-test1')
3031 self.assertEqual(self.builds[3].name, 'project-test2')
3032 self.assertEqual(self.builds[4].name, 'project-test1')
3033 self.assertEqual(self.builds[5].name, 'project-test2')
3034
Paul Belangerbaca3132016-11-04 12:49:54 -04003035 self.assertTrue(self.builds[0].hasChanges(B))
3036 self.assertFalse(self.builds[0].hasChanges(A))
3037 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003038
Paul Belangerbaca3132016-11-04 12:49:54 -04003039 self.assertTrue(self.builds[2].hasChanges(B))
3040 self.assertTrue(self.builds[2].hasChanges(C))
3041 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003042
Paul Belangerbaca3132016-11-04 12:49:54 -04003043 self.assertTrue(self.builds[4].hasChanges(B))
3044 self.assertTrue(self.builds[4].hasChanges(C))
3045 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003046
James E. Blair08d19992016-08-10 15:25:31 -07003047 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003048 self.waitUntilSettled()
3049
3050 self.assertEqual(A.data['status'], 'MERGED')
3051 self.assertEqual(A.reported, 2)
3052 self.assertEqual(B.data['status'], 'MERGED')
3053 self.assertEqual(B.reported, 2)
3054 self.assertEqual(C.data['status'], 'MERGED')
3055 self.assertEqual(C.reported, 2)
3056
3057 client.shutdown()
3058 self.assertEqual(r, True)
3059
3060 def test_client_promote_dependent(self):
3061 "Test that the RPC client can promote a dependent change"
3062 # C (depends on B) -> B -> A ; then promote C to get:
3063 # A -> C (depends on B) -> B
James E. Blair08d19992016-08-10 15:25:31 -07003064 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003065 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3066 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3067 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3068
3069 C.setDependsOn(B, 1)
3070
James E. Blair8b5408c2016-08-08 15:37:46 -07003071 A.addApproval('code-review', 2)
3072 B.addApproval('code-review', 2)
3073 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003074
James E. Blair8b5408c2016-08-08 15:37:46 -07003075 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3076 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3077 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003078
3079 self.waitUntilSettled()
3080
3081 client = zuul.rpcclient.RPCClient('127.0.0.1',
3082 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003083 r = client.promote(tenant='tenant-one',
3084 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003085 change_ids=['3,1'])
3086
James E. Blair78acec92014-02-06 07:11:32 -08003087 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003088 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003089 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003090 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003091 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003092 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003093 self.waitUntilSettled()
3094
3095 self.assertEqual(len(self.builds), 6)
3096 self.assertEqual(self.builds[0].name, 'project-test1')
3097 self.assertEqual(self.builds[1].name, 'project-test2')
3098 self.assertEqual(self.builds[2].name, 'project-test1')
3099 self.assertEqual(self.builds[3].name, 'project-test2')
3100 self.assertEqual(self.builds[4].name, 'project-test1')
3101 self.assertEqual(self.builds[5].name, 'project-test2')
3102
Paul Belangerbaca3132016-11-04 12:49:54 -04003103 self.assertTrue(self.builds[0].hasChanges(B))
3104 self.assertFalse(self.builds[0].hasChanges(A))
3105 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003106
Paul Belangerbaca3132016-11-04 12:49:54 -04003107 self.assertTrue(self.builds[2].hasChanges(B))
3108 self.assertTrue(self.builds[2].hasChanges(C))
3109 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003110
Paul Belangerbaca3132016-11-04 12:49:54 -04003111 self.assertTrue(self.builds[4].hasChanges(B))
3112 self.assertTrue(self.builds[4].hasChanges(C))
3113 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003114
James E. Blair08d19992016-08-10 15:25:31 -07003115 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003116 self.waitUntilSettled()
3117
3118 self.assertEqual(A.data['status'], 'MERGED')
3119 self.assertEqual(A.reported, 2)
3120 self.assertEqual(B.data['status'], 'MERGED')
3121 self.assertEqual(B.reported, 2)
3122 self.assertEqual(C.data['status'], 'MERGED')
3123 self.assertEqual(C.reported, 2)
3124
3125 client.shutdown()
3126 self.assertEqual(r, True)
3127
3128 def test_client_promote_negative(self):
3129 "Test that the RPC client returns errors for promotion"
James E. Blair08d19992016-08-10 15:25:31 -07003130 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003131 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003132 A.addApproval('code-review', 2)
3133 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003134 self.waitUntilSettled()
3135
3136 client = zuul.rpcclient.RPCClient('127.0.0.1',
3137 self.gearman_server.port)
3138
3139 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003140 r = client.promote(tenant='tenant-one',
3141 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003142 change_ids=['2,1', '3,1'])
3143 client.shutdown()
3144 self.assertEqual(r, False)
3145
3146 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003147 r = client.promote(tenant='tenant-one',
3148 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003149 change_ids=['4,1'])
3150 client.shutdown()
3151 self.assertEqual(r, False)
3152
James E. Blair08d19992016-08-10 15:25:31 -07003153 self.launch_server.hold_jobs_in_build = False
3154 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003155 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003156
James E. Blairec056492016-07-22 09:45:56 -07003157 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003158 def test_queue_rate_limiting(self):
3159 "Test that DependentPipelines are rate limited with dep across window"
James E. Blairf84026c2015-12-08 16:11:46 -08003160 self.updateConfigLayout(
3161 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003162 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07003163 self.launch_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003164 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3165 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3166 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3167
3168 C.setDependsOn(B, 1)
James E. Blair08d19992016-08-10 15:25:31 -07003169 self.launch_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003170
James E. Blair8b5408c2016-08-08 15:37:46 -07003171 A.addApproval('code-review', 2)
3172 B.addApproval('code-review', 2)
3173 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003174
James E. Blair8b5408c2016-08-08 15:37:46 -07003175 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3176 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3177 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003178 self.waitUntilSettled()
3179
3180 # Only A and B will have their merge jobs queued because
3181 # window is 2.
3182 self.assertEqual(len(self.builds), 2)
3183 self.assertEqual(self.builds[0].name, 'project-merge')
3184 self.assertEqual(self.builds[1].name, 'project-merge')
3185
James E. Blair08d19992016-08-10 15:25:31 -07003186 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003187 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003188 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003189 self.waitUntilSettled()
3190
3191 # Only A and B will have their test jobs queued because
3192 # window is 2.
3193 self.assertEqual(len(self.builds), 4)
3194 self.assertEqual(self.builds[0].name, 'project-test1')
3195 self.assertEqual(self.builds[1].name, 'project-test2')
3196 self.assertEqual(self.builds[2].name, 'project-test1')
3197 self.assertEqual(self.builds[3].name, 'project-test2')
3198
James E. Blair08d19992016-08-10 15:25:31 -07003199 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003200 self.waitUntilSettled()
3201
3202 queue = self.sched.layout.pipelines['gate'].queues[0]
3203 # A failed so window is reduced by 1 to 1.
3204 self.assertEqual(queue.window, 1)
3205 self.assertEqual(queue.window_floor, 1)
3206 self.assertEqual(A.data['status'], 'NEW')
3207
3208 # Gate is reset and only B's merge job is queued because
3209 # window shrunk to 1.
3210 self.assertEqual(len(self.builds), 1)
3211 self.assertEqual(self.builds[0].name, 'project-merge')
3212
James E. Blair08d19992016-08-10 15:25:31 -07003213 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003214 self.waitUntilSettled()
3215
3216 # Only B's test jobs are queued because window is still 1.
3217 self.assertEqual(len(self.builds), 2)
3218 self.assertEqual(self.builds[0].name, 'project-test1')
3219 self.assertEqual(self.builds[1].name, 'project-test2')
3220
James E. Blair08d19992016-08-10 15:25:31 -07003221 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003222 self.waitUntilSettled()
3223
3224 # B was successfully merged so window is increased to 2.
3225 self.assertEqual(queue.window, 2)
3226 self.assertEqual(queue.window_floor, 1)
3227 self.assertEqual(B.data['status'], 'MERGED')
3228
3229 # Only C is left and its merge job is queued.
3230 self.assertEqual(len(self.builds), 1)
3231 self.assertEqual(self.builds[0].name, 'project-merge')
3232
James E. Blair08d19992016-08-10 15:25:31 -07003233 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003234 self.waitUntilSettled()
3235
3236 # After successful merge job the test jobs for C are queued.
3237 self.assertEqual(len(self.builds), 2)
3238 self.assertEqual(self.builds[0].name, 'project-test1')
3239 self.assertEqual(self.builds[1].name, 'project-test2')
3240
James E. Blair08d19992016-08-10 15:25:31 -07003241 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003242 self.waitUntilSettled()
3243
3244 # C successfully merged so window is bumped to 3.
3245 self.assertEqual(queue.window, 3)
3246 self.assertEqual(queue.window_floor, 1)
3247 self.assertEqual(C.data['status'], 'MERGED')
3248
James E. Blairec056492016-07-22 09:45:56 -07003249 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003250 def test_queue_rate_limiting_dependent(self):
3251 "Test that DependentPipelines are rate limited with dep in window"
James E. Blairf84026c2015-12-08 16:11:46 -08003252 self.updateConfigLayout(
3253 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003254 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07003255 self.launch_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003256 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3257 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3258 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3259
3260 B.setDependsOn(A, 1)
3261
James E. Blair08d19992016-08-10 15:25:31 -07003262 self.launch_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003263
James E. Blair8b5408c2016-08-08 15:37:46 -07003264 A.addApproval('code-review', 2)
3265 B.addApproval('code-review', 2)
3266 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003267
James E. Blair8b5408c2016-08-08 15:37:46 -07003268 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3269 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3270 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003271 self.waitUntilSettled()
3272
3273 # Only A and B will have their merge jobs queued because
3274 # window is 2.
3275 self.assertEqual(len(self.builds), 2)
3276 self.assertEqual(self.builds[0].name, 'project-merge')
3277 self.assertEqual(self.builds[1].name, 'project-merge')
3278
James E. Blair08d19992016-08-10 15:25:31 -07003279 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003280 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003281 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003282 self.waitUntilSettled()
3283
3284 # Only A and B will have their test jobs queued because
3285 # window is 2.
3286 self.assertEqual(len(self.builds), 4)
3287 self.assertEqual(self.builds[0].name, 'project-test1')
3288 self.assertEqual(self.builds[1].name, 'project-test2')
3289 self.assertEqual(self.builds[2].name, 'project-test1')
3290 self.assertEqual(self.builds[3].name, 'project-test2')
3291
James E. Blair08d19992016-08-10 15:25:31 -07003292 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003293 self.waitUntilSettled()
3294
3295 queue = self.sched.layout.pipelines['gate'].queues[0]
3296 # A failed so window is reduced by 1 to 1.
3297 self.assertEqual(queue.window, 1)
3298 self.assertEqual(queue.window_floor, 1)
3299 self.assertEqual(A.data['status'], 'NEW')
3300 self.assertEqual(B.data['status'], 'NEW')
3301
3302 # Gate is reset and only C's merge job is queued because
3303 # window shrunk to 1 and A and B were dequeued.
3304 self.assertEqual(len(self.builds), 1)
3305 self.assertEqual(self.builds[0].name, 'project-merge')
3306
James E. Blair08d19992016-08-10 15:25:31 -07003307 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003308 self.waitUntilSettled()
3309
3310 # Only C's test jobs are queued because window is still 1.
3311 self.assertEqual(len(self.builds), 2)
3312 self.assertEqual(self.builds[0].name, 'project-test1')
3313 self.assertEqual(self.builds[1].name, 'project-test2')
3314
James E. Blair08d19992016-08-10 15:25:31 -07003315 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003316 self.waitUntilSettled()
3317
3318 # C was successfully merged so window is increased to 2.
3319 self.assertEqual(queue.window, 2)
3320 self.assertEqual(queue.window_floor, 1)
3321 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003322
James E. Blairec056492016-07-22 09:45:56 -07003323 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003324 def test_worker_update_metadata(self):
3325 "Test if a worker can send back metadata about itself"
James E. Blair08d19992016-08-10 15:25:31 -07003326 self.launch_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003327
3328 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003329 A.addApproval('code-review', 2)
3330 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003331 self.waitUntilSettled()
3332
3333 self.assertEqual(len(self.launcher.builds), 1)
3334
3335 self.log.debug('Current builds:')
3336 self.log.debug(self.launcher.builds)
3337
3338 start = time.time()
3339 while True:
3340 if time.time() - start > 10:
3341 raise Exception("Timeout waiting for gearman server to report "
3342 + "back to the client")
3343 build = self.launcher.builds.values()[0]
3344 if build.worker.name == "My Worker":
3345 break
3346 else:
3347 time.sleep(0)
3348
3349 self.log.debug(build)
3350 self.assertEqual("My Worker", build.worker.name)
3351 self.assertEqual("localhost", build.worker.hostname)
3352 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3353 self.assertEqual("zuul.example.org", build.worker.fqdn)
3354 self.assertEqual("FakeBuilder", build.worker.program)
3355 self.assertEqual("v1.1", build.worker.version)
3356 self.assertEqual({'something': 'else'}, build.worker.extra)
3357
James E. Blair08d19992016-08-10 15:25:31 -07003358 self.launch_server.hold_jobs_in_build = False
3359 self.launch_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003360 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003361
James E. Blairec056492016-07-22 09:45:56 -07003362 @skip("Disabled for early v3 development")
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003363 def test_footer_message(self):
3364 "Test a pipeline's footer message is correctly added to the report."
James E. Blairf84026c2015-12-08 16:11:46 -08003365 self.updateConfigLayout(
3366 'tests/fixtures/layout-footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003367 self.sched.reconfigure(self.config)
3368 self.registerJobs()
3369
3370 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003371 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07003372 self.launch_server.failJob('test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003373 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003374 self.waitUntilSettled()
3375
3376 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003377 B.addApproval('code-review', 2)
3378 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003379 self.waitUntilSettled()
3380
3381 self.assertEqual(2, len(self.smtp_messages))
3382
3383 failure_body = """\
3384Build failed. For information on how to proceed, see \
3385http://wiki.example.org/Test_Failures
3386
3387- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
3388- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
3389
3390For CI problems and help debugging, contact ci@example.org"""
3391
3392 success_body = """\
3393Build succeeded.
3394
3395- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
3396- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
3397
3398For CI problems and help debugging, contact ci@example.org"""
3399
3400 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
3401 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11003402
James E. Blairec056492016-07-22 09:45:56 -07003403 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003404 def test_merge_failure_reporters(self):
3405 """Check that the config is set up correctly"""
3406
James E. Blairf84026c2015-12-08 16:11:46 -08003407 self.updateConfigLayout(
3408 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003409 self.sched.reconfigure(self.config)
3410 self.registerJobs()
3411
3412 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003413 "Merge Failed.\n\nThis change or one of its cross-repo "
3414 "dependencies was unable to be automatically merged with the "
3415 "current state of its repository. Please rebase the change and "
3416 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003417 self.sched.layout.pipelines['check'].merge_failure_message)
3418 self.assertEqual(
3419 "The merge failed! For more information...",
3420 self.sched.layout.pipelines['gate'].merge_failure_message)
3421
3422 self.assertEqual(
3423 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3424 self.assertEqual(
3425 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3426
3427 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003428 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3429 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003430
3431 self.assertTrue(
3432 (
3433 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003434 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003435 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003436 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003437 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003438 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003439 ) or (
3440 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003441 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003442 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003443 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003444 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003445 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003446 )
3447 )
3448
James E. Blairec056492016-07-22 09:45:56 -07003449 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003450 def test_merge_failure_reports(self):
3451 """Check that when a change fails to merge the correct message is sent
3452 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003453 self.updateConfigLayout(
3454 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003455 self.sched.reconfigure(self.config)
3456 self.registerJobs()
3457
3458 # Check a test failure isn't reported to SMTP
3459 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003460 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07003461 self.launch_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003462 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003463 self.waitUntilSettled()
3464
3465 self.assertEqual(3, len(self.history)) # 3 jobs
3466 self.assertEqual(0, len(self.smtp_messages))
3467
3468 # Check a merge failure is reported to SMTP
3469 # B should be merged, but C will conflict with B
3470 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3471 B.addPatchset(['conflict'])
3472 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3473 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003474 B.addApproval('code-review', 2)
3475 C.addApproval('code-review', 2)
3476 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3477 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003478 self.waitUntilSettled()
3479
3480 self.assertEqual(6, len(self.history)) # A and B jobs
3481 self.assertEqual(1, len(self.smtp_messages))
3482 self.assertEqual('The merge failed! For more information...',
3483 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003484
James E. Blairec056492016-07-22 09:45:56 -07003485 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003486 def test_default_merge_failure_reports(self):
3487 """Check that the default merge failure reports are correct."""
3488
3489 # A should report success, B should report merge failure.
3490 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3491 A.addPatchset(['conflict'])
3492 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3493 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003494 A.addApproval('code-review', 2)
3495 B.addApproval('code-review', 2)
3496 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3497 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003498 self.waitUntilSettled()
3499
3500 self.assertEqual(3, len(self.history)) # A jobs
3501 self.assertEqual(A.reported, 2)
3502 self.assertEqual(B.reported, 2)
3503 self.assertEqual(A.data['status'], 'MERGED')
3504 self.assertEqual(B.data['status'], 'NEW')
3505 self.assertIn('Build succeeded', A.messages[1])
3506 self.assertIn('Merge Failed', B.messages[1])
3507 self.assertIn('automatically merged', B.messages[1])
3508 self.assertNotIn('logs.example.com', B.messages[1])
3509 self.assertNotIn('SKIPPED', B.messages[1])
3510
James E. Blairec056492016-07-22 09:45:56 -07003511 @skip("Disabled for early v3 development")
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003512 def test_swift_instructions(self):
3513 "Test that the correct swift instructions are sent to the workers"
James E. Blairf84026c2015-12-08 16:11:46 -08003514 self.updateConfigLayout(
3515 'tests/fixtures/layout-swift.yaml')
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003516 self.sched.reconfigure(self.config)
3517 self.registerJobs()
3518
James E. Blair08d19992016-08-10 15:25:31 -07003519 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003520 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3521
James E. Blair8b5408c2016-08-08 15:37:46 -07003522 A.addApproval('code-review', 2)
3523 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003524 self.waitUntilSettled()
3525
3526 self.assertEqual(
3527 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
3528 "gate/test-merge/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003529 self.builds[0].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003530 self.assertEqual(5,
3531 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
3532 split('\n')))
3533 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
3534
3535 self.assertEqual(
3536 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
3537 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003538 self.builds[1].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003539 self.assertEqual(5,
3540 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
3541 split('\n')))
3542 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
3543
3544 self.assertEqual(
3545 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
3546 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003547 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003548 self.assertEqual(5,
3549 len(self.builds[1].
3550 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
3551 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
3552
James E. Blair08d19992016-08-10 15:25:31 -07003553 self.launch_server.hold_jobs_in_build = False
3554 self.launch_server.release()
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003555 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003556
3557 def test_client_get_running_jobs(self):
3558 "Test that the RPC client can get a list of running jobs"
James E. Blair08d19992016-08-10 15:25:31 -07003559 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003560 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003561 A.addApproval('code-review', 2)
3562 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003563 self.waitUntilSettled()
3564
3565 client = zuul.rpcclient.RPCClient('127.0.0.1',
3566 self.gearman_server.port)
3567
3568 # Wait for gearman server to send the initial workData back to zuul
3569 start = time.time()
3570 while True:
3571 if time.time() - start > 10:
3572 raise Exception("Timeout waiting for gearman server to report "
3573 + "back to the client")
Clint Byrum78a69772016-12-05 10:52:07 -08003574 build = self.launch_client.builds.values()[0]
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003575 if build.worker.name == "My Worker":
3576 break
3577 else:
3578 time.sleep(0)
3579
3580 running_items = client.get_running_jobs()
3581
3582 self.assertEqual(1, len(running_items))
3583 running_item = running_items[0]
3584 self.assertEqual([], running_item['failing_reasons'])
3585 self.assertEqual([], running_item['items_behind'])
3586 self.assertEqual('https://hostname/1', running_item['url'])
3587 self.assertEqual(None, running_item['item_ahead'])
3588 self.assertEqual('org/project', running_item['project'])
3589 self.assertEqual(None, running_item['remaining_time'])
3590 self.assertEqual(True, running_item['active'])
3591 self.assertEqual('1,1', running_item['id'])
3592
3593 self.assertEqual(3, len(running_item['jobs']))
3594 for job in running_item['jobs']:
3595 if job['name'] == 'project-merge':
3596 self.assertEqual('project-merge', job['name'])
3597 self.assertEqual('gate', job['pipeline'])
3598 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003599 self.assertEqual('https://server/job/project-merge/0/',
3600 job['url'])
3601 self.assertEqual(7, len(job['worker']))
3602 self.assertEqual(False, job['canceled'])
3603 self.assertEqual(True, job['voting'])
3604 self.assertEqual(None, job['result'])
3605 self.assertEqual('gate', job['pipeline'])
3606 break
3607
James E. Blair08d19992016-08-10 15:25:31 -07003608 self.launch_server.hold_jobs_in_build = False
3609 self.launch_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003610 self.waitUntilSettled()
3611
3612 running_items = client.get_running_jobs()
3613 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003614
3615 def test_nonvoting_pipeline(self):
3616 "Test that a nonvoting pipeline (experimental) can still report"
3617
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003618 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3619 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003620 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3621 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003622
3623 self.assertEqual(self.getJobFromHistory('project-merge').result,
3624 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003625 self.assertEqual(
3626 self.getJobFromHistory('experimental-project-test').result,
3627 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003628 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003629
3630 def test_crd_gate(self):
3631 "Test cross-repo dependencies"
3632 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3633 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003634 A.addApproval('code-review', 2)
3635 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003636
3637 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3638 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3639 AM2.setMerged()
3640 AM1.setMerged()
3641
3642 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3643 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3644 BM2.setMerged()
3645 BM1.setMerged()
3646
3647 # A -> AM1 -> AM2
3648 # B -> BM1 -> BM2
3649 # A Depends-On: B
3650 # M2 is here to make sure it is never queried. If it is, it
3651 # means zuul is walking down the entire history of merged
3652 # changes.
3653
3654 B.setDependsOn(BM1, 1)
3655 BM1.setDependsOn(BM2, 1)
3656
3657 A.setDependsOn(AM1, 1)
3658 AM1.setDependsOn(AM2, 1)
3659
3660 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3661 A.subject, B.data['id'])
3662
James E. Blair8b5408c2016-08-08 15:37:46 -07003663 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003664 self.waitUntilSettled()
3665
3666 self.assertEqual(A.data['status'], 'NEW')
3667 self.assertEqual(B.data['status'], 'NEW')
3668
Paul Belanger5dccbe72016-11-14 11:17:37 -05003669 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003670 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003671
James E. Blair08d19992016-08-10 15:25:31 -07003672 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003673 B.addApproval('approved', 1)
3674 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003675 self.waitUntilSettled()
3676
James E. Blair08d19992016-08-10 15:25:31 -07003677 self.launch_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003678 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003679 self.launch_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003680 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003681 self.launch_server.hold_jobs_in_build = False
3682 self.launch_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003683 self.waitUntilSettled()
3684
3685 self.assertEqual(AM2.queried, 0)
3686 self.assertEqual(BM2.queried, 0)
3687 self.assertEqual(A.data['status'], 'MERGED')
3688 self.assertEqual(B.data['status'], 'MERGED')
3689 self.assertEqual(A.reported, 2)
3690 self.assertEqual(B.reported, 2)
3691
Paul Belanger5dccbe72016-11-14 11:17:37 -05003692 changes = self.getJobFromHistory(
3693 'project-merge', 'org/project1').changes
3694 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003695
3696 def test_crd_branch(self):
3697 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003698
3699 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003700 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3701 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3702 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3703 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003704 A.addApproval('code-review', 2)
3705 B.addApproval('code-review', 2)
3706 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003707
3708 # A Depends-On: B+C
3709 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3710 A.subject, B.data['id'])
3711
James E. Blair08d19992016-08-10 15:25:31 -07003712 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003713 B.addApproval('approved', 1)
3714 C.addApproval('approved', 1)
3715 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003716 self.waitUntilSettled()
3717
James E. Blair08d19992016-08-10 15:25:31 -07003718 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003719 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003720 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003721 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003722 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003723 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003724 self.launch_server.hold_jobs_in_build = False
3725 self.launch_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003726 self.waitUntilSettled()
3727
3728 self.assertEqual(A.data['status'], 'MERGED')
3729 self.assertEqual(B.data['status'], 'MERGED')
3730 self.assertEqual(C.data['status'], 'MERGED')
3731 self.assertEqual(A.reported, 2)
3732 self.assertEqual(B.reported, 2)
3733 self.assertEqual(C.reported, 2)
3734
Paul Belanger6379db12016-11-14 13:57:54 -05003735 changes = self.getJobFromHistory(
3736 'project-merge', 'org/project1').changes
3737 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003738
3739 def test_crd_multiline(self):
3740 "Test multiple depends-on lines in commit"
3741 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3742 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3743 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003744 A.addApproval('code-review', 2)
3745 B.addApproval('code-review', 2)
3746 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003747
3748 # A Depends-On: B+C
3749 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3750 A.subject, B.data['id'], C.data['id'])
3751
James E. Blair08d19992016-08-10 15:25:31 -07003752 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003753 B.addApproval('approved', 1)
3754 C.addApproval('approved', 1)
3755 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003756 self.waitUntilSettled()
3757
James E. Blair08d19992016-08-10 15:25:31 -07003758 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003759 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003760 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003761 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003762 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003763 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003764 self.launch_server.hold_jobs_in_build = False
3765 self.launch_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003766 self.waitUntilSettled()
3767
3768 self.assertEqual(A.data['status'], 'MERGED')
3769 self.assertEqual(B.data['status'], 'MERGED')
3770 self.assertEqual(C.data['status'], 'MERGED')
3771 self.assertEqual(A.reported, 2)
3772 self.assertEqual(B.reported, 2)
3773 self.assertEqual(C.reported, 2)
3774
Paul Belanger5dccbe72016-11-14 11:17:37 -05003775 changes = self.getJobFromHistory(
3776 'project-merge', 'org/project1').changes
3777 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003778
3779 def test_crd_unshared_gate(self):
3780 "Test cross-repo dependencies in unshared gate queues"
3781 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3782 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003783 A.addApproval('code-review', 2)
3784 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003785
3786 # A Depends-On: B
3787 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3788 A.subject, B.data['id'])
3789
3790 # A and B do not share a queue, make sure that A is unable to
3791 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003792 B.addApproval('approved', 1)
3793 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003794 self.waitUntilSettled()
3795
3796 self.assertEqual(A.data['status'], 'NEW')
3797 self.assertEqual(B.data['status'], 'NEW')
3798 self.assertEqual(A.reported, 0)
3799 self.assertEqual(B.reported, 0)
3800 self.assertEqual(len(self.history), 0)
3801
3802 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003803 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003804 self.waitUntilSettled()
3805
3806 self.assertEqual(B.data['status'], 'MERGED')
3807 self.assertEqual(B.reported, 2)
3808
3809 # Now that B is merged, A should be able to be enqueued and
3810 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003811 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003812 self.waitUntilSettled()
3813
3814 self.assertEqual(A.data['status'], 'MERGED')
3815 self.assertEqual(A.reported, 2)
3816
James E. Blair96698e22015-04-02 07:48:21 -07003817 def test_crd_gate_reverse(self):
3818 "Test reverse cross-repo dependencies"
3819 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3820 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003821 A.addApproval('code-review', 2)
3822 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003823
3824 # A Depends-On: B
3825
3826 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3827 A.subject, B.data['id'])
3828
James E. Blair8b5408c2016-08-08 15:37:46 -07003829 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003830 self.waitUntilSettled()
3831
3832 self.assertEqual(A.data['status'], 'NEW')
3833 self.assertEqual(B.data['status'], 'NEW')
3834
James E. Blair08d19992016-08-10 15:25:31 -07003835 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003836 A.addApproval('approved', 1)
3837 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003838 self.waitUntilSettled()
3839
James E. Blair08d19992016-08-10 15:25:31 -07003840 self.launch_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003841 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003842 self.launch_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003843 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003844 self.launch_server.hold_jobs_in_build = False
3845 self.launch_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003846 self.waitUntilSettled()
3847
3848 self.assertEqual(A.data['status'], 'MERGED')
3849 self.assertEqual(B.data['status'], 'MERGED')
3850 self.assertEqual(A.reported, 2)
3851 self.assertEqual(B.reported, 2)
3852
Paul Belanger5dccbe72016-11-14 11:17:37 -05003853 changes = self.getJobFromHistory(
3854 'project-merge', 'org/project1').changes
3855 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003856
James E. Blair5ee24252014-12-30 10:12:29 -08003857 def test_crd_cycle(self):
3858 "Test cross-repo dependency cycles"
3859 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3860 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003861 A.addApproval('code-review', 2)
3862 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003863
3864 # A -> B -> A (via commit-depends)
3865
3866 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3867 A.subject, B.data['id'])
3868 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3869 B.subject, A.data['id'])
3870
James E. Blair8b5408c2016-08-08 15:37:46 -07003871 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003872 self.waitUntilSettled()
3873
3874 self.assertEqual(A.reported, 0)
3875 self.assertEqual(B.reported, 0)
3876 self.assertEqual(A.data['status'], 'NEW')
3877 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003878
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003879 def test_crd_gate_unknown(self):
3880 "Test unknown projects in dependent pipeline"
3881 self.init_repo("org/unknown")
3882 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3883 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003884 A.addApproval('code-review', 2)
3885 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003886
3887 # A Depends-On: B
3888 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3889 A.subject, B.data['id'])
3890
James E. Blair8b5408c2016-08-08 15:37:46 -07003891 B.addApproval('approved', 1)
3892 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003893 self.waitUntilSettled()
3894
3895 # Unknown projects cannot share a queue with any other
3896 # since they don't have common jobs with any other (they have no jobs).
3897 # Changes which depend on unknown project changes
3898 # should not be processed in dependent pipeline
3899 self.assertEqual(A.data['status'], 'NEW')
3900 self.assertEqual(B.data['status'], 'NEW')
3901 self.assertEqual(A.reported, 0)
3902 self.assertEqual(B.reported, 0)
3903 self.assertEqual(len(self.history), 0)
3904
3905 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07003906 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003907 B.setMerged()
3908 self.waitUntilSettled()
3909 self.assertEqual(len(self.history), 0)
3910
3911 # Now that B is merged, A should be able to be enqueued and
3912 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003913 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003914 self.waitUntilSettled()
3915
3916 self.assertEqual(A.data['status'], 'MERGED')
3917 self.assertEqual(A.reported, 2)
3918 self.assertEqual(B.data['status'], 'MERGED')
3919 self.assertEqual(B.reported, 0)
3920
James E. Blairbfb8e042014-12-30 17:01:44 -08003921 def test_crd_check(self):
3922 "Test cross-repo dependencies in independent pipelines"
3923
Paul Belangerb30342b2016-11-14 12:30:43 -05003924 self.launch_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08003925 self.gearman_server.hold_jobs_in_queue = True
3926 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3927 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3928
3929 # A Depends-On: B
3930 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3931 A.subject, B.data['id'])
3932
3933 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3934 self.waitUntilSettled()
3935
3936 queue = self.gearman_server.getQueue()
3937 ref = self.getParameter(queue[-1], 'ZUUL_REF')
3938 self.gearman_server.hold_jobs_in_queue = False
3939 self.gearman_server.release()
3940 self.waitUntilSettled()
3941
Paul Belangerb30342b2016-11-14 12:30:43 -05003942 self.launch_server.release('.*-merge')
3943 self.waitUntilSettled()
3944
3945 path = os.path.join(self.builds[0].jobdir.git_root, "org/project1")
James E. Blairbfb8e042014-12-30 17:01:44 -08003946 repo = git.Repo(path)
3947 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
3948 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05003949 correct_messages = [
3950 'initial commit', 'add content from fixture', 'A-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08003951 self.assertEqual(repo_messages, correct_messages)
3952
Paul Belangerb30342b2016-11-14 12:30:43 -05003953 path = os.path.join(self.builds[0].jobdir.git_root, "org/project2")
James E. Blairbfb8e042014-12-30 17:01:44 -08003954 repo = git.Repo(path)
3955 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
3956 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05003957 correct_messages = [
3958 'initial commit', 'add content from fixture', 'B-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08003959 self.assertEqual(repo_messages, correct_messages)
3960
Paul Belangerb30342b2016-11-14 12:30:43 -05003961 self.launch_server.hold_jobs_in_build = False
3962 self.launch_server.release()
3963 self.waitUntilSettled()
3964
James E. Blairbfb8e042014-12-30 17:01:44 -08003965 self.assertEqual(A.data['status'], 'NEW')
3966 self.assertEqual(B.data['status'], 'NEW')
3967 self.assertEqual(A.reported, 1)
3968 self.assertEqual(B.reported, 0)
3969
3970 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05003971 tenant = self.sched.abide.tenants.get('tenant-one')
3972 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08003973
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003974 def test_crd_check_git_depends(self):
3975 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07003976 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003977 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3978 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3979
3980 # Add two git-dependent changes and make sure they both report
3981 # success.
3982 B.setDependsOn(A, 1)
3983 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3984 self.waitUntilSettled()
3985 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3986 self.waitUntilSettled()
3987
James E. Blairb8c16472015-05-05 14:55:26 -07003988 self.orderedRelease()
3989 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003990 self.waitUntilSettled()
3991
3992 self.assertEqual(A.data['status'], 'NEW')
3993 self.assertEqual(B.data['status'], 'NEW')
3994 self.assertEqual(A.reported, 1)
3995 self.assertEqual(B.reported, 1)
3996
3997 self.assertEqual(self.history[0].changes, '1,1')
3998 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04003999 tenant = self.sched.abide.tenants.get('tenant-one')
4000 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004001
4002 self.assertIn('Build succeeded', A.messages[0])
4003 self.assertIn('Build succeeded', B.messages[0])
4004
4005 def test_crd_check_duplicate(self):
4006 "Test duplicate check in independent pipelines"
James E. Blair08d19992016-08-10 15:25:31 -07004007 self.launch_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004008 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4009 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004010 tenant = self.sched.abide.tenants.get('tenant-one')
4011 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004012
4013 # Add two git-dependent changes...
4014 B.setDependsOn(A, 1)
4015 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4016 self.waitUntilSettled()
4017 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4018
4019 # ...make sure the live one is not duplicated...
4020 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4021 self.waitUntilSettled()
4022 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4023
4024 # ...but the non-live one is able to be.
4025 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4026 self.waitUntilSettled()
4027 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4028
Clark Boylandd849822015-03-02 12:38:14 -08004029 # Release jobs in order to avoid races with change A jobs
4030 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004031 self.orderedRelease()
James E. Blair08d19992016-08-10 15:25:31 -07004032 self.launch_server.hold_jobs_in_build = False
4033 self.launch_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004034 self.waitUntilSettled()
4035
4036 self.assertEqual(A.data['status'], 'NEW')
4037 self.assertEqual(B.data['status'], 'NEW')
4038 self.assertEqual(A.reported, 1)
4039 self.assertEqual(B.reported, 1)
4040
4041 self.assertEqual(self.history[0].changes, '1,1 2,1')
4042 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004043 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004044
4045 self.assertIn('Build succeeded', A.messages[0])
4046 self.assertIn('Build succeeded', B.messages[0])
4047
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004048 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004049 "Test cross-repo dependencies re-enqueued in independent pipelines"
4050
4051 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004052 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4053 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004054
4055 # A Depends-On: B
4056 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4057 A.subject, B.data['id'])
4058
4059 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4060 self.waitUntilSettled()
4061
4062 self.sched.reconfigure(self.config)
4063
4064 # Make sure the items still share a change queue, and the
4065 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004066 tenant = self.sched.abide.tenants.get('tenant-one')
4067 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4068 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004069 first_item = queue.queue[0]
4070 for item in queue.queue:
4071 self.assertEqual(item.queue, first_item.queue)
4072 self.assertFalse(first_item.live)
4073 self.assertTrue(queue.queue[1].live)
4074
4075 self.gearman_server.hold_jobs_in_queue = False
4076 self.gearman_server.release()
4077 self.waitUntilSettled()
4078
4079 self.assertEqual(A.data['status'], 'NEW')
4080 self.assertEqual(B.data['status'], 'NEW')
4081 self.assertEqual(A.reported, 1)
4082 self.assertEqual(B.reported, 0)
4083
4084 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004085 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004086
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004087 def test_crd_check_reconfiguration(self):
4088 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4089
4090 def test_crd_undefined_project(self):
4091 """Test that undefined projects in dependencies are handled for
4092 independent pipelines"""
4093 # It's a hack for fake gerrit,
4094 # as it implies repo creation upon the creation of any change
4095 self.init_repo("org/unknown")
4096 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4097
James E. Blairec056492016-07-22 09:45:56 -07004098 @skip("Disabled for early v3 development")
James E. Blair17dd6772015-02-09 14:45:18 -08004099 def test_crd_check_ignore_dependencies(self):
4100 "Test cross-repo dependencies can be ignored"
James E. Blairf84026c2015-12-08 16:11:46 -08004101 self.updateConfigLayout(
4102 'tests/fixtures/layout-ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004103 self.sched.reconfigure(self.config)
4104 self.registerJobs()
4105
4106 self.gearman_server.hold_jobs_in_queue = True
4107 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4108 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4109 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4110
4111 # A Depends-On: B
4112 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4113 A.subject, B.data['id'])
4114 # C git-depends on B
4115 C.setDependsOn(B, 1)
4116 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4117 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4118 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4119 self.waitUntilSettled()
4120
4121 # Make sure none of the items share a change queue, and all
4122 # are live.
4123 check_pipeline = self.sched.layout.pipelines['check']
4124 self.assertEqual(len(check_pipeline.queues), 3)
4125 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4126 for item in check_pipeline.getAllItems():
4127 self.assertTrue(item.live)
4128
4129 self.gearman_server.hold_jobs_in_queue = False
4130 self.gearman_server.release()
4131 self.waitUntilSettled()
4132
4133 self.assertEqual(A.data['status'], 'NEW')
4134 self.assertEqual(B.data['status'], 'NEW')
4135 self.assertEqual(C.data['status'], 'NEW')
4136 self.assertEqual(A.reported, 1)
4137 self.assertEqual(B.reported, 1)
4138 self.assertEqual(C.reported, 1)
4139
4140 # Each job should have tested exactly one change
4141 for job in self.history:
4142 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004143
James E. Blairec056492016-07-22 09:45:56 -07004144 @skip("Disabled for early v3 development")
James E. Blair96698e22015-04-02 07:48:21 -07004145 def test_crd_check_transitive(self):
4146 "Test transitive cross-repo dependencies"
4147 # Specifically, if A -> B -> C, and C gets a new patchset and
4148 # A gets a new patchset, ensure the test of A,2 includes B,1
4149 # and C,2 (not C,1 which would indicate stale data in the
4150 # cache for B).
4151 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4152 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4153 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4154
4155 # A Depends-On: B
4156 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4157 A.subject, B.data['id'])
4158
4159 # B Depends-On: C
4160 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4161 B.subject, C.data['id'])
4162
4163 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4164 self.waitUntilSettled()
4165 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4166
4167 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4168 self.waitUntilSettled()
4169 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4170
4171 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4172 self.waitUntilSettled()
4173 self.assertEqual(self.history[-1].changes, '3,1')
4174
4175 C.addPatchset()
4176 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4177 self.waitUntilSettled()
4178 self.assertEqual(self.history[-1].changes, '3,2')
4179
4180 A.addPatchset()
4181 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4182 self.waitUntilSettled()
4183 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004184
James E. Blaira8b90b32016-08-24 15:18:50 -07004185 def test_crd_check_unknown(self):
4186 "Test unknown projects in independent pipeline"
4187 self.init_repo("org/unknown")
4188 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4189 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4190 # A Depends-On: B
4191 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4192 A.subject, B.data['id'])
4193
4194 # Make sure zuul has seen an event on B.
4195 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4196 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4197 self.waitUntilSettled()
4198
4199 self.assertEqual(A.data['status'], 'NEW')
4200 self.assertEqual(A.reported, 1)
4201 self.assertEqual(B.data['status'], 'NEW')
4202 self.assertEqual(B.reported, 0)
4203
James E. Blair92464a22016-04-05 10:21:26 -07004204 def test_crd_cycle_join(self):
4205 "Test an updated change creates a cycle"
4206 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4207
4208 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4209 self.waitUntilSettled()
4210
4211 # Create B->A
4212 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4213 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4214 B.subject, A.data['id'])
4215 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4216 self.waitUntilSettled()
4217
4218 # Update A to add A->B (a cycle).
4219 A.addPatchset()
4220 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4221 A.subject, B.data['id'])
4222 # Normally we would submit the patchset-created event for
4223 # processing here, however, we have no way of noting whether
4224 # the dependency cycle detection correctly raised an
4225 # exception, so instead, we reach into the source driver and
4226 # call the method that would ultimately be called by the event
4227 # processing.
4228
Paul Belanger0e155e22016-11-14 14:12:23 -05004229 tenant = self.sched.abide.tenants.get('tenant-one')
4230 source = tenant.layout.pipelines['gate'].source
4231
4232 # TODO(pabelanger): As we add more source / trigger APIs we should make
4233 # it easier for users to create events for testing.
4234 event = zuul.model.TriggerEvent()
4235 event.trigger_name = 'gerrit'
4236 event.change_number = '1'
4237 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004238 with testtools.ExpectedException(
4239 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004240 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004241 self.log.debug("Got expected dependency cycle exception")
4242
4243 # Now if we update B to remove the depends-on, everything
4244 # should be okay. B; A->B
4245
4246 B.addPatchset()
4247 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004248
4249 source.getChange(event, True)
4250 event.change_number = '2'
4251 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004252
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004253 def test_disable_at(self):
4254 "Test a pipeline will only report to the disabled trigger when failing"
4255
Paul Belanger7dc76e82016-11-11 16:51:08 -05004256 self.updateConfigLayout('layout-disabled-at')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004257 self.sched.reconfigure(self.config)
4258
Paul Belanger7dc76e82016-11-11 16:51:08 -05004259 tenant = self.sched.abide.tenants.get('openstack')
4260 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004261 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004262 0, tenant.layout.pipelines['check']._consecutive_failures)
4263 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004264
4265 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4266 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4267 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4268 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4269 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4270 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4271 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4272 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4273 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4274 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4275 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4276
James E. Blair08d19992016-08-10 15:25:31 -07004277 self.launch_server.failJob('project-test1', A)
4278 self.launch_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004279 # Let C pass, resetting the counter
James E. Blair08d19992016-08-10 15:25:31 -07004280 self.launch_server.failJob('project-test1', D)
4281 self.launch_server.failJob('project-test1', E)
4282 self.launch_server.failJob('project-test1', F)
4283 self.launch_server.failJob('project-test1', G)
4284 self.launch_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004285 # I also passes but should only report to the disabled reporters
James E. Blair08d19992016-08-10 15:25:31 -07004286 self.launch_server.failJob('project-test1', J)
4287 self.launch_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004288
4289 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4290 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4291 self.waitUntilSettled()
4292
4293 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004294 2, tenant.layout.pipelines['check']._consecutive_failures)
4295 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004296
4297 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4298 self.waitUntilSettled()
4299
4300 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004301 0, tenant.layout.pipelines['check']._consecutive_failures)
4302 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004303
4304 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4305 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4306 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4307 self.waitUntilSettled()
4308
4309 # We should be disabled now
4310 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004311 3, tenant.layout.pipelines['check']._consecutive_failures)
4312 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004313
4314 # We need to wait between each of these patches to make sure the
4315 # smtp messages come back in an expected order
4316 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4317 self.waitUntilSettled()
4318 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4319 self.waitUntilSettled()
4320 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4321 self.waitUntilSettled()
4322
4323 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4324 # leaving a message on each change
4325 self.assertEqual(1, len(A.messages))
4326 self.assertIn('Build failed.', A.messages[0])
4327 self.assertEqual(1, len(B.messages))
4328 self.assertIn('Build failed.', B.messages[0])
4329 self.assertEqual(1, len(C.messages))
4330 self.assertIn('Build succeeded.', C.messages[0])
4331 self.assertEqual(1, len(D.messages))
4332 self.assertIn('Build failed.', D.messages[0])
4333 self.assertEqual(1, len(E.messages))
4334 self.assertIn('Build failed.', E.messages[0])
4335 self.assertEqual(1, len(F.messages))
4336 self.assertIn('Build failed.', F.messages[0])
4337
4338 # The last 3 (GHI) would have only reported via smtp.
4339 self.assertEqual(3, len(self.smtp_messages))
4340 self.assertEqual(0, len(G.messages))
4341 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004342 self.assertIn(
4343 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004344 self.assertEqual(0, len(H.messages))
4345 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004346 self.assertIn(
4347 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004348 self.assertEqual(0, len(I.messages))
4349 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004350 self.assertIn(
4351 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004352
4353 # Now reload the configuration (simulate a HUP) to check the pipeline
4354 # comes out of disabled
4355 self.sched.reconfigure(self.config)
4356
Paul Belanger7dc76e82016-11-11 16:51:08 -05004357 tenant = self.sched.abide.tenants.get('openstack')
4358
4359 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004360 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004361 0, tenant.layout.pipelines['check']._consecutive_failures)
4362 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004363
4364 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4365 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4366 self.waitUntilSettled()
4367
4368 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004369 2, tenant.layout.pipelines['check']._consecutive_failures)
4370 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004371
4372 # J and K went back to gerrit
4373 self.assertEqual(1, len(J.messages))
4374 self.assertIn('Build failed.', J.messages[0])
4375 self.assertEqual(1, len(K.messages))
4376 self.assertIn('Build failed.', K.messages[0])
4377 # No more messages reported via smtp
4378 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004379
Paul Belanger71d98172016-11-08 10:56:31 -05004380 def test_rerun_on_abort(self):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004381 "Test that if a launch server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004382
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004383 self.launch_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004384 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4385 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4386 self.waitUntilSettled()
4387
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004388 self.launch_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004389 self.waitUntilSettled()
4390
4391 self.assertEqual(len(self.builds), 2)
4392 self.builds[0].requeue = True
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004393 self.launch_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004394 self.waitUntilSettled()
4395
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004396 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004397 self.assertEqual(len(self.builds), 1,
4398 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004399 self.builds[0].requeue = True
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004400 self.launch_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004401 self.waitUntilSettled()
4402
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004403 self.launch_server.hold_jobs_in_build = False
4404 self.launch_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004405 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004406 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004407 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4408 self.assertEqual(A.reported, 1)
4409 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004410
James E. Blair15be0e12017-01-03 13:45:20 -08004411 def test_zookeeper_disconnect(self):
4412 "Test that jobs are launched after a zookeeper disconnect"
4413
4414 self.fake_nodepool.paused = True
4415 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4416 A.addApproval('code-review', 2)
4417 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4418 self.waitUntilSettled()
4419
4420 self.zk.client.stop()
4421 self.zk.client.start()
4422 self.fake_nodepool.paused = False
4423 self.waitUntilSettled()
4424
4425 self.assertEqual(A.data['status'], 'MERGED')
4426 self.assertEqual(A.reported, 2)
4427
James E. Blair6ab79e02017-01-06 10:10:17 -08004428 def test_nodepool_failure(self):
4429 "Test that jobs are reported after a nodepool failure"
4430
4431 self.fake_nodepool.paused = True
4432 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4433 A.addApproval('code-review', 2)
4434 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4435 self.waitUntilSettled()
4436
4437 req = self.fake_nodepool.getNodeRequests()[0]
4438 self.fake_nodepool.addFailRequest(req)
4439
4440 self.fake_nodepool.paused = False
4441 self.waitUntilSettled()
4442
4443 self.assertEqual(A.data['status'], 'NEW')
4444 self.assertEqual(A.reported, 2)
4445 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4446 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4447 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4448
Arieb6f068c2016-10-09 13:11:06 +03004449
4450class TestDuplicatePipeline(ZuulTestCase):
4451 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4452
4453 def test_duplicate_pipelines(self):
4454 "Test that a change matching multiple pipelines works"
4455
4456 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4457 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4458 self.waitUntilSettled()
4459
4460 self.assertHistory([
4461 dict(name='project-test1', result='SUCCESS', changes='1,1',
4462 pipeline='dup1'),
4463 dict(name='project-test1', result='SUCCESS', changes='1,1',
4464 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004465 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004466
4467 self.assertEqual(len(A.messages), 2)
4468
Paul Belangera46a3742016-11-09 14:23:03 -05004469 if 'dup1' in A.messages[0]:
4470 self.assertIn('dup1', A.messages[0])
4471 self.assertNotIn('dup2', A.messages[0])
4472 self.assertIn('project-test1', A.messages[0])
4473 self.assertIn('dup2', A.messages[1])
4474 self.assertNotIn('dup1', A.messages[1])
4475 self.assertIn('project-test1', A.messages[1])
4476 else:
4477 self.assertIn('dup1', A.messages[1])
4478 self.assertNotIn('dup2', A.messages[1])
4479 self.assertIn('project-test1', A.messages[1])
4480 self.assertIn('dup2', A.messages[0])
4481 self.assertNotIn('dup1', A.messages[0])
4482 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004483
4484
4485class TestSchedulerOneJobProject(ZuulTestCase):
4486 tenant_config_file = 'config/one-job-project/main.yaml'
4487
4488 def test_one_job_project(self):
4489 "Test that queueing works with one job"
4490 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4491 'master', 'A')
4492 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4493 'master', 'B')
4494 A.addApproval('code-review', 2)
4495 B.addApproval('code-review', 2)
4496 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4497 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4498 self.waitUntilSettled()
4499
4500 self.assertEqual(A.data['status'], 'MERGED')
4501 self.assertEqual(A.reported, 2)
4502 self.assertEqual(B.data['status'], 'MERGED')
4503 self.assertEqual(B.reported, 2)
Paul Belanger86085b32016-11-03 12:48:57 -04004504
4505
4506class TestSchedulerTemplatedProject(ZuulTestCase):
4507 tenant_config_file = 'config/templated-project/main.yaml'
4508
4509 def test_job_from_templates_launched(self):
4510 "Test whether a job generated via a template can be launched"
4511
4512 A = self.fake_gerrit.addFakeChange(
4513 'org/templated-project', 'master', 'A')
4514 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4515 self.waitUntilSettled()
4516
4517 self.assertEqual(self.getJobFromHistory('project-test1').result,
4518 'SUCCESS')
4519 self.assertEqual(self.getJobFromHistory('project-test2').result,
4520 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004521
4522 def test_layered_templates(self):
4523 "Test whether a job generated via a template can be launched"
4524
4525 A = self.fake_gerrit.addFakeChange(
4526 'org/layered-project', 'master', 'A')
4527 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4528 self.waitUntilSettled()
4529
4530 self.assertEqual(self.getJobFromHistory('project-test1').result,
4531 'SUCCESS')
4532 self.assertEqual(self.getJobFromHistory('project-test2').result,
4533 'SUCCESS')
4534 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4535 ).result, 'SUCCESS')
4536 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4537 ).result, 'SUCCESS')
4538 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4539 ).result, 'SUCCESS')
4540 self.assertEqual(self.getJobFromHistory('project-test6').result,
4541 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004542
4543
4544class TestSchedulerSuccessURL(ZuulTestCase):
4545 tenant_config_file = 'config/success-url/main.yaml'
4546
4547 def test_success_url(self):
4548 "Ensure bad build params are ignored"
4549 self.sched.reconfigure(self.config)
4550 self.init_repo('org/docs')
4551
4552 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4553 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4554 self.waitUntilSettled()
4555
4556 # Both builds ran: docs-draft-test + docs-draft-test2
4557 self.assertEqual(len(self.history), 2)
4558
4559 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004560 for build in self.history:
4561 if build.name == 'docs-draft-test':
4562 uuid = build.uuid[:7]
4563 break
Adam Gandelman94a60062016-11-21 16:43:14 -08004564
4565 # Two msgs: 'Starting...' + results
4566 self.assertEqual(len(self.smtp_messages), 2)
4567 body = self.smtp_messages[1]['body'].splitlines()
4568 self.assertEqual('Build succeeded.', body[0])
4569
4570 self.assertIn(
4571 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4572 'docs-draft-test/%s/publish-docs/' % uuid,
4573 body[2])
4574
4575 # NOTE: This default URL is currently hard-coded in launcher/server.py
4576 self.assertIn(
4577 '- docs-draft-test2 https://server/job',
4578 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004579
4580
Adam Gandelman4da00f62016-12-09 15:47:33 -08004581class TestSchedulerMerges(ZuulTestCase):
4582 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004583
4584 def _test_project_merge_mode(self, mode):
4585 self.launch_server.keep_jobdir = False
4586 project = 'org/project-%s' % mode
4587 self.launch_server.hold_jobs_in_build = True
4588 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4589 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4590 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4591 A.addApproval('code-review', 2)
4592 B.addApproval('code-review', 2)
4593 C.addApproval('code-review', 2)
4594 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4595 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4596 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4597 self.waitUntilSettled()
4598
4599 build = self.builds[-1]
4600 ref = self.getParameter(build, 'ZUUL_REF')
4601
4602 path = os.path.join(build.jobdir.git_root, project)
4603 repo = git.Repo(path)
4604 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4605 repo_messages.reverse()
4606
4607 self.launch_server.hold_jobs_in_build = False
4608 self.launch_server.release()
4609 self.waitUntilSettled()
4610
4611 return repo_messages
4612
4613 def _test_merge(self, mode):
4614 us_path = os.path.join(
4615 self.upstream_root, 'org/project-%s' % mode)
4616 expected_messages = [
4617 'initial commit',
4618 'add content from fixture',
4619 # the intermediate commits order is nondeterministic
4620 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4621 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4622 ]
4623 result = self._test_project_merge_mode(mode)
4624 self.assertEqual(result[:2], expected_messages[:2])
4625 self.assertEqual(result[-2:], expected_messages[-2:])
4626
4627 def test_project_merge_mode_merge(self):
4628 self._test_merge('merge')
4629
4630 def test_project_merge_mode_merge_resolve(self):
4631 self._test_merge('merge-resolve')
4632
4633 def test_project_merge_mode_cherrypick(self):
4634 expected_messages = [
4635 'initial commit',
4636 'add content from fixture',
4637 'A-1',
4638 'B-1',
4639 'C-1']
4640 result = self._test_project_merge_mode('cherry-pick')
4641 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004642
4643 def test_merge_branch(self):
4644 "Test that the right commits are on alternate branches"
4645 self.create_branch('org/project-merge-branches', 'mp')
4646
4647 self.launch_server.hold_jobs_in_build = True
4648 A = self.fake_gerrit.addFakeChange(
4649 'org/project-merge-branches', 'mp', 'A')
4650 B = self.fake_gerrit.addFakeChange(
4651 'org/project-merge-branches', 'mp', 'B')
4652 C = self.fake_gerrit.addFakeChange(
4653 'org/project-merge-branches', 'mp', 'C')
4654 A.addApproval('code-review', 2)
4655 B.addApproval('code-review', 2)
4656 C.addApproval('code-review', 2)
4657 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4658 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4659 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4660 self.waitUntilSettled()
4661
4662 self.launch_server.release('.*-merge')
4663 self.waitUntilSettled()
4664 self.launch_server.release('.*-merge')
4665 self.waitUntilSettled()
4666 self.launch_server.release('.*-merge')
4667 self.waitUntilSettled()
4668
4669 build = self.builds[-1]
4670 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
4671 ref = self.getParameter(build, 'ZUUL_REF')
4672 path = os.path.join(
4673 build.jobdir.git_root, 'org/project-merge-branches')
4674 repo = git.Repo(path)
4675
4676 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4677 repo_messages.reverse()
4678 correct_messages = [
4679 'initial commit',
4680 'add content from fixture',
4681 'mp commit',
4682 'A-1', 'B-1', 'C-1']
4683 self.assertEqual(repo_messages, correct_messages)
4684
4685 self.launch_server.hold_jobs_in_build = False
4686 self.launch_server.release()
4687 self.waitUntilSettled()
4688
4689 def test_merge_multi_branch(self):
4690 "Test that dependent changes on multiple branches are merged"
4691 self.create_branch('org/project-merge-branches', 'mp')
4692
4693 self.launch_server.hold_jobs_in_build = True
4694 A = self.fake_gerrit.addFakeChange(
4695 'org/project-merge-branches', 'master', 'A')
4696 B = self.fake_gerrit.addFakeChange(
4697 'org/project-merge-branches', 'mp', 'B')
4698 C = self.fake_gerrit.addFakeChange(
4699 'org/project-merge-branches', 'master', 'C')
4700 A.addApproval('code-review', 2)
4701 B.addApproval('code-review', 2)
4702 C.addApproval('code-review', 2)
4703 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4704 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4705 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4706 self.waitUntilSettled()
4707
4708 job_A = None
4709 for job in self.builds:
4710 if 'project-merge' in job.name:
4711 job_A = job
4712 ref_A = self.getParameter(job_A, 'ZUUL_REF')
4713 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
4714 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
4715 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
4716
4717 path = os.path.join(
4718 job_A.jobdir.git_root, "org/project-merge-branches")
4719 repo = git.Repo(path)
4720 repo_messages = [c.message.strip()
4721 for c in repo.iter_commits(ref_A)]
4722 repo_messages.reverse()
4723 correct_messages = [
4724 'initial commit', 'add content from fixture', 'A-1']
4725 self.assertEqual(repo_messages, correct_messages)
4726
4727 self.launch_server.release('.*-merge')
4728 self.waitUntilSettled()
4729
4730 job_B = None
4731 for job in self.builds:
4732 if 'project-merge' in job.name:
4733 job_B = job
4734 ref_B = self.getParameter(job_B, 'ZUUL_REF')
4735 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
4736 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
4737 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
4738
4739 path = os.path.join(
4740 job_B.jobdir.git_root, "org/project-merge-branches")
4741 repo = git.Repo(path)
4742 repo_messages = [c.message.strip()
4743 for c in repo.iter_commits(ref_B)]
4744 repo_messages.reverse()
4745 correct_messages = [
4746 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4747 self.assertEqual(repo_messages, correct_messages)
4748
4749 self.launch_server.release('.*-merge')
4750 self.waitUntilSettled()
4751
4752 job_C = None
4753 for job in self.builds:
4754 if 'project-merge' in job.name:
4755 job_C = job
4756 ref_C = self.getParameter(job_C, 'ZUUL_REF')
4757 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
4758 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
4759 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
4760 path = os.path.join(
4761 job_C.jobdir.git_root, "org/project-merge-branches")
4762 repo = git.Repo(path)
4763 repo_messages = [c.message.strip()
4764 for c in repo.iter_commits(ref_C)]
4765
4766 repo_messages.reverse()
4767 correct_messages = [
4768 'initial commit', 'add content from fixture',
4769 'A-1', 'C-1']
4770 # Ensure the right commits are in the history for this ref
4771 self.assertEqual(repo_messages, correct_messages)
4772
4773 self.launch_server.hold_jobs_in_build = False
4774 self.launch_server.release()
4775 self.waitUntilSettled()