blob: 1713cac9b09aa725105327a5f3f7aa68f9c3e72d [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. Blair1843a552013-07-03 14:19:52 -070023import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040024import urllib2
Maru Newby3fe5f852015-01-13 04:22:14 +000025import yaml
Monty Taylorbc758832013-06-17 17:22:42 -040026
James E. Blair4886cc12012-07-18 15:39:41 -070027import git
Monty Taylorbc758832013-06-17 17:22:42 -040028import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070029
Maru Newby3fe5f852015-01-13 04:22:14 +000030import zuul.change_matcher
James E. Blairb0fcae42012-07-17 11:12:10 -070031import zuul.scheduler
James E. Blairad28e912013-11-27 10:43:22 -080032import zuul.rpcclient
Joshua Hesketh1879cf72013-08-19 14:13:15 +100033import zuul.reporter.gerrit
Joshua Hesketh5fea8672013-08-19 17:32:01 +100034import zuul.reporter.smtp
James E. Blairb0fcae42012-07-17 11:12:10 -070035
Maru Newby3fe5f852015-01-13 04:22:14 +000036from tests.base import (
37 BaseTestCase,
38 ZuulTestCase,
39 repack_repo,
40)
James E. Blairb0fcae42012-07-17 11:12:10 -070041
James E. Blair1f4c2bb2013-04-26 08:40:46 -070042logging.basicConfig(level=logging.DEBUG,
43 format='%(asctime)s %(name)-32s '
44 '%(levelname)-8s %(message)s')
James E. Blairb0fcae42012-07-17 11:12:10 -070045
46
Maru Newby3fe5f852015-01-13 04:22:14 +000047class TestSchedulerConfigParsing(BaseTestCase):
48
49 def test_parse_skip_if(self):
50 job_yaml = """
51jobs:
52 - name: job_name
53 skip-if:
54 - project: ^project_name$
55 branch: ^stable/icehouse$
56 all-files-match-any:
57 - ^filename$
58 - project: ^project2_name$
59 all-files-match-any:
60 - ^filename2$
61 """.strip()
62 data = yaml.load(job_yaml)
63 config_job = data.get('jobs')[0]
64 sched = zuul.scheduler.Scheduler()
65 cm = zuul.change_matcher
66 expected = cm.MatchAny([
67 cm.MatchAll([
68 cm.ProjectMatcher('^project_name$'),
69 cm.BranchMatcher('^stable/icehouse$'),
70 cm.MatchAllFiles([cm.FileMatcher('^filename$')]),
71 ]),
72 cm.MatchAll([
73 cm.ProjectMatcher('^project2_name$'),
74 cm.MatchAllFiles([cm.FileMatcher('^filename2$')]),
75 ]),
76 ])
77 matcher = sched._parseSkipIf(config_job)
78 self.assertEqual(expected, matcher)
79
80
Clark Boylanb640e052014-04-03 16:41:46 -070081class TestScheduler(ZuulTestCase):
Antoine Mussobd86a312014-01-08 14:51:33 +010082
James E. Blairb0fcae42012-07-17 11:12:10 -070083 def test_jobs_launched(self):
84 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070085
James E. Blairb0fcae42012-07-17 11:12:10 -070086 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -070087 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -070088 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
89 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040090 self.assertEqual(self.getJobFromHistory('project-merge').result,
91 'SUCCESS')
92 self.assertEqual(self.getJobFromHistory('project-test1').result,
93 'SUCCESS')
94 self.assertEqual(self.getJobFromHistory('project-test2').result,
95 'SUCCESS')
96 self.assertEqual(A.data['status'], 'MERGED')
97 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -070098
James E. Blair66eeebf2013-07-27 17:44:32 -070099 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
100 self.assertReportedStat('zuul.pipeline.gate.current_changes',
101 value='1|g')
102 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
103 kind='ms')
104 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
105 value='1|c')
106 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
107 self.assertReportedStat('zuul.pipeline.gate.total_changes',
108 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -0700109 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -0700110 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -0700111 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -0700112 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -0700113
James E. Blair5821bd92015-09-16 08:48:15 -0700114 for build in self.builds:
115 self.assertEqual(build.parameters['ZUUL_VOTING'], '1')
116
James E. Blair3cb10702013-08-24 08:56:03 -0700117 def test_initial_pipeline_gauges(self):
118 "Test that each pipeline reported its length on start"
119 pipeline_names = self.sched.layout.pipelines.keys()
120 self.assertNotEqual(len(pipeline_names), 0)
121 for name in pipeline_names:
122 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
123 value='0|g')
124
James E. Blair42f74822013-05-14 15:18:03 -0700125 def test_duplicate_pipelines(self):
126 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -0700127
James E. Blair42f74822013-05-14 15:18:03 -0700128 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
129 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
130 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -0700131
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400132 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400133 self.history[0].name == 'project-test1'
134 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -0700135
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400136 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -0700137 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400138 self.assertIn('dup1/project-test1', A.messages[0])
139 self.assertNotIn('dup2/project-test1', A.messages[0])
140 self.assertNotIn('dup1/project-test1', A.messages[1])
141 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -0700142 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400143 self.assertIn('dup1/project-test1', A.messages[1])
144 self.assertNotIn('dup2/project-test1', A.messages[1])
145 self.assertNotIn('dup1/project-test1', A.messages[0])
146 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -0700147
James E. Blairb0fcae42012-07-17 11:12:10 -0700148 def test_parallel_changes(self):
149 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700150
151 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700152 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
153 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
154 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700155 A.addApproval('CRVW', 2)
156 B.addApproval('CRVW', 2)
157 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700158
159 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
160 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
161 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
162
163 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400164 self.assertEqual(len(self.builds), 1)
165 self.assertEqual(self.builds[0].name, 'project-merge')
166 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700167
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700168 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700169 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400170 self.assertEqual(len(self.builds), 3)
171 self.assertEqual(self.builds[0].name, 'project-test1')
172 self.assertTrue(self.job_has_changes(self.builds[0], A))
173 self.assertEqual(self.builds[1].name, 'project-test2')
174 self.assertTrue(self.job_has_changes(self.builds[1], A))
175 self.assertEqual(self.builds[2].name, 'project-merge')
176 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700177
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700178 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700179 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400180 self.assertEqual(len(self.builds), 5)
181 self.assertEqual(self.builds[0].name, 'project-test1')
182 self.assertTrue(self.job_has_changes(self.builds[0], A))
183 self.assertEqual(self.builds[1].name, 'project-test2')
184 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700185
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400186 self.assertEqual(self.builds[2].name, 'project-test1')
187 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
188 self.assertEqual(self.builds[3].name, 'project-test2')
189 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700190
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400191 self.assertEqual(self.builds[4].name, 'project-merge')
192 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700193
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700194 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700195 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400196 self.assertEqual(len(self.builds), 6)
197 self.assertEqual(self.builds[0].name, 'project-test1')
198 self.assertTrue(self.job_has_changes(self.builds[0], A))
199 self.assertEqual(self.builds[1].name, 'project-test2')
200 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700201
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400202 self.assertEqual(self.builds[2].name, 'project-test1')
203 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
204 self.assertEqual(self.builds[3].name, 'project-test2')
205 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700206
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400207 self.assertEqual(self.builds[4].name, 'project-test1')
208 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
209 self.assertEqual(self.builds[5].name, 'project-test2')
210 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700211
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700212 self.worker.hold_jobs_in_build = False
213 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700214 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400215 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700216
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400217 self.assertEqual(len(self.history), 9)
218 self.assertEqual(A.data['status'], 'MERGED')
219 self.assertEqual(B.data['status'], 'MERGED')
220 self.assertEqual(C.data['status'], 'MERGED')
221 self.assertEqual(A.reported, 2)
222 self.assertEqual(B.reported, 2)
223 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700224
225 def test_failed_changes(self):
226 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -0400227 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700228
James E. Blairb02a3bb2012-07-30 17:49:55 -0700229 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
230 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -0700231 A.addApproval('CRVW', 2)
232 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700233
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700234 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700235
James E. Blaire2819012013-06-28 17:17:26 -0400236 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
237 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700238 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -0400239
240 self.worker.release('.*-merge')
241 self.waitUntilSettled()
242
243 self.worker.hold_jobs_in_build = False
244 self.worker.release()
245
246 self.waitUntilSettled()
247 # It's certain that the merge job for change 2 will run, but
248 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400249 self.assertTrue(len(self.history) > 6)
250 self.assertEqual(A.data['status'], 'NEW')
251 self.assertEqual(B.data['status'], 'MERGED')
252 self.assertEqual(A.reported, 2)
253 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700254
255 def test_independent_queues(self):
256 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700257
258 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900259 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700260 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
261 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700262 A.addApproval('CRVW', 2)
263 B.addApproval('CRVW', 2)
264 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700265
266 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
267 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
268 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
269
James E. Blairb02a3bb2012-07-30 17:49:55 -0700270 self.waitUntilSettled()
271
272 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400273 self.assertEqual(len(self.builds), 2)
274 self.assertEqual(self.builds[0].name, 'project-merge')
275 self.assertTrue(self.job_has_changes(self.builds[0], A))
276 self.assertEqual(self.builds[1].name, 'project1-merge')
277 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700278
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700279 # Release the current merge builds
280 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700281 self.waitUntilSettled()
282 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700283 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700284 self.waitUntilSettled()
285
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700286 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -0700287 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400288 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700289
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700290 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700291 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400292 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700293
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400294 self.assertEqual(len(self.history), 11)
295 self.assertEqual(A.data['status'], 'MERGED')
296 self.assertEqual(B.data['status'], 'MERGED')
297 self.assertEqual(C.data['status'], 'MERGED')
298 self.assertEqual(A.reported, 2)
299 self.assertEqual(B.reported, 2)
300 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700301
302 def test_failed_change_at_head(self):
303 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700304
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700305 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700306 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
307 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
308 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700309 A.addApproval('CRVW', 2)
310 B.addApproval('CRVW', 2)
311 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700312
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700313 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700314
315 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
316 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
317 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
318
319 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700320
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400321 self.assertEqual(len(self.builds), 1)
322 self.assertEqual(self.builds[0].name, 'project-merge')
323 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -0700324
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700325 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700326 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700327 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700328 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700329 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700330 self.waitUntilSettled()
331
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400332 self.assertEqual(len(self.builds), 6)
333 self.assertEqual(self.builds[0].name, 'project-test1')
334 self.assertEqual(self.builds[1].name, 'project-test2')
335 self.assertEqual(self.builds[2].name, 'project-test1')
336 self.assertEqual(self.builds[3].name, 'project-test2')
337 self.assertEqual(self.builds[4].name, 'project-test1')
338 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700339
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400340 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700341 self.waitUntilSettled()
342
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400343 # project-test2, project-merge for B
344 self.assertEqual(len(self.builds), 2)
345 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -0700346
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700347 self.worker.hold_jobs_in_build = False
348 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700349 self.waitUntilSettled()
350
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400351 self.assertEqual(len(self.builds), 0)
352 self.assertEqual(len(self.history), 15)
353 self.assertEqual(A.data['status'], 'NEW')
354 self.assertEqual(B.data['status'], 'MERGED')
355 self.assertEqual(C.data['status'], 'MERGED')
356 self.assertEqual(A.reported, 2)
357 self.assertEqual(B.reported, 2)
358 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700359
James E. Blair0aac4872013-08-23 14:02:38 -0700360 def test_failed_change_in_middle(self):
361 "Test a failed change in the middle of the queue"
362
363 self.worker.hold_jobs_in_build = True
364 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
365 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
366 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
367 A.addApproval('CRVW', 2)
368 B.addApproval('CRVW', 2)
369 C.addApproval('CRVW', 2)
370
371 self.worker.addFailTest('project-test1', B)
372
373 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
374 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
375 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
376
377 self.waitUntilSettled()
378
379 self.worker.release('.*-merge')
380 self.waitUntilSettled()
381 self.worker.release('.*-merge')
382 self.waitUntilSettled()
383 self.worker.release('.*-merge')
384 self.waitUntilSettled()
385
386 self.assertEqual(len(self.builds), 6)
387 self.assertEqual(self.builds[0].name, 'project-test1')
388 self.assertEqual(self.builds[1].name, 'project-test2')
389 self.assertEqual(self.builds[2].name, 'project-test1')
390 self.assertEqual(self.builds[3].name, 'project-test2')
391 self.assertEqual(self.builds[4].name, 'project-test1')
392 self.assertEqual(self.builds[5].name, 'project-test2')
393
394 self.release(self.builds[2])
395 self.waitUntilSettled()
396
James E. Blair972e3c72013-08-29 12:04:55 -0700397 # project-test1 and project-test2 for A
398 # project-test2 for B
399 # project-merge for C (without B)
400 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700401 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
402
James E. Blair972e3c72013-08-29 12:04:55 -0700403 self.worker.release('.*-merge')
404 self.waitUntilSettled()
405
406 # project-test1 and project-test2 for A
407 # project-test2 for B
408 # project-test1 and project-test2 for C
409 self.assertEqual(len(self.builds), 5)
410
James E. Blair0aac4872013-08-23 14:02:38 -0700411 items = self.sched.layout.pipelines['gate'].getAllItems()
412 builds = items[0].current_build_set.getBuilds()
413 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
414 self.assertEqual(self.countJobResults(builds, None), 2)
415 builds = items[1].current_build_set.getBuilds()
416 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
417 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
418 self.assertEqual(self.countJobResults(builds, None), 1)
419 builds = items[2].current_build_set.getBuilds()
420 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700421 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700422
423 self.worker.hold_jobs_in_build = False
424 self.worker.release()
425 self.waitUntilSettled()
426
427 self.assertEqual(len(self.builds), 0)
428 self.assertEqual(len(self.history), 12)
429 self.assertEqual(A.data['status'], 'MERGED')
430 self.assertEqual(B.data['status'], 'NEW')
431 self.assertEqual(C.data['status'], 'MERGED')
432 self.assertEqual(A.reported, 2)
433 self.assertEqual(B.reported, 2)
434 self.assertEqual(C.reported, 2)
435
James E. Blaird466dc42012-07-31 10:42:56 -0700436 def test_failed_change_at_head_with_queue(self):
437 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700438
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700439 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700440 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
441 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
442 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700443 A.addApproval('CRVW', 2)
444 B.addApproval('CRVW', 2)
445 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700446
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700447 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700448
449 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
450 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
451 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
452
453 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700454 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400455 self.assertEqual(len(self.builds), 0)
456 self.assertEqual(len(queue), 1)
457 self.assertEqual(queue[0].name, 'build:project-merge')
458 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -0700459
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700460 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700461 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700462 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700463 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700464 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700465 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700466 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700467
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400468 self.assertEqual(len(self.builds), 0)
469 self.assertEqual(len(queue), 6)
470 self.assertEqual(queue[0].name, 'build:project-test1')
471 self.assertEqual(queue[1].name, 'build:project-test2')
472 self.assertEqual(queue[2].name, 'build:project-test1')
473 self.assertEqual(queue[3].name, 'build:project-test2')
474 self.assertEqual(queue[4].name, 'build:project-test1')
475 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700476
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700477 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700478 self.waitUntilSettled()
479
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400480 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700481 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400482 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
483 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700484
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700485 self.gearman_server.hold_jobs_in_queue = False
486 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700487 self.waitUntilSettled()
488
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400489 self.assertEqual(len(self.builds), 0)
490 self.assertEqual(len(self.history), 11)
491 self.assertEqual(A.data['status'], 'NEW')
492 self.assertEqual(B.data['status'], 'MERGED')
493 self.assertEqual(C.data['status'], 'MERGED')
494 self.assertEqual(A.reported, 2)
495 self.assertEqual(B.reported, 2)
496 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700497
James E. Blairfef71632013-09-23 11:15:47 -0700498 def test_two_failed_changes_at_head(self):
499 "Test that changes are reparented correctly if 2 fail at head"
500
501 self.worker.hold_jobs_in_build = True
502 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
503 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
504 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
505 A.addApproval('CRVW', 2)
506 B.addApproval('CRVW', 2)
507 C.addApproval('CRVW', 2)
508
509 self.worker.addFailTest('project-test1', A)
510 self.worker.addFailTest('project-test1', B)
511
512 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
513 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
514 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
515 self.waitUntilSettled()
516
517 self.worker.release('.*-merge')
518 self.waitUntilSettled()
519 self.worker.release('.*-merge')
520 self.waitUntilSettled()
521 self.worker.release('.*-merge')
522 self.waitUntilSettled()
523
524 self.assertEqual(len(self.builds), 6)
525 self.assertEqual(self.builds[0].name, 'project-test1')
526 self.assertEqual(self.builds[1].name, 'project-test2')
527 self.assertEqual(self.builds[2].name, 'project-test1')
528 self.assertEqual(self.builds[3].name, 'project-test2')
529 self.assertEqual(self.builds[4].name, 'project-test1')
530 self.assertEqual(self.builds[5].name, 'project-test2')
531
532 self.assertTrue(self.job_has_changes(self.builds[0], A))
533 self.assertTrue(self.job_has_changes(self.builds[2], A))
534 self.assertTrue(self.job_has_changes(self.builds[2], B))
535 self.assertTrue(self.job_has_changes(self.builds[4], A))
536 self.assertTrue(self.job_has_changes(self.builds[4], B))
537 self.assertTrue(self.job_has_changes(self.builds[4], C))
538
539 # Fail change B first
540 self.release(self.builds[2])
541 self.waitUntilSettled()
542
543 # restart of C after B failure
544 self.worker.release('.*-merge')
545 self.waitUntilSettled()
546
547 self.assertEqual(len(self.builds), 5)
548 self.assertEqual(self.builds[0].name, 'project-test1')
549 self.assertEqual(self.builds[1].name, 'project-test2')
550 self.assertEqual(self.builds[2].name, 'project-test2')
551 self.assertEqual(self.builds[3].name, 'project-test1')
552 self.assertEqual(self.builds[4].name, 'project-test2')
553
554 self.assertTrue(self.job_has_changes(self.builds[1], A))
555 self.assertTrue(self.job_has_changes(self.builds[2], A))
556 self.assertTrue(self.job_has_changes(self.builds[2], B))
557 self.assertTrue(self.job_has_changes(self.builds[4], A))
558 self.assertFalse(self.job_has_changes(self.builds[4], B))
559 self.assertTrue(self.job_has_changes(self.builds[4], C))
560
561 # Finish running all passing jobs for change A
562 self.release(self.builds[1])
563 self.waitUntilSettled()
564 # Fail and report change A
565 self.release(self.builds[0])
566 self.waitUntilSettled()
567
568 # restart of B,C after A failure
569 self.worker.release('.*-merge')
570 self.waitUntilSettled()
571 self.worker.release('.*-merge')
572 self.waitUntilSettled()
573
574 self.assertEqual(len(self.builds), 4)
575 self.assertEqual(self.builds[0].name, 'project-test1') # B
576 self.assertEqual(self.builds[1].name, 'project-test2') # B
577 self.assertEqual(self.builds[2].name, 'project-test1') # C
578 self.assertEqual(self.builds[3].name, 'project-test2') # C
579
580 self.assertFalse(self.job_has_changes(self.builds[1], A))
581 self.assertTrue(self.job_has_changes(self.builds[1], B))
582 self.assertFalse(self.job_has_changes(self.builds[1], C))
583
584 self.assertFalse(self.job_has_changes(self.builds[2], A))
585 # After A failed and B and C restarted, B should be back in
586 # C's tests because it has not failed yet.
587 self.assertTrue(self.job_has_changes(self.builds[2], B))
588 self.assertTrue(self.job_has_changes(self.builds[2], C))
589
590 self.worker.hold_jobs_in_build = False
591 self.worker.release()
592 self.waitUntilSettled()
593
594 self.assertEqual(len(self.builds), 0)
595 self.assertEqual(len(self.history), 21)
596 self.assertEqual(A.data['status'], 'NEW')
597 self.assertEqual(B.data['status'], 'NEW')
598 self.assertEqual(C.data['status'], 'MERGED')
599 self.assertEqual(A.reported, 2)
600 self.assertEqual(B.reported, 2)
601 self.assertEqual(C.reported, 2)
602
James E. Blair8c803f82012-07-31 16:25:42 -0700603 def test_patch_order(self):
604 "Test that dependent patches are tested in the right order"
605 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
606 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
607 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
608 A.addApproval('CRVW', 2)
609 B.addApproval('CRVW', 2)
610 C.addApproval('CRVW', 2)
611
612 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
613 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
614 M2.setMerged()
615 M1.setMerged()
616
617 # C -> B -> A -> M1 -> M2
618 # M2 is here to make sure it is never queried. If it is, it
619 # means zuul is walking down the entire history of merged
620 # changes.
621
622 C.setDependsOn(B, 1)
623 B.setDependsOn(A, 1)
624 A.setDependsOn(M1, 1)
625 M1.setDependsOn(M2, 1)
626
627 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
628
629 self.waitUntilSettled()
630
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400631 self.assertEqual(A.data['status'], 'NEW')
632 self.assertEqual(B.data['status'], 'NEW')
633 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700634
635 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
636 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
637
638 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400639 self.assertEqual(M2.queried, 0)
640 self.assertEqual(A.data['status'], 'MERGED')
641 self.assertEqual(B.data['status'], 'MERGED')
642 self.assertEqual(C.data['status'], 'MERGED')
643 self.assertEqual(A.reported, 2)
644 self.assertEqual(B.reported, 2)
645 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700646
James E. Blair063672f2015-01-29 13:09:12 -0800647 def test_needed_changes_enqueue(self):
648 "Test that a needed change is enqueued ahead"
649 # A Given a git tree like this, if we enqueue
650 # / \ change C, we should walk up and down the tree
651 # B G and enqueue changes in the order ABCDEFG.
652 # /|\ This is also the order that you would get if
653 # *C E F you enqueued changes in the order ABCDEFG, so
654 # / the ordering is stable across re-enqueue events.
655 # D
656
657 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
658 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
659 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
660 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
661 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
662 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
663 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
664 B.setDependsOn(A, 1)
665 C.setDependsOn(B, 1)
666 D.setDependsOn(C, 1)
667 E.setDependsOn(B, 1)
668 F.setDependsOn(B, 1)
669 G.setDependsOn(A, 1)
670
671 A.addApproval('CRVW', 2)
672 B.addApproval('CRVW', 2)
673 C.addApproval('CRVW', 2)
674 D.addApproval('CRVW', 2)
675 E.addApproval('CRVW', 2)
676 F.addApproval('CRVW', 2)
677 G.addApproval('CRVW', 2)
678 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
679
680 self.waitUntilSettled()
681
682 self.assertEqual(A.data['status'], 'NEW')
683 self.assertEqual(B.data['status'], 'NEW')
684 self.assertEqual(C.data['status'], 'NEW')
685 self.assertEqual(D.data['status'], 'NEW')
686 self.assertEqual(E.data['status'], 'NEW')
687 self.assertEqual(F.data['status'], 'NEW')
688 self.assertEqual(G.data['status'], 'NEW')
689
690 # We're about to add approvals to changes without adding the
691 # triggering events to Zuul, so that we can be sure that it is
692 # enqueing the changes based on dependencies, not because of
693 # triggering events. Since it will have the changes cached
694 # already (without approvals), we need to clear the cache
695 # first.
696 source = self.sched.layout.pipelines['gate'].source
697 source.maintainCache([])
698
699 self.worker.hold_jobs_in_build = True
700 A.addApproval('APRV', 1)
701 B.addApproval('APRV', 1)
702 D.addApproval('APRV', 1)
703 E.addApproval('APRV', 1)
704 F.addApproval('APRV', 1)
705 G.addApproval('APRV', 1)
706 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
707
708 for x in range(8):
709 self.worker.release('.*-merge')
710 self.waitUntilSettled()
711 self.worker.hold_jobs_in_build = False
712 self.worker.release()
713 self.waitUntilSettled()
714
715 self.assertEqual(A.data['status'], 'MERGED')
716 self.assertEqual(B.data['status'], 'MERGED')
717 self.assertEqual(C.data['status'], 'MERGED')
718 self.assertEqual(D.data['status'], 'MERGED')
719 self.assertEqual(E.data['status'], 'MERGED')
720 self.assertEqual(F.data['status'], 'MERGED')
721 self.assertEqual(G.data['status'], 'MERGED')
722 self.assertEqual(A.reported, 2)
723 self.assertEqual(B.reported, 2)
724 self.assertEqual(C.reported, 2)
725 self.assertEqual(D.reported, 2)
726 self.assertEqual(E.reported, 2)
727 self.assertEqual(F.reported, 2)
728 self.assertEqual(G.reported, 2)
729 self.assertEqual(self.history[6].changes,
730 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
731
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100732 def test_source_cache(self):
733 "Test that the source cache operates correctly"
James E. Blair0e933c52013-07-11 10:18:52 -0700734 self.worker.hold_jobs_in_build = True
735
736 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
737 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
738 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
739 A.addApproval('CRVW', 2)
740 B.addApproval('CRVW', 2)
741
742 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
743 M1.setMerged()
744
745 B.setDependsOn(A, 1)
746 A.setDependsOn(M1, 1)
747
748 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
749 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
750
751 self.waitUntilSettled()
752
753 for build in self.builds:
754 if build.parameters['ZUUL_PIPELINE'] == 'check':
755 build.release()
756 self.waitUntilSettled()
757 for build in self.builds:
758 if build.parameters['ZUUL_PIPELINE'] == 'check':
759 build.release()
760 self.waitUntilSettled()
761
762 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
763 self.waitUntilSettled()
764
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100765 self.log.debug("len %s" % self.gerrit_source._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700766 # there should still be changes in the cache
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100767 self.assertNotEqual(len(self.gerrit_source._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700768
769 self.worker.hold_jobs_in_build = False
770 self.worker.release()
771 self.waitUntilSettled()
772
773 self.assertEqual(A.data['status'], 'MERGED')
774 self.assertEqual(B.data['status'], 'MERGED')
775 self.assertEqual(A.queried, 2) # Initial and isMerged
776 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
777
James E. Blair8c803f82012-07-31 16:25:42 -0700778 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700779 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700780 # TODO: move to test_gerrit (this is a unit test!)
781 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairc0dedf82014-08-06 09:37:52 -0700782 source = self.sched.layout.pipelines['gate'].source
783 a = source._getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -0400784 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700785 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700786
787 A.addApproval('CRVW', 2)
James E. Blairc0dedf82014-08-06 09:37:52 -0700788 a = source._getChange(1, 2, refresh=True)
789 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700790
791 A.addApproval('APRV', 1)
James E. Blairc0dedf82014-08-06 09:37:52 -0700792 a = source._getChange(1, 2, refresh=True)
793 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
794 source.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -0700795
796 def test_build_configuration(self):
797 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700798
799 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -0700800 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
801 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
802 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
803 A.addApproval('CRVW', 2)
804 B.addApproval('CRVW', 2)
805 C.addApproval('CRVW', 2)
806 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
807 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
808 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
809 self.waitUntilSettled()
810
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700811 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -0700812 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700813 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -0700814 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700815 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -0700816 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700817 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700818 ref = self.getParameter(queue[-1], 'ZUUL_REF')
819 self.gearman_server.hold_jobs_in_queue = False
820 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700821 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -0700822
Monty Taylorbc758832013-06-17 17:22:42 -0400823 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -0700824 repo = git.Repo(path)
825 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
826 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -0700827 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400828 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -0700829
830 def test_build_configuration_conflict(self):
831 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700832
833 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -0700834 A = self.fake_gerrit.addFakeChange('org/conflict-project',
835 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -0700836 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700837 B = self.fake_gerrit.addFakeChange('org/conflict-project',
838 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -0700839 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700840 C = self.fake_gerrit.addFakeChange('org/conflict-project',
841 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -0700842 A.addApproval('CRVW', 2)
843 B.addApproval('CRVW', 2)
844 C.addApproval('CRVW', 2)
845 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
846 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
847 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
848 self.waitUntilSettled()
849
James E. Blair6736beb2013-07-11 15:18:15 -0700850 self.assertEqual(A.reported, 1)
851 self.assertEqual(B.reported, 1)
852 self.assertEqual(C.reported, 1)
853
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700854 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700855 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700856 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700857 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700858 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700859 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700860
861 self.assertEqual(len(self.history), 2) # A and C merge jobs
862
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700863 self.gearman_server.hold_jobs_in_queue = False
864 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700865 self.waitUntilSettled()
866
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400867 self.assertEqual(A.data['status'], 'MERGED')
868 self.assertEqual(B.data['status'], 'NEW')
869 self.assertEqual(C.data['status'], 'MERGED')
870 self.assertEqual(A.reported, 2)
871 self.assertEqual(B.reported, 2)
872 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -0700873 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -0700874
James E. Blairdaabed22012-08-15 15:38:57 -0700875 def test_post(self):
876 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700877
Zhongyue Luo5d556072012-09-21 02:00:47 +0900878 e = {
879 "type": "ref-updated",
880 "submitter": {
881 "name": "User Name",
882 },
883 "refUpdate": {
884 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
885 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
886 "refName": "master",
887 "project": "org/project",
888 }
889 }
James E. Blairdaabed22012-08-15 15:38:57 -0700890 self.fake_gerrit.addEvent(e)
891 self.waitUntilSettled()
892
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400893 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400894 self.assertEqual(len(self.history), 1)
895 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -0700896
K Jonathan Harkerf95e7232015-04-29 13:33:16 -0700897 def test_post_ignore_deletes(self):
898 "Test that deleting refs does not trigger post jobs"
899
900 e = {
901 "type": "ref-updated",
902 "submitter": {
903 "name": "User Name",
904 },
905 "refUpdate": {
906 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
907 "newRev": "0000000000000000000000000000000000000000",
908 "refName": "master",
909 "project": "org/project",
910 }
911 }
912 self.fake_gerrit.addEvent(e)
913 self.waitUntilSettled()
914
915 job_names = [x.name for x in self.history]
916 self.assertEqual(len(self.history), 0)
917 self.assertNotIn('project-post', job_names)
918
919 def test_post_ignore_deletes_negative(self):
920 "Test that deleting refs does trigger post jobs"
921
922 self.config.set('zuul', 'layout_config',
923 'tests/fixtures/layout-dont-ignore-deletes.yaml')
924 self.sched.reconfigure(self.config)
925
926 e = {
927 "type": "ref-updated",
928 "submitter": {
929 "name": "User Name",
930 },
931 "refUpdate": {
932 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
933 "newRev": "0000000000000000000000000000000000000000",
934 "refName": "master",
935 "project": "org/project",
936 }
937 }
938 self.fake_gerrit.addEvent(e)
939 self.waitUntilSettled()
940
941 job_names = [x.name for x in self.history]
942 self.assertEqual(len(self.history), 1)
943 self.assertIn('project-post', job_names)
944
James E. Blairc6294a52012-08-17 10:19:48 -0700945 def test_build_configuration_branch(self):
946 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700947
948 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -0700949 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
950 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
951 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
952 A.addApproval('CRVW', 2)
953 B.addApproval('CRVW', 2)
954 C.addApproval('CRVW', 2)
955 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
956 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
957 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
958 self.waitUntilSettled()
959
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700960 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -0700961 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700962 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -0700963 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700964 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -0700965 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700966 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700967 ref = self.getParameter(queue[-1], 'ZUUL_REF')
968 self.gearman_server.hold_jobs_in_queue = False
969 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -0700970 self.waitUntilSettled()
971
Monty Taylorbc758832013-06-17 17:22:42 -0400972 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -0700973 repo = git.Repo(path)
974 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
975 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -0700976 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400977 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -0700978
979 def test_build_configuration_branch_interaction(self):
980 "Test that switching between branches works"
981 self.test_build_configuration()
982 self.test_build_configuration_branch()
983 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -0400984 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -0700985 repo = git.Repo(path)
986 repo.heads.master.commit = repo.commit('init')
987 self.test_build_configuration()
988
989 def test_build_configuration_multi_branch(self):
990 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700991
992 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -0700993 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
994 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
995 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
996 A.addApproval('CRVW', 2)
997 B.addApproval('CRVW', 2)
998 C.addApproval('CRVW', 2)
999 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1000 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1001 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1002 self.waitUntilSettled()
James E. Blairbb1fe502014-03-04 10:15:06 -08001003 queue = self.gearman_server.getQueue()
1004 job_A = None
1005 for job in queue:
1006 if 'project-merge' in job.name:
1007 job_A = job
1008 ref_A = self.getParameter(job_A, 'ZUUL_REF')
1009 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
1010 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
1011 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
James E. Blairc6294a52012-08-17 10:19:48 -07001012
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001013 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001014 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001015 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001016 job_B = None
1017 for job in queue:
1018 if 'project-merge' in job.name:
1019 job_B = job
1020 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001021 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001022 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blairbb1fe502014-03-04 10:15:06 -08001023 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
1024
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001025 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001026 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001027 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001028 for job in queue:
1029 if 'project-merge' in job.name:
1030 job_C = job
1031 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001032 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001033 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blairbb1fe502014-03-04 10:15:06 -08001034 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001035 self.gearman_server.hold_jobs_in_queue = False
1036 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001037 self.waitUntilSettled()
1038
Monty Taylorbc758832013-06-17 17:22:42 -04001039 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001040 repo = git.Repo(path)
1041
1042 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001043 for c in repo.iter_commits(ref_C)]
James E. Blairbb1fe502014-03-04 10:15:06 -08001044 repo_shas = [c.hexsha for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001045 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001046 correct_messages = ['initial commit', 'A-1', 'C-1']
James E. Blairbb1fe502014-03-04 10:15:06 -08001047 # Ensure the right commits are in the history for this ref
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001048 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08001049 # Ensure ZUUL_REF -> ZUUL_COMMIT
1050 self.assertEqual(repo_shas[0], commit_C)
James E. Blairc6294a52012-08-17 10:19:48 -07001051
1052 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001053 for c in repo.iter_commits(ref_B)]
James E. Blairbb1fe502014-03-04 10:15:06 -08001054 repo_shas = [c.hexsha for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001055 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001056 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001057 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08001058 self.assertEqual(repo_shas[0], commit_B)
1059
1060 repo_messages = [c.message.strip()
1061 for c in repo.iter_commits(ref_A)]
1062 repo_shas = [c.hexsha for c in repo.iter_commits(ref_A)]
1063 repo_messages.reverse()
1064 correct_messages = ['initial commit', 'A-1']
1065 self.assertEqual(repo_messages, correct_messages)
1066 self.assertEqual(repo_shas[0], commit_A)
1067
1068 self.assertNotEqual(ref_A, ref_B, ref_C)
1069 self.assertNotEqual(commit_A, commit_B, commit_C)
James E. Blair7f71c802012-08-22 13:04:32 -07001070
1071 def test_one_job_project(self):
1072 "Test that queueing works with one job"
1073 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1074 'master', 'A')
1075 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1076 'master', 'B')
1077 A.addApproval('CRVW', 2)
1078 B.addApproval('CRVW', 2)
1079 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1080 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1081 self.waitUntilSettled()
1082
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001083 self.assertEqual(A.data['status'], 'MERGED')
1084 self.assertEqual(A.reported, 2)
1085 self.assertEqual(B.data['status'], 'MERGED')
1086 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001087
Antoine Musso80edd5a2013-02-13 15:37:53 +01001088 def test_job_from_templates_launched(self):
1089 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001090
Antoine Musso80edd5a2013-02-13 15:37:53 +01001091 A = self.fake_gerrit.addFakeChange(
1092 'org/templated-project', 'master', 'A')
1093 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1094 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001095
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001096 self.assertEqual(self.getJobFromHistory('project-test1').result,
1097 'SUCCESS')
1098 self.assertEqual(self.getJobFromHistory('project-test2').result,
1099 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001100
James E. Blair3e98c022013-12-16 15:25:38 -08001101 def test_layered_templates(self):
1102 "Test whether a job generated via a template can be launched"
1103
1104 A = self.fake_gerrit.addFakeChange(
1105 'org/layered-project', 'master', 'A')
1106 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1107 self.waitUntilSettled()
1108
1109 self.assertEqual(self.getJobFromHistory('project-test1').result,
1110 'SUCCESS')
1111 self.assertEqual(self.getJobFromHistory('project-test2').result,
1112 'SUCCESS')
James E. Blairaea6cf62013-12-16 15:38:12 -08001113 self.assertEqual(self.getJobFromHistory('layered-project-test3'
1114 ).result, 'SUCCESS')
1115 self.assertEqual(self.getJobFromHistory('layered-project-test4'
1116 ).result, 'SUCCESS')
James E. Blair12a92b12014-03-26 11:54:53 -07001117 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
1118 ).result, 'SUCCESS')
James E. Blair3e98c022013-12-16 15:25:38 -08001119 self.assertEqual(self.getJobFromHistory('project-test6').result,
1120 'SUCCESS')
1121
James E. Blaircaec0c52012-08-22 14:52:22 -07001122 def test_dependent_changes_dequeue(self):
1123 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001124
James E. Blaircaec0c52012-08-22 14:52:22 -07001125 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1126 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1127 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1128 A.addApproval('CRVW', 2)
1129 B.addApproval('CRVW', 2)
1130 C.addApproval('CRVW', 2)
1131
1132 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1133 M1.setMerged()
1134
1135 # C -> B -> A -> M1
1136
1137 C.setDependsOn(B, 1)
1138 B.setDependsOn(A, 1)
1139 A.setDependsOn(M1, 1)
1140
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001141 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001142
1143 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1144 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1145 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1146
1147 self.waitUntilSettled()
1148
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001149 self.assertEqual(A.data['status'], 'NEW')
1150 self.assertEqual(A.reported, 2)
1151 self.assertEqual(B.data['status'], 'NEW')
1152 self.assertEqual(B.reported, 2)
1153 self.assertEqual(C.data['status'], 'NEW')
1154 self.assertEqual(C.reported, 2)
1155 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001156
James E. Blair972e3c72013-08-29 12:04:55 -07001157 def test_failing_dependent_changes(self):
1158 "Test that failing dependent patches are taken out of stream"
1159 self.worker.hold_jobs_in_build = True
1160 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1161 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1162 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1163 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1164 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
1165 A.addApproval('CRVW', 2)
1166 B.addApproval('CRVW', 2)
1167 C.addApproval('CRVW', 2)
1168 D.addApproval('CRVW', 2)
1169 E.addApproval('CRVW', 2)
1170
1171 # E, D -> C -> B, A
1172
1173 D.setDependsOn(C, 1)
1174 C.setDependsOn(B, 1)
1175
1176 self.worker.addFailTest('project-test1', B)
1177
1178 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1179 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1180 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1181 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1182 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1183
1184 self.waitUntilSettled()
1185 self.worker.release('.*-merge')
1186 self.waitUntilSettled()
1187 self.worker.release('.*-merge')
1188 self.waitUntilSettled()
1189 self.worker.release('.*-merge')
1190 self.waitUntilSettled()
1191 self.worker.release('.*-merge')
1192 self.waitUntilSettled()
1193 self.worker.release('.*-merge')
1194 self.waitUntilSettled()
1195
1196 self.worker.hold_jobs_in_build = False
1197 for build in self.builds:
1198 if build.parameters['ZUUL_CHANGE'] != '1':
1199 build.release()
1200 self.waitUntilSettled()
1201
1202 self.worker.release()
1203 self.waitUntilSettled()
1204
1205 self.assertEqual(A.data['status'], 'MERGED')
1206 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001207 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001208 self.assertEqual(B.data['status'], 'NEW')
1209 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001210 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001211 self.assertEqual(C.data['status'], 'NEW')
1212 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001213 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001214 self.assertEqual(D.data['status'], 'NEW')
1215 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001216 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001217 self.assertEqual(E.data['status'], 'MERGED')
1218 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001219 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001220 self.assertEqual(len(self.history), 18)
1221
James E. Blairec590122012-08-22 15:19:31 -07001222 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001223 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001224 # If it's dequeued more than once, we should see extra
1225 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001226
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001227 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001228 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1229 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1230 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1231 A.addApproval('CRVW', 2)
1232 B.addApproval('CRVW', 2)
1233 C.addApproval('CRVW', 2)
1234
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001235 self.worker.addFailTest('project1-test1', A)
1236 self.worker.addFailTest('project1-test2', A)
1237 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001238
1239 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1240 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1241 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1242
1243 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001244
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001245 self.assertEqual(len(self.builds), 1)
1246 self.assertEqual(self.builds[0].name, 'project1-merge')
1247 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07001248
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001249 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001250 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001251 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001252 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001253 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001254 self.waitUntilSettled()
1255
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001256 self.assertEqual(len(self.builds), 9)
1257 self.assertEqual(self.builds[0].name, 'project1-test1')
1258 self.assertEqual(self.builds[1].name, 'project1-test2')
1259 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
1260 self.assertEqual(self.builds[3].name, 'project1-test1')
1261 self.assertEqual(self.builds[4].name, 'project1-test2')
1262 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
1263 self.assertEqual(self.builds[6].name, 'project1-test1')
1264 self.assertEqual(self.builds[7].name, 'project1-test2')
1265 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001266
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001267 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001268 self.waitUntilSettled()
1269
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001270 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1271 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001272
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001273 self.worker.hold_jobs_in_build = False
1274 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001275 self.waitUntilSettled()
1276
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001277 self.assertEqual(len(self.builds), 0)
1278 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001279
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001280 self.assertEqual(A.data['status'], 'NEW')
1281 self.assertEqual(B.data['status'], 'MERGED')
1282 self.assertEqual(C.data['status'], 'MERGED')
1283 self.assertEqual(A.reported, 2)
1284 self.assertEqual(B.reported, 2)
1285 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001286
1287 def test_nonvoting_job(self):
1288 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001289
James E. Blair4ec821f2012-08-23 15:28:28 -07001290 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1291 'master', 'A')
1292 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001293 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001294 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1295
1296 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001297
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001298 self.assertEqual(A.data['status'], 'MERGED')
1299 self.assertEqual(A.reported, 2)
1300 self.assertEqual(
1301 self.getJobFromHistory('nonvoting-project-merge').result,
1302 'SUCCESS')
1303 self.assertEqual(
1304 self.getJobFromHistory('nonvoting-project-test1').result,
1305 'SUCCESS')
1306 self.assertEqual(
1307 self.getJobFromHistory('nonvoting-project-test2').result,
1308 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001309
James E. Blair5821bd92015-09-16 08:48:15 -07001310 for build in self.builds:
1311 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1312
James E. Blaire0487072012-08-29 17:38:31 -07001313 def test_check_queue_success(self):
1314 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001315
James E. Blaire0487072012-08-29 17:38:31 -07001316 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1317 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1318
1319 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001320
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001321 self.assertEqual(A.data['status'], 'NEW')
1322 self.assertEqual(A.reported, 1)
1323 self.assertEqual(self.getJobFromHistory('project-merge').result,
1324 'SUCCESS')
1325 self.assertEqual(self.getJobFromHistory('project-test1').result,
1326 'SUCCESS')
1327 self.assertEqual(self.getJobFromHistory('project-test2').result,
1328 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001329
1330 def test_check_queue_failure(self):
1331 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001332
James E. Blaire0487072012-08-29 17:38:31 -07001333 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001334 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001335 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1336
1337 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001338
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001339 self.assertEqual(A.data['status'], 'NEW')
1340 self.assertEqual(A.reported, 1)
1341 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001342 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001343 self.assertEqual(self.getJobFromHistory('project-test1').result,
1344 'SUCCESS')
1345 self.assertEqual(self.getJobFromHistory('project-test2').result,
1346 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001347
1348 def test_dependent_behind_dequeue(self):
1349 "test that dependent changes behind dequeued changes work"
1350 # This complicated test is a reproduction of a real life bug
1351 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001352
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001353 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001354 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1355 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1356 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1357 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1358 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1359 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1360 D.setDependsOn(C, 1)
1361 E.setDependsOn(D, 1)
1362 A.addApproval('CRVW', 2)
1363 B.addApproval('CRVW', 2)
1364 C.addApproval('CRVW', 2)
1365 D.addApproval('CRVW', 2)
1366 E.addApproval('CRVW', 2)
1367 F.addApproval('CRVW', 2)
1368
1369 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001370
1371 # Change object re-use in the gerrit trigger is hidden if
1372 # changes are added in quick succession; waiting makes it more
1373 # like real life.
1374 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1375 self.waitUntilSettled()
1376 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1377 self.waitUntilSettled()
1378
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001379 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001380 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001381 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001382 self.waitUntilSettled()
1383
1384 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1385 self.waitUntilSettled()
1386 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1387 self.waitUntilSettled()
1388 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1389 self.waitUntilSettled()
1390 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1391 self.waitUntilSettled()
1392
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001393 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001394 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001395 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001396 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001397 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001398 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001399 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001400 self.waitUntilSettled()
1401
1402 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001403
1404 # Grab pointers to the jobs we want to release before
1405 # releasing any, because list indexes may change as
1406 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001407 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001408 a.release()
1409 b.release()
1410 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001411 self.waitUntilSettled()
1412
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001413 self.worker.hold_jobs_in_build = False
1414 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07001415 self.waitUntilSettled()
1416
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001417 self.assertEqual(A.data['status'], 'NEW')
1418 self.assertEqual(B.data['status'], 'MERGED')
1419 self.assertEqual(C.data['status'], 'MERGED')
1420 self.assertEqual(D.data['status'], 'MERGED')
1421 self.assertEqual(E.data['status'], 'MERGED')
1422 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001423
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001424 self.assertEqual(A.reported, 2)
1425 self.assertEqual(B.reported, 2)
1426 self.assertEqual(C.reported, 2)
1427 self.assertEqual(D.reported, 2)
1428 self.assertEqual(E.reported, 2)
1429 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001430
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001431 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1432 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001433
1434 def test_merger_repack(self):
1435 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001436
James E. Blair05fed602012-09-07 12:45:24 -07001437 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1438 A.addApproval('CRVW', 2)
1439 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1440 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001441 self.assertEqual(self.getJobFromHistory('project-merge').result,
1442 'SUCCESS')
1443 self.assertEqual(self.getJobFromHistory('project-test1').result,
1444 'SUCCESS')
1445 self.assertEqual(self.getJobFromHistory('project-test2').result,
1446 'SUCCESS')
1447 self.assertEqual(A.data['status'], 'MERGED')
1448 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001449 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07001450 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001451
Monty Taylorbc758832013-06-17 17:22:42 -04001452 path = os.path.join(self.git_root, "org/project")
1453 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001454
1455 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1456 A.addApproval('CRVW', 2)
1457 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1458 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001459 self.assertEqual(self.getJobFromHistory('project-merge').result,
1460 'SUCCESS')
1461 self.assertEqual(self.getJobFromHistory('project-test1').result,
1462 'SUCCESS')
1463 self.assertEqual(self.getJobFromHistory('project-test2').result,
1464 'SUCCESS')
1465 self.assertEqual(A.data['status'], 'MERGED')
1466 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001467
James E. Blair4886f282012-11-15 09:27:33 -08001468 def test_merger_repack_large_change(self):
1469 "Test that the merger works with large changes after a repack"
1470 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001471 # This test assumes the repo is already cloned; make sure it is
Joshua Hesketh850ccb62014-11-27 11:31:02 +11001472 url = self.sched.sources['gerrit'].getGitUrl(
James E. Blairac2c3242014-01-24 13:38:51 -08001473 self.sched.layout.projects['org/project1'])
James E. Blair4076e2b2014-01-28 12:42:20 -08001474 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08001475 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1476 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001477 path = os.path.join(self.upstream_root, "org/project1")
1478 print repack_repo(path)
1479 path = os.path.join(self.git_root, "org/project1")
1480 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001481
1482 A.addApproval('CRVW', 2)
1483 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1484 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001485 self.assertEqual(self.getJobFromHistory('project1-merge').result,
1486 'SUCCESS')
1487 self.assertEqual(self.getJobFromHistory('project1-test1').result,
1488 'SUCCESS')
1489 self.assertEqual(self.getJobFromHistory('project1-test2').result,
1490 'SUCCESS')
1491 self.assertEqual(A.data['status'], 'MERGED')
1492 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001493
James E. Blair7ee88a22012-09-12 18:59:31 +02001494 def test_nonexistent_job(self):
1495 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001496 # Set to the state immediately after a restart
1497 self.resetGearmanServer()
1498 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02001499
1500 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1501 A.addApproval('CRVW', 2)
1502 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1503 # There may be a thread about to report a lost change
1504 while A.reported < 2:
1505 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001506 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001507 self.assertFalse(job_names)
1508 self.assertEqual(A.data['status'], 'NEW')
1509 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001510 self.assertEmptyQueues()
1511
1512 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001513 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02001514 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1515 A.addApproval('CRVW', 2)
1516 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1517 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001518 self.assertEqual(self.getJobFromHistory('project-merge').result,
1519 'SUCCESS')
1520 self.assertEqual(self.getJobFromHistory('project-test1').result,
1521 'SUCCESS')
1522 self.assertEqual(self.getJobFromHistory('project-test2').result,
1523 'SUCCESS')
1524 self.assertEqual(A.data['status'], 'MERGED')
1525 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08001526
1527 def test_single_nonexistent_post_job(self):
1528 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08001529 e = {
1530 "type": "ref-updated",
1531 "submitter": {
1532 "name": "User Name",
1533 },
1534 "refUpdate": {
1535 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1536 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1537 "refName": "master",
1538 "project": "org/project",
1539 }
1540 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001541 # Set to the state immediately after a restart
1542 self.resetGearmanServer()
1543 self.launcher.negative_function_cache_ttl = 0
1544
James E. Blairf62d4282012-12-31 17:01:50 -08001545 self.fake_gerrit.addEvent(e)
1546 self.waitUntilSettled()
1547
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001548 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08001549
1550 def test_new_patchset_dequeues_old(self):
1551 "Test that a new patchset causes the old to be dequeued"
1552 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001553 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001554 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1555 M.setMerged()
1556
1557 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1558 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1559 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1560 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1561 A.addApproval('CRVW', 2)
1562 B.addApproval('CRVW', 2)
1563 C.addApproval('CRVW', 2)
1564 D.addApproval('CRVW', 2)
1565
1566 C.setDependsOn(B, 1)
1567 B.setDependsOn(A, 1)
1568 A.setDependsOn(M, 1)
1569
1570 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1571 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1572 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1573 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1574 self.waitUntilSettled()
1575
1576 B.addPatchset()
1577 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1578 self.waitUntilSettled()
1579
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001580 self.worker.hold_jobs_in_build = False
1581 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001582 self.waitUntilSettled()
1583
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001584 self.assertEqual(A.data['status'], 'MERGED')
1585 self.assertEqual(A.reported, 2)
1586 self.assertEqual(B.data['status'], 'NEW')
1587 self.assertEqual(B.reported, 2)
1588 self.assertEqual(C.data['status'], 'NEW')
1589 self.assertEqual(C.reported, 2)
1590 self.assertEqual(D.data['status'], 'MERGED')
1591 self.assertEqual(D.reported, 2)
1592 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001593
James E. Blairba437362015-02-07 11:41:52 -08001594 def test_new_patchset_check(self):
1595 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001596
1597 self.worker.hold_jobs_in_build = True
1598
1599 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001600 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1601 check_pipeline = self.sched.layout.pipelines['check']
1602
1603 # Add two git-dependent changes
1604 B.setDependsOn(A, 1)
1605 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1606 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001607 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1608 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001609
1610 # A live item, and a non-live/live pair
1611 items = check_pipeline.getAllItems()
1612 self.assertEqual(len(items), 3)
1613
1614 self.assertEqual(items[0].change.number, '1')
1615 self.assertEqual(items[0].change.patchset, '1')
1616 self.assertFalse(items[0].live)
1617
1618 self.assertEqual(items[1].change.number, '2')
1619 self.assertEqual(items[1].change.patchset, '1')
1620 self.assertTrue(items[1].live)
1621
1622 self.assertEqual(items[2].change.number, '1')
1623 self.assertEqual(items[2].change.patchset, '1')
1624 self.assertTrue(items[2].live)
1625
1626 # Add a new patchset to A
1627 A.addPatchset()
1628 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1629 self.waitUntilSettled()
1630
1631 # The live copy of A,1 should be gone, but the non-live and B
1632 # should continue, and we should have a new A,2
1633 items = check_pipeline.getAllItems()
1634 self.assertEqual(len(items), 3)
1635
1636 self.assertEqual(items[0].change.number, '1')
1637 self.assertEqual(items[0].change.patchset, '1')
1638 self.assertFalse(items[0].live)
1639
1640 self.assertEqual(items[1].change.number, '2')
1641 self.assertEqual(items[1].change.patchset, '1')
1642 self.assertTrue(items[1].live)
1643
1644 self.assertEqual(items[2].change.number, '1')
1645 self.assertEqual(items[2].change.patchset, '2')
1646 self.assertTrue(items[2].live)
1647
1648 # Add a new patchset to B
1649 B.addPatchset()
1650 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1651 self.waitUntilSettled()
1652
1653 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1654 # but we should have a new B,2 (still based on A,1)
1655 items = check_pipeline.getAllItems()
1656 self.assertEqual(len(items), 3)
1657
1658 self.assertEqual(items[0].change.number, '1')
1659 self.assertEqual(items[0].change.patchset, '2')
1660 self.assertTrue(items[0].live)
1661
1662 self.assertEqual(items[1].change.number, '1')
1663 self.assertEqual(items[1].change.patchset, '1')
1664 self.assertFalse(items[1].live)
1665
1666 self.assertEqual(items[2].change.number, '2')
1667 self.assertEqual(items[2].change.patchset, '2')
1668 self.assertTrue(items[2].live)
1669
1670 self.builds[0].release()
1671 self.waitUntilSettled()
1672 self.builds[0].release()
1673 self.waitUntilSettled()
1674 self.worker.hold_jobs_in_build = False
1675 self.worker.release()
1676 self.waitUntilSettled()
1677
1678 self.assertEqual(A.reported, 1)
1679 self.assertEqual(B.reported, 1)
1680 self.assertEqual(self.history[0].result, 'ABORTED')
1681 self.assertEqual(self.history[0].changes, '1,1')
1682 self.assertEqual(self.history[1].result, 'ABORTED')
1683 self.assertEqual(self.history[1].changes, '1,1 2,1')
1684 self.assertEqual(self.history[2].result, 'SUCCESS')
1685 self.assertEqual(self.history[2].changes, '1,2')
1686 self.assertEqual(self.history[3].result, 'SUCCESS')
1687 self.assertEqual(self.history[3].changes, '1,1 2,2')
1688
1689 def test_abandoned_gate(self):
1690 "Test that an abandoned change is dequeued from gate"
1691
1692 self.worker.hold_jobs_in_build = True
1693
1694 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1695 A.addApproval('CRVW', 2)
1696 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1697 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001698 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1699 self.assertEqual(self.builds[0].name, 'project-merge')
1700
1701 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1702 self.waitUntilSettled()
1703
Antoine Mussobd86a312014-01-08 14:51:33 +01001704 self.worker.release('.*-merge')
1705 self.waitUntilSettled()
1706
1707 self.assertEqual(len(self.builds), 0, "No job running")
Antoine Mussobd86a312014-01-08 14:51:33 +01001708 self.assertEqual(len(self.history), 1, "Only one build in history")
1709 self.assertEqual(self.history[0].result, 'ABORTED',
James E. Blairba437362015-02-07 11:41:52 -08001710 "Build should have been aborted")
1711 self.assertEqual(A.reported, 1,
1712 "Abandoned gate change should report only start")
1713
1714 def test_abandoned_check(self):
1715 "Test that an abandoned change is dequeued from check"
1716
1717 self.worker.hold_jobs_in_build = True
1718
1719 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1720 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1721 check_pipeline = self.sched.layout.pipelines['check']
1722
1723 # Add two git-dependent changes
1724 B.setDependsOn(A, 1)
1725 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1726 self.waitUntilSettled()
1727 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1728 self.waitUntilSettled()
1729 # A live item, and a non-live/live pair
1730 items = check_pipeline.getAllItems()
1731 self.assertEqual(len(items), 3)
1732
1733 self.assertEqual(items[0].change.number, '1')
1734 self.assertFalse(items[0].live)
1735
1736 self.assertEqual(items[1].change.number, '2')
1737 self.assertTrue(items[1].live)
1738
1739 self.assertEqual(items[2].change.number, '1')
1740 self.assertTrue(items[2].live)
1741
1742 # Abandon A
1743 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1744 self.waitUntilSettled()
1745
1746 # The live copy of A should be gone, but the non-live and B
1747 # should continue
1748 items = check_pipeline.getAllItems()
1749 self.assertEqual(len(items), 2)
1750
1751 self.assertEqual(items[0].change.number, '1')
1752 self.assertFalse(items[0].live)
1753
1754 self.assertEqual(items[1].change.number, '2')
1755 self.assertTrue(items[1].live)
1756
1757 self.worker.hold_jobs_in_build = False
1758 self.worker.release()
1759 self.waitUntilSettled()
1760
1761 self.assertEqual(len(self.history), 4)
1762 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001763 'Build should have been aborted')
1764 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001765 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001766
Steve Varnau7b78b312015-04-03 14:49:46 -07001767 def test_abandoned_not_timer(self):
1768 "Test that an abandoned change does not cancel timer jobs"
1769
1770 self.worker.hold_jobs_in_build = True
1771
1772 # Start timer trigger - also org/project
1773 self.config.set('zuul', 'layout_config',
1774 'tests/fixtures/layout-idle.yaml')
1775 self.sched.reconfigure(self.config)
1776 self.registerJobs()
1777 # The pipeline triggers every second, so we should have seen
1778 # several by now.
1779 time.sleep(5)
1780 self.waitUntilSettled()
1781 # Stop queuing timer triggered jobs so that the assertions
1782 # below don't race against more jobs being queued.
1783 self.config.set('zuul', 'layout_config',
1784 'tests/fixtures/layout-no-timer.yaml')
1785 self.sched.reconfigure(self.config)
1786 self.registerJobs()
1787 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1788
1789 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1790 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1791 self.waitUntilSettled()
1792 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1793
1794 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1795 self.waitUntilSettled()
1796
1797 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1798
1799 self.worker.release()
1800 self.waitUntilSettled()
1801
Arx Cruzb1b010d2013-10-28 19:49:59 -02001802 def test_zuul_url_return(self):
1803 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001804 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001805 self.worker.hold_jobs_in_build = True
1806
1807 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1808 A.addApproval('CRVW', 2)
1809 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1810 self.waitUntilSettled()
1811
1812 self.assertEqual(len(self.builds), 1)
1813 for build in self.builds:
1814 self.assertTrue('ZUUL_URL' in build.parameters)
1815
1816 self.worker.hold_jobs_in_build = False
1817 self.worker.release()
1818 self.waitUntilSettled()
1819
James E. Blair2fa50962013-01-30 21:50:41 -08001820 def test_new_patchset_dequeues_old_on_head(self):
1821 "Test that a new patchset causes the old to be dequeued (at head)"
1822 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001823 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001824 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1825 M.setMerged()
1826 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1827 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1828 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1829 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1830 A.addApproval('CRVW', 2)
1831 B.addApproval('CRVW', 2)
1832 C.addApproval('CRVW', 2)
1833 D.addApproval('CRVW', 2)
1834
1835 C.setDependsOn(B, 1)
1836 B.setDependsOn(A, 1)
1837 A.setDependsOn(M, 1)
1838
1839 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1840 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1841 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1842 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1843 self.waitUntilSettled()
1844
1845 A.addPatchset()
1846 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1847 self.waitUntilSettled()
1848
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001849 self.worker.hold_jobs_in_build = False
1850 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001851 self.waitUntilSettled()
1852
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001853 self.assertEqual(A.data['status'], 'NEW')
1854 self.assertEqual(A.reported, 2)
1855 self.assertEqual(B.data['status'], 'NEW')
1856 self.assertEqual(B.reported, 2)
1857 self.assertEqual(C.data['status'], 'NEW')
1858 self.assertEqual(C.reported, 2)
1859 self.assertEqual(D.data['status'], 'MERGED')
1860 self.assertEqual(D.reported, 2)
1861 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001862
1863 def test_new_patchset_dequeues_old_without_dependents(self):
1864 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001865 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001866 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1867 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1868 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1869 A.addApproval('CRVW', 2)
1870 B.addApproval('CRVW', 2)
1871 C.addApproval('CRVW', 2)
1872
1873 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1874 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1875 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1876 self.waitUntilSettled()
1877
1878 B.addPatchset()
1879 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1880 self.waitUntilSettled()
1881
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001882 self.worker.hold_jobs_in_build = False
1883 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001884 self.waitUntilSettled()
1885
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001886 self.assertEqual(A.data['status'], 'MERGED')
1887 self.assertEqual(A.reported, 2)
1888 self.assertEqual(B.data['status'], 'NEW')
1889 self.assertEqual(B.reported, 2)
1890 self.assertEqual(C.data['status'], 'MERGED')
1891 self.assertEqual(C.reported, 2)
1892 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001893
1894 def test_new_patchset_dequeues_old_independent_queue(self):
1895 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001896 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001897 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1898 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1899 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1900 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1901 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1902 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1903 self.waitUntilSettled()
1904
1905 B.addPatchset()
1906 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1907 self.waitUntilSettled()
1908
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001909 self.worker.hold_jobs_in_build = False
1910 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001911 self.waitUntilSettled()
1912
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001913 self.assertEqual(A.data['status'], 'NEW')
1914 self.assertEqual(A.reported, 1)
1915 self.assertEqual(B.data['status'], 'NEW')
1916 self.assertEqual(B.reported, 1)
1917 self.assertEqual(C.data['status'], 'NEW')
1918 self.assertEqual(C.reported, 1)
1919 self.assertEqual(len(self.history), 10)
1920 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001921
James E. Blair18c64442014-03-18 10:14:45 -07001922 def test_noop_job(self):
1923 "Test that the internal noop job works"
1924 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
1925 A.addApproval('CRVW', 2)
1926 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1927 self.waitUntilSettled()
1928
1929 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1930 self.assertTrue(self.sched._areAllBuildsComplete())
1931 self.assertEqual(len(self.history), 0)
1932 self.assertEqual(A.data['status'], 'MERGED')
1933 self.assertEqual(A.reported, 2)
1934
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001935 def test_no_job_project(self):
1936 "Test that reports with no jobs don't get sent"
1937 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1938 'master', 'A')
1939 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1940 self.waitUntilSettled()
1941
1942 # Change wasn't reported to
1943 self.assertEqual(A.reported, False)
1944
1945 # Check queue is empty afterwards
1946 check_pipeline = self.sched.layout.pipelines['check']
1947 items = check_pipeline.getAllItems()
1948 self.assertEqual(len(items), 0)
1949
1950 self.assertEqual(len(self.history), 0)
1951
James E. Blair7d0dedc2013-02-21 17:26:09 -08001952 def test_zuul_refs(self):
1953 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001954 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001955 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1956 M1.setMerged()
1957 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1958 M2.setMerged()
1959
1960 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1961 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1962 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1963 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1964 A.addApproval('CRVW', 2)
1965 B.addApproval('CRVW', 2)
1966 C.addApproval('CRVW', 2)
1967 D.addApproval('CRVW', 2)
1968 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1969 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1970 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1971 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1972
1973 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001974 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001975 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001976 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001977 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001978 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001979 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001980 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001981 self.waitUntilSettled()
1982
James E. Blair7d0dedc2013-02-21 17:26:09 -08001983 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001984 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001985 if x.parameters['ZUUL_CHANGE'] == '3':
1986 a_zref = x.parameters['ZUUL_REF']
1987 if x.parameters['ZUUL_CHANGE'] == '4':
1988 b_zref = x.parameters['ZUUL_REF']
1989 if x.parameters['ZUUL_CHANGE'] == '5':
1990 c_zref = x.parameters['ZUUL_REF']
1991 if x.parameters['ZUUL_CHANGE'] == '6':
1992 d_zref = x.parameters['ZUUL_REF']
1993
1994 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001995 self.assertIsNotNone(a_zref)
1996 self.assertIsNotNone(b_zref)
1997 self.assertIsNotNone(c_zref)
1998 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001999
2000 # And they should all be different
2001 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002002 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002003
2004 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002005 self.assertTrue(self.ref_has_change(a_zref, A))
2006 self.assertFalse(self.ref_has_change(a_zref, B))
2007 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002008
2009 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002010 self.assertTrue(self.ref_has_change(b_zref, A))
2011 self.assertTrue(self.ref_has_change(b_zref, B))
2012 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002013
2014 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002015 self.assertTrue(self.ref_has_change(c_zref, A))
2016 self.assertTrue(self.ref_has_change(c_zref, B))
2017 self.assertTrue(self.ref_has_change(c_zref, C))
2018 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002019
2020 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002021 self.assertTrue(self.ref_has_change(d_zref, A))
2022 self.assertTrue(self.ref_has_change(d_zref, B))
2023 self.assertTrue(self.ref_has_change(d_zref, C))
2024 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002025
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002026 self.worker.hold_jobs_in_build = False
2027 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002028 self.waitUntilSettled()
2029
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002030 self.assertEqual(A.data['status'], 'MERGED')
2031 self.assertEqual(A.reported, 2)
2032 self.assertEqual(B.data['status'], 'MERGED')
2033 self.assertEqual(B.reported, 2)
2034 self.assertEqual(C.data['status'], 'MERGED')
2035 self.assertEqual(C.reported, 2)
2036 self.assertEqual(D.data['status'], 'MERGED')
2037 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002038
James E. Blair4a28a882013-08-23 15:17:33 -07002039 def test_rerun_on_error(self):
2040 "Test that if a worker fails to run a job, it is run again"
2041 self.worker.hold_jobs_in_build = True
2042 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2043 A.addApproval('CRVW', 2)
2044 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2045 self.waitUntilSettled()
2046
2047 self.builds[0].run_error = True
2048 self.worker.hold_jobs_in_build = False
2049 self.worker.release()
2050 self.waitUntilSettled()
2051 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2052 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2053
James E. Blair412e5582013-04-22 15:50:12 -07002054 def test_statsd(self):
2055 "Test each of the statsd methods used in the scheduler"
2056 import extras
2057 statsd = extras.try_import('statsd.statsd')
2058 statsd.incr('test-incr')
2059 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002060 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002061 self.assertReportedStat('test-incr', '1|c')
2062 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002063 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002064
James E. Blairdad52252014-02-07 16:59:17 -08002065 def test_stuck_job_cleanup(self):
2066 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07002067 # This job won't be registered at startup because it is not in
2068 # the standard layout, but we need it to already be registerd
2069 # for when we reconfigure, as that is when Zuul will attempt
2070 # to run the new job.
2071 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002072 self.gearman_server.hold_jobs_in_queue = True
2073 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2074 A.addApproval('CRVW', 2)
2075 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2076 self.waitUntilSettled()
2077 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2078
2079 self.config.set('zuul', 'layout_config',
2080 'tests/fixtures/layout-no-jobs.yaml')
2081 self.sched.reconfigure(self.config)
2082 self.waitUntilSettled()
2083
James E. Blair18c64442014-03-18 10:14:45 -07002084 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002085 self.waitUntilSettled()
2086 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2087 self.assertTrue(self.sched._areAllBuildsComplete())
2088
2089 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002090 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002091 self.assertEqual(self.history[0].result, 'SUCCESS')
2092
James E. Blair879dafb2015-07-17 14:04:49 -07002093 def test_file_head(self):
2094 # This is a regression test for an observed bug. A change
2095 # with a file named "HEAD" in the root directory of the repo
2096 # was processed by a merger. It then was unable to reset the
2097 # repo because of:
2098 # GitCommandError: 'git reset --hard HEAD' returned
2099 # with exit code 128
2100 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2101 # and filename
2102 # Use '--' to separate filenames from revisions'
2103
2104 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2105 A.addPatchset(['HEAD'])
2106 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2107
2108 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2109 self.waitUntilSettled()
2110
2111 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2112 self.waitUntilSettled()
2113
2114 self.assertIn('Build succeeded', A.messages[0])
2115 self.assertIn('Build succeeded', B.messages[0])
2116
James E. Blair70c71582013-03-06 08:50:50 -08002117 def test_file_jobs(self):
2118 "Test that file jobs run only when appropriate"
2119 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2120 A.addPatchset(['pip-requires'])
2121 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2122 A.addApproval('CRVW', 2)
2123 B.addApproval('CRVW', 2)
2124 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2125 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2126 self.waitUntilSettled()
2127
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002128 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002129 if x.name == 'project-testfile']
2130
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002131 self.assertEqual(len(testfile_jobs), 1)
2132 self.assertEqual(testfile_jobs[0].changes, '1,2')
2133 self.assertEqual(A.data['status'], 'MERGED')
2134 self.assertEqual(A.reported, 2)
2135 self.assertEqual(B.data['status'], 'MERGED')
2136 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002137
Maru Newby3fe5f852015-01-13 04:22:14 +00002138 def _test_skip_if_jobs(self, branch, should_skip):
2139 "Test that jobs with a skip-if filter run only when appropriate"
2140 self.config.set('zuul', 'layout_config',
2141 'tests/fixtures/layout-skip-if.yaml')
2142 self.sched.reconfigure(self.config)
2143 self.registerJobs()
2144
2145 change = self.fake_gerrit.addFakeChange('org/project',
2146 branch,
2147 'test skip-if')
2148 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2149 self.waitUntilSettled()
2150
2151 tested_change_ids = [x.changes[0] for x in self.history
2152 if x.name == 'project-test-skip-if']
2153
2154 if should_skip:
2155 self.assertEqual([], tested_change_ids)
2156 else:
2157 self.assertIn(change.data['number'], tested_change_ids)
2158
2159 def test_skip_if_match_skips_job(self):
2160 self._test_skip_if_jobs(branch='master', should_skip=True)
2161
2162 def test_skip_if_no_match_runs_job(self):
2163 self._test_skip_if_jobs(branch='mp', should_skip=False)
2164
James E. Blair3c5e5b52013-04-26 11:17:03 -07002165 def test_test_config(self):
2166 "Test that we can test the config"
2167 sched = zuul.scheduler.Scheduler()
Joshua Hesketh850ccb62014-11-27 11:31:02 +11002168 sched.registerSource(None, 'gerrit')
James E. Blair6c358e72013-07-29 17:06:47 -07002169 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002170 sched.registerTrigger(None, 'timer')
James E. Blairc494d542014-08-06 09:23:52 -07002171 sched.registerTrigger(None, 'zuul')
Clark Boylanb640e052014-04-03 16:41:46 -07002172 sched.testConfig(self.config.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002173
2174 def test_build_description(self):
2175 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002176 self.worker.registerFunction('set_description:' +
2177 self.worker.worker_id)
2178
2179 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2180 A.addApproval('CRVW', 2)
2181 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2182 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002183 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002184 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002185 self.assertTrue(re.search("Branch.*master", desc))
2186 self.assertTrue(re.search("Pipeline.*gate", desc))
2187 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2188 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2189 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2190 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002191
James E. Blairc8a1e052014-02-25 09:29:26 -08002192 def test_queue_names(self):
2193 "Test shared change queue names"
2194 project1 = self.sched.layout.projects['org/project1']
2195 project2 = self.sched.layout.projects['org/project2']
2196 q1 = self.sched.layout.pipelines['gate'].getQueue(project1)
2197 q2 = self.sched.layout.pipelines['gate'].getQueue(project2)
2198 self.assertEqual(q1.name, 'integration')
2199 self.assertEqual(q2.name, 'integration')
2200
2201 self.config.set('zuul', 'layout_config',
2202 'tests/fixtures/layout-bad-queue.yaml')
2203 with testtools.ExpectedException(
2204 Exception, "More than one name assigned to change queue"):
2205 self.sched.reconfigure(self.config)
2206
James E. Blair64ed6f22013-07-10 14:07:23 -07002207 def test_queue_precedence(self):
2208 "Test that queue precedence works"
2209
2210 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002211 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002212 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2213 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2214 A.addApproval('CRVW', 2)
2215 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2216
2217 self.waitUntilSettled()
2218 self.gearman_server.hold_jobs_in_queue = False
2219 self.gearman_server.release()
2220 self.waitUntilSettled()
2221
James E. Blair8de58bd2013-07-18 16:23:33 -07002222 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002223 self.orderedRelease()
James E. Blair8de58bd2013-07-18 16:23:33 -07002224 self.worker.hold_jobs_in_build = False
2225 self.waitUntilSettled()
2226
James E. Blair64ed6f22013-07-10 14:07:23 -07002227 self.log.debug(self.history)
2228 self.assertEqual(self.history[0].pipeline, 'gate')
2229 self.assertEqual(self.history[1].pipeline, 'check')
2230 self.assertEqual(self.history[2].pipeline, 'gate')
2231 self.assertEqual(self.history[3].pipeline, 'gate')
2232 self.assertEqual(self.history[4].pipeline, 'check')
2233 self.assertEqual(self.history[5].pipeline, 'check')
2234
Clark Boylana5edbe42014-06-03 16:39:10 -07002235 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002236 "Test that we can retrieve JSON status info"
2237 self.worker.hold_jobs_in_build = True
2238 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2239 A.addApproval('CRVW', 2)
2240 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2241 self.waitUntilSettled()
2242
2243 port = self.webapp.server.socket.getsockname()[1]
2244
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04002245 req = urllib2.Request("http://localhost:%s/status.json" % port)
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04002246 f = urllib2.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002247 headers = f.info()
2248 self.assertIn('Content-Length', headers)
2249 self.assertIn('Content-Type', headers)
2250 self.assertEqual(headers['Content-Type'],
2251 'application/json; charset=UTF-8')
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002252 self.assertIn('Access-Control-Allow-Origin', headers)
2253 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002254 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002255 self.assertIn('Expires', headers)
James E. Blair1843a552013-07-03 14:19:52 -07002256 data = f.read()
2257
2258 self.worker.hold_jobs_in_build = False
2259 self.worker.release()
2260 self.waitUntilSettled()
2261
2262 data = json.loads(data)
2263 status_jobs = set()
2264 for p in data['pipelines']:
2265 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002266 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002267 self.assertEqual(q['window'], 20)
2268 else:
2269 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002270 for head in q['heads']:
2271 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002272 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002273 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002274 for job in change['jobs']:
2275 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002276 self.assertIn('project-merge', status_jobs)
2277 self.assertIn('project-test1', status_jobs)
2278 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002279
James E. Blairc3d428e2013-12-03 15:06:48 -08002280 def test_merging_queues(self):
2281 "Test that transitively-connected change queues are merged"
2282 self.config.set('zuul', 'layout_config',
2283 'tests/fixtures/layout-merge-queues.yaml')
2284 self.sched.reconfigure(self.config)
2285 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
2286
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002287 def test_node_label(self):
2288 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002289 self.worker.registerFunction('build:node-project-test1:debian')
2290
2291 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2292 A.addApproval('CRVW', 2)
2293 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2294 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002295
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002296 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2297 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2298 'debian')
2299 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002300
2301 def test_live_reconfiguration(self):
2302 "Test that live reconfiguration works"
2303 self.worker.hold_jobs_in_build = True
2304 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2305 A.addApproval('CRVW', 2)
2306 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2307 self.waitUntilSettled()
2308
2309 self.sched.reconfigure(self.config)
2310
2311 self.worker.hold_jobs_in_build = False
2312 self.worker.release()
2313 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002314 self.assertEqual(self.getJobFromHistory('project-merge').result,
2315 'SUCCESS')
2316 self.assertEqual(self.getJobFromHistory('project-test1').result,
2317 'SUCCESS')
2318 self.assertEqual(self.getJobFromHistory('project-test2').result,
2319 'SUCCESS')
2320 self.assertEqual(A.data['status'], 'MERGED')
2321 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002322
James E. Blair6bc782d2015-07-17 16:20:21 -07002323 def test_live_reconfiguration_merge_conflict(self):
2324 # A real-world bug: a change in a gate queue has a merge
2325 # conflict and a job is added to its project while it's
2326 # sitting in the queue. The job gets added to the change and
2327 # enqueued and the change gets stuck.
2328 self.worker.registerFunction('build:project-test3')
2329 self.worker.hold_jobs_in_build = True
2330
2331 # This change is fine. It's here to stop the queue long
2332 # enough for the next change to be subject to the
2333 # reconfiguration, as well as to provide a conflict for the
2334 # next change. This change will succeed and merge.
2335 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2336 A.addPatchset(['conflict'])
2337 A.addApproval('CRVW', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002338
2339 # This change will be in merge conflict. During the
2340 # reconfiguration, we will add a job. We want to make sure
2341 # that doesn't cause it to get stuck.
2342 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2343 B.addPatchset(['conflict'])
2344 B.addApproval('CRVW', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002345
2346 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002347 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2348
2349 self.waitUntilSettled()
2350
2351 # No jobs have run yet
2352 self.assertEqual(A.data['status'], 'NEW')
2353 self.assertEqual(A.reported, 1)
2354 self.assertEqual(B.data['status'], 'NEW')
2355 self.assertEqual(B.reported, 1)
2356 self.assertEqual(len(self.history), 0)
2357
2358 # Add the "project-test3" job.
2359 self.config.set('zuul', 'layout_config',
2360 'tests/fixtures/layout-live-'
2361 'reconfiguration-add-job.yaml')
2362 self.sched.reconfigure(self.config)
2363 self.waitUntilSettled()
2364
2365 self.worker.hold_jobs_in_build = False
2366 self.worker.release()
2367 self.waitUntilSettled()
2368
2369 self.assertEqual(A.data['status'], 'MERGED')
2370 self.assertEqual(A.reported, 2)
2371 self.assertEqual(B.data['status'], 'NEW')
2372 self.assertEqual(B.reported, 2)
2373 self.assertEqual(self.getJobFromHistory('project-merge').result,
2374 'SUCCESS')
2375 self.assertEqual(self.getJobFromHistory('project-test1').result,
2376 'SUCCESS')
2377 self.assertEqual(self.getJobFromHistory('project-test2').result,
2378 'SUCCESS')
2379 self.assertEqual(self.getJobFromHistory('project-test3').result,
2380 'SUCCESS')
2381 self.assertEqual(len(self.history), 4)
2382
James E. Blair400e8fd2015-07-30 17:44:45 -07002383 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002384 # An extrapolation of test_live_reconfiguration_merge_conflict
2385 # that tests a job added to a job tree with a failed root does
2386 # not run.
2387 self.worker.registerFunction('build:project-test3')
2388 self.worker.hold_jobs_in_build = True
2389
2390 # This change is fine. It's here to stop the queue long
2391 # enough for the next change to be subject to the
2392 # reconfiguration. This change will succeed and merge.
2393 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2394 A.addPatchset(['conflict'])
2395 A.addApproval('CRVW', 2)
2396 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2397 self.waitUntilSettled()
2398 self.worker.release('.*-merge')
2399 self.waitUntilSettled()
2400
2401 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2402 self.worker.addFailTest('project-merge', B)
2403 B.addApproval('CRVW', 2)
2404 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2405 self.waitUntilSettled()
2406
2407 self.worker.release('.*-merge')
2408 self.waitUntilSettled()
2409
2410 # Both -merge jobs have run, but no others.
2411 self.assertEqual(A.data['status'], 'NEW')
2412 self.assertEqual(A.reported, 1)
2413 self.assertEqual(B.data['status'], 'NEW')
2414 self.assertEqual(B.reported, 1)
2415 self.assertEqual(self.history[0].result, 'SUCCESS')
2416 self.assertEqual(self.history[0].name, 'project-merge')
2417 self.assertEqual(self.history[1].result, 'FAILURE')
2418 self.assertEqual(self.history[1].name, 'project-merge')
2419 self.assertEqual(len(self.history), 2)
2420
2421 # Add the "project-test3" job.
2422 self.config.set('zuul', 'layout_config',
2423 'tests/fixtures/layout-live-'
2424 'reconfiguration-add-job.yaml')
2425 self.sched.reconfigure(self.config)
2426 self.waitUntilSettled()
2427
2428 self.worker.hold_jobs_in_build = False
2429 self.worker.release()
2430 self.waitUntilSettled()
2431
2432 self.assertEqual(A.data['status'], 'MERGED')
2433 self.assertEqual(A.reported, 2)
2434 self.assertEqual(B.data['status'], 'NEW')
2435 self.assertEqual(B.reported, 2)
2436 self.assertEqual(self.history[0].result, 'SUCCESS')
2437 self.assertEqual(self.history[0].name, 'project-merge')
2438 self.assertEqual(self.history[1].result, 'FAILURE')
2439 self.assertEqual(self.history[1].name, 'project-merge')
2440 self.assertEqual(self.history[2].result, 'SUCCESS')
2441 self.assertEqual(self.history[3].result, 'SUCCESS')
2442 self.assertEqual(self.history[4].result, 'SUCCESS')
2443 self.assertEqual(len(self.history), 5)
2444
James E. Blair400e8fd2015-07-30 17:44:45 -07002445 def test_live_reconfiguration_failed_job(self):
2446 # Test that a change with a removed failing job does not
2447 # disrupt reconfiguration. If a change has a failed job and
2448 # that job is removed during a reconfiguration, we observed a
2449 # bug where the code to re-set build statuses would run on
2450 # that build and raise an exception because the job no longer
2451 # existed.
2452 self.worker.hold_jobs_in_build = True
2453
2454 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2455
2456 # This change will fail and later be removed by the reconfiguration.
2457 self.worker.addFailTest('project-test1', A)
2458
2459 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2460 self.waitUntilSettled()
2461 self.worker.release('.*-merge')
2462 self.waitUntilSettled()
2463 self.worker.release('project-test1')
2464 self.waitUntilSettled()
2465
2466 self.assertEqual(A.data['status'], 'NEW')
2467 self.assertEqual(A.reported, 0)
2468
2469 self.assertEqual(self.getJobFromHistory('project-merge').result,
2470 'SUCCESS')
2471 self.assertEqual(self.getJobFromHistory('project-test1').result,
2472 'FAILURE')
2473 self.assertEqual(len(self.history), 2)
2474
2475 # Remove the test1 job.
2476 self.config.set('zuul', 'layout_config',
2477 'tests/fixtures/layout-live-'
2478 'reconfiguration-failed-job.yaml')
2479 self.sched.reconfigure(self.config)
2480 self.waitUntilSettled()
2481
2482 self.worker.hold_jobs_in_build = False
2483 self.worker.release()
2484 self.waitUntilSettled()
2485
2486 self.assertEqual(self.getJobFromHistory('project-test2').result,
2487 'SUCCESS')
2488 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2489 'SUCCESS')
2490 self.assertEqual(len(self.history), 4)
2491
2492 self.assertEqual(A.data['status'], 'NEW')
2493 self.assertEqual(A.reported, 1)
2494 self.assertIn('Build succeeded', A.messages[0])
2495 # Ensure the removed job was not included in the report.
2496 self.assertNotIn('project-test1', A.messages[0])
2497
James E. Blairfe707d12015-08-05 15:18:15 -07002498 def test_live_reconfiguration_shared_queue(self):
2499 # Test that a change with a failing job which was removed from
2500 # this project but otherwise still exists in the system does
2501 # not disrupt reconfiguration.
2502
2503 self.worker.hold_jobs_in_build = True
2504
2505 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2506
2507 self.worker.addFailTest('project1-project2-integration', A)
2508
2509 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2510 self.waitUntilSettled()
2511 self.worker.release('.*-merge')
2512 self.waitUntilSettled()
2513 self.worker.release('project1-project2-integration')
2514 self.waitUntilSettled()
2515
2516 self.assertEqual(A.data['status'], 'NEW')
2517 self.assertEqual(A.reported, 0)
2518
2519 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2520 'SUCCESS')
2521 self.assertEqual(self.getJobFromHistory(
2522 'project1-project2-integration').result, 'FAILURE')
2523 self.assertEqual(len(self.history), 2)
2524
2525 # Remove the integration job.
2526 self.config.set('zuul', 'layout_config',
2527 'tests/fixtures/layout-live-'
2528 'reconfiguration-shared-queue.yaml')
2529 self.sched.reconfigure(self.config)
2530 self.waitUntilSettled()
2531
2532 self.worker.hold_jobs_in_build = False
2533 self.worker.release()
2534 self.waitUntilSettled()
2535
2536 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2537 'SUCCESS')
2538 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2539 'SUCCESS')
2540 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2541 'SUCCESS')
2542 self.assertEqual(self.getJobFromHistory(
2543 'project1-project2-integration').result, 'FAILURE')
2544 self.assertEqual(len(self.history), 4)
2545
2546 self.assertEqual(A.data['status'], 'NEW')
2547 self.assertEqual(A.reported, 1)
2548 self.assertIn('Build succeeded', A.messages[0])
2549 # Ensure the removed job was not included in the report.
2550 self.assertNotIn('project1-project2-integration', A.messages[0])
2551
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002552 def test_live_reconfiguration_del_project(self):
2553 # Test project deletion from layout
2554 # while changes are enqueued
2555
2556 self.worker.hold_jobs_in_build = True
2557 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2558 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2559 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2560
2561 # A Depends-On: B
2562 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2563 A.subject, B.data['id'])
2564 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2565
2566 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2567 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2568 self.waitUntilSettled()
2569 self.worker.release('.*-merge')
2570 self.waitUntilSettled()
2571 self.assertEqual(len(self.builds), 5)
2572
2573 # This layout defines only org/project, not org/project1
2574 self.config.set('zuul', 'layout_config',
2575 'tests/fixtures/layout-live-'
2576 'reconfiguration-del-project.yaml')
2577 self.sched.reconfigure(self.config)
2578 self.waitUntilSettled()
2579
2580 # Builds for C aborted, builds for A succeed,
2581 # and have change B applied ahead
2582 job_c = self.getJobFromHistory('project1-test1')
2583 self.assertEqual(job_c.changes, '3,1')
2584 self.assertEqual(job_c.result, 'ABORTED')
2585
2586 self.worker.hold_jobs_in_build = False
2587 self.worker.release()
2588 self.waitUntilSettled()
2589
2590 self.assertEqual(self.getJobFromHistory('project-test1').changes,
2591 '2,1 1,1')
2592
2593 self.assertEqual(A.data['status'], 'NEW')
2594 self.assertEqual(B.data['status'], 'NEW')
2595 self.assertEqual(C.data['status'], 'NEW')
2596 self.assertEqual(A.reported, 1)
2597 self.assertEqual(B.reported, 0)
2598 self.assertEqual(C.reported, 0)
2599
2600 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
2601 self.assertIn('Build succeeded', A.messages[0])
2602
James E. Blaire712d9f2013-07-31 11:40:11 -07002603 def test_live_reconfiguration_functions(self):
2604 "Test live reconfiguration with a custom function"
2605 self.worker.registerFunction('build:node-project-test1:debian')
2606 self.worker.registerFunction('build:node-project-test1:wheezy')
2607 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2608 A.addApproval('CRVW', 2)
2609 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2610 self.waitUntilSettled()
2611
2612 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2613 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2614 'debian')
2615 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2616
2617 self.config.set('zuul', 'layout_config',
2618 'tests/fixtures/layout-live-'
2619 'reconfiguration-functions.yaml')
2620 self.sched.reconfigure(self.config)
2621 self.worker.build_history = []
2622
2623 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
2624 B.addApproval('CRVW', 2)
2625 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2626 self.waitUntilSettled()
2627
2628 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2629 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2630 'wheezy')
2631 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2632
James E. Blair287c06d2013-07-24 10:39:30 -07002633 def test_delayed_repo_init(self):
2634 self.config.set('zuul', 'layout_config',
2635 'tests/fixtures/layout-delayed-repo-init.yaml')
2636 self.sched.reconfigure(self.config)
2637
2638 self.init_repo("org/new-project")
2639 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2640
2641 A.addApproval('CRVW', 2)
2642 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2643 self.waitUntilSettled()
2644 self.assertEqual(self.getJobFromHistory('project-merge').result,
2645 'SUCCESS')
2646 self.assertEqual(self.getJobFromHistory('project-test1').result,
2647 'SUCCESS')
2648 self.assertEqual(self.getJobFromHistory('project-test2').result,
2649 'SUCCESS')
2650 self.assertEqual(A.data['status'], 'MERGED')
2651 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002652
Clark Boylan6dbbc482013-10-18 10:57:31 -07002653 def test_repo_deleted(self):
2654 self.config.set('zuul', 'layout_config',
2655 'tests/fixtures/layout-repo-deleted.yaml')
2656 self.sched.reconfigure(self.config)
2657
2658 self.init_repo("org/delete-project")
2659 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2660
2661 A.addApproval('CRVW', 2)
2662 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2663 self.waitUntilSettled()
2664 self.assertEqual(self.getJobFromHistory('project-merge').result,
2665 'SUCCESS')
2666 self.assertEqual(self.getJobFromHistory('project-test1').result,
2667 'SUCCESS')
2668 self.assertEqual(self.getJobFromHistory('project-test2').result,
2669 'SUCCESS')
2670 self.assertEqual(A.data['status'], 'MERGED')
2671 self.assertEqual(A.reported, 2)
2672
2673 # Delete org/new-project zuul repo. Should be recloned.
2674 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
2675
2676 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2677
2678 B.addApproval('CRVW', 2)
2679 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2680 self.waitUntilSettled()
2681 self.assertEqual(self.getJobFromHistory('project-merge').result,
2682 'SUCCESS')
2683 self.assertEqual(self.getJobFromHistory('project-test1').result,
2684 'SUCCESS')
2685 self.assertEqual(self.getJobFromHistory('project-test2').result,
2686 'SUCCESS')
2687 self.assertEqual(B.data['status'], 'MERGED')
2688 self.assertEqual(B.reported, 2)
2689
James E. Blair63bb0ef2013-07-29 17:14:51 -07002690 def test_timer(self):
2691 "Test that a periodic job is triggered"
2692 self.worker.hold_jobs_in_build = True
2693 self.config.set('zuul', 'layout_config',
2694 'tests/fixtures/layout-timer.yaml')
2695 self.sched.reconfigure(self.config)
2696 self.registerJobs()
2697
Clark Boylan3ee090a2014-04-03 20:55:09 -07002698 # The pipeline triggers every second, so we should have seen
2699 # several by now.
2700 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002701 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002702
2703 self.assertEqual(len(self.builds), 2)
2704
James E. Blair63bb0ef2013-07-29 17:14:51 -07002705 port = self.webapp.server.socket.getsockname()[1]
2706
2707 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2708 data = f.read()
2709
2710 self.worker.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002711 # Stop queuing timer triggered jobs so that the assertions
2712 # below don't race against more jobs being queued.
2713 self.config.set('zuul', 'layout_config',
2714 'tests/fixtures/layout-no-timer.yaml')
2715 self.sched.reconfigure(self.config)
2716 self.registerJobs()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002717 self.worker.release()
2718 self.waitUntilSettled()
2719
2720 self.assertEqual(self.getJobFromHistory(
2721 'project-bitrot-stable-old').result, 'SUCCESS')
2722 self.assertEqual(self.getJobFromHistory(
2723 'project-bitrot-stable-older').result, 'SUCCESS')
2724
2725 data = json.loads(data)
2726 status_jobs = set()
2727 for p in data['pipelines']:
2728 for q in p['change_queues']:
2729 for head in q['heads']:
2730 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07002731 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002732 for job in change['jobs']:
2733 status_jobs.add(job['name'])
2734 self.assertIn('project-bitrot-stable-old', status_jobs)
2735 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002736
James E. Blair4f6033c2014-03-27 15:49:09 -07002737 def test_idle(self):
2738 "Test that frequent periodic jobs work"
2739 self.worker.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002740
Clark Boylan3ee090a2014-04-03 20:55:09 -07002741 for x in range(1, 3):
2742 # Test that timer triggers periodic jobs even across
2743 # layout config reloads.
2744 # Start timer trigger
2745 self.config.set('zuul', 'layout_config',
2746 'tests/fixtures/layout-idle.yaml')
2747 self.sched.reconfigure(self.config)
2748 self.registerJobs()
James E. Blair4f6033c2014-03-27 15:49:09 -07002749
Clark Boylan3ee090a2014-04-03 20:55:09 -07002750 # The pipeline triggers every second, so we should have seen
2751 # several by now.
2752 time.sleep(5)
2753 self.waitUntilSettled()
2754
2755 # Stop queuing timer triggered jobs so that the assertions
2756 # below don't race against more jobs being queued.
2757 self.config.set('zuul', 'layout_config',
2758 'tests/fixtures/layout-no-timer.yaml')
2759 self.sched.reconfigure(self.config)
2760 self.registerJobs()
2761
2762 self.assertEqual(len(self.builds), 2)
2763 self.worker.release('.*')
2764 self.waitUntilSettled()
2765 self.assertEqual(len(self.builds), 0)
2766 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002767
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002768 def test_check_smtp_pool(self):
2769 self.config.set('zuul', 'layout_config',
2770 'tests/fixtures/layout-smtp.yaml')
2771 self.sched.reconfigure(self.config)
2772
2773 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2774 self.waitUntilSettled()
2775
2776 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2777 self.waitUntilSettled()
2778
James E. Blairff80a2f2013-12-27 13:24:06 -08002779 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002780
2781 # A.messages only holds what FakeGerrit places in it. Thus we
2782 # work on the knowledge of what the first message should be as
2783 # it is only configured to go to SMTP.
2784
2785 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002786 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002787 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002788 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002789 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002790 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002791
2792 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002793 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002794 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002795 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002796 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002797 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002798
James E. Blaire5910202013-12-27 09:50:31 -08002799 def test_timer_smtp(self):
2800 "Test that a periodic job is triggered"
Clark Boylan3ee090a2014-04-03 20:55:09 -07002801 self.worker.hold_jobs_in_build = True
James E. Blaire5910202013-12-27 09:50:31 -08002802 self.config.set('zuul', 'layout_config',
2803 'tests/fixtures/layout-timer-smtp.yaml')
2804 self.sched.reconfigure(self.config)
2805 self.registerJobs()
2806
Clark Boylan3ee090a2014-04-03 20:55:09 -07002807 # The pipeline triggers every second, so we should have seen
2808 # several by now.
2809 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002810 self.waitUntilSettled()
2811
Clark Boylan3ee090a2014-04-03 20:55:09 -07002812 self.assertEqual(len(self.builds), 2)
2813 self.worker.release('.*')
2814 self.waitUntilSettled()
2815 self.assertEqual(len(self.history), 2)
2816
James E. Blaire5910202013-12-27 09:50:31 -08002817 self.assertEqual(self.getJobFromHistory(
2818 'project-bitrot-stable-old').result, 'SUCCESS')
2819 self.assertEqual(self.getJobFromHistory(
2820 'project-bitrot-stable-older').result, 'SUCCESS')
2821
James E. Blairff80a2f2013-12-27 13:24:06 -08002822 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002823
2824 # A.messages only holds what FakeGerrit places in it. Thus we
2825 # work on the knowledge of what the first message should be as
2826 # it is only configured to go to SMTP.
2827
2828 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002829 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002830 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002831 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002832 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002833 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002834
Clark Boylan3ee090a2014-04-03 20:55:09 -07002835 # Stop queuing timer triggered jobs and let any that may have
2836 # queued through so that end of test assertions pass.
2837 self.config.set('zuul', 'layout_config',
2838 'tests/fixtures/layout-no-timer.yaml')
2839 self.sched.reconfigure(self.config)
2840 self.registerJobs()
James E. Blairf8058972014-08-15 16:09:16 -07002841 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002842 self.worker.release('.*')
2843 self.waitUntilSettled()
2844
James E. Blair91e34592015-07-31 16:45:59 -07002845 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08002846 "Test that the RPC client can enqueue a change"
2847 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2848 A.addApproval('CRVW', 2)
2849 A.addApproval('APRV', 1)
2850
2851 client = zuul.rpcclient.RPCClient('127.0.0.1',
2852 self.gearman_server.port)
2853 r = client.enqueue(pipeline='gate',
2854 project='org/project',
2855 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002856 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002857 self.waitUntilSettled()
2858 self.assertEqual(self.getJobFromHistory('project-merge').result,
2859 'SUCCESS')
2860 self.assertEqual(self.getJobFromHistory('project-test1').result,
2861 'SUCCESS')
2862 self.assertEqual(self.getJobFromHistory('project-test2').result,
2863 'SUCCESS')
2864 self.assertEqual(A.data['status'], 'MERGED')
2865 self.assertEqual(A.reported, 2)
2866 self.assertEqual(r, True)
2867
James E. Blair91e34592015-07-31 16:45:59 -07002868 def test_client_enqueue_ref(self):
2869 "Test that the RPC client can enqueue a ref"
2870
2871 client = zuul.rpcclient.RPCClient('127.0.0.1',
2872 self.gearman_server.port)
2873 r = client.enqueue_ref(
2874 pipeline='post',
2875 project='org/project',
2876 trigger='gerrit',
2877 ref='master',
2878 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
2879 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
2880 self.waitUntilSettled()
2881 job_names = [x.name for x in self.history]
2882 self.assertEqual(len(self.history), 1)
2883 self.assertIn('project-post', job_names)
2884 self.assertEqual(r, True)
2885
James E. Blairad28e912013-11-27 10:43:22 -08002886 def test_client_enqueue_negative(self):
2887 "Test that the RPC client returns errors"
2888 client = zuul.rpcclient.RPCClient('127.0.0.1',
2889 self.gearman_server.port)
2890 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2891 "Invalid project"):
2892 r = client.enqueue(pipeline='gate',
2893 project='project-does-not-exist',
2894 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002895 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002896 client.shutdown()
2897 self.assertEqual(r, False)
2898
2899 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2900 "Invalid pipeline"):
2901 r = client.enqueue(pipeline='pipeline-does-not-exist',
2902 project='org/project',
2903 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002904 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002905 client.shutdown()
2906 self.assertEqual(r, False)
2907
2908 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2909 "Invalid trigger"):
2910 r = client.enqueue(pipeline='gate',
2911 project='org/project',
2912 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08002913 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002914 client.shutdown()
2915 self.assertEqual(r, False)
2916
2917 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2918 "Invalid change"):
2919 r = client.enqueue(pipeline='gate',
2920 project='org/project',
2921 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002922 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002923 client.shutdown()
2924 self.assertEqual(r, False)
2925
2926 self.waitUntilSettled()
2927 self.assertEqual(len(self.history), 0)
2928 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08002929
2930 def test_client_promote(self):
2931 "Test that the RPC client can promote a change"
2932 self.worker.hold_jobs_in_build = True
2933 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2934 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2935 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2936 A.addApproval('CRVW', 2)
2937 B.addApproval('CRVW', 2)
2938 C.addApproval('CRVW', 2)
2939
2940 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2941 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2942 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2943
2944 self.waitUntilSettled()
2945
Sean Daguef39b9ca2014-01-10 21:34:35 -05002946 items = self.sched.layout.pipelines['gate'].getAllItems()
2947 enqueue_times = {}
2948 for item in items:
2949 enqueue_times[str(item.change)] = item.enqueue_time
2950
James E. Blair36658cf2013-12-06 17:53:48 -08002951 client = zuul.rpcclient.RPCClient('127.0.0.1',
2952 self.gearman_server.port)
2953 r = client.promote(pipeline='gate',
2954 change_ids=['2,1', '3,1'])
2955
Sean Daguef39b9ca2014-01-10 21:34:35 -05002956 # ensure that enqueue times are durable
2957 items = self.sched.layout.pipelines['gate'].getAllItems()
2958 for item in items:
2959 self.assertEqual(
2960 enqueue_times[str(item.change)], item.enqueue_time)
2961
James E. Blair78acec92014-02-06 07:11:32 -08002962 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08002963 self.worker.release('.*-merge')
2964 self.waitUntilSettled()
2965 self.worker.release('.*-merge')
2966 self.waitUntilSettled()
2967 self.worker.release('.*-merge')
2968 self.waitUntilSettled()
2969
2970 self.assertEqual(len(self.builds), 6)
2971 self.assertEqual(self.builds[0].name, 'project-test1')
2972 self.assertEqual(self.builds[1].name, 'project-test2')
2973 self.assertEqual(self.builds[2].name, 'project-test1')
2974 self.assertEqual(self.builds[3].name, 'project-test2')
2975 self.assertEqual(self.builds[4].name, 'project-test1')
2976 self.assertEqual(self.builds[5].name, 'project-test2')
2977
2978 self.assertTrue(self.job_has_changes(self.builds[0], B))
2979 self.assertFalse(self.job_has_changes(self.builds[0], A))
2980 self.assertFalse(self.job_has_changes(self.builds[0], C))
2981
2982 self.assertTrue(self.job_has_changes(self.builds[2], B))
2983 self.assertTrue(self.job_has_changes(self.builds[2], C))
2984 self.assertFalse(self.job_has_changes(self.builds[2], A))
2985
2986 self.assertTrue(self.job_has_changes(self.builds[4], B))
2987 self.assertTrue(self.job_has_changes(self.builds[4], C))
2988 self.assertTrue(self.job_has_changes(self.builds[4], A))
2989
2990 self.worker.release()
2991 self.waitUntilSettled()
2992
2993 self.assertEqual(A.data['status'], 'MERGED')
2994 self.assertEqual(A.reported, 2)
2995 self.assertEqual(B.data['status'], 'MERGED')
2996 self.assertEqual(B.reported, 2)
2997 self.assertEqual(C.data['status'], 'MERGED')
2998 self.assertEqual(C.reported, 2)
2999
3000 client.shutdown()
3001 self.assertEqual(r, True)
3002
3003 def test_client_promote_dependent(self):
3004 "Test that the RPC client can promote a dependent change"
3005 # C (depends on B) -> B -> A ; then promote C to get:
3006 # A -> C (depends on B) -> B
3007 self.worker.hold_jobs_in_build = True
3008 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3009 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3010 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3011
3012 C.setDependsOn(B, 1)
3013
3014 A.addApproval('CRVW', 2)
3015 B.addApproval('CRVW', 2)
3016 C.addApproval('CRVW', 2)
3017
3018 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3019 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3020 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3021
3022 self.waitUntilSettled()
3023
3024 client = zuul.rpcclient.RPCClient('127.0.0.1',
3025 self.gearman_server.port)
3026 r = client.promote(pipeline='gate',
3027 change_ids=['3,1'])
3028
James E. Blair78acec92014-02-06 07:11:32 -08003029 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08003030 self.worker.release('.*-merge')
3031 self.waitUntilSettled()
3032 self.worker.release('.*-merge')
3033 self.waitUntilSettled()
3034 self.worker.release('.*-merge')
3035 self.waitUntilSettled()
3036
3037 self.assertEqual(len(self.builds), 6)
3038 self.assertEqual(self.builds[0].name, 'project-test1')
3039 self.assertEqual(self.builds[1].name, 'project-test2')
3040 self.assertEqual(self.builds[2].name, 'project-test1')
3041 self.assertEqual(self.builds[3].name, 'project-test2')
3042 self.assertEqual(self.builds[4].name, 'project-test1')
3043 self.assertEqual(self.builds[5].name, 'project-test2')
3044
3045 self.assertTrue(self.job_has_changes(self.builds[0], B))
3046 self.assertFalse(self.job_has_changes(self.builds[0], A))
3047 self.assertFalse(self.job_has_changes(self.builds[0], C))
3048
3049 self.assertTrue(self.job_has_changes(self.builds[2], B))
3050 self.assertTrue(self.job_has_changes(self.builds[2], C))
3051 self.assertFalse(self.job_has_changes(self.builds[2], A))
3052
3053 self.assertTrue(self.job_has_changes(self.builds[4], B))
3054 self.assertTrue(self.job_has_changes(self.builds[4], C))
3055 self.assertTrue(self.job_has_changes(self.builds[4], A))
3056
3057 self.worker.release()
3058 self.waitUntilSettled()
3059
3060 self.assertEqual(A.data['status'], 'MERGED')
3061 self.assertEqual(A.reported, 2)
3062 self.assertEqual(B.data['status'], 'MERGED')
3063 self.assertEqual(B.reported, 2)
3064 self.assertEqual(C.data['status'], 'MERGED')
3065 self.assertEqual(C.reported, 2)
3066
3067 client.shutdown()
3068 self.assertEqual(r, True)
3069
3070 def test_client_promote_negative(self):
3071 "Test that the RPC client returns errors for promotion"
3072 self.worker.hold_jobs_in_build = True
3073 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3074 A.addApproval('CRVW', 2)
3075 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3076 self.waitUntilSettled()
3077
3078 client = zuul.rpcclient.RPCClient('127.0.0.1',
3079 self.gearman_server.port)
3080
3081 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3082 r = client.promote(pipeline='nonexistent',
3083 change_ids=['2,1', '3,1'])
3084 client.shutdown()
3085 self.assertEqual(r, False)
3086
3087 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3088 r = client.promote(pipeline='gate',
3089 change_ids=['4,1'])
3090 client.shutdown()
3091 self.assertEqual(r, False)
3092
3093 self.worker.hold_jobs_in_build = False
3094 self.worker.release()
3095 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003096
3097 def test_queue_rate_limiting(self):
3098 "Test that DependentPipelines are rate limited with dep across window"
3099 self.config.set('zuul', 'layout_config',
3100 'tests/fixtures/layout-rate-limit.yaml')
3101 self.sched.reconfigure(self.config)
3102 self.worker.hold_jobs_in_build = True
3103 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3104 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3105 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3106
3107 C.setDependsOn(B, 1)
3108 self.worker.addFailTest('project-test1', A)
3109
3110 A.addApproval('CRVW', 2)
3111 B.addApproval('CRVW', 2)
3112 C.addApproval('CRVW', 2)
3113
3114 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3115 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3116 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3117 self.waitUntilSettled()
3118
3119 # Only A and B will have their merge jobs queued because
3120 # window is 2.
3121 self.assertEqual(len(self.builds), 2)
3122 self.assertEqual(self.builds[0].name, 'project-merge')
3123 self.assertEqual(self.builds[1].name, 'project-merge')
3124
3125 self.worker.release('.*-merge')
3126 self.waitUntilSettled()
3127 self.worker.release('.*-merge')
3128 self.waitUntilSettled()
3129
3130 # Only A and B will have their test jobs queued because
3131 # window is 2.
3132 self.assertEqual(len(self.builds), 4)
3133 self.assertEqual(self.builds[0].name, 'project-test1')
3134 self.assertEqual(self.builds[1].name, 'project-test2')
3135 self.assertEqual(self.builds[2].name, 'project-test1')
3136 self.assertEqual(self.builds[3].name, 'project-test2')
3137
3138 self.worker.release('project-.*')
3139 self.waitUntilSettled()
3140
3141 queue = self.sched.layout.pipelines['gate'].queues[0]
3142 # A failed so window is reduced by 1 to 1.
3143 self.assertEqual(queue.window, 1)
3144 self.assertEqual(queue.window_floor, 1)
3145 self.assertEqual(A.data['status'], 'NEW')
3146
3147 # Gate is reset and only B's merge job is queued because
3148 # window shrunk to 1.
3149 self.assertEqual(len(self.builds), 1)
3150 self.assertEqual(self.builds[0].name, 'project-merge')
3151
3152 self.worker.release('.*-merge')
3153 self.waitUntilSettled()
3154
3155 # Only B's test jobs are queued because window is still 1.
3156 self.assertEqual(len(self.builds), 2)
3157 self.assertEqual(self.builds[0].name, 'project-test1')
3158 self.assertEqual(self.builds[1].name, 'project-test2')
3159
3160 self.worker.release('project-.*')
3161 self.waitUntilSettled()
3162
3163 # B was successfully merged so window is increased to 2.
3164 self.assertEqual(queue.window, 2)
3165 self.assertEqual(queue.window_floor, 1)
3166 self.assertEqual(B.data['status'], 'MERGED')
3167
3168 # Only C is left and its merge job is queued.
3169 self.assertEqual(len(self.builds), 1)
3170 self.assertEqual(self.builds[0].name, 'project-merge')
3171
3172 self.worker.release('.*-merge')
3173 self.waitUntilSettled()
3174
3175 # After successful merge job the test jobs for C are queued.
3176 self.assertEqual(len(self.builds), 2)
3177 self.assertEqual(self.builds[0].name, 'project-test1')
3178 self.assertEqual(self.builds[1].name, 'project-test2')
3179
3180 self.worker.release('project-.*')
3181 self.waitUntilSettled()
3182
3183 # C successfully merged so window is bumped to 3.
3184 self.assertEqual(queue.window, 3)
3185 self.assertEqual(queue.window_floor, 1)
3186 self.assertEqual(C.data['status'], 'MERGED')
3187
3188 def test_queue_rate_limiting_dependent(self):
3189 "Test that DependentPipelines are rate limited with dep in window"
3190 self.config.set('zuul', 'layout_config',
3191 'tests/fixtures/layout-rate-limit.yaml')
3192 self.sched.reconfigure(self.config)
3193 self.worker.hold_jobs_in_build = True
3194 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3195 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3196 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3197
3198 B.setDependsOn(A, 1)
3199
3200 self.worker.addFailTest('project-test1', A)
3201
3202 A.addApproval('CRVW', 2)
3203 B.addApproval('CRVW', 2)
3204 C.addApproval('CRVW', 2)
3205
3206 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3207 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3208 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3209 self.waitUntilSettled()
3210
3211 # Only A and B will have their merge jobs queued because
3212 # window is 2.
3213 self.assertEqual(len(self.builds), 2)
3214 self.assertEqual(self.builds[0].name, 'project-merge')
3215 self.assertEqual(self.builds[1].name, 'project-merge')
3216
3217 self.worker.release('.*-merge')
3218 self.waitUntilSettled()
3219 self.worker.release('.*-merge')
3220 self.waitUntilSettled()
3221
3222 # Only A and B will have their test jobs queued because
3223 # window is 2.
3224 self.assertEqual(len(self.builds), 4)
3225 self.assertEqual(self.builds[0].name, 'project-test1')
3226 self.assertEqual(self.builds[1].name, 'project-test2')
3227 self.assertEqual(self.builds[2].name, 'project-test1')
3228 self.assertEqual(self.builds[3].name, 'project-test2')
3229
3230 self.worker.release('project-.*')
3231 self.waitUntilSettled()
3232
3233 queue = self.sched.layout.pipelines['gate'].queues[0]
3234 # A failed so window is reduced by 1 to 1.
3235 self.assertEqual(queue.window, 1)
3236 self.assertEqual(queue.window_floor, 1)
3237 self.assertEqual(A.data['status'], 'NEW')
3238 self.assertEqual(B.data['status'], 'NEW')
3239
3240 # Gate is reset and only C's merge job is queued because
3241 # window shrunk to 1 and A and B were dequeued.
3242 self.assertEqual(len(self.builds), 1)
3243 self.assertEqual(self.builds[0].name, 'project-merge')
3244
3245 self.worker.release('.*-merge')
3246 self.waitUntilSettled()
3247
3248 # Only C's test jobs are queued because window is still 1.
3249 self.assertEqual(len(self.builds), 2)
3250 self.assertEqual(self.builds[0].name, 'project-test1')
3251 self.assertEqual(self.builds[1].name, 'project-test2')
3252
3253 self.worker.release('project-.*')
3254 self.waitUntilSettled()
3255
3256 # C was successfully merged so window is increased to 2.
3257 self.assertEqual(queue.window, 2)
3258 self.assertEqual(queue.window_floor, 1)
3259 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003260
3261 def test_worker_update_metadata(self):
3262 "Test if a worker can send back metadata about itself"
3263 self.worker.hold_jobs_in_build = True
3264
3265 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3266 A.addApproval('CRVW', 2)
3267 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3268 self.waitUntilSettled()
3269
3270 self.assertEqual(len(self.launcher.builds), 1)
3271
3272 self.log.debug('Current builds:')
3273 self.log.debug(self.launcher.builds)
3274
3275 start = time.time()
3276 while True:
3277 if time.time() - start > 10:
3278 raise Exception("Timeout waiting for gearman server to report "
3279 + "back to the client")
3280 build = self.launcher.builds.values()[0]
3281 if build.worker.name == "My Worker":
3282 break
3283 else:
3284 time.sleep(0)
3285
3286 self.log.debug(build)
3287 self.assertEqual("My Worker", build.worker.name)
3288 self.assertEqual("localhost", build.worker.hostname)
3289 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3290 self.assertEqual("zuul.example.org", build.worker.fqdn)
3291 self.assertEqual("FakeBuilder", build.worker.program)
3292 self.assertEqual("v1.1", build.worker.version)
3293 self.assertEqual({'something': 'else'}, build.worker.extra)
3294
3295 self.worker.hold_jobs_in_build = False
3296 self.worker.release()
3297 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003298
3299 def test_footer_message(self):
3300 "Test a pipeline's footer message is correctly added to the report."
3301 self.config.set('zuul', 'layout_config',
3302 'tests/fixtures/layout-footer-message.yaml')
3303 self.sched.reconfigure(self.config)
3304 self.registerJobs()
3305
3306 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3307 A.addApproval('CRVW', 2)
3308 self.worker.addFailTest('test1', A)
3309 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3310 self.waitUntilSettled()
3311
3312 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3313 B.addApproval('CRVW', 2)
3314 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3315 self.waitUntilSettled()
3316
3317 self.assertEqual(2, len(self.smtp_messages))
3318
3319 failure_body = """\
3320Build failed. For information on how to proceed, see \
3321http://wiki.example.org/Test_Failures
3322
3323- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
3324- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
3325
3326For CI problems and help debugging, contact ci@example.org"""
3327
3328 success_body = """\
3329Build succeeded.
3330
3331- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
3332- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
3333
3334For CI problems and help debugging, contact ci@example.org"""
3335
3336 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
3337 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11003338
3339 def test_merge_failure_reporters(self):
3340 """Check that the config is set up correctly"""
3341
3342 self.config.set('zuul', 'layout_config',
3343 'tests/fixtures/layout-merge-failure.yaml')
3344 self.sched.reconfigure(self.config)
3345 self.registerJobs()
3346
3347 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003348 "Merge Failed.\n\nThis change or one of its cross-repo "
3349 "dependencies was unable to be automatically merged with the "
3350 "current state of its repository. Please rebase the change and "
3351 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003352 self.sched.layout.pipelines['check'].merge_failure_message)
3353 self.assertEqual(
3354 "The merge failed! For more information...",
3355 self.sched.layout.pipelines['gate'].merge_failure_message)
3356
3357 self.assertEqual(
3358 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3359 self.assertEqual(
3360 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3361
3362 self.assertTrue(isinstance(
3363 self.sched.layout.pipelines['check'].merge_failure_actions[0].
Joshua Heskethffe42062014-09-05 21:43:52 +10003364 reporter, zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003365
3366 self.assertTrue(
3367 (
3368 isinstance(self.sched.layout.pipelines['gate'].
3369 merge_failure_actions[0].reporter,
Joshua Heskethffe42062014-09-05 21:43:52 +10003370 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003371 isinstance(self.sched.layout.pipelines['gate'].
3372 merge_failure_actions[1].reporter,
Joshua Heskethffe42062014-09-05 21:43:52 +10003373 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003374 ) or (
3375 isinstance(self.sched.layout.pipelines['gate'].
3376 merge_failure_actions[0].reporter,
Joshua Heskethffe42062014-09-05 21:43:52 +10003377 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003378 isinstance(self.sched.layout.pipelines['gate'].
3379 merge_failure_actions[1].reporter,
Joshua Heskethffe42062014-09-05 21:43:52 +10003380 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003381 )
3382 )
3383
3384 def test_merge_failure_reports(self):
3385 """Check that when a change fails to merge the correct message is sent
3386 to the correct reporter"""
3387 self.config.set('zuul', 'layout_config',
3388 'tests/fixtures/layout-merge-failure.yaml')
3389 self.sched.reconfigure(self.config)
3390 self.registerJobs()
3391
3392 # Check a test failure isn't reported to SMTP
3393 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3394 A.addApproval('CRVW', 2)
3395 self.worker.addFailTest('project-test1', A)
3396 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3397 self.waitUntilSettled()
3398
3399 self.assertEqual(3, len(self.history)) # 3 jobs
3400 self.assertEqual(0, len(self.smtp_messages))
3401
3402 # Check a merge failure is reported to SMTP
3403 # B should be merged, but C will conflict with B
3404 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3405 B.addPatchset(['conflict'])
3406 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3407 C.addPatchset(['conflict'])
3408 B.addApproval('CRVW', 2)
3409 C.addApproval('CRVW', 2)
3410 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3411 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3412 self.waitUntilSettled()
3413
3414 self.assertEqual(6, len(self.history)) # A and B jobs
3415 self.assertEqual(1, len(self.smtp_messages))
3416 self.assertEqual('The merge failed! For more information...',
3417 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003418
3419 def test_swift_instructions(self):
3420 "Test that the correct swift instructions are sent to the workers"
3421 self.config.set('zuul', 'layout_config',
3422 'tests/fixtures/layout-swift.yaml')
3423 self.sched.reconfigure(self.config)
3424 self.registerJobs()
3425
3426 self.worker.hold_jobs_in_build = True
3427 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3428
3429 A.addApproval('CRVW', 2)
3430 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3431 self.waitUntilSettled()
3432
3433 self.assertEqual(
3434 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
3435 "gate/test-merge/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003436 self.builds[0].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003437 self.assertEqual(5,
3438 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
3439 split('\n')))
3440 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
3441
3442 self.assertEqual(
3443 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
3444 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003445 self.builds[1].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003446 self.assertEqual(5,
3447 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
3448 split('\n')))
3449 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
3450
3451 self.assertEqual(
3452 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
3453 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003454 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003455 self.assertEqual(5,
3456 len(self.builds[1].
3457 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
3458 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
3459
3460 self.worker.hold_jobs_in_build = False
3461 self.worker.release()
3462 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003463
3464 def test_client_get_running_jobs(self):
3465 "Test that the RPC client can get a list of running jobs"
3466 self.worker.hold_jobs_in_build = True
3467 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3468 A.addApproval('CRVW', 2)
3469 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3470 self.waitUntilSettled()
3471
3472 client = zuul.rpcclient.RPCClient('127.0.0.1',
3473 self.gearman_server.port)
3474
3475 # Wait for gearman server to send the initial workData back to zuul
3476 start = time.time()
3477 while True:
3478 if time.time() - start > 10:
3479 raise Exception("Timeout waiting for gearman server to report "
3480 + "back to the client")
3481 build = self.launcher.builds.values()[0]
3482 if build.worker.name == "My Worker":
3483 break
3484 else:
3485 time.sleep(0)
3486
3487 running_items = client.get_running_jobs()
3488
3489 self.assertEqual(1, len(running_items))
3490 running_item = running_items[0]
3491 self.assertEqual([], running_item['failing_reasons'])
3492 self.assertEqual([], running_item['items_behind'])
3493 self.assertEqual('https://hostname/1', running_item['url'])
3494 self.assertEqual(None, running_item['item_ahead'])
3495 self.assertEqual('org/project', running_item['project'])
3496 self.assertEqual(None, running_item['remaining_time'])
3497 self.assertEqual(True, running_item['active'])
3498 self.assertEqual('1,1', running_item['id'])
3499
3500 self.assertEqual(3, len(running_item['jobs']))
3501 for job in running_item['jobs']:
3502 if job['name'] == 'project-merge':
3503 self.assertEqual('project-merge', job['name'])
3504 self.assertEqual('gate', job['pipeline'])
3505 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003506 self.assertEqual('https://server/job/project-merge/0/',
3507 job['url'])
3508 self.assertEqual(7, len(job['worker']))
3509 self.assertEqual(False, job['canceled'])
3510 self.assertEqual(True, job['voting'])
3511 self.assertEqual(None, job['result'])
3512 self.assertEqual('gate', job['pipeline'])
3513 break
3514
3515 self.worker.hold_jobs_in_build = False
3516 self.worker.release()
3517 self.waitUntilSettled()
3518
3519 running_items = client.get_running_jobs()
3520 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003521
3522 def test_nonvoting_pipeline(self):
3523 "Test that a nonvoting pipeline (experimental) can still report"
3524
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003525 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3526 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003527 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3528 self.waitUntilSettled()
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003529 self.assertEqual(
3530 self.getJobFromHistory('experimental-project-test').result,
3531 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003532 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003533
3534 def test_crd_gate(self):
3535 "Test cross-repo dependencies"
3536 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3537 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3538 A.addApproval('CRVW', 2)
3539 B.addApproval('CRVW', 2)
3540
3541 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3542 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3543 AM2.setMerged()
3544 AM1.setMerged()
3545
3546 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3547 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3548 BM2.setMerged()
3549 BM1.setMerged()
3550
3551 # A -> AM1 -> AM2
3552 # B -> BM1 -> BM2
3553 # A Depends-On: B
3554 # M2 is here to make sure it is never queried. If it is, it
3555 # means zuul is walking down the entire history of merged
3556 # changes.
3557
3558 B.setDependsOn(BM1, 1)
3559 BM1.setDependsOn(BM2, 1)
3560
3561 A.setDependsOn(AM1, 1)
3562 AM1.setDependsOn(AM2, 1)
3563
3564 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3565 A.subject, B.data['id'])
3566
3567 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3568 self.waitUntilSettled()
3569
3570 self.assertEqual(A.data['status'], 'NEW')
3571 self.assertEqual(B.data['status'], 'NEW')
3572
3573 source = self.sched.layout.pipelines['gate'].source
3574 source.maintainCache([])
3575
3576 self.worker.hold_jobs_in_build = True
3577 B.addApproval('APRV', 1)
3578 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3579 self.waitUntilSettled()
3580
3581 self.worker.release('.*-merge')
3582 self.waitUntilSettled()
3583 self.worker.release('.*-merge')
3584 self.waitUntilSettled()
3585 self.worker.hold_jobs_in_build = False
3586 self.worker.release()
3587 self.waitUntilSettled()
3588
3589 self.assertEqual(AM2.queried, 0)
3590 self.assertEqual(BM2.queried, 0)
3591 self.assertEqual(A.data['status'], 'MERGED')
3592 self.assertEqual(B.data['status'], 'MERGED')
3593 self.assertEqual(A.reported, 2)
3594 self.assertEqual(B.reported, 2)
3595
James E. Blair8f78d882015-02-05 08:51:37 -08003596 self.assertEqual(self.getJobFromHistory('project1-merge').changes,
3597 '2,1 1,1')
3598
3599 def test_crd_branch(self):
3600 "Test cross-repo dependencies in multiple branches"
3601 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3602 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3603 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3604 C.data['id'] = B.data['id']
3605 A.addApproval('CRVW', 2)
3606 B.addApproval('CRVW', 2)
3607 C.addApproval('CRVW', 2)
3608
3609 # A Depends-On: B+C
3610 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3611 A.subject, B.data['id'])
3612
3613 self.worker.hold_jobs_in_build = True
3614 B.addApproval('APRV', 1)
3615 C.addApproval('APRV', 1)
3616 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3617 self.waitUntilSettled()
3618
3619 self.worker.release('.*-merge')
3620 self.waitUntilSettled()
3621 self.worker.release('.*-merge')
3622 self.waitUntilSettled()
3623 self.worker.release('.*-merge')
3624 self.waitUntilSettled()
3625 self.worker.hold_jobs_in_build = False
3626 self.worker.release()
3627 self.waitUntilSettled()
3628
3629 self.assertEqual(A.data['status'], 'MERGED')
3630 self.assertEqual(B.data['status'], 'MERGED')
3631 self.assertEqual(C.data['status'], 'MERGED')
3632 self.assertEqual(A.reported, 2)
3633 self.assertEqual(B.reported, 2)
3634 self.assertEqual(C.reported, 2)
3635
3636 self.assertEqual(self.getJobFromHistory('project1-merge').changes,
3637 '2,1 3,1 1,1')
3638
3639 def test_crd_multiline(self):
3640 "Test multiple depends-on lines in commit"
3641 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3642 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3643 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
3644 A.addApproval('CRVW', 2)
3645 B.addApproval('CRVW', 2)
3646 C.addApproval('CRVW', 2)
3647
3648 # A Depends-On: B+C
3649 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3650 A.subject, B.data['id'], C.data['id'])
3651
3652 self.worker.hold_jobs_in_build = True
3653 B.addApproval('APRV', 1)
3654 C.addApproval('APRV', 1)
3655 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3656 self.waitUntilSettled()
3657
3658 self.worker.release('.*-merge')
3659 self.waitUntilSettled()
3660 self.worker.release('.*-merge')
3661 self.waitUntilSettled()
3662 self.worker.release('.*-merge')
3663 self.waitUntilSettled()
3664 self.worker.hold_jobs_in_build = False
3665 self.worker.release()
3666 self.waitUntilSettled()
3667
3668 self.assertEqual(A.data['status'], 'MERGED')
3669 self.assertEqual(B.data['status'], 'MERGED')
3670 self.assertEqual(C.data['status'], 'MERGED')
3671 self.assertEqual(A.reported, 2)
3672 self.assertEqual(B.reported, 2)
3673 self.assertEqual(C.reported, 2)
3674
3675 self.assertEqual(self.getJobFromHistory('project1-merge').changes,
3676 '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003677
3678 def test_crd_unshared_gate(self):
3679 "Test cross-repo dependencies in unshared gate queues"
3680 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3681 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3682 A.addApproval('CRVW', 2)
3683 B.addApproval('CRVW', 2)
3684
3685 # A Depends-On: B
3686 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3687 A.subject, B.data['id'])
3688
3689 # A and B do not share a queue, make sure that A is unable to
3690 # enqueue B (and therefore, A is unable to be enqueued).
3691 B.addApproval('APRV', 1)
3692 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3693 self.waitUntilSettled()
3694
3695 self.assertEqual(A.data['status'], 'NEW')
3696 self.assertEqual(B.data['status'], 'NEW')
3697 self.assertEqual(A.reported, 0)
3698 self.assertEqual(B.reported, 0)
3699 self.assertEqual(len(self.history), 0)
3700
3701 # Enqueue and merge B alone.
3702 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3703 self.waitUntilSettled()
3704
3705 self.assertEqual(B.data['status'], 'MERGED')
3706 self.assertEqual(B.reported, 2)
3707
3708 # Now that B is merged, A should be able to be enqueued and
3709 # merged.
3710 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3711 self.waitUntilSettled()
3712
3713 self.assertEqual(A.data['status'], 'MERGED')
3714 self.assertEqual(A.reported, 2)
3715
James E. Blair96698e22015-04-02 07:48:21 -07003716 def test_crd_gate_reverse(self):
3717 "Test reverse cross-repo dependencies"
3718 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3719 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3720 A.addApproval('CRVW', 2)
3721 B.addApproval('CRVW', 2)
3722
3723 # A Depends-On: B
3724
3725 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3726 A.subject, B.data['id'])
3727
3728 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3729 self.waitUntilSettled()
3730
3731 self.assertEqual(A.data['status'], 'NEW')
3732 self.assertEqual(B.data['status'], 'NEW')
3733
3734 self.worker.hold_jobs_in_build = True
3735 A.addApproval('APRV', 1)
3736 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3737 self.waitUntilSettled()
3738
3739 self.worker.release('.*-merge')
3740 self.waitUntilSettled()
3741 self.worker.release('.*-merge')
3742 self.waitUntilSettled()
3743 self.worker.hold_jobs_in_build = False
3744 self.worker.release()
3745 self.waitUntilSettled()
3746
3747 self.assertEqual(A.data['status'], 'MERGED')
3748 self.assertEqual(B.data['status'], 'MERGED')
3749 self.assertEqual(A.reported, 2)
3750 self.assertEqual(B.reported, 2)
3751
3752 self.assertEqual(self.getJobFromHistory('project1-merge').changes,
3753 '2,1 1,1')
3754
James E. Blair5ee24252014-12-30 10:12:29 -08003755 def test_crd_cycle(self):
3756 "Test cross-repo dependency cycles"
3757 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3758 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3759 A.addApproval('CRVW', 2)
3760 B.addApproval('CRVW', 2)
3761
3762 # A -> B -> A (via commit-depends)
3763
3764 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3765 A.subject, B.data['id'])
3766 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3767 B.subject, A.data['id'])
3768
3769 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3770 self.waitUntilSettled()
3771
3772 self.assertEqual(A.reported, 0)
3773 self.assertEqual(B.reported, 0)
3774 self.assertEqual(A.data['status'], 'NEW')
3775 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003776
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003777 def test_crd_gate_unknown(self):
3778 "Test unknown projects in dependent pipeline"
3779 self.init_repo("org/unknown")
3780 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3781 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
3782 A.addApproval('CRVW', 2)
3783 B.addApproval('CRVW', 2)
3784
3785 # A Depends-On: B
3786 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3787 A.subject, B.data['id'])
3788
3789 B.addApproval('APRV', 1)
3790 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3791 self.waitUntilSettled()
3792
3793 # Unknown projects cannot share a queue with any other
3794 # since they don't have common jobs with any other (they have no jobs).
3795 # Changes which depend on unknown project changes
3796 # should not be processed in dependent pipeline
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 # Simulate change B being gated outside this layout
3804 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3805 B.setMerged()
3806 self.waitUntilSettled()
3807 self.assertEqual(len(self.history), 0)
3808
3809 # Now that B is merged, A should be able to be enqueued and
3810 # merged.
3811 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3812 self.waitUntilSettled()
3813
3814 self.assertEqual(A.data['status'], 'MERGED')
3815 self.assertEqual(A.reported, 2)
3816 self.assertEqual(B.data['status'], 'MERGED')
3817 self.assertEqual(B.reported, 0)
3818
James E. Blairbfb8e042014-12-30 17:01:44 -08003819 def test_crd_check(self):
3820 "Test cross-repo dependencies in independent pipelines"
3821
3822 self.gearman_server.hold_jobs_in_queue = True
3823 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3824 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3825
3826 # A Depends-On: B
3827 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3828 A.subject, B.data['id'])
3829
3830 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3831 self.waitUntilSettled()
3832
3833 queue = self.gearman_server.getQueue()
3834 ref = self.getParameter(queue[-1], 'ZUUL_REF')
3835 self.gearman_server.hold_jobs_in_queue = False
3836 self.gearman_server.release()
3837 self.waitUntilSettled()
3838
3839 path = os.path.join(self.git_root, "org/project1")
3840 repo = git.Repo(path)
3841 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
3842 repo_messages.reverse()
3843 correct_messages = ['initial commit', 'A-1']
3844 self.assertEqual(repo_messages, correct_messages)
3845
3846 path = os.path.join(self.git_root, "org/project2")
3847 repo = git.Repo(path)
3848 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
3849 repo_messages.reverse()
3850 correct_messages = ['initial commit', 'B-1']
3851 self.assertEqual(repo_messages, correct_messages)
3852
3853 self.assertEqual(A.data['status'], 'NEW')
3854 self.assertEqual(B.data['status'], 'NEW')
3855 self.assertEqual(A.reported, 1)
3856 self.assertEqual(B.reported, 0)
3857
3858 self.assertEqual(self.history[0].changes, '2,1 1,1')
3859 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08003860
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003861 def test_crd_check_git_depends(self):
3862 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07003863 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003864 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3865 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3866
3867 # Add two git-dependent changes and make sure they both report
3868 # success.
3869 B.setDependsOn(A, 1)
3870 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3871 self.waitUntilSettled()
3872 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3873 self.waitUntilSettled()
3874
James E. Blairb8c16472015-05-05 14:55:26 -07003875 self.orderedRelease()
3876 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003877 self.waitUntilSettled()
3878
3879 self.assertEqual(A.data['status'], 'NEW')
3880 self.assertEqual(B.data['status'], 'NEW')
3881 self.assertEqual(A.reported, 1)
3882 self.assertEqual(B.reported, 1)
3883
3884 self.assertEqual(self.history[0].changes, '1,1')
3885 self.assertEqual(self.history[-1].changes, '1,1 2,1')
3886 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
3887
3888 self.assertIn('Build succeeded', A.messages[0])
3889 self.assertIn('Build succeeded', B.messages[0])
3890
3891 def test_crd_check_duplicate(self):
3892 "Test duplicate check in independent pipelines"
James E. Blair1e263032015-05-07 14:35:34 -07003893 self.worker.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003894 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3895 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3896 check_pipeline = self.sched.layout.pipelines['check']
3897
3898 # Add two git-dependent changes...
3899 B.setDependsOn(A, 1)
3900 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3901 self.waitUntilSettled()
3902 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3903
3904 # ...make sure the live one is not duplicated...
3905 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3906 self.waitUntilSettled()
3907 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3908
3909 # ...but the non-live one is able to be.
3910 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3911 self.waitUntilSettled()
3912 self.assertEqual(len(check_pipeline.getAllItems()), 3)
3913
Clark Boylandd849822015-03-02 12:38:14 -08003914 # Release jobs in order to avoid races with change A jobs
3915 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07003916 self.orderedRelease()
James E. Blair1e263032015-05-07 14:35:34 -07003917 self.worker.hold_jobs_in_build = False
3918 self.worker.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003919 self.waitUntilSettled()
3920
3921 self.assertEqual(A.data['status'], 'NEW')
3922 self.assertEqual(B.data['status'], 'NEW')
3923 self.assertEqual(A.reported, 1)
3924 self.assertEqual(B.reported, 1)
3925
3926 self.assertEqual(self.history[0].changes, '1,1 2,1')
3927 self.assertEqual(self.history[1].changes, '1,1')
3928 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
3929
3930 self.assertIn('Build succeeded', A.messages[0])
3931 self.assertIn('Build succeeded', B.messages[0])
3932
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003933 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08003934 "Test cross-repo dependencies re-enqueued in independent pipelines"
3935
3936 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003937 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
3938 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08003939
3940 # A Depends-On: B
3941 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3942 A.subject, B.data['id'])
3943
3944 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3945 self.waitUntilSettled()
3946
3947 self.sched.reconfigure(self.config)
3948
3949 # Make sure the items still share a change queue, and the
3950 # first one is not live.
3951 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 1)
3952 queue = self.sched.layout.pipelines['check'].queues[0]
3953 first_item = queue.queue[0]
3954 for item in queue.queue:
3955 self.assertEqual(item.queue, first_item.queue)
3956 self.assertFalse(first_item.live)
3957 self.assertTrue(queue.queue[1].live)
3958
3959 self.gearman_server.hold_jobs_in_queue = False
3960 self.gearman_server.release()
3961 self.waitUntilSettled()
3962
3963 self.assertEqual(A.data['status'], 'NEW')
3964 self.assertEqual(B.data['status'], 'NEW')
3965 self.assertEqual(A.reported, 1)
3966 self.assertEqual(B.reported, 0)
3967
3968 self.assertEqual(self.history[0].changes, '2,1 1,1')
3969 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08003970
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003971 def test_crd_check_reconfiguration(self):
3972 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
3973
3974 def test_crd_undefined_project(self):
3975 """Test that undefined projects in dependencies are handled for
3976 independent pipelines"""
3977 # It's a hack for fake gerrit,
3978 # as it implies repo creation upon the creation of any change
3979 self.init_repo("org/unknown")
3980 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
3981
James E. Blair17dd6772015-02-09 14:45:18 -08003982 def test_crd_check_ignore_dependencies(self):
3983 "Test cross-repo dependencies can be ignored"
3984 self.config.set('zuul', 'layout_config',
3985 'tests/fixtures/layout-ignore-dependencies.yaml')
3986 self.sched.reconfigure(self.config)
3987 self.registerJobs()
3988
3989 self.gearman_server.hold_jobs_in_queue = True
3990 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3991 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3992 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
3993
3994 # A Depends-On: B
3995 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3996 A.subject, B.data['id'])
3997 # C git-depends on B
3998 C.setDependsOn(B, 1)
3999 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4000 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4001 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4002 self.waitUntilSettled()
4003
4004 # Make sure none of the items share a change queue, and all
4005 # are live.
4006 check_pipeline = self.sched.layout.pipelines['check']
4007 self.assertEqual(len(check_pipeline.queues), 3)
4008 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4009 for item in check_pipeline.getAllItems():
4010 self.assertTrue(item.live)
4011
4012 self.gearman_server.hold_jobs_in_queue = False
4013 self.gearman_server.release()
4014 self.waitUntilSettled()
4015
4016 self.assertEqual(A.data['status'], 'NEW')
4017 self.assertEqual(B.data['status'], 'NEW')
4018 self.assertEqual(C.data['status'], 'NEW')
4019 self.assertEqual(A.reported, 1)
4020 self.assertEqual(B.reported, 1)
4021 self.assertEqual(C.reported, 1)
4022
4023 # Each job should have tested exactly one change
4024 for job in self.history:
4025 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004026
4027 def test_crd_check_transitive(self):
4028 "Test transitive cross-repo dependencies"
4029 # Specifically, if A -> B -> C, and C gets a new patchset and
4030 # A gets a new patchset, ensure the test of A,2 includes B,1
4031 # and C,2 (not C,1 which would indicate stale data in the
4032 # cache for B).
4033 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4034 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4035 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4036
4037 # A Depends-On: B
4038 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4039 A.subject, B.data['id'])
4040
4041 # B Depends-On: C
4042 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4043 B.subject, C.data['id'])
4044
4045 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4046 self.waitUntilSettled()
4047 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4048
4049 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4050 self.waitUntilSettled()
4051 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4052
4053 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4054 self.waitUntilSettled()
4055 self.assertEqual(self.history[-1].changes, '3,1')
4056
4057 C.addPatchset()
4058 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4059 self.waitUntilSettled()
4060 self.assertEqual(self.history[-1].changes, '3,2')
4061
4062 A.addPatchset()
4063 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4064 self.waitUntilSettled()
4065 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004066
4067 def test_disable_at(self):
4068 "Test a pipeline will only report to the disabled trigger when failing"
4069
4070 self.config.set('zuul', 'layout_config',
4071 'tests/fixtures/layout-disable-at.yaml')
4072 self.sched.reconfigure(self.config)
4073
4074 self.assertEqual(3, self.sched.layout.pipelines['check'].disable_at)
4075 self.assertEqual(
4076 0, self.sched.layout.pipelines['check']._consecutive_failures)
4077 self.assertFalse(self.sched.layout.pipelines['check']._disabled)
4078
4079 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4080 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4081 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4082 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4083 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4084 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4085 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4086 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4087 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4088 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4089 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4090
4091 self.worker.addFailTest('project-test1', A)
4092 self.worker.addFailTest('project-test1', B)
4093 # Let C pass, resetting the counter
4094 self.worker.addFailTest('project-test1', D)
4095 self.worker.addFailTest('project-test1', E)
4096 self.worker.addFailTest('project-test1', F)
4097 self.worker.addFailTest('project-test1', G)
4098 self.worker.addFailTest('project-test1', H)
4099 # I also passes but should only report to the disabled reporters
4100 self.worker.addFailTest('project-test1', J)
4101 self.worker.addFailTest('project-test1', K)
4102
4103 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4104 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4105 self.waitUntilSettled()
4106
4107 self.assertEqual(
4108 2, self.sched.layout.pipelines['check']._consecutive_failures)
4109 self.assertFalse(self.sched.layout.pipelines['check']._disabled)
4110
4111 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4112 self.waitUntilSettled()
4113
4114 self.assertEqual(
4115 0, self.sched.layout.pipelines['check']._consecutive_failures)
4116 self.assertFalse(self.sched.layout.pipelines['check']._disabled)
4117
4118 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4119 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4120 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4121 self.waitUntilSettled()
4122
4123 # We should be disabled now
4124 self.assertEqual(
4125 3, self.sched.layout.pipelines['check']._consecutive_failures)
4126 self.assertTrue(self.sched.layout.pipelines['check']._disabled)
4127
4128 # We need to wait between each of these patches to make sure the
4129 # smtp messages come back in an expected order
4130 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4131 self.waitUntilSettled()
4132 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4133 self.waitUntilSettled()
4134 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4135 self.waitUntilSettled()
4136
4137 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4138 # leaving a message on each change
4139 self.assertEqual(1, len(A.messages))
4140 self.assertIn('Build failed.', A.messages[0])
4141 self.assertEqual(1, len(B.messages))
4142 self.assertIn('Build failed.', B.messages[0])
4143 self.assertEqual(1, len(C.messages))
4144 self.assertIn('Build succeeded.', C.messages[0])
4145 self.assertEqual(1, len(D.messages))
4146 self.assertIn('Build failed.', D.messages[0])
4147 self.assertEqual(1, len(E.messages))
4148 self.assertIn('Build failed.', E.messages[0])
4149 self.assertEqual(1, len(F.messages))
4150 self.assertIn('Build failed.', F.messages[0])
4151
4152 # The last 3 (GHI) would have only reported via smtp.
4153 self.assertEqual(3, len(self.smtp_messages))
4154 self.assertEqual(0, len(G.messages))
4155 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
4156 self.assertIn('/7/1/check', self.smtp_messages[0]['body'])
4157 self.assertEqual(0, len(H.messages))
4158 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
4159 self.assertIn('/8/1/check', self.smtp_messages[1]['body'])
4160 self.assertEqual(0, len(I.messages))
4161 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
4162 self.assertIn('/9/1/check', self.smtp_messages[2]['body'])
4163
4164 # Now reload the configuration (simulate a HUP) to check the pipeline
4165 # comes out of disabled
4166 self.sched.reconfigure(self.config)
4167
4168 self.assertEqual(3, self.sched.layout.pipelines['check'].disable_at)
4169 self.assertEqual(
4170 0, self.sched.layout.pipelines['check']._consecutive_failures)
4171 self.assertFalse(self.sched.layout.pipelines['check']._disabled)
4172
4173 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4174 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4175 self.waitUntilSettled()
4176
4177 self.assertEqual(
4178 2, self.sched.layout.pipelines['check']._consecutive_failures)
4179 self.assertFalse(self.sched.layout.pipelines['check']._disabled)
4180
4181 # J and K went back to gerrit
4182 self.assertEqual(1, len(J.messages))
4183 self.assertIn('Build failed.', J.messages[0])
4184 self.assertEqual(1, len(K.messages))
4185 self.assertIn('Build failed.', K.messages[0])
4186 # No more messages reported via smtp
4187 self.assertEqual(3, len(self.smtp_messages))