blob: 298a4a726bbbf80f9cf0adb27aeadf1ca4b03d88 [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 logging
19import os
James E. Blairb0fcae42012-07-17 11:12:10 -070020import re
James E. Blair4886cc12012-07-18 15:39:41 -070021import shutil
Monty Taylorbc758832013-06-17 17:22:42 -040022import time
James E. Blairec056492016-07-22 09:45:56 -070023from unittest import skip
Monty Taylorbc758832013-06-17 17:22:42 -040024
James E. Blair4886cc12012-07-18 15:39:41 -070025import git
Morgan Fainberg293f7f82016-05-30 14:01:22 -070026from six.moves import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040027import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070028
Maru Newby3fe5f852015-01-13 04:22:14 +000029import zuul.change_matcher
James E. Blairb0fcae42012-07-17 11:12:10 -070030import zuul.scheduler
James E. Blairad28e912013-11-27 10:43:22 -080031import zuul.rpcclient
Paul Belanger9bba4902016-11-02 16:07:33 -040032import zuul.model
James E. Blairb0fcae42012-07-17 11:12:10 -070033
Maru Newby3fe5f852015-01-13 04:22:14 +000034from tests.base import (
Maru Newby3fe5f852015-01-13 04:22:14 +000035 ZuulTestCase,
36 repack_repo,
37)
James E. Blairb0fcae42012-07-17 11:12:10 -070038
James E. Blair1f4c2bb2013-04-26 08:40:46 -070039logging.basicConfig(level=logging.DEBUG,
40 format='%(asctime)s %(name)-32s '
41 '%(levelname)-8s %(message)s')
James E. Blairb0fcae42012-07-17 11:12:10 -070042
43
Clark Boylanb640e052014-04-03 16:41:46 -070044class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070045 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010046
James E. Blairb0fcae42012-07-17 11:12:10 -070047 def test_jobs_launched(self):
48 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070049
James E. Blairb0fcae42012-07-17 11:12:10 -070050 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070051 A.addApproval('code-review', 2)
52 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070053 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040054 self.assertEqual(self.getJobFromHistory('project-merge').result,
55 'SUCCESS')
56 self.assertEqual(self.getJobFromHistory('project-test1').result,
57 'SUCCESS')
58 self.assertEqual(self.getJobFromHistory('project-test2').result,
59 'SUCCESS')
60 self.assertEqual(A.data['status'], 'MERGED')
61 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070062 self.assertEqual(self.getJobFromHistory('project-test1').node,
63 'image1')
James E. Blairb0fcae42012-07-17 11:12:10 -070064
James E. Blair552b54f2016-07-22 13:55:32 -070065 # TODOv3(jeblair): we may want to report stats by tenant (also?).
James E. Blair66eeebf2013-07-27 17:44:32 -070066 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
67 self.assertReportedStat('zuul.pipeline.gate.current_changes',
68 value='1|g')
69 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
70 kind='ms')
71 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
72 value='1|c')
73 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
74 self.assertReportedStat('zuul.pipeline.gate.total_changes',
75 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070076 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070077 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -070078 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070079 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070080
James E. Blair5821bd92015-09-16 08:48:15 -070081 for build in self.builds:
82 self.assertEqual(build.parameters['ZUUL_VOTING'], '1')
83
James E. Blair3cb10702013-08-24 08:56:03 -070084 def test_initial_pipeline_gauges(self):
85 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -070086 self.assertReportedStat('zuul.pipeline.gate.current_changes',
87 value='0|g')
88 self.assertReportedStat('zuul.pipeline.check.current_changes',
89 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -070090
James E. Blair34776ee2016-08-25 13:53:54 -070091 def test_job_branch(self):
92 "Test the correct variant of a job runs on a branch"
93 self.create_branch('org/project', 'stable')
94 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
95 A.addApproval('code-review', 2)
96 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
97 self.waitUntilSettled()
98 self.assertEqual(self.getJobFromHistory('project-test1').result,
99 'SUCCESS')
100 self.assertEqual(self.getJobFromHistory('project-test2').result,
101 'SUCCESS')
102 self.assertEqual(A.data['status'], 'MERGED')
103 self.assertEqual(A.reported, 2,
104 "A should report start and success")
105 self.assertIn('gate', A.messages[1],
106 "A should transit gate")
107 self.assertEqual(self.getJobFromHistory('project-test1').node,
108 'image2')
109
James E. Blairb0fcae42012-07-17 11:12:10 -0700110 def test_parallel_changes(self):
111 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700112
James E. Blair791b5392016-08-03 11:25:56 -0700113 self.launch_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700114 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
115 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
116 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700117 A.addApproval('code-review', 2)
118 B.addApproval('code-review', 2)
119 C.addApproval('code-review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700120
James E. Blair8b5408c2016-08-08 15:37:46 -0700121 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
122 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
123 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700124
125 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400126 self.assertEqual(len(self.builds), 1)
127 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700128 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700129
James E. Blair791b5392016-08-03 11:25:56 -0700130 self.launch_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700131 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400132 self.assertEqual(len(self.builds), 3)
133 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700134 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400135 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700136 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400137 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700138 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700139
James E. Blair791b5392016-08-03 11:25:56 -0700140 self.launch_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700141 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400142 self.assertEqual(len(self.builds), 5)
143 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700144 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400145 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700146 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700147
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400148 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700149 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400150 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700151 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700152
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400153 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700154 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700155
James E. Blair791b5392016-08-03 11:25:56 -0700156 self.launch_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700157 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400158 self.assertEqual(len(self.builds), 6)
159 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700160 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400161 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700162 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700163
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400164 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700165 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400166 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700167 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700168
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400169 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700170 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400171 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700172 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700173
James E. Blair791b5392016-08-03 11:25:56 -0700174 self.launch_server.hold_jobs_in_build = False
175 self.launch_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700176 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400177 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700178
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400179 self.assertEqual(len(self.history), 9)
180 self.assertEqual(A.data['status'], 'MERGED')
181 self.assertEqual(B.data['status'], 'MERGED')
182 self.assertEqual(C.data['status'], 'MERGED')
183 self.assertEqual(A.reported, 2)
184 self.assertEqual(B.reported, 2)
185 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700186
187 def test_failed_changes(self):
188 "Test that a change behind a failed change is retested"
James E. Blair08d19992016-08-10 15:25:31 -0700189 self.launch_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700190
James E. Blairb02a3bb2012-07-30 17:49:55 -0700191 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
192 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -0700193 A.addApproval('code-review', 2)
194 B.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700195
James E. Blair08d19992016-08-10 15:25:31 -0700196 self.launch_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700197
James E. Blair8b5408c2016-08-08 15:37:46 -0700198 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
199 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700200 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700201 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400202
James E. Blair08d19992016-08-10 15:25:31 -0700203 self.launch_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400204 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700205 # A/project-merge is complete
206 self.assertBuilds([
207 dict(name='project-test1', changes='1,1'),
208 dict(name='project-test2', changes='1,1'),
209 dict(name='project-merge', changes='1,1 2,1'),
210 ])
James E. Blaire2819012013-06-28 17:17:26 -0400211
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700212 self.launch_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400213 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700214 # A/project-merge is complete
215 # B/project-merge is complete
216 self.assertBuilds([
217 dict(name='project-test1', changes='1,1'),
218 dict(name='project-test2', changes='1,1'),
219 dict(name='project-test1', changes='1,1 2,1'),
220 dict(name='project-test2', changes='1,1 2,1'),
221 ])
222
223 # Release project-test1 for A which will fail. This will
224 # abort both running B jobs and relaunch project-merge for B.
225 self.builds[0].release()
226 self.waitUntilSettled()
227
228 self.orderedRelease()
229 self.assertHistory([
230 dict(name='project-merge', result='SUCCESS', changes='1,1'),
231 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
232 dict(name='project-test1', result='FAILURE', changes='1,1'),
233 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
234 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
235 dict(name='project-test2', result='SUCCESS', changes='1,1'),
236 dict(name='project-merge', result='SUCCESS', changes='2,1'),
237 dict(name='project-test1', result='SUCCESS', changes='2,1'),
238 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700239 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700240
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400241 self.assertEqual(A.data['status'], 'NEW')
242 self.assertEqual(B.data['status'], 'MERGED')
243 self.assertEqual(A.reported, 2)
244 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700245
246 def test_independent_queues(self):
247 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700248
James E. Blair08d19992016-08-10 15:25:31 -0700249 self.launch_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900250 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700251 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
252 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700253 A.addApproval('code-review', 2)
254 B.addApproval('code-review', 2)
255 C.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700256
James E. Blair8b5408c2016-08-08 15:37:46 -0700257 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
258 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
259 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700260
James E. Blairb02a3bb2012-07-30 17:49:55 -0700261 self.waitUntilSettled()
262
263 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700264 self.assertBuilds([
265 dict(name='project-merge', changes='1,1'),
266 dict(name='project-merge', changes='2,1'),
267 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700268
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700269 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700270 self.builds[0].release()
271 self.waitUntilSettled()
272 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700273 self.waitUntilSettled()
274 # Release the merge job for project2 which is behind project1
James E. Blair08d19992016-08-10 15:25:31 -0700275 self.launch_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700276 self.waitUntilSettled()
277
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700278 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700279 self.assertBuilds([
280 dict(name='project-test1', changes='1,1'),
281 dict(name='project-test2', changes='1,1'),
282 dict(name='project-test1', changes='2,1'),
283 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500284 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700285 dict(name='project-test1', changes='2,1 3,1'),
286 dict(name='project-test2', changes='2,1 3,1'),
287 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700288
James E. Blair5f0d49e2016-08-19 09:36:50 -0700289 self.orderedRelease()
290 self.assertHistory([
291 dict(name='project-merge', result='SUCCESS', changes='1,1'),
292 dict(name='project-merge', result='SUCCESS', changes='2,1'),
293 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
294 dict(name='project-test1', result='SUCCESS', changes='1,1'),
295 dict(name='project-test2', result='SUCCESS', changes='1,1'),
296 dict(name='project-test1', result='SUCCESS', changes='2,1'),
297 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500298 dict(
299 name='project1-project2-integration',
300 result='SUCCESS',
301 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700302 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
303 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
304 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700305
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400306 self.assertEqual(A.data['status'], 'MERGED')
307 self.assertEqual(B.data['status'], 'MERGED')
308 self.assertEqual(C.data['status'], 'MERGED')
309 self.assertEqual(A.reported, 2)
310 self.assertEqual(B.reported, 2)
311 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700312
313 def test_failed_change_at_head(self):
314 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700315
James E. Blair08d19992016-08-10 15:25:31 -0700316 self.launch_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700317 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
318 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
319 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700320 A.addApproval('code-review', 2)
321 B.addApproval('code-review', 2)
322 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700323
James E. Blair08d19992016-08-10 15:25:31 -0700324 self.launch_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700325
James E. Blair8b5408c2016-08-08 15:37:46 -0700326 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
327 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
328 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700329
330 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700331
James E. Blairb536ecc2016-08-31 10:11:42 -0700332 self.assertBuilds([
333 dict(name='project-merge', changes='1,1'),
334 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700335
James E. Blair08d19992016-08-10 15:25:31 -0700336 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700337 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700338 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700339 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700340 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700341 self.waitUntilSettled()
342
James E. Blairb536ecc2016-08-31 10:11:42 -0700343 self.assertBuilds([
344 dict(name='project-test1', changes='1,1'),
345 dict(name='project-test2', changes='1,1'),
346 dict(name='project-test1', changes='1,1 2,1'),
347 dict(name='project-test2', changes='1,1 2,1'),
348 dict(name='project-test1', changes='1,1 2,1 3,1'),
349 dict(name='project-test2', changes='1,1 2,1 3,1'),
350 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700351
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400352 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700353 self.waitUntilSettled()
354
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400355 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700356 self.assertBuilds([
357 dict(name='project-test2', changes='1,1'),
358 dict(name='project-merge', changes='2,1'),
359 ])
360 # Unordered history comparison because the aborts can finish
361 # in any order.
362 self.assertHistory([
363 dict(name='project-merge', result='SUCCESS',
364 changes='1,1'),
365 dict(name='project-merge', result='SUCCESS',
366 changes='1,1 2,1'),
367 dict(name='project-merge', result='SUCCESS',
368 changes='1,1 2,1 3,1'),
369 dict(name='project-test1', result='FAILURE',
370 changes='1,1'),
371 dict(name='project-test1', result='ABORTED',
372 changes='1,1 2,1'),
373 dict(name='project-test2', result='ABORTED',
374 changes='1,1 2,1'),
375 dict(name='project-test1', result='ABORTED',
376 changes='1,1 2,1 3,1'),
377 dict(name='project-test2', result='ABORTED',
378 changes='1,1 2,1 3,1'),
379 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700380
James E. Blairb536ecc2016-08-31 10:11:42 -0700381 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700382 self.waitUntilSettled()
James E. Blairb536ecc2016-08-31 10:11:42 -0700383 self.launch_server.release('.*-merge')
384 self.waitUntilSettled()
385 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700386
James E. Blairb536ecc2016-08-31 10:11:42 -0700387 self.assertBuilds([])
388 self.assertHistory([
389 dict(name='project-merge', result='SUCCESS',
390 changes='1,1'),
391 dict(name='project-merge', result='SUCCESS',
392 changes='1,1 2,1'),
393 dict(name='project-merge', result='SUCCESS',
394 changes='1,1 2,1 3,1'),
395 dict(name='project-test1', result='FAILURE',
396 changes='1,1'),
397 dict(name='project-test1', result='ABORTED',
398 changes='1,1 2,1'),
399 dict(name='project-test2', result='ABORTED',
400 changes='1,1 2,1'),
401 dict(name='project-test1', result='ABORTED',
402 changes='1,1 2,1 3,1'),
403 dict(name='project-test2', result='ABORTED',
404 changes='1,1 2,1 3,1'),
405 dict(name='project-merge', result='SUCCESS',
406 changes='2,1'),
407 dict(name='project-merge', result='SUCCESS',
408 changes='2,1 3,1'),
409 dict(name='project-test2', result='SUCCESS',
410 changes='1,1'),
411 dict(name='project-test1', result='SUCCESS',
412 changes='2,1'),
413 dict(name='project-test2', result='SUCCESS',
414 changes='2,1'),
415 dict(name='project-test1', result='SUCCESS',
416 changes='2,1 3,1'),
417 dict(name='project-test2', result='SUCCESS',
418 changes='2,1 3,1'),
419 ], ordered=False)
420
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400421 self.assertEqual(A.data['status'], 'NEW')
422 self.assertEqual(B.data['status'], 'MERGED')
423 self.assertEqual(C.data['status'], 'MERGED')
424 self.assertEqual(A.reported, 2)
425 self.assertEqual(B.reported, 2)
426 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700427
James E. Blair0aac4872013-08-23 14:02:38 -0700428 def test_failed_change_in_middle(self):
429 "Test a failed change in the middle of the queue"
430
James E. Blair08d19992016-08-10 15:25:31 -0700431 self.launch_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700432 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
433 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
434 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700435 A.addApproval('code-review', 2)
436 B.addApproval('code-review', 2)
437 C.addApproval('code-review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700438
James E. Blair08d19992016-08-10 15:25:31 -0700439 self.launch_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700440
James E. Blair8b5408c2016-08-08 15:37:46 -0700441 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
442 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
443 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700444
445 self.waitUntilSettled()
446
James E. Blair08d19992016-08-10 15:25:31 -0700447 self.launch_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700448 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700449 self.launch_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700450 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700451 self.launch_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700452 self.waitUntilSettled()
453
454 self.assertEqual(len(self.builds), 6)
455 self.assertEqual(self.builds[0].name, 'project-test1')
456 self.assertEqual(self.builds[1].name, 'project-test2')
457 self.assertEqual(self.builds[2].name, 'project-test1')
458 self.assertEqual(self.builds[3].name, 'project-test2')
459 self.assertEqual(self.builds[4].name, 'project-test1')
460 self.assertEqual(self.builds[5].name, 'project-test2')
461
462 self.release(self.builds[2])
463 self.waitUntilSettled()
464
James E. Blair972e3c72013-08-29 12:04:55 -0700465 # project-test1 and project-test2 for A
466 # project-test2 for B
467 # project-merge for C (without B)
468 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700469 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
470
James E. Blair08d19992016-08-10 15:25:31 -0700471 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700472 self.waitUntilSettled()
473
474 # project-test1 and project-test2 for A
475 # project-test2 for B
476 # project-test1 and project-test2 for C
477 self.assertEqual(len(self.builds), 5)
478
Paul Belanger2e2a0492016-10-30 18:33:37 -0400479 tenant = self.sched.abide.tenants.get('tenant-one')
480 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700481 builds = items[0].current_build_set.getBuilds()
482 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
483 self.assertEqual(self.countJobResults(builds, None), 2)
484 builds = items[1].current_build_set.getBuilds()
485 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
486 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
487 self.assertEqual(self.countJobResults(builds, None), 1)
488 builds = items[2].current_build_set.getBuilds()
489 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700490 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700491
James E. Blair08d19992016-08-10 15:25:31 -0700492 self.launch_server.hold_jobs_in_build = False
493 self.launch_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700494 self.waitUntilSettled()
495
496 self.assertEqual(len(self.builds), 0)
497 self.assertEqual(len(self.history), 12)
498 self.assertEqual(A.data['status'], 'MERGED')
499 self.assertEqual(B.data['status'], 'NEW')
500 self.assertEqual(C.data['status'], 'MERGED')
501 self.assertEqual(A.reported, 2)
502 self.assertEqual(B.reported, 2)
503 self.assertEqual(C.reported, 2)
504
James E. Blaird466dc42012-07-31 10:42:56 -0700505 def test_failed_change_at_head_with_queue(self):
506 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700507
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700508 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700509 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
510 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
511 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700512 A.addApproval('code-review', 2)
513 B.addApproval('code-review', 2)
514 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700515
James E. Blair08d19992016-08-10 15:25:31 -0700516 self.launch_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700517
James E. Blair8b5408c2016-08-08 15:37:46 -0700518 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
519 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
520 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700521
522 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700523 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400524 self.assertEqual(len(self.builds), 0)
525 self.assertEqual(len(queue), 1)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800526 self.assertEqual(queue[0].name, 'launcher:launch')
527 job_args = json.loads(queue[0].arguments)
528 self.assertEqual(job_args['job'], 'project-merge')
529 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700530
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700531 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700532 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700533 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700534 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700535 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700536 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700537 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700538
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400539 self.assertEqual(len(self.builds), 0)
540 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800541
542 self.assertEqual(
543 json.loads(queue[0].arguments)['job'], 'project-test1')
544 self.assertEqual(
545 json.loads(queue[1].arguments)['job'], 'project-test2')
546 self.assertEqual(
547 json.loads(queue[2].arguments)['job'], 'project-test1')
548 self.assertEqual(
549 json.loads(queue[3].arguments)['job'], 'project-test2')
550 self.assertEqual(
551 json.loads(queue[4].arguments)['job'], 'project-test1')
552 self.assertEqual(
553 json.loads(queue[5].arguments)['job'], 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700554
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700555 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700556 self.waitUntilSettled()
557
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400558 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700559 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400560 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
561 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700562
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700563 self.gearman_server.hold_jobs_in_queue = False
564 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700565 self.waitUntilSettled()
566
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400567 self.assertEqual(len(self.builds), 0)
568 self.assertEqual(len(self.history), 11)
569 self.assertEqual(A.data['status'], 'NEW')
570 self.assertEqual(B.data['status'], 'MERGED')
571 self.assertEqual(C.data['status'], 'MERGED')
572 self.assertEqual(A.reported, 2)
573 self.assertEqual(B.reported, 2)
574 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700575
James E. Blairec056492016-07-22 09:45:56 -0700576 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700577 def _test_time_database(self, iteration):
James E. Blair08d19992016-08-10 15:25:31 -0700578 self.launch_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700579 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -0700580 A.addApproval('code-review', 2)
581 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700582 self.waitUntilSettled()
583 time.sleep(2)
584
585 data = json.loads(self.sched.formatStatusJSON())
586 found_job = None
587 for pipeline in data['pipelines']:
588 if pipeline['name'] != 'gate':
589 continue
590 for queue in pipeline['change_queues']:
591 for head in queue['heads']:
592 for item in head:
593 for job in item['jobs']:
594 if job['name'] == 'project-merge':
595 found_job = job
596 break
597
598 self.assertIsNotNone(found_job)
599 if iteration == 1:
600 self.assertIsNotNone(found_job['estimated_time'])
601 self.assertIsNone(found_job['remaining_time'])
602 else:
603 self.assertIsNotNone(found_job['estimated_time'])
604 self.assertTrue(found_job['estimated_time'] >= 2)
605 self.assertIsNotNone(found_job['remaining_time'])
606
James E. Blair08d19992016-08-10 15:25:31 -0700607 self.launch_server.hold_jobs_in_build = False
608 self.launch_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700609 self.waitUntilSettled()
610
James E. Blairec056492016-07-22 09:45:56 -0700611 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700612 def test_time_database(self):
613 "Test the time database"
614
615 self._test_time_database(1)
616 self._test_time_database(2)
617
James E. Blairfef71632013-09-23 11:15:47 -0700618 def test_two_failed_changes_at_head(self):
619 "Test that changes are reparented correctly if 2 fail at head"
620
James E. Blair08d19992016-08-10 15:25:31 -0700621 self.launch_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700622 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
623 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
624 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700625 A.addApproval('code-review', 2)
626 B.addApproval('code-review', 2)
627 C.addApproval('code-review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700628
James E. Blair08d19992016-08-10 15:25:31 -0700629 self.launch_server.failJob('project-test1', A)
630 self.launch_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700631
James E. Blair8b5408c2016-08-08 15:37:46 -0700632 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
633 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
634 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700635 self.waitUntilSettled()
636
James E. Blair08d19992016-08-10 15:25:31 -0700637 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700638 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700639 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700640 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700641 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700642 self.waitUntilSettled()
643
644 self.assertEqual(len(self.builds), 6)
645 self.assertEqual(self.builds[0].name, 'project-test1')
646 self.assertEqual(self.builds[1].name, 'project-test2')
647 self.assertEqual(self.builds[2].name, 'project-test1')
648 self.assertEqual(self.builds[3].name, 'project-test2')
649 self.assertEqual(self.builds[4].name, 'project-test1')
650 self.assertEqual(self.builds[5].name, 'project-test2')
651
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400652 self.assertTrue(self.builds[0].hasChanges(A))
653 self.assertTrue(self.builds[2].hasChanges(A))
654 self.assertTrue(self.builds[2].hasChanges(B))
655 self.assertTrue(self.builds[4].hasChanges(A))
656 self.assertTrue(self.builds[4].hasChanges(B))
657 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700658
659 # Fail change B first
660 self.release(self.builds[2])
661 self.waitUntilSettled()
662
663 # restart of C after B failure
James E. Blair08d19992016-08-10 15:25:31 -0700664 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700665 self.waitUntilSettled()
666
667 self.assertEqual(len(self.builds), 5)
668 self.assertEqual(self.builds[0].name, 'project-test1')
669 self.assertEqual(self.builds[1].name, 'project-test2')
670 self.assertEqual(self.builds[2].name, 'project-test2')
671 self.assertEqual(self.builds[3].name, 'project-test1')
672 self.assertEqual(self.builds[4].name, 'project-test2')
673
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400674 self.assertTrue(self.builds[1].hasChanges(A))
675 self.assertTrue(self.builds[2].hasChanges(A))
676 self.assertTrue(self.builds[2].hasChanges(B))
677 self.assertTrue(self.builds[4].hasChanges(A))
678 self.assertFalse(self.builds[4].hasChanges(B))
679 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700680
681 # Finish running all passing jobs for change A
682 self.release(self.builds[1])
683 self.waitUntilSettled()
684 # Fail and report change A
685 self.release(self.builds[0])
686 self.waitUntilSettled()
687
688 # restart of B,C after A failure
James E. Blair08d19992016-08-10 15:25:31 -0700689 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700690 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700691 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700692 self.waitUntilSettled()
693
694 self.assertEqual(len(self.builds), 4)
695 self.assertEqual(self.builds[0].name, 'project-test1') # B
696 self.assertEqual(self.builds[1].name, 'project-test2') # B
697 self.assertEqual(self.builds[2].name, 'project-test1') # C
698 self.assertEqual(self.builds[3].name, 'project-test2') # C
699
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400700 self.assertFalse(self.builds[1].hasChanges(A))
701 self.assertTrue(self.builds[1].hasChanges(B))
702 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700703
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400704 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700705 # After A failed and B and C restarted, B should be back in
706 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400707 self.assertTrue(self.builds[2].hasChanges(B))
708 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700709
James E. Blair08d19992016-08-10 15:25:31 -0700710 self.launch_server.hold_jobs_in_build = False
711 self.launch_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700712 self.waitUntilSettled()
713
714 self.assertEqual(len(self.builds), 0)
715 self.assertEqual(len(self.history), 21)
716 self.assertEqual(A.data['status'], 'NEW')
717 self.assertEqual(B.data['status'], 'NEW')
718 self.assertEqual(C.data['status'], 'MERGED')
719 self.assertEqual(A.reported, 2)
720 self.assertEqual(B.reported, 2)
721 self.assertEqual(C.reported, 2)
722
James E. Blair8c803f82012-07-31 16:25:42 -0700723 def test_patch_order(self):
724 "Test that dependent patches are tested in the right order"
725 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
726 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
727 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700728 A.addApproval('code-review', 2)
729 B.addApproval('code-review', 2)
730 C.addApproval('code-review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700731
732 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
733 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
734 M2.setMerged()
735 M1.setMerged()
736
737 # C -> B -> A -> M1 -> M2
738 # M2 is here to make sure it is never queried. If it is, it
739 # means zuul is walking down the entire history of merged
740 # changes.
741
742 C.setDependsOn(B, 1)
743 B.setDependsOn(A, 1)
744 A.setDependsOn(M1, 1)
745 M1.setDependsOn(M2, 1)
746
James E. Blair8b5408c2016-08-08 15:37:46 -0700747 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700748
749 self.waitUntilSettled()
750
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400751 self.assertEqual(A.data['status'], 'NEW')
752 self.assertEqual(B.data['status'], 'NEW')
753 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700754
James E. Blair8b5408c2016-08-08 15:37:46 -0700755 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
756 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700757
758 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400759 self.assertEqual(M2.queried, 0)
760 self.assertEqual(A.data['status'], 'MERGED')
761 self.assertEqual(B.data['status'], 'MERGED')
762 self.assertEqual(C.data['status'], 'MERGED')
763 self.assertEqual(A.reported, 2)
764 self.assertEqual(B.reported, 2)
765 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700766
James E. Blair063672f2015-01-29 13:09:12 -0800767 def test_needed_changes_enqueue(self):
768 "Test that a needed change is enqueued ahead"
769 # A Given a git tree like this, if we enqueue
770 # / \ change C, we should walk up and down the tree
771 # B G and enqueue changes in the order ABCDEFG.
772 # /|\ This is also the order that you would get if
773 # *C E F you enqueued changes in the order ABCDEFG, so
774 # / the ordering is stable across re-enqueue events.
775 # D
776
777 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
778 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
779 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
780 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
781 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
782 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
783 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
784 B.setDependsOn(A, 1)
785 C.setDependsOn(B, 1)
786 D.setDependsOn(C, 1)
787 E.setDependsOn(B, 1)
788 F.setDependsOn(B, 1)
789 G.setDependsOn(A, 1)
790
James E. Blair8b5408c2016-08-08 15:37:46 -0700791 A.addApproval('code-review', 2)
792 B.addApproval('code-review', 2)
793 C.addApproval('code-review', 2)
794 D.addApproval('code-review', 2)
795 E.addApproval('code-review', 2)
796 F.addApproval('code-review', 2)
797 G.addApproval('code-review', 2)
798 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800799
800 self.waitUntilSettled()
801
802 self.assertEqual(A.data['status'], 'NEW')
803 self.assertEqual(B.data['status'], 'NEW')
804 self.assertEqual(C.data['status'], 'NEW')
805 self.assertEqual(D.data['status'], 'NEW')
806 self.assertEqual(E.data['status'], 'NEW')
807 self.assertEqual(F.data['status'], 'NEW')
808 self.assertEqual(G.data['status'], 'NEW')
809
810 # We're about to add approvals to changes without adding the
811 # triggering events to Zuul, so that we can be sure that it is
812 # enqueing the changes based on dependencies, not because of
813 # triggering events. Since it will have the changes cached
814 # already (without approvals), we need to clear the cache
815 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400816 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100817 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800818
James E. Blair08d19992016-08-10 15:25:31 -0700819 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -0700820 A.addApproval('approved', 1)
821 B.addApproval('approved', 1)
822 D.addApproval('approved', 1)
823 E.addApproval('approved', 1)
824 F.addApproval('approved', 1)
825 G.addApproval('approved', 1)
826 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800827
828 for x in range(8):
James E. Blair08d19992016-08-10 15:25:31 -0700829 self.launch_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800830 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700831 self.launch_server.hold_jobs_in_build = False
832 self.launch_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800833 self.waitUntilSettled()
834
835 self.assertEqual(A.data['status'], 'MERGED')
836 self.assertEqual(B.data['status'], 'MERGED')
837 self.assertEqual(C.data['status'], 'MERGED')
838 self.assertEqual(D.data['status'], 'MERGED')
839 self.assertEqual(E.data['status'], 'MERGED')
840 self.assertEqual(F.data['status'], 'MERGED')
841 self.assertEqual(G.data['status'], 'MERGED')
842 self.assertEqual(A.reported, 2)
843 self.assertEqual(B.reported, 2)
844 self.assertEqual(C.reported, 2)
845 self.assertEqual(D.reported, 2)
846 self.assertEqual(E.reported, 2)
847 self.assertEqual(F.reported, 2)
848 self.assertEqual(G.reported, 2)
849 self.assertEqual(self.history[6].changes,
850 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
851
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100852 def test_source_cache(self):
853 "Test that the source cache operates correctly"
James E. Blair08d19992016-08-10 15:25:31 -0700854 self.launch_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700855
856 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
857 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
858 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
James E. Blair8b5408c2016-08-08 15:37:46 -0700859 A.addApproval('code-review', 2)
860 B.addApproval('code-review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700861
862 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
863 M1.setMerged()
864
865 B.setDependsOn(A, 1)
866 A.setDependsOn(M1, 1)
867
James E. Blair8b5408c2016-08-08 15:37:46 -0700868 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700869 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
870
871 self.waitUntilSettled()
872
873 for build in self.builds:
874 if build.parameters['ZUUL_PIPELINE'] == 'check':
875 build.release()
876 self.waitUntilSettled()
877 for build in self.builds:
878 if build.parameters['ZUUL_PIPELINE'] == 'check':
879 build.release()
880 self.waitUntilSettled()
881
James E. Blair8b5408c2016-08-08 15:37:46 -0700882 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700883 self.waitUntilSettled()
884
Joshua Hesketh352264b2015-08-11 23:42:08 +1000885 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700886 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000887 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700888
James E. Blair08d19992016-08-10 15:25:31 -0700889 self.launch_server.hold_jobs_in_build = False
890 self.launch_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700891 self.waitUntilSettled()
892
893 self.assertEqual(A.data['status'], 'MERGED')
894 self.assertEqual(B.data['status'], 'MERGED')
895 self.assertEqual(A.queried, 2) # Initial and isMerged
896 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
897
James E. Blair8c803f82012-07-31 16:25:42 -0700898 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700899 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700900 # TODO: move to test_gerrit (this is a unit test!)
901 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400902 tenant = self.sched.abide.tenants.get('tenant-one')
903 source = tenant.layout.pipelines['gate'].source
904
905 # TODO(pabelanger): As we add more source / trigger APIs we should make
906 # it easier for users to create events for testing.
907 event = zuul.model.TriggerEvent()
908 event.trigger_name = 'gerrit'
909 event.change_number = '1'
910 event.patch_number = '2'
911
912 a = source.getChange(event)
913 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700914 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700915
James E. Blair8b5408c2016-08-08 15:37:46 -0700916 A.addApproval('code-review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400917 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700918 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700919
James E. Blair8b5408c2016-08-08 15:37:46 -0700920 A.addApproval('approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400921 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700922 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700923
James E. Blairec056492016-07-22 09:45:56 -0700924 @skip("Disabled for early v3 development")
James E. Blair973721f2012-08-15 10:19:43 -0700925 def test_build_configuration_conflict(self):
926 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700927
928 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -0700929 A = self.fake_gerrit.addFakeChange('org/conflict-project',
930 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -0700931 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700932 B = self.fake_gerrit.addFakeChange('org/conflict-project',
933 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -0700934 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700935 C = self.fake_gerrit.addFakeChange('org/conflict-project',
936 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700937 A.addApproval('code-review', 2)
938 B.addApproval('code-review', 2)
939 C.addApproval('code-review', 2)
940 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
941 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
942 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700943 self.waitUntilSettled()
944
James E. Blair6736beb2013-07-11 15:18:15 -0700945 self.assertEqual(A.reported, 1)
946 self.assertEqual(B.reported, 1)
947 self.assertEqual(C.reported, 1)
948
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700949 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700950 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700951 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700952 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700953 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700954 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700955
956 self.assertEqual(len(self.history), 2) # A and C merge jobs
957
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700958 self.gearman_server.hold_jobs_in_queue = False
959 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700960 self.waitUntilSettled()
961
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400962 self.assertEqual(A.data['status'], 'MERGED')
963 self.assertEqual(B.data['status'], 'NEW')
964 self.assertEqual(C.data['status'], 'MERGED')
965 self.assertEqual(A.reported, 2)
966 self.assertEqual(B.reported, 2)
967 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -0700968 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -0700969
James E. Blairdaabed22012-08-15 15:38:57 -0700970 def test_post(self):
971 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700972
Zhongyue Luo5d556072012-09-21 02:00:47 +0900973 e = {
974 "type": "ref-updated",
975 "submitter": {
976 "name": "User Name",
977 },
978 "refUpdate": {
979 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
980 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
981 "refName": "master",
982 "project": "org/project",
983 }
984 }
James E. Blairdaabed22012-08-15 15:38:57 -0700985 self.fake_gerrit.addEvent(e)
986 self.waitUntilSettled()
987
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400988 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400989 self.assertEqual(len(self.history), 1)
990 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -0700991
K Jonathan Harkerf95e7232015-04-29 13:33:16 -0700992 def test_post_ignore_deletes(self):
993 "Test that deleting refs does not trigger post jobs"
994
995 e = {
996 "type": "ref-updated",
997 "submitter": {
998 "name": "User Name",
999 },
1000 "refUpdate": {
1001 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1002 "newRev": "0000000000000000000000000000000000000000",
1003 "refName": "master",
1004 "project": "org/project",
1005 }
1006 }
1007 self.fake_gerrit.addEvent(e)
1008 self.waitUntilSettled()
1009
1010 job_names = [x.name for x in self.history]
1011 self.assertEqual(len(self.history), 0)
1012 self.assertNotIn('project-post', job_names)
1013
1014 def test_post_ignore_deletes_negative(self):
1015 "Test that deleting refs does trigger post jobs"
1016
Adam Gandelmanc5e4f1d2016-11-29 14:27:17 -08001017 self.updateConfigLayout('layout-dont-ignore-ref-deletes')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001018 self.sched.reconfigure(self.config)
1019
1020 e = {
1021 "type": "ref-updated",
1022 "submitter": {
1023 "name": "User Name",
1024 },
1025 "refUpdate": {
1026 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1027 "newRev": "0000000000000000000000000000000000000000",
1028 "refName": "master",
1029 "project": "org/project",
1030 }
1031 }
1032 self.fake_gerrit.addEvent(e)
1033 self.waitUntilSettled()
1034
1035 job_names = [x.name for x in self.history]
1036 self.assertEqual(len(self.history), 1)
1037 self.assertIn('project-post', job_names)
1038
James E. Blairec056492016-07-22 09:45:56 -07001039 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001040 def test_build_configuration_branch_interaction(self):
1041 "Test that switching between branches works"
1042 self.test_build_configuration()
1043 self.test_build_configuration_branch()
1044 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001045 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001046 repo = git.Repo(path)
1047 repo.heads.master.commit = repo.commit('init')
1048 self.test_build_configuration()
1049
James E. Blaircaec0c52012-08-22 14:52:22 -07001050 def test_dependent_changes_dequeue(self):
1051 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001052
James E. Blaircaec0c52012-08-22 14:52:22 -07001053 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1054 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1055 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001056 A.addApproval('code-review', 2)
1057 B.addApproval('code-review', 2)
1058 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001059
1060 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1061 M1.setMerged()
1062
1063 # C -> B -> A -> M1
1064
1065 C.setDependsOn(B, 1)
1066 B.setDependsOn(A, 1)
1067 A.setDependsOn(M1, 1)
1068
James E. Blair08d19992016-08-10 15:25:31 -07001069 self.launch_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001070
James E. Blair8b5408c2016-08-08 15:37:46 -07001071 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1072 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1073 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001074
1075 self.waitUntilSettled()
1076
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001077 self.assertEqual(A.data['status'], 'NEW')
1078 self.assertEqual(A.reported, 2)
1079 self.assertEqual(B.data['status'], 'NEW')
1080 self.assertEqual(B.reported, 2)
1081 self.assertEqual(C.data['status'], 'NEW')
1082 self.assertEqual(C.reported, 2)
1083 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001084
James E. Blair972e3c72013-08-29 12:04:55 -07001085 def test_failing_dependent_changes(self):
1086 "Test that failing dependent patches are taken out of stream"
James E. Blair08d19992016-08-10 15:25:31 -07001087 self.launch_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001088 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1089 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1090 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1091 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1092 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001093 A.addApproval('code-review', 2)
1094 B.addApproval('code-review', 2)
1095 C.addApproval('code-review', 2)
1096 D.addApproval('code-review', 2)
1097 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001098
1099 # E, D -> C -> B, A
1100
1101 D.setDependsOn(C, 1)
1102 C.setDependsOn(B, 1)
1103
James E. Blair08d19992016-08-10 15:25:31 -07001104 self.launch_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001105
James E. Blair8b5408c2016-08-08 15:37:46 -07001106 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1107 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1108 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1109 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1110 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001111
1112 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001113 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001114 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001115 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001116 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001117 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001118 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001119 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001120 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001121 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001122 self.waitUntilSettled()
1123
James E. Blair08d19992016-08-10 15:25:31 -07001124 self.launch_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001125 for build in self.builds:
1126 if build.parameters['ZUUL_CHANGE'] != '1':
1127 build.release()
1128 self.waitUntilSettled()
1129
James E. Blair08d19992016-08-10 15:25:31 -07001130 self.launch_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001131 self.waitUntilSettled()
1132
1133 self.assertEqual(A.data['status'], 'MERGED')
1134 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001135 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001136 self.assertEqual(B.data['status'], 'NEW')
1137 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001138 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001139 self.assertEqual(C.data['status'], 'NEW')
1140 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001141 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001142 self.assertEqual(D.data['status'], 'NEW')
1143 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001144 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001145 self.assertEqual(E.data['status'], 'MERGED')
1146 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001147 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001148 self.assertEqual(len(self.history), 18)
1149
James E. Blairec590122012-08-22 15:19:31 -07001150 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001151 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001152 # If it's dequeued more than once, we should see extra
1153 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001154
James E. Blair08d19992016-08-10 15:25:31 -07001155 self.launch_server.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001156 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1157 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1158 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001159 A.addApproval('code-review', 2)
1160 B.addApproval('code-review', 2)
1161 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001162
Paul Belanger58bf6912016-11-11 19:36:01 -05001163 self.launch_server.failJob('project-test1', A)
1164 self.launch_server.failJob('project-test2', A)
James E. Blair08d19992016-08-10 15:25:31 -07001165 self.launch_server.failJob('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001166
James E. Blair8b5408c2016-08-08 15:37:46 -07001167 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1168 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1169 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001170
1171 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001172
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001173 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001174 self.assertEqual(self.builds[0].name, 'project-merge')
1175 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001176
James E. Blair08d19992016-08-10 15:25:31 -07001177 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001178 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001179 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001180 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001181 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001182 self.waitUntilSettled()
1183
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001184 self.assertEqual(len(self.builds), 9)
Paul Belanger58bf6912016-11-11 19:36:01 -05001185 self.assertEqual(self.builds[0].name, 'project-test1')
1186 self.assertEqual(self.builds[1].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001187 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001188 self.assertEqual(self.builds[3].name, 'project-test1')
1189 self.assertEqual(self.builds[4].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001190 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001191 self.assertEqual(self.builds[6].name, 'project-test1')
1192 self.assertEqual(self.builds[7].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001193 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001194
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001195 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001196 self.waitUntilSettled()
1197
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001198 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1199 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001200
James E. Blair08d19992016-08-10 15:25:31 -07001201 self.launch_server.hold_jobs_in_build = False
1202 self.launch_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001203 self.waitUntilSettled()
1204
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001205 self.assertEqual(len(self.builds), 0)
1206 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001207
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001208 self.assertEqual(A.data['status'], 'NEW')
1209 self.assertEqual(B.data['status'], 'MERGED')
1210 self.assertEqual(C.data['status'], 'MERGED')
1211 self.assertEqual(A.reported, 2)
1212 self.assertEqual(B.reported, 2)
1213 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001214
1215 def test_nonvoting_job(self):
1216 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001217
James E. Blair4ec821f2012-08-23 15:28:28 -07001218 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1219 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001220 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07001221 self.launch_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001222 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001223
1224 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001225
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001226 self.assertEqual(A.data['status'], 'MERGED')
1227 self.assertEqual(A.reported, 2)
1228 self.assertEqual(
1229 self.getJobFromHistory('nonvoting-project-merge').result,
1230 'SUCCESS')
1231 self.assertEqual(
1232 self.getJobFromHistory('nonvoting-project-test1').result,
1233 'SUCCESS')
1234 self.assertEqual(
1235 self.getJobFromHistory('nonvoting-project-test2').result,
1236 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001237
James E. Blair5821bd92015-09-16 08:48:15 -07001238 for build in self.builds:
1239 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1240
James E. Blaire0487072012-08-29 17:38:31 -07001241 def test_check_queue_success(self):
1242 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001243
James E. Blaire0487072012-08-29 17:38:31 -07001244 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1245 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1246
1247 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001248
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001249 self.assertEqual(A.data['status'], 'NEW')
1250 self.assertEqual(A.reported, 1)
1251 self.assertEqual(self.getJobFromHistory('project-merge').result,
1252 'SUCCESS')
1253 self.assertEqual(self.getJobFromHistory('project-test1').result,
1254 'SUCCESS')
1255 self.assertEqual(self.getJobFromHistory('project-test2').result,
1256 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001257
1258 def test_check_queue_failure(self):
1259 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001260
James E. Blaire0487072012-08-29 17:38:31 -07001261 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair08d19992016-08-10 15:25:31 -07001262 self.launch_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001263 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1264
1265 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001266
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001267 self.assertEqual(A.data['status'], 'NEW')
1268 self.assertEqual(A.reported, 1)
1269 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001270 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001271 self.assertEqual(self.getJobFromHistory('project-test1').result,
1272 'SUCCESS')
1273 self.assertEqual(self.getJobFromHistory('project-test2').result,
1274 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001275
James E. Blairec056492016-07-22 09:45:56 -07001276 @skip("Disabled for early v3 development")
James E. Blair127bc182012-08-28 15:55:15 -07001277 def test_dependent_behind_dequeue(self):
1278 "test that dependent changes behind dequeued changes work"
1279 # This complicated test is a reproduction of a real life bug
1280 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001281
James E. Blair08d19992016-08-10 15:25:31 -07001282 self.launch_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001283 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1284 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1285 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1286 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1287 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1288 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1289 D.setDependsOn(C, 1)
1290 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001291 A.addApproval('code-review', 2)
1292 B.addApproval('code-review', 2)
1293 C.addApproval('code-review', 2)
1294 D.addApproval('code-review', 2)
1295 E.addApproval('code-review', 2)
1296 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001297
1298 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001299
1300 # Change object re-use in the gerrit trigger is hidden if
1301 # changes are added in quick succession; waiting makes it more
1302 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001303 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001304 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001305 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001306 self.waitUntilSettled()
1307
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()
James E. Blair08d19992016-08-10 15:25:31 -07001310 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001311 self.waitUntilSettled()
1312
James E. Blair8b5408c2016-08-08 15:37:46 -07001313 self.fake_gerrit.addEvent(C.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(D.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(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001318 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001319 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001320 self.waitUntilSettled()
1321
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()
James E. Blair08d19992016-08-10 15:25:31 -07001328 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001329 self.waitUntilSettled()
1330
1331 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001332
1333 # Grab pointers to the jobs we want to release before
1334 # releasing any, because list indexes may change as
1335 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001336 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001337 a.release()
1338 b.release()
1339 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001340 self.waitUntilSettled()
1341
James E. Blair08d19992016-08-10 15:25:31 -07001342 self.launch_server.hold_jobs_in_build = False
1343 self.launch_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001344 self.waitUntilSettled()
1345
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001346 self.assertEqual(A.data['status'], 'NEW')
1347 self.assertEqual(B.data['status'], 'MERGED')
1348 self.assertEqual(C.data['status'], 'MERGED')
1349 self.assertEqual(D.data['status'], 'MERGED')
1350 self.assertEqual(E.data['status'], 'MERGED')
1351 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001352
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001353 self.assertEqual(A.reported, 2)
1354 self.assertEqual(B.reported, 2)
1355 self.assertEqual(C.reported, 2)
1356 self.assertEqual(D.reported, 2)
1357 self.assertEqual(E.reported, 2)
1358 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001359
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001360 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1361 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001362
1363 def test_merger_repack(self):
1364 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001365
James E. Blair05fed602012-09-07 12:45:24 -07001366 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001367 A.addApproval('code-review', 2)
1368 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001369 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001370 self.assertEqual(self.getJobFromHistory('project-merge').result,
1371 'SUCCESS')
1372 self.assertEqual(self.getJobFromHistory('project-test1').result,
1373 'SUCCESS')
1374 self.assertEqual(self.getJobFromHistory('project-test2').result,
1375 'SUCCESS')
1376 self.assertEqual(A.data['status'], 'MERGED')
1377 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001378 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001379 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001380
Monty Taylorbc758832013-06-17 17:22:42 -04001381 path = os.path.join(self.git_root, "org/project")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001382 print(repack_repo(path))
James E. Blair05fed602012-09-07 12:45:24 -07001383
1384 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001385 A.addApproval('code-review', 2)
1386 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001387 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001388 self.assertEqual(self.getJobFromHistory('project-merge').result,
1389 'SUCCESS')
1390 self.assertEqual(self.getJobFromHistory('project-test1').result,
1391 'SUCCESS')
1392 self.assertEqual(self.getJobFromHistory('project-test2').result,
1393 'SUCCESS')
1394 self.assertEqual(A.data['status'], 'MERGED')
1395 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001396
James E. Blair4886f282012-11-15 09:27:33 -08001397 def test_merger_repack_large_change(self):
1398 "Test that the merger works with large changes after a repack"
1399 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001400 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001401 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Hesketh352264b2015-08-11 23:42:08 +10001402 url = self.fake_gerrit.getGitUrl(
Paul Belanger503bded2016-11-11 19:43:38 -05001403 tenant.layout.project_configs.get('org/project1'))
James E. Blair4076e2b2014-01-28 12:42:20 -08001404 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08001405 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1406 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001407 path = os.path.join(self.upstream_root, "org/project1")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001408 print(repack_repo(path))
Monty Taylorbc758832013-06-17 17:22:42 -04001409 path = os.path.join(self.git_root, "org/project1")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001410 print(repack_repo(path))
James E. Blair4886f282012-11-15 09:27:33 -08001411
James E. Blair8b5408c2016-08-08 15:37:46 -07001412 A.addApproval('code-review', 2)
1413 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001414 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001415 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001416 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001417 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001418 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001419 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001420 'SUCCESS')
1421 self.assertEqual(A.data['status'], 'MERGED')
1422 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001423
James E. Blair2fa50962013-01-30 21:50:41 -08001424 def test_new_patchset_dequeues_old(self):
1425 "Test that a new patchset causes the old to be dequeued"
1426 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair08d19992016-08-10 15:25:31 -07001427 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001428 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1429 M.setMerged()
1430
1431 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1432 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1433 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1434 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001435 A.addApproval('code-review', 2)
1436 B.addApproval('code-review', 2)
1437 C.addApproval('code-review', 2)
1438 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001439
1440 C.setDependsOn(B, 1)
1441 B.setDependsOn(A, 1)
1442 A.setDependsOn(M, 1)
1443
James E. Blair8b5408c2016-08-08 15:37:46 -07001444 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1445 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1446 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1447 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001448 self.waitUntilSettled()
1449
1450 B.addPatchset()
1451 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1452 self.waitUntilSettled()
1453
James E. Blair08d19992016-08-10 15:25:31 -07001454 self.launch_server.hold_jobs_in_build = False
1455 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001456 self.waitUntilSettled()
1457
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001458 self.assertEqual(A.data['status'], 'MERGED')
1459 self.assertEqual(A.reported, 2)
1460 self.assertEqual(B.data['status'], 'NEW')
1461 self.assertEqual(B.reported, 2)
1462 self.assertEqual(C.data['status'], 'NEW')
1463 self.assertEqual(C.reported, 2)
1464 self.assertEqual(D.data['status'], 'MERGED')
1465 self.assertEqual(D.reported, 2)
1466 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001467
James E. Blairba437362015-02-07 11:41:52 -08001468 def test_new_patchset_check(self):
1469 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001470
James E. Blair08d19992016-08-10 15:25:31 -07001471 self.launch_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001472
1473 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001474 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001475 tenant = self.sched.abide.tenants.get('tenant-one')
1476 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001477
1478 # Add two git-dependent changes
1479 B.setDependsOn(A, 1)
1480 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1481 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001482 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1483 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001484
1485 # A live item, and a non-live/live pair
1486 items = check_pipeline.getAllItems()
1487 self.assertEqual(len(items), 3)
1488
1489 self.assertEqual(items[0].change.number, '1')
1490 self.assertEqual(items[0].change.patchset, '1')
1491 self.assertFalse(items[0].live)
1492
1493 self.assertEqual(items[1].change.number, '2')
1494 self.assertEqual(items[1].change.patchset, '1')
1495 self.assertTrue(items[1].live)
1496
1497 self.assertEqual(items[2].change.number, '1')
1498 self.assertEqual(items[2].change.patchset, '1')
1499 self.assertTrue(items[2].live)
1500
1501 # Add a new patchset to A
1502 A.addPatchset()
1503 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1504 self.waitUntilSettled()
1505
1506 # The live copy of A,1 should be gone, but the non-live and B
1507 # should continue, and we should have a new A,2
1508 items = check_pipeline.getAllItems()
1509 self.assertEqual(len(items), 3)
1510
1511 self.assertEqual(items[0].change.number, '1')
1512 self.assertEqual(items[0].change.patchset, '1')
1513 self.assertFalse(items[0].live)
1514
1515 self.assertEqual(items[1].change.number, '2')
1516 self.assertEqual(items[1].change.patchset, '1')
1517 self.assertTrue(items[1].live)
1518
1519 self.assertEqual(items[2].change.number, '1')
1520 self.assertEqual(items[2].change.patchset, '2')
1521 self.assertTrue(items[2].live)
1522
1523 # Add a new patchset to B
1524 B.addPatchset()
1525 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1526 self.waitUntilSettled()
1527
1528 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1529 # but we should have a new B,2 (still based on A,1)
1530 items = check_pipeline.getAllItems()
1531 self.assertEqual(len(items), 3)
1532
1533 self.assertEqual(items[0].change.number, '1')
1534 self.assertEqual(items[0].change.patchset, '2')
1535 self.assertTrue(items[0].live)
1536
1537 self.assertEqual(items[1].change.number, '1')
1538 self.assertEqual(items[1].change.patchset, '1')
1539 self.assertFalse(items[1].live)
1540
1541 self.assertEqual(items[2].change.number, '2')
1542 self.assertEqual(items[2].change.patchset, '2')
1543 self.assertTrue(items[2].live)
1544
1545 self.builds[0].release()
1546 self.waitUntilSettled()
1547 self.builds[0].release()
1548 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001549 self.launch_server.hold_jobs_in_build = False
1550 self.launch_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001551 self.waitUntilSettled()
1552
1553 self.assertEqual(A.reported, 1)
1554 self.assertEqual(B.reported, 1)
1555 self.assertEqual(self.history[0].result, 'ABORTED')
1556 self.assertEqual(self.history[0].changes, '1,1')
1557 self.assertEqual(self.history[1].result, 'ABORTED')
1558 self.assertEqual(self.history[1].changes, '1,1 2,1')
1559 self.assertEqual(self.history[2].result, 'SUCCESS')
1560 self.assertEqual(self.history[2].changes, '1,2')
1561 self.assertEqual(self.history[3].result, 'SUCCESS')
1562 self.assertEqual(self.history[3].changes, '1,1 2,2')
1563
1564 def test_abandoned_gate(self):
1565 "Test that an abandoned change is dequeued from gate"
1566
James E. Blair08d19992016-08-10 15:25:31 -07001567 self.launch_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001568
1569 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001570 A.addApproval('code-review', 2)
1571 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001572 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001573 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1574 self.assertEqual(self.builds[0].name, 'project-merge')
1575
1576 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1577 self.waitUntilSettled()
1578
James E. Blair08d19992016-08-10 15:25:31 -07001579 self.launch_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001580 self.waitUntilSettled()
1581
Arie5b4048c2016-10-05 16:12:06 +03001582 self.assertBuilds([])
1583 self.assertHistory([
1584 dict(name='project-merge', result='ABORTED', changes='1,1')],
1585 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001586 self.assertEqual(A.reported, 1,
1587 "Abandoned gate change should report only start")
1588
1589 def test_abandoned_check(self):
1590 "Test that an abandoned change is dequeued from check"
1591
James E. Blair08d19992016-08-10 15:25:31 -07001592 self.launch_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001593
1594 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1595 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001596 tenant = self.sched.abide.tenants.get('tenant-one')
1597 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001598
1599 # Add two git-dependent changes
1600 B.setDependsOn(A, 1)
1601 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1602 self.waitUntilSettled()
1603 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1604 self.waitUntilSettled()
1605 # A live item, and a non-live/live pair
1606 items = check_pipeline.getAllItems()
1607 self.assertEqual(len(items), 3)
1608
1609 self.assertEqual(items[0].change.number, '1')
1610 self.assertFalse(items[0].live)
1611
1612 self.assertEqual(items[1].change.number, '2')
1613 self.assertTrue(items[1].live)
1614
1615 self.assertEqual(items[2].change.number, '1')
1616 self.assertTrue(items[2].live)
1617
1618 # Abandon A
1619 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1620 self.waitUntilSettled()
1621
1622 # The live copy of A should be gone, but the non-live and B
1623 # should continue
1624 items = check_pipeline.getAllItems()
1625 self.assertEqual(len(items), 2)
1626
1627 self.assertEqual(items[0].change.number, '1')
1628 self.assertFalse(items[0].live)
1629
1630 self.assertEqual(items[1].change.number, '2')
1631 self.assertTrue(items[1].live)
1632
James E. Blair08d19992016-08-10 15:25:31 -07001633 self.launch_server.hold_jobs_in_build = False
1634 self.launch_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001635 self.waitUntilSettled()
1636
1637 self.assertEqual(len(self.history), 4)
1638 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001639 'Build should have been aborted')
1640 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001641 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001642
James E. Blairec056492016-07-22 09:45:56 -07001643 @skip("Disabled for early v3 development")
Steve Varnau7b78b312015-04-03 14:49:46 -07001644 def test_abandoned_not_timer(self):
1645 "Test that an abandoned change does not cancel timer jobs"
1646
James E. Blair08d19992016-08-10 15:25:31 -07001647 self.launch_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001648
1649 # Start timer trigger - also org/project
James E. Blairf84026c2015-12-08 16:11:46 -08001650 self.updateConfigLayout(
1651 'tests/fixtures/layout-idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001652 self.sched.reconfigure(self.config)
1653 self.registerJobs()
1654 # The pipeline triggers every second, so we should have seen
1655 # several by now.
1656 time.sleep(5)
1657 self.waitUntilSettled()
1658 # Stop queuing timer triggered jobs so that the assertions
1659 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08001660 self.updateConfigLayout(
1661 'tests/fixtures/layout-no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001662 self.sched.reconfigure(self.config)
1663 self.registerJobs()
1664 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1665
1666 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1667 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1668 self.waitUntilSettled()
1669 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1670
1671 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1672 self.waitUntilSettled()
1673
1674 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1675
James E. Blair08d19992016-08-10 15:25:31 -07001676 self.launch_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001677 self.waitUntilSettled()
1678
Arx Cruzb1b010d2013-10-28 19:49:59 -02001679 def test_zuul_url_return(self):
1680 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001681 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
James E. Blair08d19992016-08-10 15:25:31 -07001682 self.launch_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001683
1684 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001685 A.addApproval('code-review', 2)
1686 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001687 self.waitUntilSettled()
1688
1689 self.assertEqual(len(self.builds), 1)
1690 for build in self.builds:
1691 self.assertTrue('ZUUL_URL' in build.parameters)
1692
James E. Blair08d19992016-08-10 15:25:31 -07001693 self.launch_server.hold_jobs_in_build = False
1694 self.launch_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001695 self.waitUntilSettled()
1696
James E. Blair2fa50962013-01-30 21:50:41 -08001697 def test_new_patchset_dequeues_old_on_head(self):
1698 "Test that a new patchset causes the old to be dequeued (at head)"
1699 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair08d19992016-08-10 15:25:31 -07001700 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001701 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1702 M.setMerged()
1703 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1704 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1705 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1706 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001707 A.addApproval('code-review', 2)
1708 B.addApproval('code-review', 2)
1709 C.addApproval('code-review', 2)
1710 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001711
1712 C.setDependsOn(B, 1)
1713 B.setDependsOn(A, 1)
1714 A.setDependsOn(M, 1)
1715
James E. Blair8b5408c2016-08-08 15:37:46 -07001716 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1717 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1718 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1719 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001720 self.waitUntilSettled()
1721
1722 A.addPatchset()
1723 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1724 self.waitUntilSettled()
1725
James E. Blair08d19992016-08-10 15:25:31 -07001726 self.launch_server.hold_jobs_in_build = False
1727 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001728 self.waitUntilSettled()
1729
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001730 self.assertEqual(A.data['status'], 'NEW')
1731 self.assertEqual(A.reported, 2)
1732 self.assertEqual(B.data['status'], 'NEW')
1733 self.assertEqual(B.reported, 2)
1734 self.assertEqual(C.data['status'], 'NEW')
1735 self.assertEqual(C.reported, 2)
1736 self.assertEqual(D.data['status'], 'MERGED')
1737 self.assertEqual(D.reported, 2)
1738 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001739
1740 def test_new_patchset_dequeues_old_without_dependents(self):
1741 "Test that a new patchset causes only the old to be dequeued"
James E. Blair08d19992016-08-10 15:25:31 -07001742 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001743 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1744 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1745 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001746 A.addApproval('code-review', 2)
1747 B.addApproval('code-review', 2)
1748 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001749
James E. Blair8b5408c2016-08-08 15:37:46 -07001750 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1751 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1752 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001753 self.waitUntilSettled()
1754
1755 B.addPatchset()
1756 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1757 self.waitUntilSettled()
1758
James E. Blair08d19992016-08-10 15:25:31 -07001759 self.launch_server.hold_jobs_in_build = False
1760 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001761 self.waitUntilSettled()
1762
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001763 self.assertEqual(A.data['status'], 'MERGED')
1764 self.assertEqual(A.reported, 2)
1765 self.assertEqual(B.data['status'], 'NEW')
1766 self.assertEqual(B.reported, 2)
1767 self.assertEqual(C.data['status'], 'MERGED')
1768 self.assertEqual(C.reported, 2)
1769 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001770
1771 def test_new_patchset_dequeues_old_independent_queue(self):
1772 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair08d19992016-08-10 15:25:31 -07001773 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001774 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1775 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1776 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1777 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1778 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1779 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1780 self.waitUntilSettled()
1781
1782 B.addPatchset()
1783 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1784 self.waitUntilSettled()
1785
James E. Blair08d19992016-08-10 15:25:31 -07001786 self.launch_server.hold_jobs_in_build = False
1787 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001788 self.waitUntilSettled()
1789
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001790 self.assertEqual(A.data['status'], 'NEW')
1791 self.assertEqual(A.reported, 1)
1792 self.assertEqual(B.data['status'], 'NEW')
1793 self.assertEqual(B.reported, 1)
1794 self.assertEqual(C.data['status'], 'NEW')
1795 self.assertEqual(C.reported, 1)
1796 self.assertEqual(len(self.history), 10)
1797 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001798
James E. Blair18c64442014-03-18 10:14:45 -07001799 def test_noop_job(self):
1800 "Test that the internal noop job works"
1801 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001802 A.addApproval('code-review', 2)
1803 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001804 self.waitUntilSettled()
1805
1806 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1807 self.assertTrue(self.sched._areAllBuildsComplete())
1808 self.assertEqual(len(self.history), 0)
1809 self.assertEqual(A.data['status'], 'MERGED')
1810 self.assertEqual(A.reported, 2)
1811
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001812 def test_no_job_project(self):
1813 "Test that reports with no jobs don't get sent"
1814 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1815 'master', 'A')
1816 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1817 self.waitUntilSettled()
1818
1819 # Change wasn't reported to
1820 self.assertEqual(A.reported, False)
1821
1822 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001823 tenant = self.sched.abide.tenants.get('tenant-one')
1824 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001825 items = check_pipeline.getAllItems()
1826 self.assertEqual(len(items), 0)
1827
1828 self.assertEqual(len(self.history), 0)
1829
James E. Blair7d0dedc2013-02-21 17:26:09 -08001830 def test_zuul_refs(self):
1831 "Test that zuul refs exist and have the right changes"
James E. Blair08d19992016-08-10 15:25:31 -07001832 self.launch_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001833 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1834 M1.setMerged()
1835 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1836 M2.setMerged()
1837
1838 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1839 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1840 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1841 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001842 A.addApproval('code-review', 2)
1843 B.addApproval('code-review', 2)
1844 C.addApproval('code-review', 2)
1845 D.addApproval('code-review', 2)
1846 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1847 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1848 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1849 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001850
1851 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001852 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001853 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001854 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001855 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001856 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001857 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001858 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001859 self.waitUntilSettled()
1860
James E. Blair7d0dedc2013-02-21 17:26:09 -08001861 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08001862 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001863 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001864 if x.parameters['ZUUL_CHANGE'] == '3':
1865 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001866 a_build = x
1867 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001868 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001869 b_build = x
1870 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001871 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001872 c_build = x
1873 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001874 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001875 d_build = x
1876 if a_build and b_build and c_build and d_build:
1877 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08001878
1879 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001880 self.assertIsNotNone(a_zref)
1881 self.assertIsNotNone(b_zref)
1882 self.assertIsNotNone(c_zref)
1883 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001884
1885 # And they should all be different
1886 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001887 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001888
Clint Byrum3343e3e2016-11-15 16:05:03 -08001889 # should have a, not b, and should not be in project2
1890 self.assertTrue(a_build.hasChanges(A))
1891 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001892
Clint Byrum3343e3e2016-11-15 16:05:03 -08001893 # should have a and b, and should not be in project2
1894 self.assertTrue(b_build.hasChanges(A, B))
1895 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001896
Clint Byrum3343e3e2016-11-15 16:05:03 -08001897 # should have a and b in 1, c in 2
1898 self.assertTrue(c_build.hasChanges(A, B, C))
1899 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001900
Clint Byrum3343e3e2016-11-15 16:05:03 -08001901 # should have a and b in 1, c and d in 2
1902 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001903
James E. Blair08d19992016-08-10 15:25:31 -07001904 self.launch_server.hold_jobs_in_build = False
1905 self.launch_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08001906 self.waitUntilSettled()
1907
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001908 self.assertEqual(A.data['status'], 'MERGED')
1909 self.assertEqual(A.reported, 2)
1910 self.assertEqual(B.data['status'], 'MERGED')
1911 self.assertEqual(B.reported, 2)
1912 self.assertEqual(C.data['status'], 'MERGED')
1913 self.assertEqual(C.reported, 2)
1914 self.assertEqual(D.data['status'], 'MERGED')
1915 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08001916
James E. Blair4a28a882013-08-23 15:17:33 -07001917 def test_rerun_on_error(self):
1918 "Test that if a worker fails to run a job, it is run again"
James E. Blair08d19992016-08-10 15:25:31 -07001919 self.launch_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07001920 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001921 A.addApproval('code-review', 2)
1922 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07001923 self.waitUntilSettled()
1924
1925 self.builds[0].run_error = True
James E. Blair08d19992016-08-10 15:25:31 -07001926 self.launch_server.hold_jobs_in_build = False
1927 self.launch_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07001928 self.waitUntilSettled()
1929 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
1930 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
1931
James E. Blair412e5582013-04-22 15:50:12 -07001932 def test_statsd(self):
1933 "Test each of the statsd methods used in the scheduler"
1934 import extras
1935 statsd = extras.try_import('statsd.statsd')
1936 statsd.incr('test-incr')
1937 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07001938 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07001939 self.assertReportedStat('test-incr', '1|c')
1940 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07001941 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07001942
James E. Blairec056492016-07-22 09:45:56 -07001943 @skip("Disabled for early v3 development")
James E. Blairdad52252014-02-07 16:59:17 -08001944 def test_stuck_job_cleanup(self):
1945 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07001946 # This job won't be registered at startup because it is not in
1947 # the standard layout, but we need it to already be registerd
1948 # for when we reconfigure, as that is when Zuul will attempt
1949 # to run the new job.
1950 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001951 self.gearman_server.hold_jobs_in_queue = True
1952 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001953 A.addApproval('code-review', 2)
1954 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08001955 self.waitUntilSettled()
1956 self.assertEqual(len(self.gearman_server.getQueue()), 1)
1957
James E. Blairf84026c2015-12-08 16:11:46 -08001958 self.updateConfigLayout(
1959 'tests/fixtures/layout-no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08001960 self.sched.reconfigure(self.config)
1961 self.waitUntilSettled()
1962
James E. Blair18c64442014-03-18 10:14:45 -07001963 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001964 self.waitUntilSettled()
1965 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1966 self.assertTrue(self.sched._areAllBuildsComplete())
1967
1968 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07001969 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001970 self.assertEqual(self.history[0].result, 'SUCCESS')
1971
James E. Blair879dafb2015-07-17 14:04:49 -07001972 def test_file_head(self):
1973 # This is a regression test for an observed bug. A change
1974 # with a file named "HEAD" in the root directory of the repo
1975 # was processed by a merger. It then was unable to reset the
1976 # repo because of:
1977 # GitCommandError: 'git reset --hard HEAD' returned
1978 # with exit code 128
1979 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
1980 # and filename
1981 # Use '--' to separate filenames from revisions'
1982
1983 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04001984 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07001985 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1986
1987 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1988 self.waitUntilSettled()
1989
1990 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1991 self.waitUntilSettled()
1992
1993 self.assertIn('Build succeeded', A.messages[0])
1994 self.assertIn('Build succeeded', B.messages[0])
1995
James E. Blairec056492016-07-22 09:45:56 -07001996 @skip("Disabled for early v3 development")
James E. Blair70c71582013-03-06 08:50:50 -08001997 def test_file_jobs(self):
1998 "Test that file jobs run only when appropriate"
1999 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2000 A.addPatchset(['pip-requires'])
2001 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002002 A.addApproval('code-review', 2)
2003 B.addApproval('code-review', 2)
2004 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2005 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002006 self.waitUntilSettled()
2007
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002008 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002009 if x.name == 'project-testfile']
2010
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002011 self.assertEqual(len(testfile_jobs), 1)
2012 self.assertEqual(testfile_jobs[0].changes, '1,2')
2013 self.assertEqual(A.data['status'], 'MERGED')
2014 self.assertEqual(A.reported, 2)
2015 self.assertEqual(B.data['status'], 'MERGED')
2016 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002017
Clint Byrum3ee39f32016-11-17 23:45:07 -08002018 def _test_irrelevant_files_jobs(self, should_skip):
2019 "Test that jobs with irrelevant-files filter run only when appropriate"
2020 self.updateConfigLayout('layout-irrelevant-files')
Maru Newby3fe5f852015-01-13 04:22:14 +00002021 self.sched.reconfigure(self.config)
Clint Byrum3ee39f32016-11-17 23:45:07 -08002022
2023 if should_skip:
2024 files = {'ignoreme': 'ignored\n'}
2025 else:
2026 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002027
2028 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002029 'master',
2030 'test irrelevant-files',
2031 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002032 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2033 self.waitUntilSettled()
2034
2035 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002036 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002037
2038 if should_skip:
2039 self.assertEqual([], tested_change_ids)
2040 else:
2041 self.assertIn(change.data['number'], tested_change_ids)
2042
Clint Byrum3ee39f32016-11-17 23:45:07 -08002043 def test_irrelevant_files_match_skips_job(self):
2044 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002045
Clint Byrum3ee39f32016-11-17 23:45:07 -08002046 def test_irrelevant_files_no_match_runs_job(self):
2047 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002048
Clint Byrum85493602016-11-18 11:59:47 -08002049 def test_inherited_jobs_keep_matchers(self):
2050 self.updateConfigLayout('layout-inheritance')
2051 self.sched.reconfigure(self.config)
2052
2053 files = {'ignoreme': 'ignored\n'}
2054
2055 change = self.fake_gerrit.addFakeChange('org/project',
2056 'master',
2057 'test irrelevant-files',
2058 files=files)
2059 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2060 self.waitUntilSettled()
2061
2062 run_jobs = set([build.name for build in self.history])
2063
2064 self.assertEqual(set(['project-test-nomatch-starts-empty',
2065 'project-test-nomatch-starts-full']), run_jobs)
2066
James E. Blairec056492016-07-22 09:45:56 -07002067 @skip("Disabled for early v3 development")
James E. Blair3c5e5b52013-04-26 11:17:03 -07002068 def test_test_config(self):
2069 "Test that we can test the config"
James E. Blairf84026c2015-12-08 16:11:46 -08002070 self.sched.testConfig(self.config.get('zuul', 'tenant_config'),
Joshua Hesketh352264b2015-08-11 23:42:08 +10002071 self.connections)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002072
James E. Blairec056492016-07-22 09:45:56 -07002073 @skip("Disabled for early v3 development")
James E. Blairc8a1e052014-02-25 09:29:26 -08002074 def test_queue_names(self):
2075 "Test shared change queue names"
2076 project1 = self.sched.layout.projects['org/project1']
2077 project2 = self.sched.layout.projects['org/project2']
2078 q1 = self.sched.layout.pipelines['gate'].getQueue(project1)
2079 q2 = self.sched.layout.pipelines['gate'].getQueue(project2)
2080 self.assertEqual(q1.name, 'integration')
2081 self.assertEqual(q2.name, 'integration')
2082
James E. Blairf84026c2015-12-08 16:11:46 -08002083 self.updateConfigLayout(
2084 'tests/fixtures/layout-bad-queue.yaml')
James E. Blairc8a1e052014-02-25 09:29:26 -08002085 with testtools.ExpectedException(
2086 Exception, "More than one name assigned to change queue"):
2087 self.sched.reconfigure(self.config)
2088
James E. Blair64ed6f22013-07-10 14:07:23 -07002089 def test_queue_precedence(self):
2090 "Test that queue precedence works"
2091
2092 self.gearman_server.hold_jobs_in_queue = True
James E. Blair08d19992016-08-10 15:25:31 -07002093 self.launch_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002094 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2095 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002096 A.addApproval('code-review', 2)
2097 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002098
2099 self.waitUntilSettled()
2100 self.gearman_server.hold_jobs_in_queue = False
2101 self.gearman_server.release()
2102 self.waitUntilSettled()
2103
James E. Blair8de58bd2013-07-18 16:23:33 -07002104 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002105 self.orderedRelease()
James E. Blair08d19992016-08-10 15:25:31 -07002106 self.launch_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002107 self.waitUntilSettled()
2108
James E. Blair64ed6f22013-07-10 14:07:23 -07002109 self.log.debug(self.history)
2110 self.assertEqual(self.history[0].pipeline, 'gate')
2111 self.assertEqual(self.history[1].pipeline, 'check')
2112 self.assertEqual(self.history[2].pipeline, 'gate')
2113 self.assertEqual(self.history[3].pipeline, 'gate')
2114 self.assertEqual(self.history[4].pipeline, 'check')
2115 self.assertEqual(self.history[5].pipeline, 'check')
2116
James E. Blairec056492016-07-22 09:45:56 -07002117 @skip("Disabled for early v3 development")
Clark Boylana5edbe42014-06-03 16:39:10 -07002118 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002119 "Test that we can retrieve JSON status info"
James E. Blair08d19992016-08-10 15:25:31 -07002120 self.launch_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002121 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002122 A.addApproval('code-review', 2)
2123 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002124 self.waitUntilSettled()
2125
James E. Blair08d19992016-08-10 15:25:31 -07002126 self.launch_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002127 self.waitUntilSettled()
2128
James E. Blair1843a552013-07-03 14:19:52 -07002129 port = self.webapp.server.socket.getsockname()[1]
2130
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002131 req = urllib.request.Request("http://localhost:%s/status.json" % port)
2132 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002133 headers = f.info()
2134 self.assertIn('Content-Length', headers)
2135 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002136 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2137 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002138 self.assertIn('Access-Control-Allow-Origin', headers)
2139 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002140 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002141 self.assertIn('Expires', headers)
James E. Blair1843a552013-07-03 14:19:52 -07002142 data = f.read()
2143
James E. Blair08d19992016-08-10 15:25:31 -07002144 self.launch_server.hold_jobs_in_build = False
2145 self.launch_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002146 self.waitUntilSettled()
2147
2148 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002149 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002150 for p in data['pipelines']:
2151 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002152 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002153 self.assertEqual(q['window'], 20)
2154 else:
2155 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002156 for head in q['heads']:
2157 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002158 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002159 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002160 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002161 status_jobs.append(job)
2162 self.assertEqual('project-merge', status_jobs[0]['name'])
2163 self.assertEqual('https://server/job/project-merge/0/',
2164 status_jobs[0]['url'])
2165 self.assertEqual('http://logs.example.com/1/1/gate/project-merge/0',
2166 status_jobs[0]['report_url'])
2167
2168 self.assertEqual('project-test1', status_jobs[1]['name'])
2169 self.assertEqual('https://server/job/project-test1/1/',
2170 status_jobs[1]['url'])
2171 self.assertEqual('http://logs.example.com/1/1/gate/project-test1/1',
2172 status_jobs[1]['report_url'])
2173
2174 self.assertEqual('project-test2', status_jobs[2]['name'])
2175 self.assertEqual('https://server/job/project-test2/2/',
2176 status_jobs[2]['url'])
2177 self.assertEqual('http://logs.example.com/1/1/gate/project-test2/2',
2178 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002179
James E. Blairec056492016-07-22 09:45:56 -07002180 @skip("Disabled for early v3 development")
James E. Blairc3d428e2013-12-03 15:06:48 -08002181 def test_merging_queues(self):
2182 "Test that transitively-connected change queues are merged"
James E. Blairf84026c2015-12-08 16:11:46 -08002183 self.updateConfigLayout(
2184 'tests/fixtures/layout-merge-queues.yaml')
James E. Blairc3d428e2013-12-03 15:06:48 -08002185 self.sched.reconfigure(self.config)
2186 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
2187
James E. Blairec056492016-07-22 09:45:56 -07002188 @skip("Disabled for early v3 development")
James E. Blairaf17a972016-02-03 15:07:18 -08002189 def test_mutex(self):
2190 "Test job mutexes"
2191 self.config.set('zuul', 'layout_config',
2192 'tests/fixtures/layout-mutex.yaml')
2193 self.sched.reconfigure(self.config)
2194
James E. Blair08d19992016-08-10 15:25:31 -07002195 self.launch_server.hold_jobs_in_build = True
James E. Blairaf17a972016-02-03 15:07:18 -08002196 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2197 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2198 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2199
2200 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2201 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2202 self.waitUntilSettled()
2203 self.assertEqual(len(self.builds), 3)
2204 self.assertEqual(self.builds[0].name, 'project-test1')
2205 self.assertEqual(self.builds[1].name, 'mutex-one')
2206 self.assertEqual(self.builds[2].name, 'project-test1')
2207
James E. Blair08d19992016-08-10 15:25:31 -07002208 self.launch_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002209 self.waitUntilSettled()
2210
2211 self.assertEqual(len(self.builds), 3)
2212 self.assertEqual(self.builds[0].name, 'project-test1')
2213 self.assertEqual(self.builds[1].name, 'project-test1')
2214 self.assertEqual(self.builds[2].name, 'mutex-two')
2215 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2216
James E. Blair08d19992016-08-10 15:25:31 -07002217 self.launch_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002218 self.waitUntilSettled()
2219
2220 self.assertEqual(len(self.builds), 3)
2221 self.assertEqual(self.builds[0].name, 'project-test1')
2222 self.assertEqual(self.builds[1].name, 'project-test1')
2223 self.assertEqual(self.builds[2].name, 'mutex-one')
2224 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2225
James E. Blair08d19992016-08-10 15:25:31 -07002226 self.launch_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002227 self.waitUntilSettled()
2228
2229 self.assertEqual(len(self.builds), 3)
2230 self.assertEqual(self.builds[0].name, 'project-test1')
2231 self.assertEqual(self.builds[1].name, 'project-test1')
2232 self.assertEqual(self.builds[2].name, 'mutex-two')
2233 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2234
James E. Blair08d19992016-08-10 15:25:31 -07002235 self.launch_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002236 self.waitUntilSettled()
2237
2238 self.assertEqual(len(self.builds), 2)
2239 self.assertEqual(self.builds[0].name, 'project-test1')
2240 self.assertEqual(self.builds[1].name, 'project-test1')
2241 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2242
James E. Blair08d19992016-08-10 15:25:31 -07002243 self.launch_server.hold_jobs_in_build = False
2244 self.launch_server.release()
James E. Blairaf17a972016-02-03 15:07:18 -08002245
2246 self.waitUntilSettled()
2247 self.assertEqual(len(self.builds), 0)
2248
2249 self.assertEqual(A.reported, 1)
2250 self.assertEqual(B.reported, 1)
2251 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2252
James E. Blairec056492016-07-22 09:45:56 -07002253 @skip("Disabled for early v3 development")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002254 def test_node_label(self):
2255 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002256 self.worker.registerFunction('build:node-project-test1:debian')
2257
2258 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002259 A.addApproval('code-review', 2)
2260 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002261 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002262
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002263 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2264 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2265 'debian')
2266 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002267
2268 def test_live_reconfiguration(self):
2269 "Test that live reconfiguration works"
James E. Blair08d19992016-08-10 15:25:31 -07002270 self.launch_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002271 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002272 A.addApproval('code-review', 2)
2273 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002274 self.waitUntilSettled()
2275
2276 self.sched.reconfigure(self.config)
2277
James E. Blair08d19992016-08-10 15:25:31 -07002278 self.launch_server.hold_jobs_in_build = False
2279 self.launch_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002280 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002281 self.assertEqual(self.getJobFromHistory('project-merge').result,
2282 'SUCCESS')
2283 self.assertEqual(self.getJobFromHistory('project-test1').result,
2284 'SUCCESS')
2285 self.assertEqual(self.getJobFromHistory('project-test2').result,
2286 'SUCCESS')
2287 self.assertEqual(A.data['status'], 'MERGED')
2288 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002289
James E. Blairec056492016-07-22 09:45:56 -07002290 @skip("Disabled for early v3 development")
James E. Blair6bc782d2015-07-17 16:20:21 -07002291 def test_live_reconfiguration_merge_conflict(self):
2292 # A real-world bug: a change in a gate queue has a merge
2293 # conflict and a job is added to its project while it's
2294 # sitting in the queue. The job gets added to the change and
2295 # enqueued and the change gets stuck.
2296 self.worker.registerFunction('build:project-test3')
James E. Blair08d19992016-08-10 15:25:31 -07002297 self.launch_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002298
2299 # This change is fine. It's here to stop the queue long
2300 # enough for the next change to be subject to the
2301 # reconfiguration, as well as to provide a conflict for the
2302 # next change. This change will succeed and merge.
2303 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2304 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002305 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002306
2307 # This change will be in merge conflict. During the
2308 # reconfiguration, we will add a job. We want to make sure
2309 # that doesn't cause it to get stuck.
2310 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2311 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002312 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002313
James E. Blair8b5408c2016-08-08 15:37:46 -07002314 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2315 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002316
2317 self.waitUntilSettled()
2318
2319 # No jobs have run yet
2320 self.assertEqual(A.data['status'], 'NEW')
2321 self.assertEqual(A.reported, 1)
2322 self.assertEqual(B.data['status'], 'NEW')
2323 self.assertEqual(B.reported, 1)
2324 self.assertEqual(len(self.history), 0)
2325
2326 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002327 self.updateConfigLayout(
2328 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002329 self.sched.reconfigure(self.config)
2330 self.waitUntilSettled()
2331
James E. Blair08d19992016-08-10 15:25:31 -07002332 self.launch_server.hold_jobs_in_build = False
2333 self.launch_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002334 self.waitUntilSettled()
2335
2336 self.assertEqual(A.data['status'], 'MERGED')
2337 self.assertEqual(A.reported, 2)
2338 self.assertEqual(B.data['status'], 'NEW')
2339 self.assertEqual(B.reported, 2)
2340 self.assertEqual(self.getJobFromHistory('project-merge').result,
2341 'SUCCESS')
2342 self.assertEqual(self.getJobFromHistory('project-test1').result,
2343 'SUCCESS')
2344 self.assertEqual(self.getJobFromHistory('project-test2').result,
2345 'SUCCESS')
2346 self.assertEqual(self.getJobFromHistory('project-test3').result,
2347 'SUCCESS')
2348 self.assertEqual(len(self.history), 4)
2349
James E. Blairec056492016-07-22 09:45:56 -07002350 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002351 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002352 # An extrapolation of test_live_reconfiguration_merge_conflict
2353 # that tests a job added to a job tree with a failed root does
2354 # not run.
2355 self.worker.registerFunction('build:project-test3')
James E. Blair08d19992016-08-10 15:25:31 -07002356 self.launch_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002357
2358 # This change is fine. It's here to stop the queue long
2359 # enough for the next change to be subject to the
2360 # reconfiguration. This change will succeed and merge.
2361 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2362 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002363 A.addApproval('code-review', 2)
2364 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002365 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002366 self.launch_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002367 self.waitUntilSettled()
2368
2369 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair08d19992016-08-10 15:25:31 -07002370 self.launch_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002371 B.addApproval('code-review', 2)
2372 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002373 self.waitUntilSettled()
2374
James E. Blair08d19992016-08-10 15:25:31 -07002375 self.launch_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002376 self.waitUntilSettled()
2377
2378 # Both -merge jobs have run, but no others.
2379 self.assertEqual(A.data['status'], 'NEW')
2380 self.assertEqual(A.reported, 1)
2381 self.assertEqual(B.data['status'], 'NEW')
2382 self.assertEqual(B.reported, 1)
2383 self.assertEqual(self.history[0].result, 'SUCCESS')
2384 self.assertEqual(self.history[0].name, 'project-merge')
2385 self.assertEqual(self.history[1].result, 'FAILURE')
2386 self.assertEqual(self.history[1].name, 'project-merge')
2387 self.assertEqual(len(self.history), 2)
2388
2389 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002390 self.updateConfigLayout(
2391 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002392 self.sched.reconfigure(self.config)
2393 self.waitUntilSettled()
2394
James E. Blair08d19992016-08-10 15:25:31 -07002395 self.launch_server.hold_jobs_in_build = False
2396 self.launch_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002397 self.waitUntilSettled()
2398
2399 self.assertEqual(A.data['status'], 'MERGED')
2400 self.assertEqual(A.reported, 2)
2401 self.assertEqual(B.data['status'], 'NEW')
2402 self.assertEqual(B.reported, 2)
2403 self.assertEqual(self.history[0].result, 'SUCCESS')
2404 self.assertEqual(self.history[0].name, 'project-merge')
2405 self.assertEqual(self.history[1].result, 'FAILURE')
2406 self.assertEqual(self.history[1].name, 'project-merge')
2407 self.assertEqual(self.history[2].result, 'SUCCESS')
2408 self.assertEqual(self.history[3].result, 'SUCCESS')
2409 self.assertEqual(self.history[4].result, 'SUCCESS')
2410 self.assertEqual(len(self.history), 5)
2411
James E. Blairec056492016-07-22 09:45:56 -07002412 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002413 def test_live_reconfiguration_failed_job(self):
2414 # Test that a change with a removed failing job does not
2415 # disrupt reconfiguration. If a change has a failed job and
2416 # that job is removed during a reconfiguration, we observed a
2417 # bug where the code to re-set build statuses would run on
2418 # that build and raise an exception because the job no longer
2419 # existed.
James E. Blair08d19992016-08-10 15:25:31 -07002420 self.launch_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002421
2422 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2423
2424 # This change will fail and later be removed by the reconfiguration.
James E. Blair08d19992016-08-10 15:25:31 -07002425 self.launch_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002426
2427 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2428 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002429 self.launch_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002430 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002431 self.launch_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002432 self.waitUntilSettled()
2433
2434 self.assertEqual(A.data['status'], 'NEW')
2435 self.assertEqual(A.reported, 0)
2436
2437 self.assertEqual(self.getJobFromHistory('project-merge').result,
2438 'SUCCESS')
2439 self.assertEqual(self.getJobFromHistory('project-test1').result,
2440 'FAILURE')
2441 self.assertEqual(len(self.history), 2)
2442
2443 # Remove the test1 job.
James E. Blairf84026c2015-12-08 16:11:46 -08002444 self.updateConfigLayout(
2445 'tests/fixtures/layout-live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002446 self.sched.reconfigure(self.config)
2447 self.waitUntilSettled()
2448
James E. Blair08d19992016-08-10 15:25:31 -07002449 self.launch_server.hold_jobs_in_build = False
2450 self.launch_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002451 self.waitUntilSettled()
2452
2453 self.assertEqual(self.getJobFromHistory('project-test2').result,
2454 'SUCCESS')
2455 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2456 'SUCCESS')
2457 self.assertEqual(len(self.history), 4)
2458
2459 self.assertEqual(A.data['status'], 'NEW')
2460 self.assertEqual(A.reported, 1)
2461 self.assertIn('Build succeeded', A.messages[0])
2462 # Ensure the removed job was not included in the report.
2463 self.assertNotIn('project-test1', A.messages[0])
2464
James E. Blairec056492016-07-22 09:45:56 -07002465 @skip("Disabled for early v3 development")
James E. Blairfe707d12015-08-05 15:18:15 -07002466 def test_live_reconfiguration_shared_queue(self):
2467 # Test that a change with a failing job which was removed from
2468 # this project but otherwise still exists in the system does
2469 # not disrupt reconfiguration.
2470
James E. Blair08d19992016-08-10 15:25:31 -07002471 self.launch_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002472
2473 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2474
James E. Blair08d19992016-08-10 15:25:31 -07002475 self.launch_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002476
2477 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2478 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002479 self.launch_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002480 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002481 self.launch_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002482 self.waitUntilSettled()
2483
2484 self.assertEqual(A.data['status'], 'NEW')
2485 self.assertEqual(A.reported, 0)
2486
2487 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2488 'SUCCESS')
2489 self.assertEqual(self.getJobFromHistory(
2490 'project1-project2-integration').result, 'FAILURE')
2491 self.assertEqual(len(self.history), 2)
2492
2493 # Remove the integration job.
James E. Blairf84026c2015-12-08 16:11:46 -08002494 self.updateConfigLayout(
2495 'tests/fixtures/layout-live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002496 self.sched.reconfigure(self.config)
2497 self.waitUntilSettled()
2498
James E. Blair08d19992016-08-10 15:25:31 -07002499 self.launch_server.hold_jobs_in_build = False
2500 self.launch_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002501 self.waitUntilSettled()
2502
2503 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2504 'SUCCESS')
2505 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2506 'SUCCESS')
2507 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2508 'SUCCESS')
2509 self.assertEqual(self.getJobFromHistory(
2510 'project1-project2-integration').result, 'FAILURE')
2511 self.assertEqual(len(self.history), 4)
2512
2513 self.assertEqual(A.data['status'], 'NEW')
2514 self.assertEqual(A.reported, 1)
2515 self.assertIn('Build succeeded', A.messages[0])
2516 # Ensure the removed job was not included in the report.
2517 self.assertNotIn('project1-project2-integration', A.messages[0])
2518
James E. Blairec056492016-07-22 09:45:56 -07002519 @skip("Disabled for early v3 development")
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002520 def test_double_live_reconfiguration_shared_queue(self):
2521 # This was a real-world regression. A change is added to
2522 # gate; a reconfigure happens, a second change which depends
2523 # on the first is added, and a second reconfiguration happens.
2524 # Ensure that both changes merge.
2525
2526 # A failure may indicate incorrect caching or cleaning up of
2527 # references during a reconfiguration.
James E. Blair08d19992016-08-10 15:25:31 -07002528 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002529
2530 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2531 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2532 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002533 A.addApproval('code-review', 2)
2534 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002535
2536 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002537 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002538 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002539 self.launch_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002540 self.waitUntilSettled()
2541
2542 # Reconfigure (with only one change in the pipeline).
2543 self.sched.reconfigure(self.config)
2544 self.waitUntilSettled()
2545
2546 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002547 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002548 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002549 self.launch_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002550 self.waitUntilSettled()
2551
2552 # Reconfigure (with both in the pipeline).
2553 self.sched.reconfigure(self.config)
2554 self.waitUntilSettled()
2555
James E. Blair08d19992016-08-10 15:25:31 -07002556 self.launch_server.hold_jobs_in_build = False
2557 self.launch_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002558 self.waitUntilSettled()
2559
2560 self.assertEqual(len(self.history), 8)
2561
2562 self.assertEqual(A.data['status'], 'MERGED')
2563 self.assertEqual(A.reported, 2)
2564 self.assertEqual(B.data['status'], 'MERGED')
2565 self.assertEqual(B.reported, 2)
2566
James E. Blairec056492016-07-22 09:45:56 -07002567 @skip("Disabled for early v3 development")
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002568 def test_live_reconfiguration_del_project(self):
2569 # Test project deletion from layout
2570 # while changes are enqueued
2571
James E. Blair08d19992016-08-10 15:25:31 -07002572 self.launch_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002573 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2574 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2575 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2576
2577 # A Depends-On: B
2578 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2579 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002580 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002581
2582 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2583 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2584 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002585 self.launch_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002586 self.waitUntilSettled()
2587 self.assertEqual(len(self.builds), 5)
2588
2589 # This layout defines only org/project, not org/project1
James E. Blairf84026c2015-12-08 16:11:46 -08002590 self.updateConfigLayout(
2591 'tests/fixtures/layout-live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002592 self.sched.reconfigure(self.config)
2593 self.waitUntilSettled()
2594
2595 # Builds for C aborted, builds for A succeed,
2596 # and have change B applied ahead
2597 job_c = self.getJobFromHistory('project1-test1')
2598 self.assertEqual(job_c.changes, '3,1')
2599 self.assertEqual(job_c.result, 'ABORTED')
2600
James E. Blair08d19992016-08-10 15:25:31 -07002601 self.launch_server.hold_jobs_in_build = False
2602 self.launch_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002603 self.waitUntilSettled()
2604
2605 self.assertEqual(self.getJobFromHistory('project-test1').changes,
2606 '2,1 1,1')
2607
2608 self.assertEqual(A.data['status'], 'NEW')
2609 self.assertEqual(B.data['status'], 'NEW')
2610 self.assertEqual(C.data['status'], 'NEW')
2611 self.assertEqual(A.reported, 1)
2612 self.assertEqual(B.reported, 0)
2613 self.assertEqual(C.reported, 0)
2614
2615 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
2616 self.assertIn('Build succeeded', A.messages[0])
2617
James E. Blairec056492016-07-22 09:45:56 -07002618 @skip("Disabled for early v3 development")
James E. Blaire712d9f2013-07-31 11:40:11 -07002619 def test_live_reconfiguration_functions(self):
2620 "Test live reconfiguration with a custom function"
2621 self.worker.registerFunction('build:node-project-test1:debian')
2622 self.worker.registerFunction('build:node-project-test1:wheezy')
2623 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002624 A.addApproval('code-review', 2)
2625 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002626 self.waitUntilSettled()
2627
2628 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2629 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2630 'debian')
2631 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2632
James E. Blairf84026c2015-12-08 16:11:46 -08002633 self.updateConfigLayout(
2634 'tests/fixtures/layout-live-reconfiguration-functions.yaml')
James E. Blaire712d9f2013-07-31 11:40:11 -07002635 self.sched.reconfigure(self.config)
2636 self.worker.build_history = []
2637
2638 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002639 B.addApproval('code-review', 2)
2640 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002641 self.waitUntilSettled()
2642
2643 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2644 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2645 'wheezy')
2646 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2647
James E. Blairec056492016-07-22 09:45:56 -07002648 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002649 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002650 self.updateConfigLayout(
2651 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002652 self.sched.reconfigure(self.config)
2653
2654 self.init_repo("org/new-project")
2655 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2656
James E. Blair8b5408c2016-08-08 15:37:46 -07002657 A.addApproval('code-review', 2)
2658 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002659 self.waitUntilSettled()
2660 self.assertEqual(self.getJobFromHistory('project-merge').result,
2661 'SUCCESS')
2662 self.assertEqual(self.getJobFromHistory('project-test1').result,
2663 'SUCCESS')
2664 self.assertEqual(self.getJobFromHistory('project-test2').result,
2665 'SUCCESS')
2666 self.assertEqual(A.data['status'], 'MERGED')
2667 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002668
Clark Boylan6dbbc482013-10-18 10:57:31 -07002669 def test_repo_deleted(self):
Paul Belanger66e95962016-11-11 12:11:06 -05002670 self.updateConfigLayout('layout-repo-deleted')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002671 self.sched.reconfigure(self.config)
2672
2673 self.init_repo("org/delete-project")
2674 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2675
James E. Blair8b5408c2016-08-08 15:37:46 -07002676 A.addApproval('code-review', 2)
2677 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002678 self.waitUntilSettled()
2679 self.assertEqual(self.getJobFromHistory('project-merge').result,
2680 'SUCCESS')
2681 self.assertEqual(self.getJobFromHistory('project-test1').result,
2682 'SUCCESS')
2683 self.assertEqual(self.getJobFromHistory('project-test2').result,
2684 'SUCCESS')
2685 self.assertEqual(A.data['status'], 'MERGED')
2686 self.assertEqual(A.reported, 2)
2687
2688 # Delete org/new-project zuul repo. Should be recloned.
2689 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
2690
2691 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2692
James E. Blair8b5408c2016-08-08 15:37:46 -07002693 B.addApproval('code-review', 2)
2694 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002695 self.waitUntilSettled()
2696 self.assertEqual(self.getJobFromHistory('project-merge').result,
2697 'SUCCESS')
2698 self.assertEqual(self.getJobFromHistory('project-test1').result,
2699 'SUCCESS')
2700 self.assertEqual(self.getJobFromHistory('project-test2').result,
2701 'SUCCESS')
2702 self.assertEqual(B.data['status'], 'MERGED')
2703 self.assertEqual(B.reported, 2)
2704
James E. Blairec056492016-07-22 09:45:56 -07002705 @skip("Disabled for early v3 development")
James E. Blair456f2fb2016-02-09 09:29:33 -08002706 def test_tags(self):
2707 "Test job tags"
2708 self.config.set('zuul', 'layout_config',
2709 'tests/fixtures/layout-tags.yaml')
2710 self.sched.reconfigure(self.config)
2711
2712 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2713 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2714 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2715 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2716 self.waitUntilSettled()
2717
2718 results = {'project1-merge': 'extratag merge project1',
2719 'project2-merge': 'merge'}
2720
2721 for build in self.history:
2722 self.assertEqual(results.get(build.name, ''),
2723 build.parameters.get('BUILD_TAGS'))
2724
James E. Blairec056492016-07-22 09:45:56 -07002725 @skip("Disabled for early v3 development")
James E. Blair63bb0ef2013-07-29 17:14:51 -07002726 def test_timer(self):
2727 "Test that a periodic job is triggered"
James E. Blair08d19992016-08-10 15:25:31 -07002728 self.launch_server.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08002729 self.updateConfigLayout(
2730 'tests/fixtures/layout-timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002731 self.sched.reconfigure(self.config)
2732 self.registerJobs()
2733
Clark Boylan3ee090a2014-04-03 20:55:09 -07002734 # The pipeline triggers every second, so we should have seen
2735 # several by now.
2736 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002737 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002738
2739 self.assertEqual(len(self.builds), 2)
2740
James E. Blair63bb0ef2013-07-29 17:14:51 -07002741 port = self.webapp.server.socket.getsockname()[1]
2742
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002743 req = urllib.request.Request("http://localhost:%s/status.json" % port)
2744 f = urllib.request.urlopen(req)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002745 data = f.read()
2746
James E. Blair08d19992016-08-10 15:25:31 -07002747 self.launch_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002748 # Stop queuing timer triggered jobs so that the assertions
2749 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002750 self.updateConfigLayout(
2751 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002752 self.sched.reconfigure(self.config)
2753 self.registerJobs()
James E. Blair08d19992016-08-10 15:25:31 -07002754 self.launch_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002755 self.waitUntilSettled()
2756
2757 self.assertEqual(self.getJobFromHistory(
2758 'project-bitrot-stable-old').result, 'SUCCESS')
2759 self.assertEqual(self.getJobFromHistory(
2760 'project-bitrot-stable-older').result, 'SUCCESS')
2761
2762 data = json.loads(data)
2763 status_jobs = set()
2764 for p in data['pipelines']:
2765 for q in p['change_queues']:
2766 for head in q['heads']:
2767 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07002768 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002769 for job in change['jobs']:
2770 status_jobs.add(job['name'])
2771 self.assertIn('project-bitrot-stable-old', status_jobs)
2772 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002773
James E. Blairec056492016-07-22 09:45:56 -07002774 @skip("Disabled for early v3 development")
James E. Blair4f6033c2014-03-27 15:49:09 -07002775 def test_idle(self):
2776 "Test that frequent periodic jobs work"
James E. Blair08d19992016-08-10 15:25:31 -07002777 self.launch_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002778
Clark Boylan3ee090a2014-04-03 20:55:09 -07002779 for x in range(1, 3):
2780 # Test that timer triggers periodic jobs even across
2781 # layout config reloads.
2782 # Start timer trigger
James E. Blairf84026c2015-12-08 16:11:46 -08002783 self.updateConfigLayout(
2784 'tests/fixtures/layout-idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002785 self.sched.reconfigure(self.config)
2786 self.registerJobs()
James E. Blair995fc0f2016-02-04 16:48:31 -08002787 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002788
Clark Boylan3ee090a2014-04-03 20:55:09 -07002789 # The pipeline triggers every second, so we should have seen
2790 # several by now.
2791 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002792
2793 # Stop queuing timer triggered jobs so that the assertions
2794 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002795 self.updateConfigLayout(
2796 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002797 self.sched.reconfigure(self.config)
2798 self.registerJobs()
James E. Blair995fc0f2016-02-04 16:48:31 -08002799 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002800
2801 self.assertEqual(len(self.builds), 2)
James E. Blair08d19992016-08-10 15:25:31 -07002802 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002803 self.waitUntilSettled()
2804 self.assertEqual(len(self.builds), 0)
2805 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002806
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002807 def test_check_smtp_pool(self):
Paul Belanger01be8ca2016-11-11 12:21:06 -05002808 self.updateConfigLayout('layout-smtp')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002809 self.sched.reconfigure(self.config)
2810
2811 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2812 self.waitUntilSettled()
2813
2814 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2815 self.waitUntilSettled()
2816
James E. Blairff80a2f2013-12-27 13:24:06 -08002817 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002818
2819 # A.messages only holds what FakeGerrit places in it. Thus we
2820 # work on the knowledge of what the first message should be as
2821 # it is only configured to go to SMTP.
2822
2823 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002824 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002825 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002826 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002827 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002828 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002829
2830 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002831 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002832 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002833 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002834 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002835 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002836
James E. Blairec056492016-07-22 09:45:56 -07002837 @skip("Disabled for early v3 development")
James E. Blaire5910202013-12-27 09:50:31 -08002838 def test_timer_smtp(self):
2839 "Test that a periodic job is triggered"
James E. Blair08d19992016-08-10 15:25:31 -07002840 self.launch_server.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08002841 self.updateConfigLayout(
2842 'tests/fixtures/layout-timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002843 self.sched.reconfigure(self.config)
2844 self.registerJobs()
2845
Clark Boylan3ee090a2014-04-03 20:55:09 -07002846 # The pipeline triggers every second, so we should have seen
2847 # several by now.
2848 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002849 self.waitUntilSettled()
2850
Clark Boylan3ee090a2014-04-03 20:55:09 -07002851 self.assertEqual(len(self.builds), 2)
James E. Blair08d19992016-08-10 15:25:31 -07002852 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002853 self.waitUntilSettled()
2854 self.assertEqual(len(self.history), 2)
2855
James E. Blaire5910202013-12-27 09:50:31 -08002856 self.assertEqual(self.getJobFromHistory(
2857 'project-bitrot-stable-old').result, 'SUCCESS')
2858 self.assertEqual(self.getJobFromHistory(
2859 'project-bitrot-stable-older').result, 'SUCCESS')
2860
James E. Blairff80a2f2013-12-27 13:24:06 -08002861 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002862
2863 # A.messages only holds what FakeGerrit places in it. Thus we
2864 # work on the knowledge of what the first message should be as
2865 # it is only configured to go to SMTP.
2866
2867 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002868 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002869 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002870 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002871 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002872 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002873
Clark Boylan3ee090a2014-04-03 20:55:09 -07002874 # Stop queuing timer triggered jobs and let any that may have
2875 # queued through so that end of test assertions pass.
James E. Blairf84026c2015-12-08 16:11:46 -08002876 self.updateConfigLayout(
2877 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002878 self.sched.reconfigure(self.config)
2879 self.registerJobs()
James E. Blairf8058972014-08-15 16:09:16 -07002880 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002881 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002882 self.waitUntilSettled()
2883
James E. Blair91e34592015-07-31 16:45:59 -07002884 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08002885 "Test that the RPC client can enqueue a change"
2886 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002887 A.addApproval('code-review', 2)
2888 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08002889
2890 client = zuul.rpcclient.RPCClient('127.0.0.1',
2891 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04002892 r = client.enqueue(tenant='tenant-one',
2893 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002894 project='org/project',
2895 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002896 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002897 self.waitUntilSettled()
2898 self.assertEqual(self.getJobFromHistory('project-merge').result,
2899 'SUCCESS')
2900 self.assertEqual(self.getJobFromHistory('project-test1').result,
2901 'SUCCESS')
2902 self.assertEqual(self.getJobFromHistory('project-test2').result,
2903 'SUCCESS')
2904 self.assertEqual(A.data['status'], 'MERGED')
2905 self.assertEqual(A.reported, 2)
2906 self.assertEqual(r, True)
2907
James E. Blairec056492016-07-22 09:45:56 -07002908 @skip("Disabled for early v3 development")
James E. Blair91e34592015-07-31 16:45:59 -07002909 def test_client_enqueue_ref(self):
2910 "Test that the RPC client can enqueue a ref"
2911
2912 client = zuul.rpcclient.RPCClient('127.0.0.1',
2913 self.gearman_server.port)
2914 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04002915 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07002916 pipeline='post',
2917 project='org/project',
2918 trigger='gerrit',
2919 ref='master',
2920 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
2921 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
2922 self.waitUntilSettled()
2923 job_names = [x.name for x in self.history]
2924 self.assertEqual(len(self.history), 1)
2925 self.assertIn('project-post', job_names)
2926 self.assertEqual(r, True)
2927
James E. Blairad28e912013-11-27 10:43:22 -08002928 def test_client_enqueue_negative(self):
2929 "Test that the RPC client returns errors"
2930 client = zuul.rpcclient.RPCClient('127.0.0.1',
2931 self.gearman_server.port)
2932 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04002933 "Invalid tenant"):
2934 r = client.enqueue(tenant='tenant-foo',
2935 pipeline='gate',
2936 project='org/project',
2937 trigger='gerrit',
2938 change='1,1')
2939 client.shutdown()
2940 self.assertEqual(r, False)
2941
2942 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08002943 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002944 r = client.enqueue(tenant='tenant-one',
2945 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002946 project='project-does-not-exist',
2947 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002948 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002949 client.shutdown()
2950 self.assertEqual(r, False)
2951
2952 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2953 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002954 r = client.enqueue(tenant='tenant-one',
2955 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08002956 project='org/project',
2957 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002958 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002959 client.shutdown()
2960 self.assertEqual(r, False)
2961
2962 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2963 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002964 r = client.enqueue(tenant='tenant-one',
2965 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002966 project='org/project',
2967 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08002968 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002969 client.shutdown()
2970 self.assertEqual(r, False)
2971
2972 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2973 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002974 r = client.enqueue(tenant='tenant-one',
2975 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002976 project='org/project',
2977 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002978 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002979 client.shutdown()
2980 self.assertEqual(r, False)
2981
2982 self.waitUntilSettled()
2983 self.assertEqual(len(self.history), 0)
2984 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08002985
2986 def test_client_promote(self):
2987 "Test that the RPC client can promote a change"
James E. Blair08d19992016-08-10 15:25:31 -07002988 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08002989 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2990 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2991 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07002992 A.addApproval('code-review', 2)
2993 B.addApproval('code-review', 2)
2994 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08002995
James E. Blair8b5408c2016-08-08 15:37:46 -07002996 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2997 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
2998 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08002999
3000 self.waitUntilSettled()
3001
Paul Belangerbaca3132016-11-04 12:49:54 -04003002 tenant = self.sched.abide.tenants.get('tenant-one')
3003 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003004 enqueue_times = {}
3005 for item in items:
3006 enqueue_times[str(item.change)] = item.enqueue_time
3007
James E. Blair36658cf2013-12-06 17:53:48 -08003008 client = zuul.rpcclient.RPCClient('127.0.0.1',
3009 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003010 r = client.promote(tenant='tenant-one',
3011 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003012 change_ids=['2,1', '3,1'])
3013
Sean Daguef39b9ca2014-01-10 21:34:35 -05003014 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003015 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003016 for item in items:
3017 self.assertEqual(
3018 enqueue_times[str(item.change)], item.enqueue_time)
3019
James E. Blair78acec92014-02-06 07:11:32 -08003020 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003021 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003022 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003023 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003024 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003025 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003026 self.waitUntilSettled()
3027
3028 self.assertEqual(len(self.builds), 6)
3029 self.assertEqual(self.builds[0].name, 'project-test1')
3030 self.assertEqual(self.builds[1].name, 'project-test2')
3031 self.assertEqual(self.builds[2].name, 'project-test1')
3032 self.assertEqual(self.builds[3].name, 'project-test2')
3033 self.assertEqual(self.builds[4].name, 'project-test1')
3034 self.assertEqual(self.builds[5].name, 'project-test2')
3035
Paul Belangerbaca3132016-11-04 12:49:54 -04003036 self.assertTrue(self.builds[0].hasChanges(B))
3037 self.assertFalse(self.builds[0].hasChanges(A))
3038 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003039
Paul Belangerbaca3132016-11-04 12:49:54 -04003040 self.assertTrue(self.builds[2].hasChanges(B))
3041 self.assertTrue(self.builds[2].hasChanges(C))
3042 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003043
Paul Belangerbaca3132016-11-04 12:49:54 -04003044 self.assertTrue(self.builds[4].hasChanges(B))
3045 self.assertTrue(self.builds[4].hasChanges(C))
3046 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003047
James E. Blair08d19992016-08-10 15:25:31 -07003048 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003049 self.waitUntilSettled()
3050
3051 self.assertEqual(A.data['status'], 'MERGED')
3052 self.assertEqual(A.reported, 2)
3053 self.assertEqual(B.data['status'], 'MERGED')
3054 self.assertEqual(B.reported, 2)
3055 self.assertEqual(C.data['status'], 'MERGED')
3056 self.assertEqual(C.reported, 2)
3057
3058 client.shutdown()
3059 self.assertEqual(r, True)
3060
3061 def test_client_promote_dependent(self):
3062 "Test that the RPC client can promote a dependent change"
3063 # C (depends on B) -> B -> A ; then promote C to get:
3064 # A -> C (depends on B) -> B
James E. Blair08d19992016-08-10 15:25:31 -07003065 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003066 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3067 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3068 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3069
3070 C.setDependsOn(B, 1)
3071
James E. Blair8b5408c2016-08-08 15:37:46 -07003072 A.addApproval('code-review', 2)
3073 B.addApproval('code-review', 2)
3074 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003075
James E. Blair8b5408c2016-08-08 15:37:46 -07003076 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3077 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3078 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003079
3080 self.waitUntilSettled()
3081
3082 client = zuul.rpcclient.RPCClient('127.0.0.1',
3083 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003084 r = client.promote(tenant='tenant-one',
3085 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003086 change_ids=['3,1'])
3087
James E. Blair78acec92014-02-06 07:11:32 -08003088 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003089 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003090 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003091 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003092 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003093 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003094 self.waitUntilSettled()
3095
3096 self.assertEqual(len(self.builds), 6)
3097 self.assertEqual(self.builds[0].name, 'project-test1')
3098 self.assertEqual(self.builds[1].name, 'project-test2')
3099 self.assertEqual(self.builds[2].name, 'project-test1')
3100 self.assertEqual(self.builds[3].name, 'project-test2')
3101 self.assertEqual(self.builds[4].name, 'project-test1')
3102 self.assertEqual(self.builds[5].name, 'project-test2')
3103
Paul Belangerbaca3132016-11-04 12:49:54 -04003104 self.assertTrue(self.builds[0].hasChanges(B))
3105 self.assertFalse(self.builds[0].hasChanges(A))
3106 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003107
Paul Belangerbaca3132016-11-04 12:49:54 -04003108 self.assertTrue(self.builds[2].hasChanges(B))
3109 self.assertTrue(self.builds[2].hasChanges(C))
3110 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003111
Paul Belangerbaca3132016-11-04 12:49:54 -04003112 self.assertTrue(self.builds[4].hasChanges(B))
3113 self.assertTrue(self.builds[4].hasChanges(C))
3114 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003115
James E. Blair08d19992016-08-10 15:25:31 -07003116 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003117 self.waitUntilSettled()
3118
3119 self.assertEqual(A.data['status'], 'MERGED')
3120 self.assertEqual(A.reported, 2)
3121 self.assertEqual(B.data['status'], 'MERGED')
3122 self.assertEqual(B.reported, 2)
3123 self.assertEqual(C.data['status'], 'MERGED')
3124 self.assertEqual(C.reported, 2)
3125
3126 client.shutdown()
3127 self.assertEqual(r, True)
3128
3129 def test_client_promote_negative(self):
3130 "Test that the RPC client returns errors for promotion"
James E. Blair08d19992016-08-10 15:25:31 -07003131 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003132 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003133 A.addApproval('code-review', 2)
3134 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003135 self.waitUntilSettled()
3136
3137 client = zuul.rpcclient.RPCClient('127.0.0.1',
3138 self.gearman_server.port)
3139
3140 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003141 r = client.promote(tenant='tenant-one',
3142 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003143 change_ids=['2,1', '3,1'])
3144 client.shutdown()
3145 self.assertEqual(r, False)
3146
3147 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003148 r = client.promote(tenant='tenant-one',
3149 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003150 change_ids=['4,1'])
3151 client.shutdown()
3152 self.assertEqual(r, False)
3153
James E. Blair08d19992016-08-10 15:25:31 -07003154 self.launch_server.hold_jobs_in_build = False
3155 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003156 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003157
James E. Blairec056492016-07-22 09:45:56 -07003158 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003159 def test_queue_rate_limiting(self):
3160 "Test that DependentPipelines are rate limited with dep across window"
James E. Blairf84026c2015-12-08 16:11:46 -08003161 self.updateConfigLayout(
3162 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003163 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07003164 self.launch_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003165 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3166 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3167 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3168
3169 C.setDependsOn(B, 1)
James E. Blair08d19992016-08-10 15:25:31 -07003170 self.launch_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003171
James E. Blair8b5408c2016-08-08 15:37:46 -07003172 A.addApproval('code-review', 2)
3173 B.addApproval('code-review', 2)
3174 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003175
James E. Blair8b5408c2016-08-08 15:37:46 -07003176 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3177 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3178 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003179 self.waitUntilSettled()
3180
3181 # Only A and B will have their merge jobs queued because
3182 # window is 2.
3183 self.assertEqual(len(self.builds), 2)
3184 self.assertEqual(self.builds[0].name, 'project-merge')
3185 self.assertEqual(self.builds[1].name, 'project-merge')
3186
James E. Blair08d19992016-08-10 15:25:31 -07003187 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003188 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003189 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003190 self.waitUntilSettled()
3191
3192 # Only A and B will have their test jobs queued because
3193 # window is 2.
3194 self.assertEqual(len(self.builds), 4)
3195 self.assertEqual(self.builds[0].name, 'project-test1')
3196 self.assertEqual(self.builds[1].name, 'project-test2')
3197 self.assertEqual(self.builds[2].name, 'project-test1')
3198 self.assertEqual(self.builds[3].name, 'project-test2')
3199
James E. Blair08d19992016-08-10 15:25:31 -07003200 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003201 self.waitUntilSettled()
3202
3203 queue = self.sched.layout.pipelines['gate'].queues[0]
3204 # A failed so window is reduced by 1 to 1.
3205 self.assertEqual(queue.window, 1)
3206 self.assertEqual(queue.window_floor, 1)
3207 self.assertEqual(A.data['status'], 'NEW')
3208
3209 # Gate is reset and only B's merge job is queued because
3210 # window shrunk to 1.
3211 self.assertEqual(len(self.builds), 1)
3212 self.assertEqual(self.builds[0].name, 'project-merge')
3213
James E. Blair08d19992016-08-10 15:25:31 -07003214 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003215 self.waitUntilSettled()
3216
3217 # Only B's test jobs are queued because window is still 1.
3218 self.assertEqual(len(self.builds), 2)
3219 self.assertEqual(self.builds[0].name, 'project-test1')
3220 self.assertEqual(self.builds[1].name, 'project-test2')
3221
James E. Blair08d19992016-08-10 15:25:31 -07003222 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003223 self.waitUntilSettled()
3224
3225 # B was successfully merged so window is increased to 2.
3226 self.assertEqual(queue.window, 2)
3227 self.assertEqual(queue.window_floor, 1)
3228 self.assertEqual(B.data['status'], 'MERGED')
3229
3230 # Only C is left and its merge job is queued.
3231 self.assertEqual(len(self.builds), 1)
3232 self.assertEqual(self.builds[0].name, 'project-merge')
3233
James E. Blair08d19992016-08-10 15:25:31 -07003234 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003235 self.waitUntilSettled()
3236
3237 # After successful merge job the test jobs for C are queued.
3238 self.assertEqual(len(self.builds), 2)
3239 self.assertEqual(self.builds[0].name, 'project-test1')
3240 self.assertEqual(self.builds[1].name, 'project-test2')
3241
James E. Blair08d19992016-08-10 15:25:31 -07003242 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003243 self.waitUntilSettled()
3244
3245 # C successfully merged so window is bumped to 3.
3246 self.assertEqual(queue.window, 3)
3247 self.assertEqual(queue.window_floor, 1)
3248 self.assertEqual(C.data['status'], 'MERGED')
3249
James E. Blairec056492016-07-22 09:45:56 -07003250 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003251 def test_queue_rate_limiting_dependent(self):
3252 "Test that DependentPipelines are rate limited with dep in window"
James E. Blairf84026c2015-12-08 16:11:46 -08003253 self.updateConfigLayout(
3254 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003255 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07003256 self.launch_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003257 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3258 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3259 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3260
3261 B.setDependsOn(A, 1)
3262
James E. Blair08d19992016-08-10 15:25:31 -07003263 self.launch_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003264
James E. Blair8b5408c2016-08-08 15:37:46 -07003265 A.addApproval('code-review', 2)
3266 B.addApproval('code-review', 2)
3267 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003268
James E. Blair8b5408c2016-08-08 15:37:46 -07003269 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3270 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3271 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003272 self.waitUntilSettled()
3273
3274 # Only A and B will have their merge jobs queued because
3275 # window is 2.
3276 self.assertEqual(len(self.builds), 2)
3277 self.assertEqual(self.builds[0].name, 'project-merge')
3278 self.assertEqual(self.builds[1].name, 'project-merge')
3279
James E. Blair08d19992016-08-10 15:25:31 -07003280 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003281 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003282 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003283 self.waitUntilSettled()
3284
3285 # Only A and B will have their test jobs queued because
3286 # window is 2.
3287 self.assertEqual(len(self.builds), 4)
3288 self.assertEqual(self.builds[0].name, 'project-test1')
3289 self.assertEqual(self.builds[1].name, 'project-test2')
3290 self.assertEqual(self.builds[2].name, 'project-test1')
3291 self.assertEqual(self.builds[3].name, 'project-test2')
3292
James E. Blair08d19992016-08-10 15:25:31 -07003293 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003294 self.waitUntilSettled()
3295
3296 queue = self.sched.layout.pipelines['gate'].queues[0]
3297 # A failed so window is reduced by 1 to 1.
3298 self.assertEqual(queue.window, 1)
3299 self.assertEqual(queue.window_floor, 1)
3300 self.assertEqual(A.data['status'], 'NEW')
3301 self.assertEqual(B.data['status'], 'NEW')
3302
3303 # Gate is reset and only C's merge job is queued because
3304 # window shrunk to 1 and A and B were dequeued.
3305 self.assertEqual(len(self.builds), 1)
3306 self.assertEqual(self.builds[0].name, 'project-merge')
3307
James E. Blair08d19992016-08-10 15:25:31 -07003308 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003309 self.waitUntilSettled()
3310
3311 # Only C's test jobs are queued because window is still 1.
3312 self.assertEqual(len(self.builds), 2)
3313 self.assertEqual(self.builds[0].name, 'project-test1')
3314 self.assertEqual(self.builds[1].name, 'project-test2')
3315
James E. Blair08d19992016-08-10 15:25:31 -07003316 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003317 self.waitUntilSettled()
3318
3319 # C was successfully merged so window is increased to 2.
3320 self.assertEqual(queue.window, 2)
3321 self.assertEqual(queue.window_floor, 1)
3322 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003323
James E. Blairec056492016-07-22 09:45:56 -07003324 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003325 def test_worker_update_metadata(self):
3326 "Test if a worker can send back metadata about itself"
James E. Blair08d19992016-08-10 15:25:31 -07003327 self.launch_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003328
3329 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003330 A.addApproval('code-review', 2)
3331 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003332 self.waitUntilSettled()
3333
3334 self.assertEqual(len(self.launcher.builds), 1)
3335
3336 self.log.debug('Current builds:')
3337 self.log.debug(self.launcher.builds)
3338
3339 start = time.time()
3340 while True:
3341 if time.time() - start > 10:
3342 raise Exception("Timeout waiting for gearman server to report "
3343 + "back to the client")
3344 build = self.launcher.builds.values()[0]
3345 if build.worker.name == "My Worker":
3346 break
3347 else:
3348 time.sleep(0)
3349
3350 self.log.debug(build)
3351 self.assertEqual("My Worker", build.worker.name)
3352 self.assertEqual("localhost", build.worker.hostname)
3353 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3354 self.assertEqual("zuul.example.org", build.worker.fqdn)
3355 self.assertEqual("FakeBuilder", build.worker.program)
3356 self.assertEqual("v1.1", build.worker.version)
3357 self.assertEqual({'something': 'else'}, build.worker.extra)
3358
James E. Blair08d19992016-08-10 15:25:31 -07003359 self.launch_server.hold_jobs_in_build = False
3360 self.launch_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003361 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003362
James E. Blairec056492016-07-22 09:45:56 -07003363 @skip("Disabled for early v3 development")
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003364 def test_footer_message(self):
3365 "Test a pipeline's footer message is correctly added to the report."
James E. Blairf84026c2015-12-08 16:11:46 -08003366 self.updateConfigLayout(
3367 'tests/fixtures/layout-footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003368 self.sched.reconfigure(self.config)
3369 self.registerJobs()
3370
3371 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003372 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07003373 self.launch_server.failJob('test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003374 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003375 self.waitUntilSettled()
3376
3377 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003378 B.addApproval('code-review', 2)
3379 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003380 self.waitUntilSettled()
3381
3382 self.assertEqual(2, len(self.smtp_messages))
3383
3384 failure_body = """\
3385Build failed. For information on how to proceed, see \
3386http://wiki.example.org/Test_Failures
3387
3388- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
3389- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
3390
3391For CI problems and help debugging, contact ci@example.org"""
3392
3393 success_body = """\
3394Build succeeded.
3395
3396- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
3397- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
3398
3399For CI problems and help debugging, contact ci@example.org"""
3400
3401 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
3402 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11003403
James E. Blairec056492016-07-22 09:45:56 -07003404 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003405 def test_merge_failure_reporters(self):
3406 """Check that the config is set up correctly"""
3407
James E. Blairf84026c2015-12-08 16:11:46 -08003408 self.updateConfigLayout(
3409 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003410 self.sched.reconfigure(self.config)
3411 self.registerJobs()
3412
3413 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003414 "Merge Failed.\n\nThis change or one of its cross-repo "
3415 "dependencies was unable to be automatically merged with the "
3416 "current state of its repository. Please rebase the change and "
3417 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003418 self.sched.layout.pipelines['check'].merge_failure_message)
3419 self.assertEqual(
3420 "The merge failed! For more information...",
3421 self.sched.layout.pipelines['gate'].merge_failure_message)
3422
3423 self.assertEqual(
3424 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3425 self.assertEqual(
3426 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3427
3428 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003429 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3430 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003431
3432 self.assertTrue(
3433 (
3434 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003435 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003436 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003437 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003438 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003439 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003440 ) or (
3441 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003442 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003443 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003444 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003445 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003446 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003447 )
3448 )
3449
James E. Blairec056492016-07-22 09:45:56 -07003450 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003451 def test_merge_failure_reports(self):
3452 """Check that when a change fails to merge the correct message is sent
3453 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003454 self.updateConfigLayout(
3455 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003456 self.sched.reconfigure(self.config)
3457 self.registerJobs()
3458
3459 # Check a test failure isn't reported to SMTP
3460 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003461 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07003462 self.launch_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003463 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003464 self.waitUntilSettled()
3465
3466 self.assertEqual(3, len(self.history)) # 3 jobs
3467 self.assertEqual(0, len(self.smtp_messages))
3468
3469 # Check a merge failure is reported to SMTP
3470 # B should be merged, but C will conflict with B
3471 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3472 B.addPatchset(['conflict'])
3473 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3474 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003475 B.addApproval('code-review', 2)
3476 C.addApproval('code-review', 2)
3477 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3478 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003479 self.waitUntilSettled()
3480
3481 self.assertEqual(6, len(self.history)) # A and B jobs
3482 self.assertEqual(1, len(self.smtp_messages))
3483 self.assertEqual('The merge failed! For more information...',
3484 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003485
James E. Blairec056492016-07-22 09:45:56 -07003486 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003487 def test_default_merge_failure_reports(self):
3488 """Check that the default merge failure reports are correct."""
3489
3490 # A should report success, B should report merge failure.
3491 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3492 A.addPatchset(['conflict'])
3493 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3494 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003495 A.addApproval('code-review', 2)
3496 B.addApproval('code-review', 2)
3497 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3498 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003499 self.waitUntilSettled()
3500
3501 self.assertEqual(3, len(self.history)) # A jobs
3502 self.assertEqual(A.reported, 2)
3503 self.assertEqual(B.reported, 2)
3504 self.assertEqual(A.data['status'], 'MERGED')
3505 self.assertEqual(B.data['status'], 'NEW')
3506 self.assertIn('Build succeeded', A.messages[1])
3507 self.assertIn('Merge Failed', B.messages[1])
3508 self.assertIn('automatically merged', B.messages[1])
3509 self.assertNotIn('logs.example.com', B.messages[1])
3510 self.assertNotIn('SKIPPED', B.messages[1])
3511
James E. Blairec056492016-07-22 09:45:56 -07003512 @skip("Disabled for early v3 development")
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003513 def test_swift_instructions(self):
3514 "Test that the correct swift instructions are sent to the workers"
James E. Blairf84026c2015-12-08 16:11:46 -08003515 self.updateConfigLayout(
3516 'tests/fixtures/layout-swift.yaml')
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003517 self.sched.reconfigure(self.config)
3518 self.registerJobs()
3519
James E. Blair08d19992016-08-10 15:25:31 -07003520 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003521 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3522
James E. Blair8b5408c2016-08-08 15:37:46 -07003523 A.addApproval('code-review', 2)
3524 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003525 self.waitUntilSettled()
3526
3527 self.assertEqual(
3528 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
3529 "gate/test-merge/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003530 self.builds[0].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003531 self.assertEqual(5,
3532 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
3533 split('\n')))
3534 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
3535
3536 self.assertEqual(
3537 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
3538 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003539 self.builds[1].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003540 self.assertEqual(5,
3541 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
3542 split('\n')))
3543 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
3544
3545 self.assertEqual(
3546 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
3547 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003548 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003549 self.assertEqual(5,
3550 len(self.builds[1].
3551 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
3552 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
3553
James E. Blair08d19992016-08-10 15:25:31 -07003554 self.launch_server.hold_jobs_in_build = False
3555 self.launch_server.release()
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003556 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003557
3558 def test_client_get_running_jobs(self):
3559 "Test that the RPC client can get a list of running jobs"
James E. Blair08d19992016-08-10 15:25:31 -07003560 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003561 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003562 A.addApproval('code-review', 2)
3563 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003564 self.waitUntilSettled()
3565
3566 client = zuul.rpcclient.RPCClient('127.0.0.1',
3567 self.gearman_server.port)
3568
3569 # Wait for gearman server to send the initial workData back to zuul
3570 start = time.time()
3571 while True:
3572 if time.time() - start > 10:
3573 raise Exception("Timeout waiting for gearman server to report "
3574 + "back to the client")
Clint Byrum78a69772016-12-05 10:52:07 -08003575 build = self.launch_client.builds.values()[0]
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003576 if build.worker.name == "My Worker":
3577 break
3578 else:
3579 time.sleep(0)
3580
3581 running_items = client.get_running_jobs()
3582
3583 self.assertEqual(1, len(running_items))
3584 running_item = running_items[0]
3585 self.assertEqual([], running_item['failing_reasons'])
3586 self.assertEqual([], running_item['items_behind'])
3587 self.assertEqual('https://hostname/1', running_item['url'])
3588 self.assertEqual(None, running_item['item_ahead'])
3589 self.assertEqual('org/project', running_item['project'])
3590 self.assertEqual(None, running_item['remaining_time'])
3591 self.assertEqual(True, running_item['active'])
3592 self.assertEqual('1,1', running_item['id'])
3593
3594 self.assertEqual(3, len(running_item['jobs']))
3595 for job in running_item['jobs']:
3596 if job['name'] == 'project-merge':
3597 self.assertEqual('project-merge', job['name'])
3598 self.assertEqual('gate', job['pipeline'])
3599 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003600 self.assertEqual('https://server/job/project-merge/0/',
3601 job['url'])
3602 self.assertEqual(7, len(job['worker']))
3603 self.assertEqual(False, job['canceled'])
3604 self.assertEqual(True, job['voting'])
3605 self.assertEqual(None, job['result'])
3606 self.assertEqual('gate', job['pipeline'])
3607 break
3608
James E. Blair08d19992016-08-10 15:25:31 -07003609 self.launch_server.hold_jobs_in_build = False
3610 self.launch_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003611 self.waitUntilSettled()
3612
3613 running_items = client.get_running_jobs()
3614 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003615
3616 def test_nonvoting_pipeline(self):
3617 "Test that a nonvoting pipeline (experimental) can still report"
3618
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003619 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3620 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003621 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3622 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003623
3624 self.assertEqual(self.getJobFromHistory('project-merge').result,
3625 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003626 self.assertEqual(
3627 self.getJobFromHistory('experimental-project-test').result,
3628 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003629 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003630
3631 def test_crd_gate(self):
3632 "Test cross-repo dependencies"
3633 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3634 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003635 A.addApproval('code-review', 2)
3636 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003637
3638 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3639 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3640 AM2.setMerged()
3641 AM1.setMerged()
3642
3643 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3644 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3645 BM2.setMerged()
3646 BM1.setMerged()
3647
3648 # A -> AM1 -> AM2
3649 # B -> BM1 -> BM2
3650 # A Depends-On: B
3651 # M2 is here to make sure it is never queried. If it is, it
3652 # means zuul is walking down the entire history of merged
3653 # changes.
3654
3655 B.setDependsOn(BM1, 1)
3656 BM1.setDependsOn(BM2, 1)
3657
3658 A.setDependsOn(AM1, 1)
3659 AM1.setDependsOn(AM2, 1)
3660
3661 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3662 A.subject, B.data['id'])
3663
James E. Blair8b5408c2016-08-08 15:37:46 -07003664 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003665 self.waitUntilSettled()
3666
3667 self.assertEqual(A.data['status'], 'NEW')
3668 self.assertEqual(B.data['status'], 'NEW')
3669
Paul Belanger5dccbe72016-11-14 11:17:37 -05003670 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003671 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003672
James E. Blair08d19992016-08-10 15:25:31 -07003673 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003674 B.addApproval('approved', 1)
3675 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003676 self.waitUntilSettled()
3677
James E. Blair08d19992016-08-10 15:25:31 -07003678 self.launch_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003679 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003680 self.launch_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003681 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003682 self.launch_server.hold_jobs_in_build = False
3683 self.launch_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003684 self.waitUntilSettled()
3685
3686 self.assertEqual(AM2.queried, 0)
3687 self.assertEqual(BM2.queried, 0)
3688 self.assertEqual(A.data['status'], 'MERGED')
3689 self.assertEqual(B.data['status'], 'MERGED')
3690 self.assertEqual(A.reported, 2)
3691 self.assertEqual(B.reported, 2)
3692
Paul Belanger5dccbe72016-11-14 11:17:37 -05003693 changes = self.getJobFromHistory(
3694 'project-merge', 'org/project1').changes
3695 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003696
3697 def test_crd_branch(self):
3698 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003699
3700 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003701 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3702 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3703 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3704 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003705 A.addApproval('code-review', 2)
3706 B.addApproval('code-review', 2)
3707 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003708
3709 # A Depends-On: B+C
3710 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3711 A.subject, B.data['id'])
3712
James E. Blair08d19992016-08-10 15:25:31 -07003713 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003714 B.addApproval('approved', 1)
3715 C.addApproval('approved', 1)
3716 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003717 self.waitUntilSettled()
3718
James E. Blair08d19992016-08-10 15:25:31 -07003719 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003720 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003721 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003722 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003723 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003724 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003725 self.launch_server.hold_jobs_in_build = False
3726 self.launch_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003727 self.waitUntilSettled()
3728
3729 self.assertEqual(A.data['status'], 'MERGED')
3730 self.assertEqual(B.data['status'], 'MERGED')
3731 self.assertEqual(C.data['status'], 'MERGED')
3732 self.assertEqual(A.reported, 2)
3733 self.assertEqual(B.reported, 2)
3734 self.assertEqual(C.reported, 2)
3735
Paul Belanger6379db12016-11-14 13:57:54 -05003736 changes = self.getJobFromHistory(
3737 'project-merge', 'org/project1').changes
3738 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003739
3740 def test_crd_multiline(self):
3741 "Test multiple depends-on lines in commit"
3742 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3743 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3744 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003745 A.addApproval('code-review', 2)
3746 B.addApproval('code-review', 2)
3747 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003748
3749 # A Depends-On: B+C
3750 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3751 A.subject, B.data['id'], C.data['id'])
3752
James E. Blair08d19992016-08-10 15:25:31 -07003753 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003754 B.addApproval('approved', 1)
3755 C.addApproval('approved', 1)
3756 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003757 self.waitUntilSettled()
3758
James E. Blair08d19992016-08-10 15:25:31 -07003759 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003760 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003761 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003762 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003763 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003764 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003765 self.launch_server.hold_jobs_in_build = False
3766 self.launch_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003767 self.waitUntilSettled()
3768
3769 self.assertEqual(A.data['status'], 'MERGED')
3770 self.assertEqual(B.data['status'], 'MERGED')
3771 self.assertEqual(C.data['status'], 'MERGED')
3772 self.assertEqual(A.reported, 2)
3773 self.assertEqual(B.reported, 2)
3774 self.assertEqual(C.reported, 2)
3775
Paul Belanger5dccbe72016-11-14 11:17:37 -05003776 changes = self.getJobFromHistory(
3777 'project-merge', 'org/project1').changes
3778 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003779
3780 def test_crd_unshared_gate(self):
3781 "Test cross-repo dependencies in unshared gate queues"
3782 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3783 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003784 A.addApproval('code-review', 2)
3785 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003786
3787 # A Depends-On: B
3788 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3789 A.subject, B.data['id'])
3790
3791 # A and B do not share a queue, make sure that A is unable to
3792 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003793 B.addApproval('approved', 1)
3794 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003795 self.waitUntilSettled()
3796
3797 self.assertEqual(A.data['status'], 'NEW')
3798 self.assertEqual(B.data['status'], 'NEW')
3799 self.assertEqual(A.reported, 0)
3800 self.assertEqual(B.reported, 0)
3801 self.assertEqual(len(self.history), 0)
3802
3803 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003804 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003805 self.waitUntilSettled()
3806
3807 self.assertEqual(B.data['status'], 'MERGED')
3808 self.assertEqual(B.reported, 2)
3809
3810 # Now that B is merged, A should be able to be enqueued and
3811 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003812 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003813 self.waitUntilSettled()
3814
3815 self.assertEqual(A.data['status'], 'MERGED')
3816 self.assertEqual(A.reported, 2)
3817
James E. Blair96698e22015-04-02 07:48:21 -07003818 def test_crd_gate_reverse(self):
3819 "Test reverse cross-repo dependencies"
3820 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3821 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003822 A.addApproval('code-review', 2)
3823 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003824
3825 # A Depends-On: B
3826
3827 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3828 A.subject, B.data['id'])
3829
James E. Blair8b5408c2016-08-08 15:37:46 -07003830 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003831 self.waitUntilSettled()
3832
3833 self.assertEqual(A.data['status'], 'NEW')
3834 self.assertEqual(B.data['status'], 'NEW')
3835
James E. Blair08d19992016-08-10 15:25:31 -07003836 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003837 A.addApproval('approved', 1)
3838 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003839 self.waitUntilSettled()
3840
James E. Blair08d19992016-08-10 15:25:31 -07003841 self.launch_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003842 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003843 self.launch_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003844 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003845 self.launch_server.hold_jobs_in_build = False
3846 self.launch_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003847 self.waitUntilSettled()
3848
3849 self.assertEqual(A.data['status'], 'MERGED')
3850 self.assertEqual(B.data['status'], 'MERGED')
3851 self.assertEqual(A.reported, 2)
3852 self.assertEqual(B.reported, 2)
3853
Paul Belanger5dccbe72016-11-14 11:17:37 -05003854 changes = self.getJobFromHistory(
3855 'project-merge', 'org/project1').changes
3856 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003857
James E. Blair5ee24252014-12-30 10:12:29 -08003858 def test_crd_cycle(self):
3859 "Test cross-repo dependency cycles"
3860 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3861 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003862 A.addApproval('code-review', 2)
3863 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003864
3865 # A -> B -> A (via commit-depends)
3866
3867 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3868 A.subject, B.data['id'])
3869 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3870 B.subject, A.data['id'])
3871
James E. Blair8b5408c2016-08-08 15:37:46 -07003872 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003873 self.waitUntilSettled()
3874
3875 self.assertEqual(A.reported, 0)
3876 self.assertEqual(B.reported, 0)
3877 self.assertEqual(A.data['status'], 'NEW')
3878 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003879
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003880 def test_crd_gate_unknown(self):
3881 "Test unknown projects in dependent pipeline"
3882 self.init_repo("org/unknown")
3883 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3884 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003885 A.addApproval('code-review', 2)
3886 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003887
3888 # A Depends-On: B
3889 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3890 A.subject, B.data['id'])
3891
James E. Blair8b5408c2016-08-08 15:37:46 -07003892 B.addApproval('approved', 1)
3893 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003894 self.waitUntilSettled()
3895
3896 # Unknown projects cannot share a queue with any other
3897 # since they don't have common jobs with any other (they have no jobs).
3898 # Changes which depend on unknown project changes
3899 # should not be processed in dependent pipeline
3900 self.assertEqual(A.data['status'], 'NEW')
3901 self.assertEqual(B.data['status'], 'NEW')
3902 self.assertEqual(A.reported, 0)
3903 self.assertEqual(B.reported, 0)
3904 self.assertEqual(len(self.history), 0)
3905
3906 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07003907 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003908 B.setMerged()
3909 self.waitUntilSettled()
3910 self.assertEqual(len(self.history), 0)
3911
3912 # Now that B is merged, A should be able to be enqueued and
3913 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003914 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003915 self.waitUntilSettled()
3916
3917 self.assertEqual(A.data['status'], 'MERGED')
3918 self.assertEqual(A.reported, 2)
3919 self.assertEqual(B.data['status'], 'MERGED')
3920 self.assertEqual(B.reported, 0)
3921
James E. Blairbfb8e042014-12-30 17:01:44 -08003922 def test_crd_check(self):
3923 "Test cross-repo dependencies in independent pipelines"
3924
Paul Belangerb30342b2016-11-14 12:30:43 -05003925 self.launch_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08003926 self.gearman_server.hold_jobs_in_queue = True
3927 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3928 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3929
3930 # A Depends-On: B
3931 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3932 A.subject, B.data['id'])
3933
3934 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3935 self.waitUntilSettled()
3936
3937 queue = self.gearman_server.getQueue()
3938 ref = self.getParameter(queue[-1], 'ZUUL_REF')
3939 self.gearman_server.hold_jobs_in_queue = False
3940 self.gearman_server.release()
3941 self.waitUntilSettled()
3942
Paul Belangerb30342b2016-11-14 12:30:43 -05003943 self.launch_server.release('.*-merge')
3944 self.waitUntilSettled()
3945
3946 path = os.path.join(self.builds[0].jobdir.git_root, "org/project1")
James E. Blairbfb8e042014-12-30 17:01:44 -08003947 repo = git.Repo(path)
3948 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
3949 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05003950 correct_messages = [
3951 'initial commit', 'add content from fixture', 'A-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08003952 self.assertEqual(repo_messages, correct_messages)
3953
Paul Belangerb30342b2016-11-14 12:30:43 -05003954 path = os.path.join(self.builds[0].jobdir.git_root, "org/project2")
James E. Blairbfb8e042014-12-30 17:01:44 -08003955 repo = git.Repo(path)
3956 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
3957 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05003958 correct_messages = [
3959 'initial commit', 'add content from fixture', 'B-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08003960 self.assertEqual(repo_messages, correct_messages)
3961
Paul Belangerb30342b2016-11-14 12:30:43 -05003962 self.launch_server.hold_jobs_in_build = False
3963 self.launch_server.release()
3964 self.waitUntilSettled()
3965
James E. Blairbfb8e042014-12-30 17:01:44 -08003966 self.assertEqual(A.data['status'], 'NEW')
3967 self.assertEqual(B.data['status'], 'NEW')
3968 self.assertEqual(A.reported, 1)
3969 self.assertEqual(B.reported, 0)
3970
3971 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05003972 tenant = self.sched.abide.tenants.get('tenant-one')
3973 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08003974
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003975 def test_crd_check_git_depends(self):
3976 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07003977 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003978 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3979 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3980
3981 # Add two git-dependent changes and make sure they both report
3982 # success.
3983 B.setDependsOn(A, 1)
3984 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3985 self.waitUntilSettled()
3986 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3987 self.waitUntilSettled()
3988
James E. Blairb8c16472015-05-05 14:55:26 -07003989 self.orderedRelease()
3990 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003991 self.waitUntilSettled()
3992
3993 self.assertEqual(A.data['status'], 'NEW')
3994 self.assertEqual(B.data['status'], 'NEW')
3995 self.assertEqual(A.reported, 1)
3996 self.assertEqual(B.reported, 1)
3997
3998 self.assertEqual(self.history[0].changes, '1,1')
3999 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004000 tenant = self.sched.abide.tenants.get('tenant-one')
4001 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004002
4003 self.assertIn('Build succeeded', A.messages[0])
4004 self.assertIn('Build succeeded', B.messages[0])
4005
4006 def test_crd_check_duplicate(self):
4007 "Test duplicate check in independent pipelines"
James E. Blair08d19992016-08-10 15:25:31 -07004008 self.launch_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004009 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4010 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004011 tenant = self.sched.abide.tenants.get('tenant-one')
4012 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004013
4014 # Add two git-dependent changes...
4015 B.setDependsOn(A, 1)
4016 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4017 self.waitUntilSettled()
4018 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4019
4020 # ...make sure the live one is not duplicated...
4021 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4022 self.waitUntilSettled()
4023 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4024
4025 # ...but the non-live one is able to be.
4026 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4027 self.waitUntilSettled()
4028 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4029
Clark Boylandd849822015-03-02 12:38:14 -08004030 # Release jobs in order to avoid races with change A jobs
4031 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004032 self.orderedRelease()
James E. Blair08d19992016-08-10 15:25:31 -07004033 self.launch_server.hold_jobs_in_build = False
4034 self.launch_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004035 self.waitUntilSettled()
4036
4037 self.assertEqual(A.data['status'], 'NEW')
4038 self.assertEqual(B.data['status'], 'NEW')
4039 self.assertEqual(A.reported, 1)
4040 self.assertEqual(B.reported, 1)
4041
4042 self.assertEqual(self.history[0].changes, '1,1 2,1')
4043 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004044 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004045
4046 self.assertIn('Build succeeded', A.messages[0])
4047 self.assertIn('Build succeeded', B.messages[0])
4048
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004049 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004050 "Test cross-repo dependencies re-enqueued in independent pipelines"
4051
4052 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004053 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4054 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004055
4056 # A Depends-On: B
4057 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4058 A.subject, B.data['id'])
4059
4060 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4061 self.waitUntilSettled()
4062
4063 self.sched.reconfigure(self.config)
4064
4065 # Make sure the items still share a change queue, and the
4066 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004067 tenant = self.sched.abide.tenants.get('tenant-one')
4068 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4069 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004070 first_item = queue.queue[0]
4071 for item in queue.queue:
4072 self.assertEqual(item.queue, first_item.queue)
4073 self.assertFalse(first_item.live)
4074 self.assertTrue(queue.queue[1].live)
4075
4076 self.gearman_server.hold_jobs_in_queue = False
4077 self.gearman_server.release()
4078 self.waitUntilSettled()
4079
4080 self.assertEqual(A.data['status'], 'NEW')
4081 self.assertEqual(B.data['status'], 'NEW')
4082 self.assertEqual(A.reported, 1)
4083 self.assertEqual(B.reported, 0)
4084
4085 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004086 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004087
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004088 def test_crd_check_reconfiguration(self):
4089 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4090
4091 def test_crd_undefined_project(self):
4092 """Test that undefined projects in dependencies are handled for
4093 independent pipelines"""
4094 # It's a hack for fake gerrit,
4095 # as it implies repo creation upon the creation of any change
4096 self.init_repo("org/unknown")
4097 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4098
James E. Blairec056492016-07-22 09:45:56 -07004099 @skip("Disabled for early v3 development")
James E. Blair17dd6772015-02-09 14:45:18 -08004100 def test_crd_check_ignore_dependencies(self):
4101 "Test cross-repo dependencies can be ignored"
James E. Blairf84026c2015-12-08 16:11:46 -08004102 self.updateConfigLayout(
4103 'tests/fixtures/layout-ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004104 self.sched.reconfigure(self.config)
4105 self.registerJobs()
4106
4107 self.gearman_server.hold_jobs_in_queue = True
4108 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4109 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4110 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4111
4112 # A Depends-On: B
4113 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4114 A.subject, B.data['id'])
4115 # C git-depends on B
4116 C.setDependsOn(B, 1)
4117 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4118 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4119 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4120 self.waitUntilSettled()
4121
4122 # Make sure none of the items share a change queue, and all
4123 # are live.
4124 check_pipeline = self.sched.layout.pipelines['check']
4125 self.assertEqual(len(check_pipeline.queues), 3)
4126 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4127 for item in check_pipeline.getAllItems():
4128 self.assertTrue(item.live)
4129
4130 self.gearman_server.hold_jobs_in_queue = False
4131 self.gearman_server.release()
4132 self.waitUntilSettled()
4133
4134 self.assertEqual(A.data['status'], 'NEW')
4135 self.assertEqual(B.data['status'], 'NEW')
4136 self.assertEqual(C.data['status'], 'NEW')
4137 self.assertEqual(A.reported, 1)
4138 self.assertEqual(B.reported, 1)
4139 self.assertEqual(C.reported, 1)
4140
4141 # Each job should have tested exactly one change
4142 for job in self.history:
4143 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004144
James E. Blairec056492016-07-22 09:45:56 -07004145 @skip("Disabled for early v3 development")
James E. Blair96698e22015-04-02 07:48:21 -07004146 def test_crd_check_transitive(self):
4147 "Test transitive cross-repo dependencies"
4148 # Specifically, if A -> B -> C, and C gets a new patchset and
4149 # A gets a new patchset, ensure the test of A,2 includes B,1
4150 # and C,2 (not C,1 which would indicate stale data in the
4151 # cache for B).
4152 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4153 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4154 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4155
4156 # A Depends-On: B
4157 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4158 A.subject, B.data['id'])
4159
4160 # B Depends-On: C
4161 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4162 B.subject, C.data['id'])
4163
4164 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4165 self.waitUntilSettled()
4166 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4167
4168 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4169 self.waitUntilSettled()
4170 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4171
4172 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4173 self.waitUntilSettled()
4174 self.assertEqual(self.history[-1].changes, '3,1')
4175
4176 C.addPatchset()
4177 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4178 self.waitUntilSettled()
4179 self.assertEqual(self.history[-1].changes, '3,2')
4180
4181 A.addPatchset()
4182 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4183 self.waitUntilSettled()
4184 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004185
James E. Blaira8b90b32016-08-24 15:18:50 -07004186 def test_crd_check_unknown(self):
4187 "Test unknown projects in independent pipeline"
4188 self.init_repo("org/unknown")
4189 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4190 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4191 # A Depends-On: B
4192 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4193 A.subject, B.data['id'])
4194
4195 # Make sure zuul has seen an event on B.
4196 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4197 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4198 self.waitUntilSettled()
4199
4200 self.assertEqual(A.data['status'], 'NEW')
4201 self.assertEqual(A.reported, 1)
4202 self.assertEqual(B.data['status'], 'NEW')
4203 self.assertEqual(B.reported, 0)
4204
James E. Blair92464a22016-04-05 10:21:26 -07004205 def test_crd_cycle_join(self):
4206 "Test an updated change creates a cycle"
4207 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4208
4209 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4210 self.waitUntilSettled()
4211
4212 # Create B->A
4213 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4214 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4215 B.subject, A.data['id'])
4216 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4217 self.waitUntilSettled()
4218
4219 # Update A to add A->B (a cycle).
4220 A.addPatchset()
4221 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4222 A.subject, B.data['id'])
4223 # Normally we would submit the patchset-created event for
4224 # processing here, however, we have no way of noting whether
4225 # the dependency cycle detection correctly raised an
4226 # exception, so instead, we reach into the source driver and
4227 # call the method that would ultimately be called by the event
4228 # processing.
4229
Paul Belanger0e155e22016-11-14 14:12:23 -05004230 tenant = self.sched.abide.tenants.get('tenant-one')
4231 source = tenant.layout.pipelines['gate'].source
4232
4233 # TODO(pabelanger): As we add more source / trigger APIs we should make
4234 # it easier for users to create events for testing.
4235 event = zuul.model.TriggerEvent()
4236 event.trigger_name = 'gerrit'
4237 event.change_number = '1'
4238 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004239 with testtools.ExpectedException(
4240 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004241 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004242 self.log.debug("Got expected dependency cycle exception")
4243
4244 # Now if we update B to remove the depends-on, everything
4245 # should be okay. B; A->B
4246
4247 B.addPatchset()
4248 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004249
4250 source.getChange(event, True)
4251 event.change_number = '2'
4252 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004253
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004254 def test_disable_at(self):
4255 "Test a pipeline will only report to the disabled trigger when failing"
4256
Paul Belanger7dc76e82016-11-11 16:51:08 -05004257 self.updateConfigLayout('layout-disabled-at')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004258 self.sched.reconfigure(self.config)
4259
Paul Belanger7dc76e82016-11-11 16:51:08 -05004260 tenant = self.sched.abide.tenants.get('openstack')
4261 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004262 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004263 0, tenant.layout.pipelines['check']._consecutive_failures)
4264 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004265
4266 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4267 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4268 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4269 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4270 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4271 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4272 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4273 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4274 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4275 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4276 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4277
James E. Blair08d19992016-08-10 15:25:31 -07004278 self.launch_server.failJob('project-test1', A)
4279 self.launch_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004280 # Let C pass, resetting the counter
James E. Blair08d19992016-08-10 15:25:31 -07004281 self.launch_server.failJob('project-test1', D)
4282 self.launch_server.failJob('project-test1', E)
4283 self.launch_server.failJob('project-test1', F)
4284 self.launch_server.failJob('project-test1', G)
4285 self.launch_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004286 # I also passes but should only report to the disabled reporters
James E. Blair08d19992016-08-10 15:25:31 -07004287 self.launch_server.failJob('project-test1', J)
4288 self.launch_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004289
4290 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4291 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4292 self.waitUntilSettled()
4293
4294 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004295 2, tenant.layout.pipelines['check']._consecutive_failures)
4296 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004297
4298 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4299 self.waitUntilSettled()
4300
4301 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004302 0, tenant.layout.pipelines['check']._consecutive_failures)
4303 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004304
4305 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4306 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4307 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4308 self.waitUntilSettled()
4309
4310 # We should be disabled now
4311 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004312 3, tenant.layout.pipelines['check']._consecutive_failures)
4313 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004314
4315 # We need to wait between each of these patches to make sure the
4316 # smtp messages come back in an expected order
4317 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4318 self.waitUntilSettled()
4319 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4320 self.waitUntilSettled()
4321 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4322 self.waitUntilSettled()
4323
4324 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4325 # leaving a message on each change
4326 self.assertEqual(1, len(A.messages))
4327 self.assertIn('Build failed.', A.messages[0])
4328 self.assertEqual(1, len(B.messages))
4329 self.assertIn('Build failed.', B.messages[0])
4330 self.assertEqual(1, len(C.messages))
4331 self.assertIn('Build succeeded.', C.messages[0])
4332 self.assertEqual(1, len(D.messages))
4333 self.assertIn('Build failed.', D.messages[0])
4334 self.assertEqual(1, len(E.messages))
4335 self.assertIn('Build failed.', E.messages[0])
4336 self.assertEqual(1, len(F.messages))
4337 self.assertIn('Build failed.', F.messages[0])
4338
4339 # The last 3 (GHI) would have only reported via smtp.
4340 self.assertEqual(3, len(self.smtp_messages))
4341 self.assertEqual(0, len(G.messages))
4342 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004343 self.assertIn(
4344 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004345 self.assertEqual(0, len(H.messages))
4346 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004347 self.assertIn(
4348 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004349 self.assertEqual(0, len(I.messages))
4350 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004351 self.assertIn(
4352 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004353
4354 # Now reload the configuration (simulate a HUP) to check the pipeline
4355 # comes out of disabled
4356 self.sched.reconfigure(self.config)
4357
Paul Belanger7dc76e82016-11-11 16:51:08 -05004358 tenant = self.sched.abide.tenants.get('openstack')
4359
4360 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004361 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004362 0, tenant.layout.pipelines['check']._consecutive_failures)
4363 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004364
4365 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4366 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4367 self.waitUntilSettled()
4368
4369 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004370 2, tenant.layout.pipelines['check']._consecutive_failures)
4371 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004372
4373 # J and K went back to gerrit
4374 self.assertEqual(1, len(J.messages))
4375 self.assertIn('Build failed.', J.messages[0])
4376 self.assertEqual(1, len(K.messages))
4377 self.assertIn('Build failed.', K.messages[0])
4378 # No more messages reported via smtp
4379 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004380
Paul Belanger71d98172016-11-08 10:56:31 -05004381 def test_rerun_on_abort(self):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004382 "Test that if a launch server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004383
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004384 self.launch_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004385 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4386 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4387 self.waitUntilSettled()
4388
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004389 self.launch_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004390 self.waitUntilSettled()
4391
4392 self.assertEqual(len(self.builds), 2)
4393 self.builds[0].requeue = True
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004394 self.launch_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004395 self.waitUntilSettled()
4396
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004397 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004398 self.assertEqual(len(self.builds), 1,
4399 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004400 self.builds[0].requeue = True
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004401 self.launch_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004402 self.waitUntilSettled()
4403
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004404 self.launch_server.hold_jobs_in_build = False
4405 self.launch_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004406 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004407 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004408 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4409 self.assertEqual(A.reported, 1)
4410 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004411
James E. Blair15be0e12017-01-03 13:45:20 -08004412 def test_zookeeper_disconnect(self):
4413 "Test that jobs are launched after a zookeeper disconnect"
4414
4415 self.fake_nodepool.paused = True
4416 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4417 A.addApproval('code-review', 2)
4418 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4419 self.waitUntilSettled()
4420
4421 self.zk.client.stop()
4422 self.zk.client.start()
4423 self.fake_nodepool.paused = False
4424 self.waitUntilSettled()
4425
4426 self.assertEqual(A.data['status'], 'MERGED')
4427 self.assertEqual(A.reported, 2)
4428
James E. Blair6ab79e02017-01-06 10:10:17 -08004429 def test_nodepool_failure(self):
4430 "Test that jobs are reported after a nodepool failure"
4431
4432 self.fake_nodepool.paused = True
4433 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4434 A.addApproval('code-review', 2)
4435 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4436 self.waitUntilSettled()
4437
4438 req = self.fake_nodepool.getNodeRequests()[0]
4439 self.fake_nodepool.addFailRequest(req)
4440
4441 self.fake_nodepool.paused = False
4442 self.waitUntilSettled()
4443
4444 self.assertEqual(A.data['status'], 'NEW')
4445 self.assertEqual(A.reported, 2)
4446 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4447 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4448 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4449
Arieb6f068c2016-10-09 13:11:06 +03004450
4451class TestDuplicatePipeline(ZuulTestCase):
4452 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4453
4454 def test_duplicate_pipelines(self):
4455 "Test that a change matching multiple pipelines works"
4456
4457 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4458 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4459 self.waitUntilSettled()
4460
4461 self.assertHistory([
4462 dict(name='project-test1', result='SUCCESS', changes='1,1',
4463 pipeline='dup1'),
4464 dict(name='project-test1', result='SUCCESS', changes='1,1',
4465 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004466 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004467
4468 self.assertEqual(len(A.messages), 2)
4469
Paul Belangera46a3742016-11-09 14:23:03 -05004470 if 'dup1' in A.messages[0]:
4471 self.assertIn('dup1', A.messages[0])
4472 self.assertNotIn('dup2', A.messages[0])
4473 self.assertIn('project-test1', A.messages[0])
4474 self.assertIn('dup2', A.messages[1])
4475 self.assertNotIn('dup1', A.messages[1])
4476 self.assertIn('project-test1', A.messages[1])
4477 else:
4478 self.assertIn('dup1', A.messages[1])
4479 self.assertNotIn('dup2', A.messages[1])
4480 self.assertIn('project-test1', A.messages[1])
4481 self.assertIn('dup2', A.messages[0])
4482 self.assertNotIn('dup1', A.messages[0])
4483 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004484
4485
4486class TestSchedulerOneJobProject(ZuulTestCase):
4487 tenant_config_file = 'config/one-job-project/main.yaml'
4488
4489 def test_one_job_project(self):
4490 "Test that queueing works with one job"
4491 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4492 'master', 'A')
4493 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4494 'master', 'B')
4495 A.addApproval('code-review', 2)
4496 B.addApproval('code-review', 2)
4497 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4498 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4499 self.waitUntilSettled()
4500
4501 self.assertEqual(A.data['status'], 'MERGED')
4502 self.assertEqual(A.reported, 2)
4503 self.assertEqual(B.data['status'], 'MERGED')
4504 self.assertEqual(B.reported, 2)
Paul Belanger86085b32016-11-03 12:48:57 -04004505
4506
4507class TestSchedulerTemplatedProject(ZuulTestCase):
4508 tenant_config_file = 'config/templated-project/main.yaml'
4509
4510 def test_job_from_templates_launched(self):
4511 "Test whether a job generated via a template can be launched"
4512
4513 A = self.fake_gerrit.addFakeChange(
4514 'org/templated-project', 'master', 'A')
4515 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4516 self.waitUntilSettled()
4517
4518 self.assertEqual(self.getJobFromHistory('project-test1').result,
4519 'SUCCESS')
4520 self.assertEqual(self.getJobFromHistory('project-test2').result,
4521 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004522
4523 def test_layered_templates(self):
4524 "Test whether a job generated via a template can be launched"
4525
4526 A = self.fake_gerrit.addFakeChange(
4527 'org/layered-project', 'master', 'A')
4528 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4529 self.waitUntilSettled()
4530
4531 self.assertEqual(self.getJobFromHistory('project-test1').result,
4532 'SUCCESS')
4533 self.assertEqual(self.getJobFromHistory('project-test2').result,
4534 'SUCCESS')
4535 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4536 ).result, 'SUCCESS')
4537 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4538 ).result, 'SUCCESS')
4539 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4540 ).result, 'SUCCESS')
4541 self.assertEqual(self.getJobFromHistory('project-test6').result,
4542 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004543
4544
4545class TestSchedulerSuccessURL(ZuulTestCase):
4546 tenant_config_file = 'config/success-url/main.yaml'
4547
4548 def test_success_url(self):
4549 "Ensure bad build params are ignored"
4550 self.sched.reconfigure(self.config)
4551 self.init_repo('org/docs')
4552
4553 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4554 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4555 self.waitUntilSettled()
4556
4557 # Both builds ran: docs-draft-test + docs-draft-test2
4558 self.assertEqual(len(self.history), 2)
4559
4560 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004561 for build in self.history:
4562 if build.name == 'docs-draft-test':
4563 uuid = build.uuid[:7]
4564 break
Adam Gandelman94a60062016-11-21 16:43:14 -08004565
4566 # Two msgs: 'Starting...' + results
4567 self.assertEqual(len(self.smtp_messages), 2)
4568 body = self.smtp_messages[1]['body'].splitlines()
4569 self.assertEqual('Build succeeded.', body[0])
4570
4571 self.assertIn(
4572 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4573 'docs-draft-test/%s/publish-docs/' % uuid,
4574 body[2])
4575
4576 # NOTE: This default URL is currently hard-coded in launcher/server.py
4577 self.assertIn(
4578 '- docs-draft-test2 https://server/job',
4579 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004580
4581
Adam Gandelman4da00f62016-12-09 15:47:33 -08004582class TestSchedulerMerges(ZuulTestCase):
4583 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004584
4585 def _test_project_merge_mode(self, mode):
4586 self.launch_server.keep_jobdir = False
4587 project = 'org/project-%s' % mode
4588 self.launch_server.hold_jobs_in_build = True
4589 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4590 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4591 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4592 A.addApproval('code-review', 2)
4593 B.addApproval('code-review', 2)
4594 C.addApproval('code-review', 2)
4595 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4596 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4597 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4598 self.waitUntilSettled()
4599
4600 build = self.builds[-1]
4601 ref = self.getParameter(build, 'ZUUL_REF')
4602
4603 path = os.path.join(build.jobdir.git_root, project)
4604 repo = git.Repo(path)
4605 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4606 repo_messages.reverse()
4607
4608 self.launch_server.hold_jobs_in_build = False
4609 self.launch_server.release()
4610 self.waitUntilSettled()
4611
4612 return repo_messages
4613
4614 def _test_merge(self, mode):
4615 us_path = os.path.join(
4616 self.upstream_root, 'org/project-%s' % mode)
4617 expected_messages = [
4618 'initial commit',
4619 'add content from fixture',
4620 # the intermediate commits order is nondeterministic
4621 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4622 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4623 ]
4624 result = self._test_project_merge_mode(mode)
4625 self.assertEqual(result[:2], expected_messages[:2])
4626 self.assertEqual(result[-2:], expected_messages[-2:])
4627
4628 def test_project_merge_mode_merge(self):
4629 self._test_merge('merge')
4630
4631 def test_project_merge_mode_merge_resolve(self):
4632 self._test_merge('merge-resolve')
4633
4634 def test_project_merge_mode_cherrypick(self):
4635 expected_messages = [
4636 'initial commit',
4637 'add content from fixture',
4638 'A-1',
4639 'B-1',
4640 'C-1']
4641 result = self._test_project_merge_mode('cherry-pick')
4642 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004643
4644 def test_merge_branch(self):
4645 "Test that the right commits are on alternate branches"
4646 self.create_branch('org/project-merge-branches', 'mp')
4647
4648 self.launch_server.hold_jobs_in_build = True
4649 A = self.fake_gerrit.addFakeChange(
4650 'org/project-merge-branches', 'mp', 'A')
4651 B = self.fake_gerrit.addFakeChange(
4652 'org/project-merge-branches', 'mp', 'B')
4653 C = self.fake_gerrit.addFakeChange(
4654 'org/project-merge-branches', 'mp', 'C')
4655 A.addApproval('code-review', 2)
4656 B.addApproval('code-review', 2)
4657 C.addApproval('code-review', 2)
4658 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4659 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4660 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4661 self.waitUntilSettled()
4662
4663 self.launch_server.release('.*-merge')
4664 self.waitUntilSettled()
4665 self.launch_server.release('.*-merge')
4666 self.waitUntilSettled()
4667 self.launch_server.release('.*-merge')
4668 self.waitUntilSettled()
4669
4670 build = self.builds[-1]
4671 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
4672 ref = self.getParameter(build, 'ZUUL_REF')
4673 path = os.path.join(
4674 build.jobdir.git_root, 'org/project-merge-branches')
4675 repo = git.Repo(path)
4676
4677 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4678 repo_messages.reverse()
4679 correct_messages = [
4680 'initial commit',
4681 'add content from fixture',
4682 'mp commit',
4683 'A-1', 'B-1', 'C-1']
4684 self.assertEqual(repo_messages, correct_messages)
4685
4686 self.launch_server.hold_jobs_in_build = False
4687 self.launch_server.release()
4688 self.waitUntilSettled()
4689
4690 def test_merge_multi_branch(self):
4691 "Test that dependent changes on multiple branches are merged"
4692 self.create_branch('org/project-merge-branches', 'mp')
4693
4694 self.launch_server.hold_jobs_in_build = True
4695 A = self.fake_gerrit.addFakeChange(
4696 'org/project-merge-branches', 'master', 'A')
4697 B = self.fake_gerrit.addFakeChange(
4698 'org/project-merge-branches', 'mp', 'B')
4699 C = self.fake_gerrit.addFakeChange(
4700 'org/project-merge-branches', 'master', 'C')
4701 A.addApproval('code-review', 2)
4702 B.addApproval('code-review', 2)
4703 C.addApproval('code-review', 2)
4704 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4705 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4706 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4707 self.waitUntilSettled()
4708
4709 job_A = None
4710 for job in self.builds:
4711 if 'project-merge' in job.name:
4712 job_A = job
4713 ref_A = self.getParameter(job_A, 'ZUUL_REF')
4714 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
4715 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
4716 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
4717
4718 path = os.path.join(
4719 job_A.jobdir.git_root, "org/project-merge-branches")
4720 repo = git.Repo(path)
4721 repo_messages = [c.message.strip()
4722 for c in repo.iter_commits(ref_A)]
4723 repo_messages.reverse()
4724 correct_messages = [
4725 'initial commit', 'add content from fixture', 'A-1']
4726 self.assertEqual(repo_messages, correct_messages)
4727
4728 self.launch_server.release('.*-merge')
4729 self.waitUntilSettled()
4730
4731 job_B = None
4732 for job in self.builds:
4733 if 'project-merge' in job.name:
4734 job_B = job
4735 ref_B = self.getParameter(job_B, 'ZUUL_REF')
4736 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
4737 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
4738 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
4739
4740 path = os.path.join(
4741 job_B.jobdir.git_root, "org/project-merge-branches")
4742 repo = git.Repo(path)
4743 repo_messages = [c.message.strip()
4744 for c in repo.iter_commits(ref_B)]
4745 repo_messages.reverse()
4746 correct_messages = [
4747 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4748 self.assertEqual(repo_messages, correct_messages)
4749
4750 self.launch_server.release('.*-merge')
4751 self.waitUntilSettled()
4752
4753 job_C = None
4754 for job in self.builds:
4755 if 'project-merge' in job.name:
4756 job_C = job
4757 ref_C = self.getParameter(job_C, 'ZUUL_REF')
4758 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
4759 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
4760 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
4761 path = os.path.join(
4762 job_C.jobdir.git_root, "org/project-merge-branches")
4763 repo = git.Repo(path)
4764 repo_messages = [c.message.strip()
4765 for c in repo.iter_commits(ref_C)]
4766
4767 repo_messages.reverse()
4768 correct_messages = [
4769 'initial commit', 'add content from fixture',
4770 'A-1', 'C-1']
4771 # Ensure the right commits are in the history for this ref
4772 self.assertEqual(repo_messages, correct_messages)
4773
4774 self.launch_server.hold_jobs_in_build = False
4775 self.launch_server.release()
4776 self.waitUntilSettled()