blob: 85ac6006ef9b71231a664762841a3d8c59648bfa [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
James E. Blair83005782015-12-11 14:46:03 -080049 def setUp(self):
50 self.skip("Disabled for early v3 development")
51
Maru Newby3fe5f852015-01-13 04:22:14 +000052 def test_parse_skip_if(self):
53 job_yaml = """
54jobs:
55 - name: job_name
56 skip-if:
57 - project: ^project_name$
58 branch: ^stable/icehouse$
59 all-files-match-any:
60 - ^filename$
61 - project: ^project2_name$
62 all-files-match-any:
63 - ^filename2$
64 """.strip()
65 data = yaml.load(job_yaml)
66 config_job = data.get('jobs')[0]
Joshua Hesketh352264b2015-08-11 23:42:08 +100067 sched = zuul.scheduler.Scheduler({})
Maru Newby3fe5f852015-01-13 04:22:14 +000068 cm = zuul.change_matcher
69 expected = cm.MatchAny([
70 cm.MatchAll([
71 cm.ProjectMatcher('^project_name$'),
72 cm.BranchMatcher('^stable/icehouse$'),
73 cm.MatchAllFiles([cm.FileMatcher('^filename$')]),
74 ]),
75 cm.MatchAll([
76 cm.ProjectMatcher('^project2_name$'),
77 cm.MatchAllFiles([cm.FileMatcher('^filename2$')]),
78 ]),
79 ])
80 matcher = sched._parseSkipIf(config_job)
81 self.assertEqual(expected, matcher)
82
83
Clark Boylanb640e052014-04-03 16:41:46 -070084class TestScheduler(ZuulTestCase):
Antoine Mussobd86a312014-01-08 14:51:33 +010085
James E. Blair509cada2015-12-08 16:31:16 -080086 def setUp(self):
87 self.skip("Disabled for early v3 development")
88
James E. Blairb0fcae42012-07-17 11:12:10 -070089 def test_jobs_launched(self):
90 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070091
James E. Blairb0fcae42012-07-17 11:12:10 -070092 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -070093 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -070094 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
95 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040096 self.assertEqual(self.getJobFromHistory('project-merge').result,
97 'SUCCESS')
98 self.assertEqual(self.getJobFromHistory('project-test1').result,
99 'SUCCESS')
100 self.assertEqual(self.getJobFromHistory('project-test2').result,
101 'SUCCESS')
102 self.assertEqual(A.data['status'], 'MERGED')
103 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700104
James E. Blair66eeebf2013-07-27 17:44:32 -0700105 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
106 self.assertReportedStat('zuul.pipeline.gate.current_changes',
107 value='1|g')
108 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
109 kind='ms')
110 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
111 value='1|c')
112 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
113 self.assertReportedStat('zuul.pipeline.gate.total_changes',
114 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -0700115 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -0700116 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -0700117 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -0700118 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -0700119
James E. Blair5821bd92015-09-16 08:48:15 -0700120 for build in self.builds:
121 self.assertEqual(build.parameters['ZUUL_VOTING'], '1')
122
James E. Blair3cb10702013-08-24 08:56:03 -0700123 def test_initial_pipeline_gauges(self):
124 "Test that each pipeline reported its length on start"
125 pipeline_names = self.sched.layout.pipelines.keys()
126 self.assertNotEqual(len(pipeline_names), 0)
127 for name in pipeline_names:
128 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
129 value='0|g')
130
James E. Blair42f74822013-05-14 15:18:03 -0700131 def test_duplicate_pipelines(self):
132 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -0700133
James E. Blair42f74822013-05-14 15:18:03 -0700134 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
135 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
136 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -0700137
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400138 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400139 self.history[0].name == 'project-test1'
140 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -0700141
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400142 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -0700143 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400144 self.assertIn('dup1/project-test1', A.messages[0])
145 self.assertNotIn('dup2/project-test1', A.messages[0])
146 self.assertNotIn('dup1/project-test1', A.messages[1])
147 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -0700148 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400149 self.assertIn('dup1/project-test1', A.messages[1])
150 self.assertNotIn('dup2/project-test1', A.messages[1])
151 self.assertNotIn('dup1/project-test1', A.messages[0])
152 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -0700153
James E. Blairb0fcae42012-07-17 11:12:10 -0700154 def test_parallel_changes(self):
155 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700156
157 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700158 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
159 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
160 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700161 A.addApproval('CRVW', 2)
162 B.addApproval('CRVW', 2)
163 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700164
165 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
166 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
167 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
168
169 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400170 self.assertEqual(len(self.builds), 1)
171 self.assertEqual(self.builds[0].name, 'project-merge')
172 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700173
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700174 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700175 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400176 self.assertEqual(len(self.builds), 3)
177 self.assertEqual(self.builds[0].name, 'project-test1')
178 self.assertTrue(self.job_has_changes(self.builds[0], A))
179 self.assertEqual(self.builds[1].name, 'project-test2')
180 self.assertTrue(self.job_has_changes(self.builds[1], A))
181 self.assertEqual(self.builds[2].name, 'project-merge')
182 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700183
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700184 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700185 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400186 self.assertEqual(len(self.builds), 5)
187 self.assertEqual(self.builds[0].name, 'project-test1')
188 self.assertTrue(self.job_has_changes(self.builds[0], A))
189 self.assertEqual(self.builds[1].name, 'project-test2')
190 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700191
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400192 self.assertEqual(self.builds[2].name, 'project-test1')
193 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
194 self.assertEqual(self.builds[3].name, 'project-test2')
195 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700196
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400197 self.assertEqual(self.builds[4].name, 'project-merge')
198 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700199
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700200 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700201 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400202 self.assertEqual(len(self.builds), 6)
203 self.assertEqual(self.builds[0].name, 'project-test1')
204 self.assertTrue(self.job_has_changes(self.builds[0], A))
205 self.assertEqual(self.builds[1].name, 'project-test2')
206 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700207
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400208 self.assertEqual(self.builds[2].name, 'project-test1')
209 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
210 self.assertEqual(self.builds[3].name, 'project-test2')
211 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700212
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400213 self.assertEqual(self.builds[4].name, 'project-test1')
214 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
215 self.assertEqual(self.builds[5].name, 'project-test2')
216 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700217
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700218 self.worker.hold_jobs_in_build = False
219 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700220 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400221 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700222
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400223 self.assertEqual(len(self.history), 9)
224 self.assertEqual(A.data['status'], 'MERGED')
225 self.assertEqual(B.data['status'], 'MERGED')
226 self.assertEqual(C.data['status'], 'MERGED')
227 self.assertEqual(A.reported, 2)
228 self.assertEqual(B.reported, 2)
229 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700230
231 def test_failed_changes(self):
232 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -0400233 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700234
James E. Blairb02a3bb2012-07-30 17:49:55 -0700235 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
236 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -0700237 A.addApproval('CRVW', 2)
238 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700239
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700240 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700241
James E. Blaire2819012013-06-28 17:17:26 -0400242 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
243 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700244 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -0400245
246 self.worker.release('.*-merge')
247 self.waitUntilSettled()
248
249 self.worker.hold_jobs_in_build = False
250 self.worker.release()
251
252 self.waitUntilSettled()
253 # It's certain that the merge job for change 2 will run, but
254 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400255 self.assertTrue(len(self.history) > 6)
256 self.assertEqual(A.data['status'], 'NEW')
257 self.assertEqual(B.data['status'], 'MERGED')
258 self.assertEqual(A.reported, 2)
259 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700260
261 def test_independent_queues(self):
262 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700263
264 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900265 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700266 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
267 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700268 A.addApproval('CRVW', 2)
269 B.addApproval('CRVW', 2)
270 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700271
272 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
273 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
274 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
275
James E. Blairb02a3bb2012-07-30 17:49:55 -0700276 self.waitUntilSettled()
277
278 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400279 self.assertEqual(len(self.builds), 2)
280 self.assertEqual(self.builds[0].name, 'project-merge')
281 self.assertTrue(self.job_has_changes(self.builds[0], A))
282 self.assertEqual(self.builds[1].name, 'project1-merge')
283 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700284
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700285 # Release the current merge builds
286 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700287 self.waitUntilSettled()
288 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700289 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700290 self.waitUntilSettled()
291
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700292 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -0700293 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400294 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700295
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700296 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700297 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400298 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700299
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400300 self.assertEqual(len(self.history), 11)
301 self.assertEqual(A.data['status'], 'MERGED')
302 self.assertEqual(B.data['status'], 'MERGED')
303 self.assertEqual(C.data['status'], 'MERGED')
304 self.assertEqual(A.reported, 2)
305 self.assertEqual(B.reported, 2)
306 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700307
308 def test_failed_change_at_head(self):
309 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700310
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700311 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700312 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
313 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
314 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700315 A.addApproval('CRVW', 2)
316 B.addApproval('CRVW', 2)
317 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700318
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700319 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700320
321 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
322 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
323 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
324
325 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700326
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400327 self.assertEqual(len(self.builds), 1)
328 self.assertEqual(self.builds[0].name, 'project-merge')
329 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -0700330
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700331 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700332 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700333 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700334 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700335 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700336 self.waitUntilSettled()
337
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400338 self.assertEqual(len(self.builds), 6)
339 self.assertEqual(self.builds[0].name, 'project-test1')
340 self.assertEqual(self.builds[1].name, 'project-test2')
341 self.assertEqual(self.builds[2].name, 'project-test1')
342 self.assertEqual(self.builds[3].name, 'project-test2')
343 self.assertEqual(self.builds[4].name, 'project-test1')
344 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700345
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400346 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700347 self.waitUntilSettled()
348
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400349 # project-test2, project-merge for B
350 self.assertEqual(len(self.builds), 2)
351 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -0700352
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700353 self.worker.hold_jobs_in_build = False
354 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700355 self.waitUntilSettled()
356
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400357 self.assertEqual(len(self.builds), 0)
358 self.assertEqual(len(self.history), 15)
359 self.assertEqual(A.data['status'], 'NEW')
360 self.assertEqual(B.data['status'], 'MERGED')
361 self.assertEqual(C.data['status'], 'MERGED')
362 self.assertEqual(A.reported, 2)
363 self.assertEqual(B.reported, 2)
364 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700365
James E. Blair0aac4872013-08-23 14:02:38 -0700366 def test_failed_change_in_middle(self):
367 "Test a failed change in the middle of the queue"
368
369 self.worker.hold_jobs_in_build = True
370 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
371 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
372 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
373 A.addApproval('CRVW', 2)
374 B.addApproval('CRVW', 2)
375 C.addApproval('CRVW', 2)
376
377 self.worker.addFailTest('project-test1', B)
378
379 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
380 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
381 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
382
383 self.waitUntilSettled()
384
385 self.worker.release('.*-merge')
386 self.waitUntilSettled()
387 self.worker.release('.*-merge')
388 self.waitUntilSettled()
389 self.worker.release('.*-merge')
390 self.waitUntilSettled()
391
392 self.assertEqual(len(self.builds), 6)
393 self.assertEqual(self.builds[0].name, 'project-test1')
394 self.assertEqual(self.builds[1].name, 'project-test2')
395 self.assertEqual(self.builds[2].name, 'project-test1')
396 self.assertEqual(self.builds[3].name, 'project-test2')
397 self.assertEqual(self.builds[4].name, 'project-test1')
398 self.assertEqual(self.builds[5].name, 'project-test2')
399
400 self.release(self.builds[2])
401 self.waitUntilSettled()
402
James E. Blair972e3c72013-08-29 12:04:55 -0700403 # project-test1 and project-test2 for A
404 # project-test2 for B
405 # project-merge for C (without B)
406 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700407 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
408
James E. Blair972e3c72013-08-29 12:04:55 -0700409 self.worker.release('.*-merge')
410 self.waitUntilSettled()
411
412 # project-test1 and project-test2 for A
413 # project-test2 for B
414 # project-test1 and project-test2 for C
415 self.assertEqual(len(self.builds), 5)
416
James E. Blair0aac4872013-08-23 14:02:38 -0700417 items = self.sched.layout.pipelines['gate'].getAllItems()
418 builds = items[0].current_build_set.getBuilds()
419 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
420 self.assertEqual(self.countJobResults(builds, None), 2)
421 builds = items[1].current_build_set.getBuilds()
422 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
423 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
424 self.assertEqual(self.countJobResults(builds, None), 1)
425 builds = items[2].current_build_set.getBuilds()
426 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700427 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700428
429 self.worker.hold_jobs_in_build = False
430 self.worker.release()
431 self.waitUntilSettled()
432
433 self.assertEqual(len(self.builds), 0)
434 self.assertEqual(len(self.history), 12)
435 self.assertEqual(A.data['status'], 'MERGED')
436 self.assertEqual(B.data['status'], 'NEW')
437 self.assertEqual(C.data['status'], 'MERGED')
438 self.assertEqual(A.reported, 2)
439 self.assertEqual(B.reported, 2)
440 self.assertEqual(C.reported, 2)
441
James E. Blaird466dc42012-07-31 10:42:56 -0700442 def test_failed_change_at_head_with_queue(self):
443 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700444
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700445 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700446 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
447 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
448 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700449 A.addApproval('CRVW', 2)
450 B.addApproval('CRVW', 2)
451 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700452
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700453 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700454
455 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
456 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
457 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
458
459 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700460 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400461 self.assertEqual(len(self.builds), 0)
462 self.assertEqual(len(queue), 1)
463 self.assertEqual(queue[0].name, 'build:project-merge')
464 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -0700465
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700466 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700467 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700468 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700469 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700470 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700471 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700472 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700473
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400474 self.assertEqual(len(self.builds), 0)
475 self.assertEqual(len(queue), 6)
476 self.assertEqual(queue[0].name, 'build:project-test1')
477 self.assertEqual(queue[1].name, 'build:project-test2')
478 self.assertEqual(queue[2].name, 'build:project-test1')
479 self.assertEqual(queue[3].name, 'build:project-test2')
480 self.assertEqual(queue[4].name, 'build:project-test1')
481 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700482
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700483 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700484 self.waitUntilSettled()
485
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400486 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700487 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400488 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
489 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700490
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700491 self.gearman_server.hold_jobs_in_queue = False
492 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700493 self.waitUntilSettled()
494
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400495 self.assertEqual(len(self.builds), 0)
496 self.assertEqual(len(self.history), 11)
497 self.assertEqual(A.data['status'], 'NEW')
498 self.assertEqual(B.data['status'], 'MERGED')
499 self.assertEqual(C.data['status'], 'MERGED')
500 self.assertEqual(A.reported, 2)
501 self.assertEqual(B.reported, 2)
502 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700503
James E. Blairfef71632013-09-23 11:15:47 -0700504 def test_two_failed_changes_at_head(self):
505 "Test that changes are reparented correctly if 2 fail at head"
506
507 self.worker.hold_jobs_in_build = True
508 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
509 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
510 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
511 A.addApproval('CRVW', 2)
512 B.addApproval('CRVW', 2)
513 C.addApproval('CRVW', 2)
514
515 self.worker.addFailTest('project-test1', A)
516 self.worker.addFailTest('project-test1', B)
517
518 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
519 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
520 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
521 self.waitUntilSettled()
522
523 self.worker.release('.*-merge')
524 self.waitUntilSettled()
525 self.worker.release('.*-merge')
526 self.waitUntilSettled()
527 self.worker.release('.*-merge')
528 self.waitUntilSettled()
529
530 self.assertEqual(len(self.builds), 6)
531 self.assertEqual(self.builds[0].name, 'project-test1')
532 self.assertEqual(self.builds[1].name, 'project-test2')
533 self.assertEqual(self.builds[2].name, 'project-test1')
534 self.assertEqual(self.builds[3].name, 'project-test2')
535 self.assertEqual(self.builds[4].name, 'project-test1')
536 self.assertEqual(self.builds[5].name, 'project-test2')
537
538 self.assertTrue(self.job_has_changes(self.builds[0], A))
539 self.assertTrue(self.job_has_changes(self.builds[2], A))
540 self.assertTrue(self.job_has_changes(self.builds[2], B))
541 self.assertTrue(self.job_has_changes(self.builds[4], A))
542 self.assertTrue(self.job_has_changes(self.builds[4], B))
543 self.assertTrue(self.job_has_changes(self.builds[4], C))
544
545 # Fail change B first
546 self.release(self.builds[2])
547 self.waitUntilSettled()
548
549 # restart of C after B failure
550 self.worker.release('.*-merge')
551 self.waitUntilSettled()
552
553 self.assertEqual(len(self.builds), 5)
554 self.assertEqual(self.builds[0].name, 'project-test1')
555 self.assertEqual(self.builds[1].name, 'project-test2')
556 self.assertEqual(self.builds[2].name, 'project-test2')
557 self.assertEqual(self.builds[3].name, 'project-test1')
558 self.assertEqual(self.builds[4].name, 'project-test2')
559
560 self.assertTrue(self.job_has_changes(self.builds[1], A))
561 self.assertTrue(self.job_has_changes(self.builds[2], A))
562 self.assertTrue(self.job_has_changes(self.builds[2], B))
563 self.assertTrue(self.job_has_changes(self.builds[4], A))
564 self.assertFalse(self.job_has_changes(self.builds[4], B))
565 self.assertTrue(self.job_has_changes(self.builds[4], C))
566
567 # Finish running all passing jobs for change A
568 self.release(self.builds[1])
569 self.waitUntilSettled()
570 # Fail and report change A
571 self.release(self.builds[0])
572 self.waitUntilSettled()
573
574 # restart of B,C after A failure
575 self.worker.release('.*-merge')
576 self.waitUntilSettled()
577 self.worker.release('.*-merge')
578 self.waitUntilSettled()
579
580 self.assertEqual(len(self.builds), 4)
581 self.assertEqual(self.builds[0].name, 'project-test1') # B
582 self.assertEqual(self.builds[1].name, 'project-test2') # B
583 self.assertEqual(self.builds[2].name, 'project-test1') # C
584 self.assertEqual(self.builds[3].name, 'project-test2') # C
585
586 self.assertFalse(self.job_has_changes(self.builds[1], A))
587 self.assertTrue(self.job_has_changes(self.builds[1], B))
588 self.assertFalse(self.job_has_changes(self.builds[1], C))
589
590 self.assertFalse(self.job_has_changes(self.builds[2], A))
591 # After A failed and B and C restarted, B should be back in
592 # C's tests because it has not failed yet.
593 self.assertTrue(self.job_has_changes(self.builds[2], B))
594 self.assertTrue(self.job_has_changes(self.builds[2], C))
595
596 self.worker.hold_jobs_in_build = False
597 self.worker.release()
598 self.waitUntilSettled()
599
600 self.assertEqual(len(self.builds), 0)
601 self.assertEqual(len(self.history), 21)
602 self.assertEqual(A.data['status'], 'NEW')
603 self.assertEqual(B.data['status'], 'NEW')
604 self.assertEqual(C.data['status'], 'MERGED')
605 self.assertEqual(A.reported, 2)
606 self.assertEqual(B.reported, 2)
607 self.assertEqual(C.reported, 2)
608
James E. Blair8c803f82012-07-31 16:25:42 -0700609 def test_patch_order(self):
610 "Test that dependent patches are tested in the right order"
611 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
612 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
613 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
614 A.addApproval('CRVW', 2)
615 B.addApproval('CRVW', 2)
616 C.addApproval('CRVW', 2)
617
618 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
619 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
620 M2.setMerged()
621 M1.setMerged()
622
623 # C -> B -> A -> M1 -> M2
624 # M2 is here to make sure it is never queried. If it is, it
625 # means zuul is walking down the entire history of merged
626 # changes.
627
628 C.setDependsOn(B, 1)
629 B.setDependsOn(A, 1)
630 A.setDependsOn(M1, 1)
631 M1.setDependsOn(M2, 1)
632
633 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
634
635 self.waitUntilSettled()
636
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400637 self.assertEqual(A.data['status'], 'NEW')
638 self.assertEqual(B.data['status'], 'NEW')
639 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700640
641 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
642 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
643
644 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400645 self.assertEqual(M2.queried, 0)
646 self.assertEqual(A.data['status'], 'MERGED')
647 self.assertEqual(B.data['status'], 'MERGED')
648 self.assertEqual(C.data['status'], 'MERGED')
649 self.assertEqual(A.reported, 2)
650 self.assertEqual(B.reported, 2)
651 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700652
James E. Blair063672f2015-01-29 13:09:12 -0800653 def test_needed_changes_enqueue(self):
654 "Test that a needed change is enqueued ahead"
655 # A Given a git tree like this, if we enqueue
656 # / \ change C, we should walk up and down the tree
657 # B G and enqueue changes in the order ABCDEFG.
658 # /|\ This is also the order that you would get if
659 # *C E F you enqueued changes in the order ABCDEFG, so
660 # / the ordering is stable across re-enqueue events.
661 # D
662
663 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
664 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
665 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
666 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
667 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
668 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
669 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
670 B.setDependsOn(A, 1)
671 C.setDependsOn(B, 1)
672 D.setDependsOn(C, 1)
673 E.setDependsOn(B, 1)
674 F.setDependsOn(B, 1)
675 G.setDependsOn(A, 1)
676
677 A.addApproval('CRVW', 2)
678 B.addApproval('CRVW', 2)
679 C.addApproval('CRVW', 2)
680 D.addApproval('CRVW', 2)
681 E.addApproval('CRVW', 2)
682 F.addApproval('CRVW', 2)
683 G.addApproval('CRVW', 2)
684 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
685
686 self.waitUntilSettled()
687
688 self.assertEqual(A.data['status'], 'NEW')
689 self.assertEqual(B.data['status'], 'NEW')
690 self.assertEqual(C.data['status'], 'NEW')
691 self.assertEqual(D.data['status'], 'NEW')
692 self.assertEqual(E.data['status'], 'NEW')
693 self.assertEqual(F.data['status'], 'NEW')
694 self.assertEqual(G.data['status'], 'NEW')
695
696 # We're about to add approvals to changes without adding the
697 # triggering events to Zuul, so that we can be sure that it is
698 # enqueing the changes based on dependencies, not because of
699 # triggering events. Since it will have the changes cached
700 # already (without approvals), we need to clear the cache
701 # first.
702 source = self.sched.layout.pipelines['gate'].source
703 source.maintainCache([])
704
705 self.worker.hold_jobs_in_build = True
706 A.addApproval('APRV', 1)
707 B.addApproval('APRV', 1)
708 D.addApproval('APRV', 1)
709 E.addApproval('APRV', 1)
710 F.addApproval('APRV', 1)
711 G.addApproval('APRV', 1)
712 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
713
714 for x in range(8):
715 self.worker.release('.*-merge')
716 self.waitUntilSettled()
717 self.worker.hold_jobs_in_build = False
718 self.worker.release()
719 self.waitUntilSettled()
720
721 self.assertEqual(A.data['status'], 'MERGED')
722 self.assertEqual(B.data['status'], 'MERGED')
723 self.assertEqual(C.data['status'], 'MERGED')
724 self.assertEqual(D.data['status'], 'MERGED')
725 self.assertEqual(E.data['status'], 'MERGED')
726 self.assertEqual(F.data['status'], 'MERGED')
727 self.assertEqual(G.data['status'], 'MERGED')
728 self.assertEqual(A.reported, 2)
729 self.assertEqual(B.reported, 2)
730 self.assertEqual(C.reported, 2)
731 self.assertEqual(D.reported, 2)
732 self.assertEqual(E.reported, 2)
733 self.assertEqual(F.reported, 2)
734 self.assertEqual(G.reported, 2)
735 self.assertEqual(self.history[6].changes,
736 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
737
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100738 def test_source_cache(self):
739 "Test that the source cache operates correctly"
James E. Blair0e933c52013-07-11 10:18:52 -0700740 self.worker.hold_jobs_in_build = True
741
742 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
743 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
744 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
745 A.addApproval('CRVW', 2)
746 B.addApproval('CRVW', 2)
747
748 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
749 M1.setMerged()
750
751 B.setDependsOn(A, 1)
752 A.setDependsOn(M1, 1)
753
754 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
755 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
756
757 self.waitUntilSettled()
758
759 for build in self.builds:
760 if build.parameters['ZUUL_PIPELINE'] == 'check':
761 build.release()
762 self.waitUntilSettled()
763 for build in self.builds:
764 if build.parameters['ZUUL_PIPELINE'] == 'check':
765 build.release()
766 self.waitUntilSettled()
767
768 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
769 self.waitUntilSettled()
770
Joshua Hesketh352264b2015-08-11 23:42:08 +1000771 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700772 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000773 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700774
775 self.worker.hold_jobs_in_build = False
776 self.worker.release()
777 self.waitUntilSettled()
778
779 self.assertEqual(A.data['status'], 'MERGED')
780 self.assertEqual(B.data['status'], 'MERGED')
781 self.assertEqual(A.queried, 2) # Initial and isMerged
782 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
783
James E. Blair8c803f82012-07-31 16:25:42 -0700784 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700785 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700786 # TODO: move to test_gerrit (this is a unit test!)
787 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairc0dedf82014-08-06 09:37:52 -0700788 source = self.sched.layout.pipelines['gate'].source
789 a = source._getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -0400790 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700791 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700792
793 A.addApproval('CRVW', 2)
James E. Blairc0dedf82014-08-06 09:37:52 -0700794 a = source._getChange(1, 2, refresh=True)
795 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700796
797 A.addApproval('APRV', 1)
James E. Blairc0dedf82014-08-06 09:37:52 -0700798 a = source._getChange(1, 2, refresh=True)
799 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
800 source.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -0700801
802 def test_build_configuration(self):
803 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700804
805 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -0700806 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
807 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
808 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
809 A.addApproval('CRVW', 2)
810 B.addApproval('CRVW', 2)
811 C.addApproval('CRVW', 2)
812 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
813 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
814 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
815 self.waitUntilSettled()
816
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700817 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -0700818 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700819 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -0700820 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700821 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -0700822 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700823 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700824 ref = self.getParameter(queue[-1], 'ZUUL_REF')
825 self.gearman_server.hold_jobs_in_queue = False
826 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700827 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -0700828
Monty Taylorbc758832013-06-17 17:22:42 -0400829 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -0700830 repo = git.Repo(path)
831 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
832 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -0700833 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400834 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -0700835
836 def test_build_configuration_conflict(self):
837 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700838
839 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -0700840 A = self.fake_gerrit.addFakeChange('org/conflict-project',
841 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -0700842 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700843 B = self.fake_gerrit.addFakeChange('org/conflict-project',
844 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -0700845 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700846 C = self.fake_gerrit.addFakeChange('org/conflict-project',
847 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -0700848 A.addApproval('CRVW', 2)
849 B.addApproval('CRVW', 2)
850 C.addApproval('CRVW', 2)
851 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
852 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
853 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
854 self.waitUntilSettled()
855
James E. Blair6736beb2013-07-11 15:18:15 -0700856 self.assertEqual(A.reported, 1)
857 self.assertEqual(B.reported, 1)
858 self.assertEqual(C.reported, 1)
859
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700860 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700861 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700862 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700863 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700864 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700865 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700866
867 self.assertEqual(len(self.history), 2) # A and C merge jobs
868
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700869 self.gearman_server.hold_jobs_in_queue = False
870 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700871 self.waitUntilSettled()
872
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400873 self.assertEqual(A.data['status'], 'MERGED')
874 self.assertEqual(B.data['status'], 'NEW')
875 self.assertEqual(C.data['status'], 'MERGED')
876 self.assertEqual(A.reported, 2)
877 self.assertEqual(B.reported, 2)
878 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -0700879 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -0700880
James E. Blairdaabed22012-08-15 15:38:57 -0700881 def test_post(self):
882 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700883
Zhongyue Luo5d556072012-09-21 02:00:47 +0900884 e = {
885 "type": "ref-updated",
886 "submitter": {
887 "name": "User Name",
888 },
889 "refUpdate": {
890 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
891 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
892 "refName": "master",
893 "project": "org/project",
894 }
895 }
James E. Blairdaabed22012-08-15 15:38:57 -0700896 self.fake_gerrit.addEvent(e)
897 self.waitUntilSettled()
898
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400899 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400900 self.assertEqual(len(self.history), 1)
901 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -0700902
K Jonathan Harkerf95e7232015-04-29 13:33:16 -0700903 def test_post_ignore_deletes(self):
904 "Test that deleting refs does not trigger post jobs"
905
906 e = {
907 "type": "ref-updated",
908 "submitter": {
909 "name": "User Name",
910 },
911 "refUpdate": {
912 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
913 "newRev": "0000000000000000000000000000000000000000",
914 "refName": "master",
915 "project": "org/project",
916 }
917 }
918 self.fake_gerrit.addEvent(e)
919 self.waitUntilSettled()
920
921 job_names = [x.name for x in self.history]
922 self.assertEqual(len(self.history), 0)
923 self.assertNotIn('project-post', job_names)
924
925 def test_post_ignore_deletes_negative(self):
926 "Test that deleting refs does trigger post jobs"
927
James E. Blairf84026c2015-12-08 16:11:46 -0800928 self.updateConfigLayout(
929 'tests/fixtures/layout-dont-ignore-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -0700930 self.sched.reconfigure(self.config)
931
932 e = {
933 "type": "ref-updated",
934 "submitter": {
935 "name": "User Name",
936 },
937 "refUpdate": {
938 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
939 "newRev": "0000000000000000000000000000000000000000",
940 "refName": "master",
941 "project": "org/project",
942 }
943 }
944 self.fake_gerrit.addEvent(e)
945 self.waitUntilSettled()
946
947 job_names = [x.name for x in self.history]
948 self.assertEqual(len(self.history), 1)
949 self.assertIn('project-post', job_names)
950
James E. Blairc6294a52012-08-17 10:19:48 -0700951 def test_build_configuration_branch(self):
952 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700953
954 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -0700955 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
956 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
957 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
958 A.addApproval('CRVW', 2)
959 B.addApproval('CRVW', 2)
960 C.addApproval('CRVW', 2)
961 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
962 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
963 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
964 self.waitUntilSettled()
965
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700966 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -0700967 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700968 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -0700969 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700970 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -0700971 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700972 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700973 ref = self.getParameter(queue[-1], 'ZUUL_REF')
974 self.gearman_server.hold_jobs_in_queue = False
975 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -0700976 self.waitUntilSettled()
977
Monty Taylorbc758832013-06-17 17:22:42 -0400978 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -0700979 repo = git.Repo(path)
980 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
981 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -0700982 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400983 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -0700984
985 def test_build_configuration_branch_interaction(self):
986 "Test that switching between branches works"
987 self.test_build_configuration()
988 self.test_build_configuration_branch()
989 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -0400990 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -0700991 repo = git.Repo(path)
992 repo.heads.master.commit = repo.commit('init')
993 self.test_build_configuration()
994
995 def test_build_configuration_multi_branch(self):
996 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700997
998 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -0700999 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1000 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1001 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1002 A.addApproval('CRVW', 2)
1003 B.addApproval('CRVW', 2)
1004 C.addApproval('CRVW', 2)
1005 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1006 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1007 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1008 self.waitUntilSettled()
James E. Blairbb1fe502014-03-04 10:15:06 -08001009 queue = self.gearman_server.getQueue()
1010 job_A = None
1011 for job in queue:
1012 if 'project-merge' in job.name:
1013 job_A = job
1014 ref_A = self.getParameter(job_A, 'ZUUL_REF')
1015 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
1016 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
1017 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
James E. Blairc6294a52012-08-17 10:19:48 -07001018
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001019 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001020 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001021 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001022 job_B = None
1023 for job in queue:
1024 if 'project-merge' in job.name:
1025 job_B = job
1026 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001027 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001028 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blairbb1fe502014-03-04 10:15:06 -08001029 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
1030
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001031 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001032 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001033 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001034 for job in queue:
1035 if 'project-merge' in job.name:
1036 job_C = job
1037 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001038 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001039 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blairbb1fe502014-03-04 10:15:06 -08001040 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001041 self.gearman_server.hold_jobs_in_queue = False
1042 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001043 self.waitUntilSettled()
1044
Monty Taylorbc758832013-06-17 17:22:42 -04001045 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001046 repo = git.Repo(path)
1047
1048 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001049 for c in repo.iter_commits(ref_C)]
James E. Blairbb1fe502014-03-04 10:15:06 -08001050 repo_shas = [c.hexsha for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001051 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001052 correct_messages = ['initial commit', 'A-1', 'C-1']
James E. Blairbb1fe502014-03-04 10:15:06 -08001053 # Ensure the right commits are in the history for this ref
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001054 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08001055 # Ensure ZUUL_REF -> ZUUL_COMMIT
1056 self.assertEqual(repo_shas[0], commit_C)
James E. Blairc6294a52012-08-17 10:19:48 -07001057
1058 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001059 for c in repo.iter_commits(ref_B)]
James E. Blairbb1fe502014-03-04 10:15:06 -08001060 repo_shas = [c.hexsha for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001061 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001062 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001063 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08001064 self.assertEqual(repo_shas[0], commit_B)
1065
1066 repo_messages = [c.message.strip()
1067 for c in repo.iter_commits(ref_A)]
1068 repo_shas = [c.hexsha for c in repo.iter_commits(ref_A)]
1069 repo_messages.reverse()
1070 correct_messages = ['initial commit', 'A-1']
1071 self.assertEqual(repo_messages, correct_messages)
1072 self.assertEqual(repo_shas[0], commit_A)
1073
1074 self.assertNotEqual(ref_A, ref_B, ref_C)
1075 self.assertNotEqual(commit_A, commit_B, commit_C)
James E. Blair7f71c802012-08-22 13:04:32 -07001076
1077 def test_one_job_project(self):
1078 "Test that queueing works with one job"
1079 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1080 'master', 'A')
1081 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1082 'master', 'B')
1083 A.addApproval('CRVW', 2)
1084 B.addApproval('CRVW', 2)
1085 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1086 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1087 self.waitUntilSettled()
1088
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001089 self.assertEqual(A.data['status'], 'MERGED')
1090 self.assertEqual(A.reported, 2)
1091 self.assertEqual(B.data['status'], 'MERGED')
1092 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001093
Antoine Musso80edd5a2013-02-13 15:37:53 +01001094 def test_job_from_templates_launched(self):
1095 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001096
Antoine Musso80edd5a2013-02-13 15:37:53 +01001097 A = self.fake_gerrit.addFakeChange(
1098 'org/templated-project', 'master', 'A')
1099 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1100 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001101
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001102 self.assertEqual(self.getJobFromHistory('project-test1').result,
1103 'SUCCESS')
1104 self.assertEqual(self.getJobFromHistory('project-test2').result,
1105 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001106
James E. Blair3e98c022013-12-16 15:25:38 -08001107 def test_layered_templates(self):
1108 "Test whether a job generated via a template can be launched"
1109
1110 A = self.fake_gerrit.addFakeChange(
1111 'org/layered-project', 'master', 'A')
1112 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1113 self.waitUntilSettled()
1114
1115 self.assertEqual(self.getJobFromHistory('project-test1').result,
1116 'SUCCESS')
1117 self.assertEqual(self.getJobFromHistory('project-test2').result,
1118 'SUCCESS')
James E. Blairaea6cf62013-12-16 15:38:12 -08001119 self.assertEqual(self.getJobFromHistory('layered-project-test3'
1120 ).result, 'SUCCESS')
1121 self.assertEqual(self.getJobFromHistory('layered-project-test4'
1122 ).result, 'SUCCESS')
James E. Blair12a92b12014-03-26 11:54:53 -07001123 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
1124 ).result, 'SUCCESS')
James E. Blair3e98c022013-12-16 15:25:38 -08001125 self.assertEqual(self.getJobFromHistory('project-test6').result,
1126 'SUCCESS')
1127
James E. Blaircaec0c52012-08-22 14:52:22 -07001128 def test_dependent_changes_dequeue(self):
1129 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001130
James E. Blaircaec0c52012-08-22 14:52:22 -07001131 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1132 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1133 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1134 A.addApproval('CRVW', 2)
1135 B.addApproval('CRVW', 2)
1136 C.addApproval('CRVW', 2)
1137
1138 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1139 M1.setMerged()
1140
1141 # C -> B -> A -> M1
1142
1143 C.setDependsOn(B, 1)
1144 B.setDependsOn(A, 1)
1145 A.setDependsOn(M1, 1)
1146
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001147 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001148
1149 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1150 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1151 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1152
1153 self.waitUntilSettled()
1154
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001155 self.assertEqual(A.data['status'], 'NEW')
1156 self.assertEqual(A.reported, 2)
1157 self.assertEqual(B.data['status'], 'NEW')
1158 self.assertEqual(B.reported, 2)
1159 self.assertEqual(C.data['status'], 'NEW')
1160 self.assertEqual(C.reported, 2)
1161 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001162
James E. Blair972e3c72013-08-29 12:04:55 -07001163 def test_failing_dependent_changes(self):
1164 "Test that failing dependent patches are taken out of stream"
1165 self.worker.hold_jobs_in_build = True
1166 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1167 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1168 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1169 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1170 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
1171 A.addApproval('CRVW', 2)
1172 B.addApproval('CRVW', 2)
1173 C.addApproval('CRVW', 2)
1174 D.addApproval('CRVW', 2)
1175 E.addApproval('CRVW', 2)
1176
1177 # E, D -> C -> B, A
1178
1179 D.setDependsOn(C, 1)
1180 C.setDependsOn(B, 1)
1181
1182 self.worker.addFailTest('project-test1', B)
1183
1184 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1185 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1186 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1187 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1188 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1189
1190 self.waitUntilSettled()
1191 self.worker.release('.*-merge')
1192 self.waitUntilSettled()
1193 self.worker.release('.*-merge')
1194 self.waitUntilSettled()
1195 self.worker.release('.*-merge')
1196 self.waitUntilSettled()
1197 self.worker.release('.*-merge')
1198 self.waitUntilSettled()
1199 self.worker.release('.*-merge')
1200 self.waitUntilSettled()
1201
1202 self.worker.hold_jobs_in_build = False
1203 for build in self.builds:
1204 if build.parameters['ZUUL_CHANGE'] != '1':
1205 build.release()
1206 self.waitUntilSettled()
1207
1208 self.worker.release()
1209 self.waitUntilSettled()
1210
1211 self.assertEqual(A.data['status'], 'MERGED')
1212 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001213 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001214 self.assertEqual(B.data['status'], 'NEW')
1215 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001216 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001217 self.assertEqual(C.data['status'], 'NEW')
1218 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001219 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001220 self.assertEqual(D.data['status'], 'NEW')
1221 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001222 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001223 self.assertEqual(E.data['status'], 'MERGED')
1224 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001225 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001226 self.assertEqual(len(self.history), 18)
1227
James E. Blairec590122012-08-22 15:19:31 -07001228 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001229 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001230 # If it's dequeued more than once, we should see extra
1231 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001232
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001233 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001234 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1235 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1236 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1237 A.addApproval('CRVW', 2)
1238 B.addApproval('CRVW', 2)
1239 C.addApproval('CRVW', 2)
1240
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001241 self.worker.addFailTest('project1-test1', A)
1242 self.worker.addFailTest('project1-test2', A)
1243 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001244
1245 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1246 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1247 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1248
1249 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001250
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001251 self.assertEqual(len(self.builds), 1)
1252 self.assertEqual(self.builds[0].name, 'project1-merge')
1253 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07001254
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001255 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001256 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001257 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001258 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001259 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001260 self.waitUntilSettled()
1261
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001262 self.assertEqual(len(self.builds), 9)
1263 self.assertEqual(self.builds[0].name, 'project1-test1')
1264 self.assertEqual(self.builds[1].name, 'project1-test2')
1265 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
1266 self.assertEqual(self.builds[3].name, 'project1-test1')
1267 self.assertEqual(self.builds[4].name, 'project1-test2')
1268 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
1269 self.assertEqual(self.builds[6].name, 'project1-test1')
1270 self.assertEqual(self.builds[7].name, 'project1-test2')
1271 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001272
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001273 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001274 self.waitUntilSettled()
1275
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001276 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1277 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001278
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001279 self.worker.hold_jobs_in_build = False
1280 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001281 self.waitUntilSettled()
1282
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001283 self.assertEqual(len(self.builds), 0)
1284 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001285
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001286 self.assertEqual(A.data['status'], 'NEW')
1287 self.assertEqual(B.data['status'], 'MERGED')
1288 self.assertEqual(C.data['status'], 'MERGED')
1289 self.assertEqual(A.reported, 2)
1290 self.assertEqual(B.reported, 2)
1291 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001292
1293 def test_nonvoting_job(self):
1294 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001295
James E. Blair4ec821f2012-08-23 15:28:28 -07001296 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1297 'master', 'A')
1298 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001299 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001300 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1301
1302 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001303
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001304 self.assertEqual(A.data['status'], 'MERGED')
1305 self.assertEqual(A.reported, 2)
1306 self.assertEqual(
1307 self.getJobFromHistory('nonvoting-project-merge').result,
1308 'SUCCESS')
1309 self.assertEqual(
1310 self.getJobFromHistory('nonvoting-project-test1').result,
1311 'SUCCESS')
1312 self.assertEqual(
1313 self.getJobFromHistory('nonvoting-project-test2').result,
1314 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001315
James E. Blair5821bd92015-09-16 08:48:15 -07001316 for build in self.builds:
1317 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1318
James E. Blaire0487072012-08-29 17:38:31 -07001319 def test_check_queue_success(self):
1320 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001321
James E. Blaire0487072012-08-29 17:38:31 -07001322 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1323 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1324
1325 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001326
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001327 self.assertEqual(A.data['status'], 'NEW')
1328 self.assertEqual(A.reported, 1)
1329 self.assertEqual(self.getJobFromHistory('project-merge').result,
1330 'SUCCESS')
1331 self.assertEqual(self.getJobFromHistory('project-test1').result,
1332 'SUCCESS')
1333 self.assertEqual(self.getJobFromHistory('project-test2').result,
1334 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001335
1336 def test_check_queue_failure(self):
1337 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001338
James E. Blaire0487072012-08-29 17:38:31 -07001339 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001340 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001341 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1342
1343 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001344
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001345 self.assertEqual(A.data['status'], 'NEW')
1346 self.assertEqual(A.reported, 1)
1347 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001348 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001349 self.assertEqual(self.getJobFromHistory('project-test1').result,
1350 'SUCCESS')
1351 self.assertEqual(self.getJobFromHistory('project-test2').result,
1352 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001353
1354 def test_dependent_behind_dequeue(self):
1355 "test that dependent changes behind dequeued changes work"
1356 # This complicated test is a reproduction of a real life bug
1357 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001358
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001359 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001360 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1361 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1362 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1363 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1364 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1365 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1366 D.setDependsOn(C, 1)
1367 E.setDependsOn(D, 1)
1368 A.addApproval('CRVW', 2)
1369 B.addApproval('CRVW', 2)
1370 C.addApproval('CRVW', 2)
1371 D.addApproval('CRVW', 2)
1372 E.addApproval('CRVW', 2)
1373 F.addApproval('CRVW', 2)
1374
1375 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001376
1377 # Change object re-use in the gerrit trigger is hidden if
1378 # changes are added in quick succession; waiting makes it more
1379 # like real life.
1380 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1381 self.waitUntilSettled()
1382 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1383 self.waitUntilSettled()
1384
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001385 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001386 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001387 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001388 self.waitUntilSettled()
1389
1390 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1391 self.waitUntilSettled()
1392 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1393 self.waitUntilSettled()
1394 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1395 self.waitUntilSettled()
1396 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1397 self.waitUntilSettled()
1398
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001399 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001400 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001401 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001402 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001403 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001404 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001405 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001406 self.waitUntilSettled()
1407
1408 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001409
1410 # Grab pointers to the jobs we want to release before
1411 # releasing any, because list indexes may change as
1412 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001413 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001414 a.release()
1415 b.release()
1416 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001417 self.waitUntilSettled()
1418
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001419 self.worker.hold_jobs_in_build = False
1420 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07001421 self.waitUntilSettled()
1422
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001423 self.assertEqual(A.data['status'], 'NEW')
1424 self.assertEqual(B.data['status'], 'MERGED')
1425 self.assertEqual(C.data['status'], 'MERGED')
1426 self.assertEqual(D.data['status'], 'MERGED')
1427 self.assertEqual(E.data['status'], 'MERGED')
1428 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001429
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001430 self.assertEqual(A.reported, 2)
1431 self.assertEqual(B.reported, 2)
1432 self.assertEqual(C.reported, 2)
1433 self.assertEqual(D.reported, 2)
1434 self.assertEqual(E.reported, 2)
1435 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001436
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001437 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1438 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001439
1440 def test_merger_repack(self):
1441 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001442
James E. Blair05fed602012-09-07 12:45:24 -07001443 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1444 A.addApproval('CRVW', 2)
1445 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1446 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001447 self.assertEqual(self.getJobFromHistory('project-merge').result,
1448 'SUCCESS')
1449 self.assertEqual(self.getJobFromHistory('project-test1').result,
1450 'SUCCESS')
1451 self.assertEqual(self.getJobFromHistory('project-test2').result,
1452 'SUCCESS')
1453 self.assertEqual(A.data['status'], 'MERGED')
1454 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001455 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07001456 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001457
Monty Taylorbc758832013-06-17 17:22:42 -04001458 path = os.path.join(self.git_root, "org/project")
1459 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001460
1461 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1462 A.addApproval('CRVW', 2)
1463 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1464 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001465 self.assertEqual(self.getJobFromHistory('project-merge').result,
1466 'SUCCESS')
1467 self.assertEqual(self.getJobFromHistory('project-test1').result,
1468 'SUCCESS')
1469 self.assertEqual(self.getJobFromHistory('project-test2').result,
1470 'SUCCESS')
1471 self.assertEqual(A.data['status'], 'MERGED')
1472 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001473
James E. Blair4886f282012-11-15 09:27:33 -08001474 def test_merger_repack_large_change(self):
1475 "Test that the merger works with large changes after a repack"
1476 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001477 # This test assumes the repo is already cloned; make sure it is
Joshua Hesketh352264b2015-08-11 23:42:08 +10001478 url = self.fake_gerrit.getGitUrl(
James E. Blairac2c3242014-01-24 13:38:51 -08001479 self.sched.layout.projects['org/project1'])
James E. Blair4076e2b2014-01-28 12:42:20 -08001480 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08001481 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1482 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001483 path = os.path.join(self.upstream_root, "org/project1")
1484 print repack_repo(path)
1485 path = os.path.join(self.git_root, "org/project1")
1486 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001487
1488 A.addApproval('CRVW', 2)
1489 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1490 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001491 self.assertEqual(self.getJobFromHistory('project1-merge').result,
1492 'SUCCESS')
1493 self.assertEqual(self.getJobFromHistory('project1-test1').result,
1494 'SUCCESS')
1495 self.assertEqual(self.getJobFromHistory('project1-test2').result,
1496 'SUCCESS')
1497 self.assertEqual(A.data['status'], 'MERGED')
1498 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001499
James E. Blair7ee88a22012-09-12 18:59:31 +02001500 def test_nonexistent_job(self):
1501 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001502 # Set to the state immediately after a restart
1503 self.resetGearmanServer()
1504 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02001505
1506 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1507 A.addApproval('CRVW', 2)
1508 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1509 # There may be a thread about to report a lost change
1510 while A.reported < 2:
1511 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001512 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001513 self.assertFalse(job_names)
1514 self.assertEqual(A.data['status'], 'NEW')
1515 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001516 self.assertEmptyQueues()
1517
1518 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001519 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02001520 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1521 A.addApproval('CRVW', 2)
1522 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1523 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001524 self.assertEqual(self.getJobFromHistory('project-merge').result,
1525 'SUCCESS')
1526 self.assertEqual(self.getJobFromHistory('project-test1').result,
1527 'SUCCESS')
1528 self.assertEqual(self.getJobFromHistory('project-test2').result,
1529 'SUCCESS')
1530 self.assertEqual(A.data['status'], 'MERGED')
1531 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08001532
1533 def test_single_nonexistent_post_job(self):
1534 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08001535 e = {
1536 "type": "ref-updated",
1537 "submitter": {
1538 "name": "User Name",
1539 },
1540 "refUpdate": {
1541 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1542 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1543 "refName": "master",
1544 "project": "org/project",
1545 }
1546 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001547 # Set to the state immediately after a restart
1548 self.resetGearmanServer()
1549 self.launcher.negative_function_cache_ttl = 0
1550
James E. Blairf62d4282012-12-31 17:01:50 -08001551 self.fake_gerrit.addEvent(e)
1552 self.waitUntilSettled()
1553
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001554 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08001555
1556 def test_new_patchset_dequeues_old(self):
1557 "Test that a new patchset causes the old to be dequeued"
1558 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001559 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001560 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1561 M.setMerged()
1562
1563 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1564 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1565 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1566 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1567 A.addApproval('CRVW', 2)
1568 B.addApproval('CRVW', 2)
1569 C.addApproval('CRVW', 2)
1570 D.addApproval('CRVW', 2)
1571
1572 C.setDependsOn(B, 1)
1573 B.setDependsOn(A, 1)
1574 A.setDependsOn(M, 1)
1575
1576 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1577 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1578 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1579 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1580 self.waitUntilSettled()
1581
1582 B.addPatchset()
1583 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1584 self.waitUntilSettled()
1585
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001586 self.worker.hold_jobs_in_build = False
1587 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001588 self.waitUntilSettled()
1589
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001590 self.assertEqual(A.data['status'], 'MERGED')
1591 self.assertEqual(A.reported, 2)
1592 self.assertEqual(B.data['status'], 'NEW')
1593 self.assertEqual(B.reported, 2)
1594 self.assertEqual(C.data['status'], 'NEW')
1595 self.assertEqual(C.reported, 2)
1596 self.assertEqual(D.data['status'], 'MERGED')
1597 self.assertEqual(D.reported, 2)
1598 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001599
James E. Blairba437362015-02-07 11:41:52 -08001600 def test_new_patchset_check(self):
1601 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001602
1603 self.worker.hold_jobs_in_build = True
1604
1605 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001606 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1607 check_pipeline = self.sched.layout.pipelines['check']
1608
1609 # Add two git-dependent changes
1610 B.setDependsOn(A, 1)
1611 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1612 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001613 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1614 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001615
1616 # A live item, and a non-live/live pair
1617 items = check_pipeline.getAllItems()
1618 self.assertEqual(len(items), 3)
1619
1620 self.assertEqual(items[0].change.number, '1')
1621 self.assertEqual(items[0].change.patchset, '1')
1622 self.assertFalse(items[0].live)
1623
1624 self.assertEqual(items[1].change.number, '2')
1625 self.assertEqual(items[1].change.patchset, '1')
1626 self.assertTrue(items[1].live)
1627
1628 self.assertEqual(items[2].change.number, '1')
1629 self.assertEqual(items[2].change.patchset, '1')
1630 self.assertTrue(items[2].live)
1631
1632 # Add a new patchset to A
1633 A.addPatchset()
1634 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1635 self.waitUntilSettled()
1636
1637 # The live copy of A,1 should be gone, but the non-live and B
1638 # should continue, and we should have a new A,2
1639 items = check_pipeline.getAllItems()
1640 self.assertEqual(len(items), 3)
1641
1642 self.assertEqual(items[0].change.number, '1')
1643 self.assertEqual(items[0].change.patchset, '1')
1644 self.assertFalse(items[0].live)
1645
1646 self.assertEqual(items[1].change.number, '2')
1647 self.assertEqual(items[1].change.patchset, '1')
1648 self.assertTrue(items[1].live)
1649
1650 self.assertEqual(items[2].change.number, '1')
1651 self.assertEqual(items[2].change.patchset, '2')
1652 self.assertTrue(items[2].live)
1653
1654 # Add a new patchset to B
1655 B.addPatchset()
1656 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1657 self.waitUntilSettled()
1658
1659 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1660 # but we should have a new B,2 (still based on A,1)
1661 items = check_pipeline.getAllItems()
1662 self.assertEqual(len(items), 3)
1663
1664 self.assertEqual(items[0].change.number, '1')
1665 self.assertEqual(items[0].change.patchset, '2')
1666 self.assertTrue(items[0].live)
1667
1668 self.assertEqual(items[1].change.number, '1')
1669 self.assertEqual(items[1].change.patchset, '1')
1670 self.assertFalse(items[1].live)
1671
1672 self.assertEqual(items[2].change.number, '2')
1673 self.assertEqual(items[2].change.patchset, '2')
1674 self.assertTrue(items[2].live)
1675
1676 self.builds[0].release()
1677 self.waitUntilSettled()
1678 self.builds[0].release()
1679 self.waitUntilSettled()
1680 self.worker.hold_jobs_in_build = False
1681 self.worker.release()
1682 self.waitUntilSettled()
1683
1684 self.assertEqual(A.reported, 1)
1685 self.assertEqual(B.reported, 1)
1686 self.assertEqual(self.history[0].result, 'ABORTED')
1687 self.assertEqual(self.history[0].changes, '1,1')
1688 self.assertEqual(self.history[1].result, 'ABORTED')
1689 self.assertEqual(self.history[1].changes, '1,1 2,1')
1690 self.assertEqual(self.history[2].result, 'SUCCESS')
1691 self.assertEqual(self.history[2].changes, '1,2')
1692 self.assertEqual(self.history[3].result, 'SUCCESS')
1693 self.assertEqual(self.history[3].changes, '1,1 2,2')
1694
1695 def test_abandoned_gate(self):
1696 "Test that an abandoned change is dequeued from gate"
1697
1698 self.worker.hold_jobs_in_build = True
1699
1700 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1701 A.addApproval('CRVW', 2)
1702 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1703 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001704 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1705 self.assertEqual(self.builds[0].name, 'project-merge')
1706
1707 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1708 self.waitUntilSettled()
1709
Antoine Mussobd86a312014-01-08 14:51:33 +01001710 self.worker.release('.*-merge')
1711 self.waitUntilSettled()
1712
1713 self.assertEqual(len(self.builds), 0, "No job running")
Antoine Mussobd86a312014-01-08 14:51:33 +01001714 self.assertEqual(len(self.history), 1, "Only one build in history")
1715 self.assertEqual(self.history[0].result, 'ABORTED',
James E. Blairba437362015-02-07 11:41:52 -08001716 "Build should have been aborted")
1717 self.assertEqual(A.reported, 1,
1718 "Abandoned gate change should report only start")
1719
1720 def test_abandoned_check(self):
1721 "Test that an abandoned change is dequeued from check"
1722
1723 self.worker.hold_jobs_in_build = True
1724
1725 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1726 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1727 check_pipeline = self.sched.layout.pipelines['check']
1728
1729 # Add two git-dependent changes
1730 B.setDependsOn(A, 1)
1731 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1732 self.waitUntilSettled()
1733 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1734 self.waitUntilSettled()
1735 # A live item, and a non-live/live pair
1736 items = check_pipeline.getAllItems()
1737 self.assertEqual(len(items), 3)
1738
1739 self.assertEqual(items[0].change.number, '1')
1740 self.assertFalse(items[0].live)
1741
1742 self.assertEqual(items[1].change.number, '2')
1743 self.assertTrue(items[1].live)
1744
1745 self.assertEqual(items[2].change.number, '1')
1746 self.assertTrue(items[2].live)
1747
1748 # Abandon A
1749 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1750 self.waitUntilSettled()
1751
1752 # The live copy of A should be gone, but the non-live and B
1753 # should continue
1754 items = check_pipeline.getAllItems()
1755 self.assertEqual(len(items), 2)
1756
1757 self.assertEqual(items[0].change.number, '1')
1758 self.assertFalse(items[0].live)
1759
1760 self.assertEqual(items[1].change.number, '2')
1761 self.assertTrue(items[1].live)
1762
1763 self.worker.hold_jobs_in_build = False
1764 self.worker.release()
1765 self.waitUntilSettled()
1766
1767 self.assertEqual(len(self.history), 4)
1768 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001769 'Build should have been aborted')
1770 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001771 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001772
Steve Varnau7b78b312015-04-03 14:49:46 -07001773 def test_abandoned_not_timer(self):
1774 "Test that an abandoned change does not cancel timer jobs"
1775
1776 self.worker.hold_jobs_in_build = True
1777
1778 # Start timer trigger - also org/project
James E. Blairf84026c2015-12-08 16:11:46 -08001779 self.updateConfigLayout(
1780 'tests/fixtures/layout-idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001781 self.sched.reconfigure(self.config)
1782 self.registerJobs()
1783 # The pipeline triggers every second, so we should have seen
1784 # several by now.
1785 time.sleep(5)
1786 self.waitUntilSettled()
1787 # Stop queuing timer triggered jobs so that the assertions
1788 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08001789 self.updateConfigLayout(
1790 'tests/fixtures/layout-no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001791 self.sched.reconfigure(self.config)
1792 self.registerJobs()
1793 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1794
1795 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1796 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1797 self.waitUntilSettled()
1798 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1799
1800 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1801 self.waitUntilSettled()
1802
1803 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1804
1805 self.worker.release()
1806 self.waitUntilSettled()
1807
Arx Cruzb1b010d2013-10-28 19:49:59 -02001808 def test_zuul_url_return(self):
1809 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001810 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001811 self.worker.hold_jobs_in_build = True
1812
1813 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1814 A.addApproval('CRVW', 2)
1815 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1816 self.waitUntilSettled()
1817
1818 self.assertEqual(len(self.builds), 1)
1819 for build in self.builds:
1820 self.assertTrue('ZUUL_URL' in build.parameters)
1821
1822 self.worker.hold_jobs_in_build = False
1823 self.worker.release()
1824 self.waitUntilSettled()
1825
James E. Blair2fa50962013-01-30 21:50:41 -08001826 def test_new_patchset_dequeues_old_on_head(self):
1827 "Test that a new patchset causes the old to be dequeued (at head)"
1828 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001829 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001830 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1831 M.setMerged()
1832 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1833 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1834 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1835 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1836 A.addApproval('CRVW', 2)
1837 B.addApproval('CRVW', 2)
1838 C.addApproval('CRVW', 2)
1839 D.addApproval('CRVW', 2)
1840
1841 C.setDependsOn(B, 1)
1842 B.setDependsOn(A, 1)
1843 A.setDependsOn(M, 1)
1844
1845 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1846 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1847 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1848 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1849 self.waitUntilSettled()
1850
1851 A.addPatchset()
1852 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1853 self.waitUntilSettled()
1854
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001855 self.worker.hold_jobs_in_build = False
1856 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001857 self.waitUntilSettled()
1858
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001859 self.assertEqual(A.data['status'], 'NEW')
1860 self.assertEqual(A.reported, 2)
1861 self.assertEqual(B.data['status'], 'NEW')
1862 self.assertEqual(B.reported, 2)
1863 self.assertEqual(C.data['status'], 'NEW')
1864 self.assertEqual(C.reported, 2)
1865 self.assertEqual(D.data['status'], 'MERGED')
1866 self.assertEqual(D.reported, 2)
1867 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001868
1869 def test_new_patchset_dequeues_old_without_dependents(self):
1870 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001871 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001872 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1873 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1874 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1875 A.addApproval('CRVW', 2)
1876 B.addApproval('CRVW', 2)
1877 C.addApproval('CRVW', 2)
1878
1879 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1880 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1881 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1882 self.waitUntilSettled()
1883
1884 B.addPatchset()
1885 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1886 self.waitUntilSettled()
1887
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001888 self.worker.hold_jobs_in_build = False
1889 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001890 self.waitUntilSettled()
1891
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001892 self.assertEqual(A.data['status'], 'MERGED')
1893 self.assertEqual(A.reported, 2)
1894 self.assertEqual(B.data['status'], 'NEW')
1895 self.assertEqual(B.reported, 2)
1896 self.assertEqual(C.data['status'], 'MERGED')
1897 self.assertEqual(C.reported, 2)
1898 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001899
1900 def test_new_patchset_dequeues_old_independent_queue(self):
1901 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001902 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001903 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1904 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1905 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1906 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1907 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1908 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1909 self.waitUntilSettled()
1910
1911 B.addPatchset()
1912 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1913 self.waitUntilSettled()
1914
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001915 self.worker.hold_jobs_in_build = False
1916 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001917 self.waitUntilSettled()
1918
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001919 self.assertEqual(A.data['status'], 'NEW')
1920 self.assertEqual(A.reported, 1)
1921 self.assertEqual(B.data['status'], 'NEW')
1922 self.assertEqual(B.reported, 1)
1923 self.assertEqual(C.data['status'], 'NEW')
1924 self.assertEqual(C.reported, 1)
1925 self.assertEqual(len(self.history), 10)
1926 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001927
James E. Blair18c64442014-03-18 10:14:45 -07001928 def test_noop_job(self):
1929 "Test that the internal noop job works"
1930 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
1931 A.addApproval('CRVW', 2)
1932 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1933 self.waitUntilSettled()
1934
1935 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1936 self.assertTrue(self.sched._areAllBuildsComplete())
1937 self.assertEqual(len(self.history), 0)
1938 self.assertEqual(A.data['status'], 'MERGED')
1939 self.assertEqual(A.reported, 2)
1940
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001941 def test_no_job_project(self):
1942 "Test that reports with no jobs don't get sent"
1943 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1944 'master', 'A')
1945 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1946 self.waitUntilSettled()
1947
1948 # Change wasn't reported to
1949 self.assertEqual(A.reported, False)
1950
1951 # Check queue is empty afterwards
1952 check_pipeline = self.sched.layout.pipelines['check']
1953 items = check_pipeline.getAllItems()
1954 self.assertEqual(len(items), 0)
1955
1956 self.assertEqual(len(self.history), 0)
1957
James E. Blair7d0dedc2013-02-21 17:26:09 -08001958 def test_zuul_refs(self):
1959 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001960 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001961 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1962 M1.setMerged()
1963 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1964 M2.setMerged()
1965
1966 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1967 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1968 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1969 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1970 A.addApproval('CRVW', 2)
1971 B.addApproval('CRVW', 2)
1972 C.addApproval('CRVW', 2)
1973 D.addApproval('CRVW', 2)
1974 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1975 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1976 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1977 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1978
1979 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()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001982 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001983 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001984 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001985 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001986 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001987 self.waitUntilSettled()
1988
James E. Blair7d0dedc2013-02-21 17:26:09 -08001989 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001990 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001991 if x.parameters['ZUUL_CHANGE'] == '3':
1992 a_zref = x.parameters['ZUUL_REF']
1993 if x.parameters['ZUUL_CHANGE'] == '4':
1994 b_zref = x.parameters['ZUUL_REF']
1995 if x.parameters['ZUUL_CHANGE'] == '5':
1996 c_zref = x.parameters['ZUUL_REF']
1997 if x.parameters['ZUUL_CHANGE'] == '6':
1998 d_zref = x.parameters['ZUUL_REF']
1999
2000 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002001 self.assertIsNotNone(a_zref)
2002 self.assertIsNotNone(b_zref)
2003 self.assertIsNotNone(c_zref)
2004 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002005
2006 # And they should all be different
2007 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002008 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002009
2010 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002011 self.assertTrue(self.ref_has_change(a_zref, A))
2012 self.assertFalse(self.ref_has_change(a_zref, B))
2013 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002014
2015 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002016 self.assertTrue(self.ref_has_change(b_zref, A))
2017 self.assertTrue(self.ref_has_change(b_zref, B))
2018 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002019
2020 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002021 self.assertTrue(self.ref_has_change(c_zref, A))
2022 self.assertTrue(self.ref_has_change(c_zref, B))
2023 self.assertTrue(self.ref_has_change(c_zref, C))
2024 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002025
2026 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002027 self.assertTrue(self.ref_has_change(d_zref, A))
2028 self.assertTrue(self.ref_has_change(d_zref, B))
2029 self.assertTrue(self.ref_has_change(d_zref, C))
2030 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002031
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002032 self.worker.hold_jobs_in_build = False
2033 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002034 self.waitUntilSettled()
2035
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002036 self.assertEqual(A.data['status'], 'MERGED')
2037 self.assertEqual(A.reported, 2)
2038 self.assertEqual(B.data['status'], 'MERGED')
2039 self.assertEqual(B.reported, 2)
2040 self.assertEqual(C.data['status'], 'MERGED')
2041 self.assertEqual(C.reported, 2)
2042 self.assertEqual(D.data['status'], 'MERGED')
2043 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002044
James E. Blair4a28a882013-08-23 15:17:33 -07002045 def test_rerun_on_error(self):
2046 "Test that if a worker fails to run a job, it is run again"
2047 self.worker.hold_jobs_in_build = True
2048 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2049 A.addApproval('CRVW', 2)
2050 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2051 self.waitUntilSettled()
2052
2053 self.builds[0].run_error = True
2054 self.worker.hold_jobs_in_build = False
2055 self.worker.release()
2056 self.waitUntilSettled()
2057 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2058 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2059
James E. Blair412e5582013-04-22 15:50:12 -07002060 def test_statsd(self):
2061 "Test each of the statsd methods used in the scheduler"
2062 import extras
2063 statsd = extras.try_import('statsd.statsd')
2064 statsd.incr('test-incr')
2065 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002066 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002067 self.assertReportedStat('test-incr', '1|c')
2068 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002069 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002070
James E. Blairdad52252014-02-07 16:59:17 -08002071 def test_stuck_job_cleanup(self):
2072 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07002073 # This job won't be registered at startup because it is not in
2074 # the standard layout, but we need it to already be registerd
2075 # for when we reconfigure, as that is when Zuul will attempt
2076 # to run the new job.
2077 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002078 self.gearman_server.hold_jobs_in_queue = True
2079 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2080 A.addApproval('CRVW', 2)
2081 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2082 self.waitUntilSettled()
2083 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2084
James E. Blairf84026c2015-12-08 16:11:46 -08002085 self.updateConfigLayout(
2086 'tests/fixtures/layout-no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002087 self.sched.reconfigure(self.config)
2088 self.waitUntilSettled()
2089
James E. Blair18c64442014-03-18 10:14:45 -07002090 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002091 self.waitUntilSettled()
2092 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2093 self.assertTrue(self.sched._areAllBuildsComplete())
2094
2095 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002096 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002097 self.assertEqual(self.history[0].result, 'SUCCESS')
2098
James E. Blair879dafb2015-07-17 14:04:49 -07002099 def test_file_head(self):
2100 # This is a regression test for an observed bug. A change
2101 # with a file named "HEAD" in the root directory of the repo
2102 # was processed by a merger. It then was unable to reset the
2103 # repo because of:
2104 # GitCommandError: 'git reset --hard HEAD' returned
2105 # with exit code 128
2106 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2107 # and filename
2108 # Use '--' to separate filenames from revisions'
2109
2110 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2111 A.addPatchset(['HEAD'])
2112 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2113
2114 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2115 self.waitUntilSettled()
2116
2117 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2118 self.waitUntilSettled()
2119
2120 self.assertIn('Build succeeded', A.messages[0])
2121 self.assertIn('Build succeeded', B.messages[0])
2122
James E. Blair70c71582013-03-06 08:50:50 -08002123 def test_file_jobs(self):
2124 "Test that file jobs run only when appropriate"
2125 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2126 A.addPatchset(['pip-requires'])
2127 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2128 A.addApproval('CRVW', 2)
2129 B.addApproval('CRVW', 2)
2130 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2131 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2132 self.waitUntilSettled()
2133
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002134 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002135 if x.name == 'project-testfile']
2136
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002137 self.assertEqual(len(testfile_jobs), 1)
2138 self.assertEqual(testfile_jobs[0].changes, '1,2')
2139 self.assertEqual(A.data['status'], 'MERGED')
2140 self.assertEqual(A.reported, 2)
2141 self.assertEqual(B.data['status'], 'MERGED')
2142 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002143
Maru Newby3fe5f852015-01-13 04:22:14 +00002144 def _test_skip_if_jobs(self, branch, should_skip):
2145 "Test that jobs with a skip-if filter run only when appropriate"
James E. Blairf84026c2015-12-08 16:11:46 -08002146 self.updateConfigLayout(
2147 'tests/fixtures/layout-skip-if.yaml')
Maru Newby3fe5f852015-01-13 04:22:14 +00002148 self.sched.reconfigure(self.config)
2149 self.registerJobs()
2150
2151 change = self.fake_gerrit.addFakeChange('org/project',
2152 branch,
2153 'test skip-if')
2154 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2155 self.waitUntilSettled()
2156
2157 tested_change_ids = [x.changes[0] for x in self.history
2158 if x.name == 'project-test-skip-if']
2159
2160 if should_skip:
2161 self.assertEqual([], tested_change_ids)
2162 else:
2163 self.assertIn(change.data['number'], tested_change_ids)
2164
2165 def test_skip_if_match_skips_job(self):
2166 self._test_skip_if_jobs(branch='master', should_skip=True)
2167
2168 def test_skip_if_no_match_runs_job(self):
2169 self._test_skip_if_jobs(branch='mp', should_skip=False)
2170
James E. Blair3c5e5b52013-04-26 11:17:03 -07002171 def test_test_config(self):
2172 "Test that we can test the config"
James E. Blairf84026c2015-12-08 16:11:46 -08002173 self.sched.testConfig(self.config.get('zuul', 'tenant_config'),
Joshua Hesketh352264b2015-08-11 23:42:08 +10002174 self.connections)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002175
2176 def test_build_description(self):
2177 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002178 self.worker.registerFunction('set_description:' +
2179 self.worker.worker_id)
2180
2181 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2182 A.addApproval('CRVW', 2)
2183 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2184 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002185 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002186 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002187 self.assertTrue(re.search("Branch.*master", desc))
2188 self.assertTrue(re.search("Pipeline.*gate", desc))
2189 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2190 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2191 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2192 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002193
James E. Blairc8a1e052014-02-25 09:29:26 -08002194 def test_queue_names(self):
2195 "Test shared change queue names"
2196 project1 = self.sched.layout.projects['org/project1']
2197 project2 = self.sched.layout.projects['org/project2']
2198 q1 = self.sched.layout.pipelines['gate'].getQueue(project1)
2199 q2 = self.sched.layout.pipelines['gate'].getQueue(project2)
2200 self.assertEqual(q1.name, 'integration')
2201 self.assertEqual(q2.name, 'integration')
2202
James E. Blairf84026c2015-12-08 16:11:46 -08002203 self.updateConfigLayout(
2204 'tests/fixtures/layout-bad-queue.yaml')
James E. Blairc8a1e052014-02-25 09:29:26 -08002205 with testtools.ExpectedException(
2206 Exception, "More than one name assigned to change queue"):
2207 self.sched.reconfigure(self.config)
2208
James E. Blair64ed6f22013-07-10 14:07:23 -07002209 def test_queue_precedence(self):
2210 "Test that queue precedence works"
2211
2212 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002213 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002214 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2215 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2216 A.addApproval('CRVW', 2)
2217 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2218
2219 self.waitUntilSettled()
2220 self.gearman_server.hold_jobs_in_queue = False
2221 self.gearman_server.release()
2222 self.waitUntilSettled()
2223
James E. Blair8de58bd2013-07-18 16:23:33 -07002224 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002225 self.orderedRelease()
James E. Blair8de58bd2013-07-18 16:23:33 -07002226 self.worker.hold_jobs_in_build = False
2227 self.waitUntilSettled()
2228
James E. Blair64ed6f22013-07-10 14:07:23 -07002229 self.log.debug(self.history)
2230 self.assertEqual(self.history[0].pipeline, 'gate')
2231 self.assertEqual(self.history[1].pipeline, 'check')
2232 self.assertEqual(self.history[2].pipeline, 'gate')
2233 self.assertEqual(self.history[3].pipeline, 'gate')
2234 self.assertEqual(self.history[4].pipeline, 'check')
2235 self.assertEqual(self.history[5].pipeline, 'check')
2236
Clark Boylana5edbe42014-06-03 16:39:10 -07002237 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002238 "Test that we can retrieve JSON status info"
2239 self.worker.hold_jobs_in_build = True
2240 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2241 A.addApproval('CRVW', 2)
2242 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2243 self.waitUntilSettled()
2244
2245 port = self.webapp.server.socket.getsockname()[1]
2246
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04002247 req = urllib2.Request("http://localhost:%s/status.json" % port)
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04002248 f = urllib2.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002249 headers = f.info()
2250 self.assertIn('Content-Length', headers)
2251 self.assertIn('Content-Type', headers)
2252 self.assertEqual(headers['Content-Type'],
2253 'application/json; charset=UTF-8')
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002254 self.assertIn('Access-Control-Allow-Origin', headers)
2255 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002256 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002257 self.assertIn('Expires', headers)
James E. Blair1843a552013-07-03 14:19:52 -07002258 data = f.read()
2259
2260 self.worker.hold_jobs_in_build = False
2261 self.worker.release()
2262 self.waitUntilSettled()
2263
2264 data = json.loads(data)
2265 status_jobs = set()
2266 for p in data['pipelines']:
2267 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002268 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002269 self.assertEqual(q['window'], 20)
2270 else:
2271 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002272 for head in q['heads']:
2273 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002274 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002275 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002276 for job in change['jobs']:
2277 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002278 self.assertIn('project-merge', status_jobs)
2279 self.assertIn('project-test1', status_jobs)
2280 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002281
James E. Blairc3d428e2013-12-03 15:06:48 -08002282 def test_merging_queues(self):
2283 "Test that transitively-connected change queues are merged"
James E. Blairf84026c2015-12-08 16:11:46 -08002284 self.updateConfigLayout(
2285 'tests/fixtures/layout-merge-queues.yaml')
James E. Blairc3d428e2013-12-03 15:06:48 -08002286 self.sched.reconfigure(self.config)
2287 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
2288
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002289 def test_node_label(self):
2290 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002291 self.worker.registerFunction('build:node-project-test1:debian')
2292
2293 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2294 A.addApproval('CRVW', 2)
2295 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2296 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002297
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002298 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2299 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2300 'debian')
2301 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002302
2303 def test_live_reconfiguration(self):
2304 "Test that live reconfiguration works"
2305 self.worker.hold_jobs_in_build = True
2306 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2307 A.addApproval('CRVW', 2)
2308 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2309 self.waitUntilSettled()
2310
2311 self.sched.reconfigure(self.config)
2312
2313 self.worker.hold_jobs_in_build = False
2314 self.worker.release()
2315 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002316 self.assertEqual(self.getJobFromHistory('project-merge').result,
2317 'SUCCESS')
2318 self.assertEqual(self.getJobFromHistory('project-test1').result,
2319 'SUCCESS')
2320 self.assertEqual(self.getJobFromHistory('project-test2').result,
2321 'SUCCESS')
2322 self.assertEqual(A.data['status'], 'MERGED')
2323 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002324
James E. Blair6bc782d2015-07-17 16:20:21 -07002325 def test_live_reconfiguration_merge_conflict(self):
2326 # A real-world bug: a change in a gate queue has a merge
2327 # conflict and a job is added to its project while it's
2328 # sitting in the queue. The job gets added to the change and
2329 # enqueued and the change gets stuck.
2330 self.worker.registerFunction('build:project-test3')
2331 self.worker.hold_jobs_in_build = True
2332
2333 # This change is fine. It's here to stop the queue long
2334 # enough for the next change to be subject to the
2335 # reconfiguration, as well as to provide a conflict for the
2336 # next change. This change will succeed and merge.
2337 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2338 A.addPatchset(['conflict'])
2339 A.addApproval('CRVW', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002340
2341 # This change will be in merge conflict. During the
2342 # reconfiguration, we will add a job. We want to make sure
2343 # that doesn't cause it to get stuck.
2344 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2345 B.addPatchset(['conflict'])
2346 B.addApproval('CRVW', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002347
2348 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002349 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2350
2351 self.waitUntilSettled()
2352
2353 # No jobs have run yet
2354 self.assertEqual(A.data['status'], 'NEW')
2355 self.assertEqual(A.reported, 1)
2356 self.assertEqual(B.data['status'], 'NEW')
2357 self.assertEqual(B.reported, 1)
2358 self.assertEqual(len(self.history), 0)
2359
2360 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002361 self.updateConfigLayout(
2362 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002363 self.sched.reconfigure(self.config)
2364 self.waitUntilSettled()
2365
2366 self.worker.hold_jobs_in_build = False
2367 self.worker.release()
2368 self.waitUntilSettled()
2369
2370 self.assertEqual(A.data['status'], 'MERGED')
2371 self.assertEqual(A.reported, 2)
2372 self.assertEqual(B.data['status'], 'NEW')
2373 self.assertEqual(B.reported, 2)
2374 self.assertEqual(self.getJobFromHistory('project-merge').result,
2375 'SUCCESS')
2376 self.assertEqual(self.getJobFromHistory('project-test1').result,
2377 'SUCCESS')
2378 self.assertEqual(self.getJobFromHistory('project-test2').result,
2379 'SUCCESS')
2380 self.assertEqual(self.getJobFromHistory('project-test3').result,
2381 'SUCCESS')
2382 self.assertEqual(len(self.history), 4)
2383
James E. Blair400e8fd2015-07-30 17:44:45 -07002384 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002385 # An extrapolation of test_live_reconfiguration_merge_conflict
2386 # that tests a job added to a job tree with a failed root does
2387 # not run.
2388 self.worker.registerFunction('build:project-test3')
2389 self.worker.hold_jobs_in_build = True
2390
2391 # This change is fine. It's here to stop the queue long
2392 # enough for the next change to be subject to the
2393 # reconfiguration. This change will succeed and merge.
2394 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2395 A.addPatchset(['conflict'])
2396 A.addApproval('CRVW', 2)
2397 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2398 self.waitUntilSettled()
2399 self.worker.release('.*-merge')
2400 self.waitUntilSettled()
2401
2402 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2403 self.worker.addFailTest('project-merge', B)
2404 B.addApproval('CRVW', 2)
2405 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2406 self.waitUntilSettled()
2407
2408 self.worker.release('.*-merge')
2409 self.waitUntilSettled()
2410
2411 # Both -merge jobs have run, but no others.
2412 self.assertEqual(A.data['status'], 'NEW')
2413 self.assertEqual(A.reported, 1)
2414 self.assertEqual(B.data['status'], 'NEW')
2415 self.assertEqual(B.reported, 1)
2416 self.assertEqual(self.history[0].result, 'SUCCESS')
2417 self.assertEqual(self.history[0].name, 'project-merge')
2418 self.assertEqual(self.history[1].result, 'FAILURE')
2419 self.assertEqual(self.history[1].name, 'project-merge')
2420 self.assertEqual(len(self.history), 2)
2421
2422 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002423 self.updateConfigLayout(
2424 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002425 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.
James E. Blairf84026c2015-12-08 16:11:46 -08002476 self.updateConfigLayout(
2477 'tests/fixtures/layout-live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002478 self.sched.reconfigure(self.config)
2479 self.waitUntilSettled()
2480
2481 self.worker.hold_jobs_in_build = False
2482 self.worker.release()
2483 self.waitUntilSettled()
2484
2485 self.assertEqual(self.getJobFromHistory('project-test2').result,
2486 'SUCCESS')
2487 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2488 'SUCCESS')
2489 self.assertEqual(len(self.history), 4)
2490
2491 self.assertEqual(A.data['status'], 'NEW')
2492 self.assertEqual(A.reported, 1)
2493 self.assertIn('Build succeeded', A.messages[0])
2494 # Ensure the removed job was not included in the report.
2495 self.assertNotIn('project-test1', A.messages[0])
2496
James E. Blairfe707d12015-08-05 15:18:15 -07002497 def test_live_reconfiguration_shared_queue(self):
2498 # Test that a change with a failing job which was removed from
2499 # this project but otherwise still exists in the system does
2500 # not disrupt reconfiguration.
2501
2502 self.worker.hold_jobs_in_build = True
2503
2504 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2505
2506 self.worker.addFailTest('project1-project2-integration', A)
2507
2508 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2509 self.waitUntilSettled()
2510 self.worker.release('.*-merge')
2511 self.waitUntilSettled()
2512 self.worker.release('project1-project2-integration')
2513 self.waitUntilSettled()
2514
2515 self.assertEqual(A.data['status'], 'NEW')
2516 self.assertEqual(A.reported, 0)
2517
2518 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2519 'SUCCESS')
2520 self.assertEqual(self.getJobFromHistory(
2521 'project1-project2-integration').result, 'FAILURE')
2522 self.assertEqual(len(self.history), 2)
2523
2524 # Remove the integration job.
James E. Blairf84026c2015-12-08 16:11:46 -08002525 self.updateConfigLayout(
2526 'tests/fixtures/layout-live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002527 self.sched.reconfigure(self.config)
2528 self.waitUntilSettled()
2529
2530 self.worker.hold_jobs_in_build = False
2531 self.worker.release()
2532 self.waitUntilSettled()
2533
2534 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2535 'SUCCESS')
2536 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2537 'SUCCESS')
2538 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2539 'SUCCESS')
2540 self.assertEqual(self.getJobFromHistory(
2541 'project1-project2-integration').result, 'FAILURE')
2542 self.assertEqual(len(self.history), 4)
2543
2544 self.assertEqual(A.data['status'], 'NEW')
2545 self.assertEqual(A.reported, 1)
2546 self.assertIn('Build succeeded', A.messages[0])
2547 # Ensure the removed job was not included in the report.
2548 self.assertNotIn('project1-project2-integration', A.messages[0])
2549
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002550 def test_live_reconfiguration_del_project(self):
2551 # Test project deletion from layout
2552 # while changes are enqueued
2553
2554 self.worker.hold_jobs_in_build = True
2555 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2556 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2557 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2558
2559 # A Depends-On: B
2560 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2561 A.subject, B.data['id'])
2562 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2563
2564 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2565 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2566 self.waitUntilSettled()
2567 self.worker.release('.*-merge')
2568 self.waitUntilSettled()
2569 self.assertEqual(len(self.builds), 5)
2570
2571 # This layout defines only org/project, not org/project1
James E. Blairf84026c2015-12-08 16:11:46 -08002572 self.updateConfigLayout(
2573 'tests/fixtures/layout-live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002574 self.sched.reconfigure(self.config)
2575 self.waitUntilSettled()
2576
2577 # Builds for C aborted, builds for A succeed,
2578 # and have change B applied ahead
2579 job_c = self.getJobFromHistory('project1-test1')
2580 self.assertEqual(job_c.changes, '3,1')
2581 self.assertEqual(job_c.result, 'ABORTED')
2582
2583 self.worker.hold_jobs_in_build = False
2584 self.worker.release()
2585 self.waitUntilSettled()
2586
2587 self.assertEqual(self.getJobFromHistory('project-test1').changes,
2588 '2,1 1,1')
2589
2590 self.assertEqual(A.data['status'], 'NEW')
2591 self.assertEqual(B.data['status'], 'NEW')
2592 self.assertEqual(C.data['status'], 'NEW')
2593 self.assertEqual(A.reported, 1)
2594 self.assertEqual(B.reported, 0)
2595 self.assertEqual(C.reported, 0)
2596
2597 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
2598 self.assertIn('Build succeeded', A.messages[0])
2599
James E. Blaire712d9f2013-07-31 11:40:11 -07002600 def test_live_reconfiguration_functions(self):
2601 "Test live reconfiguration with a custom function"
2602 self.worker.registerFunction('build:node-project-test1:debian')
2603 self.worker.registerFunction('build:node-project-test1:wheezy')
2604 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2605 A.addApproval('CRVW', 2)
2606 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2607 self.waitUntilSettled()
2608
2609 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2610 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2611 'debian')
2612 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2613
James E. Blairf84026c2015-12-08 16:11:46 -08002614 self.updateConfigLayout(
2615 'tests/fixtures/layout-live-reconfiguration-functions.yaml')
James E. Blaire712d9f2013-07-31 11:40:11 -07002616 self.sched.reconfigure(self.config)
2617 self.worker.build_history = []
2618
2619 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
2620 B.addApproval('CRVW', 2)
2621 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2622 self.waitUntilSettled()
2623
2624 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2625 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2626 'wheezy')
2627 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2628
James E. Blair287c06d2013-07-24 10:39:30 -07002629 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002630 self.updateConfigLayout(
2631 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002632 self.sched.reconfigure(self.config)
2633
2634 self.init_repo("org/new-project")
2635 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2636
2637 A.addApproval('CRVW', 2)
2638 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2639 self.waitUntilSettled()
2640 self.assertEqual(self.getJobFromHistory('project-merge').result,
2641 'SUCCESS')
2642 self.assertEqual(self.getJobFromHistory('project-test1').result,
2643 'SUCCESS')
2644 self.assertEqual(self.getJobFromHistory('project-test2').result,
2645 'SUCCESS')
2646 self.assertEqual(A.data['status'], 'MERGED')
2647 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002648
Clark Boylan6dbbc482013-10-18 10:57:31 -07002649 def test_repo_deleted(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002650 self.updateConfigLayout(
2651 'tests/fixtures/layout-repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002652 self.sched.reconfigure(self.config)
2653
2654 self.init_repo("org/delete-project")
2655 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2656
2657 A.addApproval('CRVW', 2)
2658 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2659 self.waitUntilSettled()
2660 self.assertEqual(self.getJobFromHistory('project-merge').result,
2661 'SUCCESS')
2662 self.assertEqual(self.getJobFromHistory('project-test1').result,
2663 'SUCCESS')
2664 self.assertEqual(self.getJobFromHistory('project-test2').result,
2665 'SUCCESS')
2666 self.assertEqual(A.data['status'], 'MERGED')
2667 self.assertEqual(A.reported, 2)
2668
2669 # Delete org/new-project zuul repo. Should be recloned.
2670 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
2671
2672 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2673
2674 B.addApproval('CRVW', 2)
2675 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2676 self.waitUntilSettled()
2677 self.assertEqual(self.getJobFromHistory('project-merge').result,
2678 'SUCCESS')
2679 self.assertEqual(self.getJobFromHistory('project-test1').result,
2680 'SUCCESS')
2681 self.assertEqual(self.getJobFromHistory('project-test2').result,
2682 'SUCCESS')
2683 self.assertEqual(B.data['status'], 'MERGED')
2684 self.assertEqual(B.reported, 2)
2685
James E. Blair63bb0ef2013-07-29 17:14:51 -07002686 def test_timer(self):
2687 "Test that a periodic job is triggered"
2688 self.worker.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08002689 self.updateConfigLayout(
2690 'tests/fixtures/layout-timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002691 self.sched.reconfigure(self.config)
2692 self.registerJobs()
2693
Clark Boylan3ee090a2014-04-03 20:55:09 -07002694 # The pipeline triggers every second, so we should have seen
2695 # several by now.
2696 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002697 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002698
2699 self.assertEqual(len(self.builds), 2)
2700
James E. Blair63bb0ef2013-07-29 17:14:51 -07002701 port = self.webapp.server.socket.getsockname()[1]
2702
2703 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2704 data = f.read()
2705
2706 self.worker.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002707 # Stop queuing timer triggered jobs so that the assertions
2708 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002709 self.updateConfigLayout(
2710 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002711 self.sched.reconfigure(self.config)
2712 self.registerJobs()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002713 self.worker.release()
2714 self.waitUntilSettled()
2715
2716 self.assertEqual(self.getJobFromHistory(
2717 'project-bitrot-stable-old').result, 'SUCCESS')
2718 self.assertEqual(self.getJobFromHistory(
2719 'project-bitrot-stable-older').result, 'SUCCESS')
2720
2721 data = json.loads(data)
2722 status_jobs = set()
2723 for p in data['pipelines']:
2724 for q in p['change_queues']:
2725 for head in q['heads']:
2726 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07002727 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002728 for job in change['jobs']:
2729 status_jobs.add(job['name'])
2730 self.assertIn('project-bitrot-stable-old', status_jobs)
2731 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002732
James E. Blair4f6033c2014-03-27 15:49:09 -07002733 def test_idle(self):
2734 "Test that frequent periodic jobs work"
2735 self.worker.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002736
Clark Boylan3ee090a2014-04-03 20:55:09 -07002737 for x in range(1, 3):
2738 # Test that timer triggers periodic jobs even across
2739 # layout config reloads.
2740 # Start timer trigger
James E. Blairf84026c2015-12-08 16:11:46 -08002741 self.updateConfigLayout(
2742 'tests/fixtures/layout-idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002743 self.sched.reconfigure(self.config)
2744 self.registerJobs()
James E. Blair4f6033c2014-03-27 15:49:09 -07002745
Clark Boylan3ee090a2014-04-03 20:55:09 -07002746 # The pipeline triggers every second, so we should have seen
2747 # several by now.
2748 time.sleep(5)
2749 self.waitUntilSettled()
2750
2751 # Stop queuing timer triggered jobs so that the assertions
2752 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002753 self.updateConfigLayout(
2754 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002755 self.sched.reconfigure(self.config)
2756 self.registerJobs()
2757
2758 self.assertEqual(len(self.builds), 2)
2759 self.worker.release('.*')
2760 self.waitUntilSettled()
2761 self.assertEqual(len(self.builds), 0)
2762 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002763
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002764 def test_check_smtp_pool(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002765 self.updateConfigLayout(
2766 'tests/fixtures/layout-smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002767 self.sched.reconfigure(self.config)
2768
2769 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2770 self.waitUntilSettled()
2771
2772 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2773 self.waitUntilSettled()
2774
James E. Blairff80a2f2013-12-27 13:24:06 -08002775 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002776
2777 # A.messages only holds what FakeGerrit places in it. Thus we
2778 # work on the knowledge of what the first message should be as
2779 # it is only configured to go to SMTP.
2780
2781 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002782 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002783 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002784 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002785 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002786 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002787
2788 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002789 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002790 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002791 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002792 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002793 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002794
James E. Blaire5910202013-12-27 09:50:31 -08002795 def test_timer_smtp(self):
2796 "Test that a periodic job is triggered"
Clark Boylan3ee090a2014-04-03 20:55:09 -07002797 self.worker.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08002798 self.updateConfigLayout(
2799 'tests/fixtures/layout-timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002800 self.sched.reconfigure(self.config)
2801 self.registerJobs()
2802
Clark Boylan3ee090a2014-04-03 20:55:09 -07002803 # The pipeline triggers every second, so we should have seen
2804 # several by now.
2805 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002806 self.waitUntilSettled()
2807
Clark Boylan3ee090a2014-04-03 20:55:09 -07002808 self.assertEqual(len(self.builds), 2)
2809 self.worker.release('.*')
2810 self.waitUntilSettled()
2811 self.assertEqual(len(self.history), 2)
2812
James E. Blaire5910202013-12-27 09:50:31 -08002813 self.assertEqual(self.getJobFromHistory(
2814 'project-bitrot-stable-old').result, 'SUCCESS')
2815 self.assertEqual(self.getJobFromHistory(
2816 'project-bitrot-stable-older').result, 'SUCCESS')
2817
James E. Blairff80a2f2013-12-27 13:24:06 -08002818 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002819
2820 # A.messages only holds what FakeGerrit places in it. Thus we
2821 # work on the knowledge of what the first message should be as
2822 # it is only configured to go to SMTP.
2823
2824 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002825 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002826 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002827 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002828 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002829 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002830
Clark Boylan3ee090a2014-04-03 20:55:09 -07002831 # Stop queuing timer triggered jobs and let any that may have
2832 # queued through so that end of test assertions pass.
James E. Blairf84026c2015-12-08 16:11:46 -08002833 self.updateConfigLayout(
2834 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002835 self.sched.reconfigure(self.config)
2836 self.registerJobs()
James E. Blairf8058972014-08-15 16:09:16 -07002837 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002838 self.worker.release('.*')
2839 self.waitUntilSettled()
2840
James E. Blair91e34592015-07-31 16:45:59 -07002841 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08002842 "Test that the RPC client can enqueue a change"
2843 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2844 A.addApproval('CRVW', 2)
2845 A.addApproval('APRV', 1)
2846
2847 client = zuul.rpcclient.RPCClient('127.0.0.1',
2848 self.gearman_server.port)
2849 r = client.enqueue(pipeline='gate',
2850 project='org/project',
2851 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002852 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002853 self.waitUntilSettled()
2854 self.assertEqual(self.getJobFromHistory('project-merge').result,
2855 'SUCCESS')
2856 self.assertEqual(self.getJobFromHistory('project-test1').result,
2857 'SUCCESS')
2858 self.assertEqual(self.getJobFromHistory('project-test2').result,
2859 'SUCCESS')
2860 self.assertEqual(A.data['status'], 'MERGED')
2861 self.assertEqual(A.reported, 2)
2862 self.assertEqual(r, True)
2863
James E. Blair91e34592015-07-31 16:45:59 -07002864 def test_client_enqueue_ref(self):
2865 "Test that the RPC client can enqueue a ref"
2866
2867 client = zuul.rpcclient.RPCClient('127.0.0.1',
2868 self.gearman_server.port)
2869 r = client.enqueue_ref(
2870 pipeline='post',
2871 project='org/project',
2872 trigger='gerrit',
2873 ref='master',
2874 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
2875 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
2876 self.waitUntilSettled()
2877 job_names = [x.name for x in self.history]
2878 self.assertEqual(len(self.history), 1)
2879 self.assertIn('project-post', job_names)
2880 self.assertEqual(r, True)
2881
James E. Blairad28e912013-11-27 10:43:22 -08002882 def test_client_enqueue_negative(self):
2883 "Test that the RPC client returns errors"
2884 client = zuul.rpcclient.RPCClient('127.0.0.1',
2885 self.gearman_server.port)
2886 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2887 "Invalid project"):
2888 r = client.enqueue(pipeline='gate',
2889 project='project-does-not-exist',
2890 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002891 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002892 client.shutdown()
2893 self.assertEqual(r, False)
2894
2895 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2896 "Invalid pipeline"):
2897 r = client.enqueue(pipeline='pipeline-does-not-exist',
2898 project='org/project',
2899 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002900 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002901 client.shutdown()
2902 self.assertEqual(r, False)
2903
2904 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2905 "Invalid trigger"):
2906 r = client.enqueue(pipeline='gate',
2907 project='org/project',
2908 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08002909 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002910 client.shutdown()
2911 self.assertEqual(r, False)
2912
2913 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2914 "Invalid change"):
2915 r = client.enqueue(pipeline='gate',
2916 project='org/project',
2917 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002918 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002919 client.shutdown()
2920 self.assertEqual(r, False)
2921
2922 self.waitUntilSettled()
2923 self.assertEqual(len(self.history), 0)
2924 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08002925
2926 def test_client_promote(self):
2927 "Test that the RPC client can promote a change"
2928 self.worker.hold_jobs_in_build = True
2929 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2930 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2931 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2932 A.addApproval('CRVW', 2)
2933 B.addApproval('CRVW', 2)
2934 C.addApproval('CRVW', 2)
2935
2936 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2937 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2938 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2939
2940 self.waitUntilSettled()
2941
Sean Daguef39b9ca2014-01-10 21:34:35 -05002942 items = self.sched.layout.pipelines['gate'].getAllItems()
2943 enqueue_times = {}
2944 for item in items:
2945 enqueue_times[str(item.change)] = item.enqueue_time
2946
James E. Blair36658cf2013-12-06 17:53:48 -08002947 client = zuul.rpcclient.RPCClient('127.0.0.1',
2948 self.gearman_server.port)
2949 r = client.promote(pipeline='gate',
2950 change_ids=['2,1', '3,1'])
2951
Sean Daguef39b9ca2014-01-10 21:34:35 -05002952 # ensure that enqueue times are durable
2953 items = self.sched.layout.pipelines['gate'].getAllItems()
2954 for item in items:
2955 self.assertEqual(
2956 enqueue_times[str(item.change)], item.enqueue_time)
2957
James E. Blair78acec92014-02-06 07:11:32 -08002958 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08002959 self.worker.release('.*-merge')
2960 self.waitUntilSettled()
2961 self.worker.release('.*-merge')
2962 self.waitUntilSettled()
2963 self.worker.release('.*-merge')
2964 self.waitUntilSettled()
2965
2966 self.assertEqual(len(self.builds), 6)
2967 self.assertEqual(self.builds[0].name, 'project-test1')
2968 self.assertEqual(self.builds[1].name, 'project-test2')
2969 self.assertEqual(self.builds[2].name, 'project-test1')
2970 self.assertEqual(self.builds[3].name, 'project-test2')
2971 self.assertEqual(self.builds[4].name, 'project-test1')
2972 self.assertEqual(self.builds[5].name, 'project-test2')
2973
2974 self.assertTrue(self.job_has_changes(self.builds[0], B))
2975 self.assertFalse(self.job_has_changes(self.builds[0], A))
2976 self.assertFalse(self.job_has_changes(self.builds[0], C))
2977
2978 self.assertTrue(self.job_has_changes(self.builds[2], B))
2979 self.assertTrue(self.job_has_changes(self.builds[2], C))
2980 self.assertFalse(self.job_has_changes(self.builds[2], A))
2981
2982 self.assertTrue(self.job_has_changes(self.builds[4], B))
2983 self.assertTrue(self.job_has_changes(self.builds[4], C))
2984 self.assertTrue(self.job_has_changes(self.builds[4], A))
2985
2986 self.worker.release()
2987 self.waitUntilSettled()
2988
2989 self.assertEqual(A.data['status'], 'MERGED')
2990 self.assertEqual(A.reported, 2)
2991 self.assertEqual(B.data['status'], 'MERGED')
2992 self.assertEqual(B.reported, 2)
2993 self.assertEqual(C.data['status'], 'MERGED')
2994 self.assertEqual(C.reported, 2)
2995
2996 client.shutdown()
2997 self.assertEqual(r, True)
2998
2999 def test_client_promote_dependent(self):
3000 "Test that the RPC client can promote a dependent change"
3001 # C (depends on B) -> B -> A ; then promote C to get:
3002 # A -> C (depends on B) -> B
3003 self.worker.hold_jobs_in_build = True
3004 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3005 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3006 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3007
3008 C.setDependsOn(B, 1)
3009
3010 A.addApproval('CRVW', 2)
3011 B.addApproval('CRVW', 2)
3012 C.addApproval('CRVW', 2)
3013
3014 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3015 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3016 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3017
3018 self.waitUntilSettled()
3019
3020 client = zuul.rpcclient.RPCClient('127.0.0.1',
3021 self.gearman_server.port)
3022 r = client.promote(pipeline='gate',
3023 change_ids=['3,1'])
3024
James E. Blair78acec92014-02-06 07:11:32 -08003025 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08003026 self.worker.release('.*-merge')
3027 self.waitUntilSettled()
3028 self.worker.release('.*-merge')
3029 self.waitUntilSettled()
3030 self.worker.release('.*-merge')
3031 self.waitUntilSettled()
3032
3033 self.assertEqual(len(self.builds), 6)
3034 self.assertEqual(self.builds[0].name, 'project-test1')
3035 self.assertEqual(self.builds[1].name, 'project-test2')
3036 self.assertEqual(self.builds[2].name, 'project-test1')
3037 self.assertEqual(self.builds[3].name, 'project-test2')
3038 self.assertEqual(self.builds[4].name, 'project-test1')
3039 self.assertEqual(self.builds[5].name, 'project-test2')
3040
3041 self.assertTrue(self.job_has_changes(self.builds[0], B))
3042 self.assertFalse(self.job_has_changes(self.builds[0], A))
3043 self.assertFalse(self.job_has_changes(self.builds[0], C))
3044
3045 self.assertTrue(self.job_has_changes(self.builds[2], B))
3046 self.assertTrue(self.job_has_changes(self.builds[2], C))
3047 self.assertFalse(self.job_has_changes(self.builds[2], A))
3048
3049 self.assertTrue(self.job_has_changes(self.builds[4], B))
3050 self.assertTrue(self.job_has_changes(self.builds[4], C))
3051 self.assertTrue(self.job_has_changes(self.builds[4], A))
3052
3053 self.worker.release()
3054 self.waitUntilSettled()
3055
3056 self.assertEqual(A.data['status'], 'MERGED')
3057 self.assertEqual(A.reported, 2)
3058 self.assertEqual(B.data['status'], 'MERGED')
3059 self.assertEqual(B.reported, 2)
3060 self.assertEqual(C.data['status'], 'MERGED')
3061 self.assertEqual(C.reported, 2)
3062
3063 client.shutdown()
3064 self.assertEqual(r, True)
3065
3066 def test_client_promote_negative(self):
3067 "Test that the RPC client returns errors for promotion"
3068 self.worker.hold_jobs_in_build = True
3069 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3070 A.addApproval('CRVW', 2)
3071 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3072 self.waitUntilSettled()
3073
3074 client = zuul.rpcclient.RPCClient('127.0.0.1',
3075 self.gearman_server.port)
3076
3077 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3078 r = client.promote(pipeline='nonexistent',
3079 change_ids=['2,1', '3,1'])
3080 client.shutdown()
3081 self.assertEqual(r, False)
3082
3083 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3084 r = client.promote(pipeline='gate',
3085 change_ids=['4,1'])
3086 client.shutdown()
3087 self.assertEqual(r, False)
3088
3089 self.worker.hold_jobs_in_build = False
3090 self.worker.release()
3091 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003092
3093 def test_queue_rate_limiting(self):
3094 "Test that DependentPipelines are rate limited with dep across window"
James E. Blairf84026c2015-12-08 16:11:46 -08003095 self.updateConfigLayout(
3096 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003097 self.sched.reconfigure(self.config)
3098 self.worker.hold_jobs_in_build = True
3099 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3100 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3101 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3102
3103 C.setDependsOn(B, 1)
3104 self.worker.addFailTest('project-test1', A)
3105
3106 A.addApproval('CRVW', 2)
3107 B.addApproval('CRVW', 2)
3108 C.addApproval('CRVW', 2)
3109
3110 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3111 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3112 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3113 self.waitUntilSettled()
3114
3115 # Only A and B will have their merge jobs queued because
3116 # window is 2.
3117 self.assertEqual(len(self.builds), 2)
3118 self.assertEqual(self.builds[0].name, 'project-merge')
3119 self.assertEqual(self.builds[1].name, 'project-merge')
3120
3121 self.worker.release('.*-merge')
3122 self.waitUntilSettled()
3123 self.worker.release('.*-merge')
3124 self.waitUntilSettled()
3125
3126 # Only A and B will have their test jobs queued because
3127 # window is 2.
3128 self.assertEqual(len(self.builds), 4)
3129 self.assertEqual(self.builds[0].name, 'project-test1')
3130 self.assertEqual(self.builds[1].name, 'project-test2')
3131 self.assertEqual(self.builds[2].name, 'project-test1')
3132 self.assertEqual(self.builds[3].name, 'project-test2')
3133
3134 self.worker.release('project-.*')
3135 self.waitUntilSettled()
3136
3137 queue = self.sched.layout.pipelines['gate'].queues[0]
3138 # A failed so window is reduced by 1 to 1.
3139 self.assertEqual(queue.window, 1)
3140 self.assertEqual(queue.window_floor, 1)
3141 self.assertEqual(A.data['status'], 'NEW')
3142
3143 # Gate is reset and only B's merge job is queued because
3144 # window shrunk to 1.
3145 self.assertEqual(len(self.builds), 1)
3146 self.assertEqual(self.builds[0].name, 'project-merge')
3147
3148 self.worker.release('.*-merge')
3149 self.waitUntilSettled()
3150
3151 # Only B's test jobs are queued because window is still 1.
3152 self.assertEqual(len(self.builds), 2)
3153 self.assertEqual(self.builds[0].name, 'project-test1')
3154 self.assertEqual(self.builds[1].name, 'project-test2')
3155
3156 self.worker.release('project-.*')
3157 self.waitUntilSettled()
3158
3159 # B was successfully merged so window is increased to 2.
3160 self.assertEqual(queue.window, 2)
3161 self.assertEqual(queue.window_floor, 1)
3162 self.assertEqual(B.data['status'], 'MERGED')
3163
3164 # Only C is left and its merge job is queued.
3165 self.assertEqual(len(self.builds), 1)
3166 self.assertEqual(self.builds[0].name, 'project-merge')
3167
3168 self.worker.release('.*-merge')
3169 self.waitUntilSettled()
3170
3171 # After successful merge job the test jobs for C are queued.
3172 self.assertEqual(len(self.builds), 2)
3173 self.assertEqual(self.builds[0].name, 'project-test1')
3174 self.assertEqual(self.builds[1].name, 'project-test2')
3175
3176 self.worker.release('project-.*')
3177 self.waitUntilSettled()
3178
3179 # C successfully merged so window is bumped to 3.
3180 self.assertEqual(queue.window, 3)
3181 self.assertEqual(queue.window_floor, 1)
3182 self.assertEqual(C.data['status'], 'MERGED')
3183
3184 def test_queue_rate_limiting_dependent(self):
3185 "Test that DependentPipelines are rate limited with dep in window"
James E. Blairf84026c2015-12-08 16:11:46 -08003186 self.updateConfigLayout(
3187 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003188 self.sched.reconfigure(self.config)
3189 self.worker.hold_jobs_in_build = True
3190 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3191 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3192 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3193
3194 B.setDependsOn(A, 1)
3195
3196 self.worker.addFailTest('project-test1', A)
3197
3198 A.addApproval('CRVW', 2)
3199 B.addApproval('CRVW', 2)
3200 C.addApproval('CRVW', 2)
3201
3202 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3203 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3204 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3205 self.waitUntilSettled()
3206
3207 # Only A and B will have their merge jobs queued because
3208 # window is 2.
3209 self.assertEqual(len(self.builds), 2)
3210 self.assertEqual(self.builds[0].name, 'project-merge')
3211 self.assertEqual(self.builds[1].name, 'project-merge')
3212
3213 self.worker.release('.*-merge')
3214 self.waitUntilSettled()
3215 self.worker.release('.*-merge')
3216 self.waitUntilSettled()
3217
3218 # Only A and B will have their test jobs queued because
3219 # window is 2.
3220 self.assertEqual(len(self.builds), 4)
3221 self.assertEqual(self.builds[0].name, 'project-test1')
3222 self.assertEqual(self.builds[1].name, 'project-test2')
3223 self.assertEqual(self.builds[2].name, 'project-test1')
3224 self.assertEqual(self.builds[3].name, 'project-test2')
3225
3226 self.worker.release('project-.*')
3227 self.waitUntilSettled()
3228
3229 queue = self.sched.layout.pipelines['gate'].queues[0]
3230 # A failed so window is reduced by 1 to 1.
3231 self.assertEqual(queue.window, 1)
3232 self.assertEqual(queue.window_floor, 1)
3233 self.assertEqual(A.data['status'], 'NEW')
3234 self.assertEqual(B.data['status'], 'NEW')
3235
3236 # Gate is reset and only C's merge job is queued because
3237 # window shrunk to 1 and A and B were dequeued.
3238 self.assertEqual(len(self.builds), 1)
3239 self.assertEqual(self.builds[0].name, 'project-merge')
3240
3241 self.worker.release('.*-merge')
3242 self.waitUntilSettled()
3243
3244 # Only C's test jobs are queued because window is still 1.
3245 self.assertEqual(len(self.builds), 2)
3246 self.assertEqual(self.builds[0].name, 'project-test1')
3247 self.assertEqual(self.builds[1].name, 'project-test2')
3248
3249 self.worker.release('project-.*')
3250 self.waitUntilSettled()
3251
3252 # C was successfully merged so window is increased to 2.
3253 self.assertEqual(queue.window, 2)
3254 self.assertEqual(queue.window_floor, 1)
3255 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003256
3257 def test_worker_update_metadata(self):
3258 "Test if a worker can send back metadata about itself"
3259 self.worker.hold_jobs_in_build = True
3260
3261 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3262 A.addApproval('CRVW', 2)
3263 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3264 self.waitUntilSettled()
3265
3266 self.assertEqual(len(self.launcher.builds), 1)
3267
3268 self.log.debug('Current builds:')
3269 self.log.debug(self.launcher.builds)
3270
3271 start = time.time()
3272 while True:
3273 if time.time() - start > 10:
3274 raise Exception("Timeout waiting for gearman server to report "
3275 + "back to the client")
3276 build = self.launcher.builds.values()[0]
3277 if build.worker.name == "My Worker":
3278 break
3279 else:
3280 time.sleep(0)
3281
3282 self.log.debug(build)
3283 self.assertEqual("My Worker", build.worker.name)
3284 self.assertEqual("localhost", build.worker.hostname)
3285 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3286 self.assertEqual("zuul.example.org", build.worker.fqdn)
3287 self.assertEqual("FakeBuilder", build.worker.program)
3288 self.assertEqual("v1.1", build.worker.version)
3289 self.assertEqual({'something': 'else'}, build.worker.extra)
3290
3291 self.worker.hold_jobs_in_build = False
3292 self.worker.release()
3293 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003294
3295 def test_footer_message(self):
3296 "Test a pipeline's footer message is correctly added to the report."
James E. Blairf84026c2015-12-08 16:11:46 -08003297 self.updateConfigLayout(
3298 'tests/fixtures/layout-footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003299 self.sched.reconfigure(self.config)
3300 self.registerJobs()
3301
3302 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3303 A.addApproval('CRVW', 2)
3304 self.worker.addFailTest('test1', A)
3305 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3306 self.waitUntilSettled()
3307
3308 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3309 B.addApproval('CRVW', 2)
3310 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3311 self.waitUntilSettled()
3312
3313 self.assertEqual(2, len(self.smtp_messages))
3314
3315 failure_body = """\
3316Build failed. For information on how to proceed, see \
3317http://wiki.example.org/Test_Failures
3318
3319- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
3320- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
3321
3322For CI problems and help debugging, contact ci@example.org"""
3323
3324 success_body = """\
3325Build succeeded.
3326
3327- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
3328- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
3329
3330For CI problems and help debugging, contact ci@example.org"""
3331
3332 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
3333 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11003334
3335 def test_merge_failure_reporters(self):
3336 """Check that the config is set up correctly"""
3337
James E. Blairf84026c2015-12-08 16:11:46 -08003338 self.updateConfigLayout(
3339 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003340 self.sched.reconfigure(self.config)
3341 self.registerJobs()
3342
3343 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003344 "Merge Failed.\n\nThis change or one of its cross-repo "
3345 "dependencies was unable to be automatically merged with the "
3346 "current state of its repository. Please rebase the change and "
3347 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003348 self.sched.layout.pipelines['check'].merge_failure_message)
3349 self.assertEqual(
3350 "The merge failed! For more information...",
3351 self.sched.layout.pipelines['gate'].merge_failure_message)
3352
3353 self.assertEqual(
3354 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3355 self.assertEqual(
3356 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3357
3358 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003359 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3360 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003361
3362 self.assertTrue(
3363 (
3364 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003365 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003366 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003367 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003368 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003369 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003370 ) or (
3371 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003372 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003373 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003374 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003375 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003376 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003377 )
3378 )
3379
3380 def test_merge_failure_reports(self):
3381 """Check that when a change fails to merge the correct message is sent
3382 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003383 self.updateConfigLayout(
3384 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003385 self.sched.reconfigure(self.config)
3386 self.registerJobs()
3387
3388 # Check a test failure isn't reported to SMTP
3389 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3390 A.addApproval('CRVW', 2)
3391 self.worker.addFailTest('project-test1', A)
3392 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3393 self.waitUntilSettled()
3394
3395 self.assertEqual(3, len(self.history)) # 3 jobs
3396 self.assertEqual(0, len(self.smtp_messages))
3397
3398 # Check a merge failure is reported to SMTP
3399 # B should be merged, but C will conflict with B
3400 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3401 B.addPatchset(['conflict'])
3402 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3403 C.addPatchset(['conflict'])
3404 B.addApproval('CRVW', 2)
3405 C.addApproval('CRVW', 2)
3406 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3407 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3408 self.waitUntilSettled()
3409
3410 self.assertEqual(6, len(self.history)) # A and B jobs
3411 self.assertEqual(1, len(self.smtp_messages))
3412 self.assertEqual('The merge failed! For more information...',
3413 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003414
3415 def test_swift_instructions(self):
3416 "Test that the correct swift instructions are sent to the workers"
James E. Blairf84026c2015-12-08 16:11:46 -08003417 self.updateConfigLayout(
3418 'tests/fixtures/layout-swift.yaml')
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003419 self.sched.reconfigure(self.config)
3420 self.registerJobs()
3421
3422 self.worker.hold_jobs_in_build = True
3423 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3424
3425 A.addApproval('CRVW', 2)
3426 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3427 self.waitUntilSettled()
3428
3429 self.assertEqual(
3430 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
3431 "gate/test-merge/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003432 self.builds[0].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003433 self.assertEqual(5,
3434 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
3435 split('\n')))
3436 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
3437
3438 self.assertEqual(
3439 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
3440 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003441 self.builds[1].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003442 self.assertEqual(5,
3443 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
3444 split('\n')))
3445 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
3446
3447 self.assertEqual(
3448 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
3449 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003450 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003451 self.assertEqual(5,
3452 len(self.builds[1].
3453 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
3454 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
3455
3456 self.worker.hold_jobs_in_build = False
3457 self.worker.release()
3458 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003459
3460 def test_client_get_running_jobs(self):
3461 "Test that the RPC client can get a list of running jobs"
3462 self.worker.hold_jobs_in_build = True
3463 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3464 A.addApproval('CRVW', 2)
3465 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3466 self.waitUntilSettled()
3467
3468 client = zuul.rpcclient.RPCClient('127.0.0.1',
3469 self.gearman_server.port)
3470
3471 # Wait for gearman server to send the initial workData back to zuul
3472 start = time.time()
3473 while True:
3474 if time.time() - start > 10:
3475 raise Exception("Timeout waiting for gearman server to report "
3476 + "back to the client")
3477 build = self.launcher.builds.values()[0]
3478 if build.worker.name == "My Worker":
3479 break
3480 else:
3481 time.sleep(0)
3482
3483 running_items = client.get_running_jobs()
3484
3485 self.assertEqual(1, len(running_items))
3486 running_item = running_items[0]
3487 self.assertEqual([], running_item['failing_reasons'])
3488 self.assertEqual([], running_item['items_behind'])
3489 self.assertEqual('https://hostname/1', running_item['url'])
3490 self.assertEqual(None, running_item['item_ahead'])
3491 self.assertEqual('org/project', running_item['project'])
3492 self.assertEqual(None, running_item['remaining_time'])
3493 self.assertEqual(True, running_item['active'])
3494 self.assertEqual('1,1', running_item['id'])
3495
3496 self.assertEqual(3, len(running_item['jobs']))
3497 for job in running_item['jobs']:
3498 if job['name'] == 'project-merge':
3499 self.assertEqual('project-merge', job['name'])
3500 self.assertEqual('gate', job['pipeline'])
3501 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003502 self.assertEqual('https://server/job/project-merge/0/',
3503 job['url'])
3504 self.assertEqual(7, len(job['worker']))
3505 self.assertEqual(False, job['canceled'])
3506 self.assertEqual(True, job['voting'])
3507 self.assertEqual(None, job['result'])
3508 self.assertEqual('gate', job['pipeline'])
3509 break
3510
3511 self.worker.hold_jobs_in_build = False
3512 self.worker.release()
3513 self.waitUntilSettled()
3514
3515 running_items = client.get_running_jobs()
3516 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003517
3518 def test_nonvoting_pipeline(self):
3519 "Test that a nonvoting pipeline (experimental) can still report"
3520
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003521 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3522 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003523 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3524 self.waitUntilSettled()
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003525 self.assertEqual(
3526 self.getJobFromHistory('experimental-project-test').result,
3527 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003528 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003529
3530 def test_crd_gate(self):
3531 "Test cross-repo dependencies"
3532 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3533 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3534 A.addApproval('CRVW', 2)
3535 B.addApproval('CRVW', 2)
3536
3537 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3538 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3539 AM2.setMerged()
3540 AM1.setMerged()
3541
3542 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3543 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3544 BM2.setMerged()
3545 BM1.setMerged()
3546
3547 # A -> AM1 -> AM2
3548 # B -> BM1 -> BM2
3549 # A Depends-On: B
3550 # M2 is here to make sure it is never queried. If it is, it
3551 # means zuul is walking down the entire history of merged
3552 # changes.
3553
3554 B.setDependsOn(BM1, 1)
3555 BM1.setDependsOn(BM2, 1)
3556
3557 A.setDependsOn(AM1, 1)
3558 AM1.setDependsOn(AM2, 1)
3559
3560 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3561 A.subject, B.data['id'])
3562
3563 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3564 self.waitUntilSettled()
3565
3566 self.assertEqual(A.data['status'], 'NEW')
3567 self.assertEqual(B.data['status'], 'NEW')
3568
3569 source = self.sched.layout.pipelines['gate'].source
3570 source.maintainCache([])
3571
3572 self.worker.hold_jobs_in_build = True
3573 B.addApproval('APRV', 1)
3574 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3575 self.waitUntilSettled()
3576
3577 self.worker.release('.*-merge')
3578 self.waitUntilSettled()
3579 self.worker.release('.*-merge')
3580 self.waitUntilSettled()
3581 self.worker.hold_jobs_in_build = False
3582 self.worker.release()
3583 self.waitUntilSettled()
3584
3585 self.assertEqual(AM2.queried, 0)
3586 self.assertEqual(BM2.queried, 0)
3587 self.assertEqual(A.data['status'], 'MERGED')
3588 self.assertEqual(B.data['status'], 'MERGED')
3589 self.assertEqual(A.reported, 2)
3590 self.assertEqual(B.reported, 2)
3591
James E. Blair8f78d882015-02-05 08:51:37 -08003592 self.assertEqual(self.getJobFromHistory('project1-merge').changes,
3593 '2,1 1,1')
3594
3595 def test_crd_branch(self):
3596 "Test cross-repo dependencies in multiple branches"
3597 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3598 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3599 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3600 C.data['id'] = B.data['id']
3601 A.addApproval('CRVW', 2)
3602 B.addApproval('CRVW', 2)
3603 C.addApproval('CRVW', 2)
3604
3605 # A Depends-On: B+C
3606 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3607 A.subject, B.data['id'])
3608
3609 self.worker.hold_jobs_in_build = True
3610 B.addApproval('APRV', 1)
3611 C.addApproval('APRV', 1)
3612 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3613 self.waitUntilSettled()
3614
3615 self.worker.release('.*-merge')
3616 self.waitUntilSettled()
3617 self.worker.release('.*-merge')
3618 self.waitUntilSettled()
3619 self.worker.release('.*-merge')
3620 self.waitUntilSettled()
3621 self.worker.hold_jobs_in_build = False
3622 self.worker.release()
3623 self.waitUntilSettled()
3624
3625 self.assertEqual(A.data['status'], 'MERGED')
3626 self.assertEqual(B.data['status'], 'MERGED')
3627 self.assertEqual(C.data['status'], 'MERGED')
3628 self.assertEqual(A.reported, 2)
3629 self.assertEqual(B.reported, 2)
3630 self.assertEqual(C.reported, 2)
3631
3632 self.assertEqual(self.getJobFromHistory('project1-merge').changes,
3633 '2,1 3,1 1,1')
3634
3635 def test_crd_multiline(self):
3636 "Test multiple depends-on lines in commit"
3637 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3638 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3639 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
3640 A.addApproval('CRVW', 2)
3641 B.addApproval('CRVW', 2)
3642 C.addApproval('CRVW', 2)
3643
3644 # A Depends-On: B+C
3645 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3646 A.subject, B.data['id'], C.data['id'])
3647
3648 self.worker.hold_jobs_in_build = True
3649 B.addApproval('APRV', 1)
3650 C.addApproval('APRV', 1)
3651 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3652 self.waitUntilSettled()
3653
3654 self.worker.release('.*-merge')
3655 self.waitUntilSettled()
3656 self.worker.release('.*-merge')
3657 self.waitUntilSettled()
3658 self.worker.release('.*-merge')
3659 self.waitUntilSettled()
3660 self.worker.hold_jobs_in_build = False
3661 self.worker.release()
3662 self.waitUntilSettled()
3663
3664 self.assertEqual(A.data['status'], 'MERGED')
3665 self.assertEqual(B.data['status'], 'MERGED')
3666 self.assertEqual(C.data['status'], 'MERGED')
3667 self.assertEqual(A.reported, 2)
3668 self.assertEqual(B.reported, 2)
3669 self.assertEqual(C.reported, 2)
3670
3671 self.assertEqual(self.getJobFromHistory('project1-merge').changes,
3672 '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003673
3674 def test_crd_unshared_gate(self):
3675 "Test cross-repo dependencies in unshared gate queues"
3676 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3677 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3678 A.addApproval('CRVW', 2)
3679 B.addApproval('CRVW', 2)
3680
3681 # A Depends-On: B
3682 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3683 A.subject, B.data['id'])
3684
3685 # A and B do not share a queue, make sure that A is unable to
3686 # enqueue B (and therefore, A is unable to be enqueued).
3687 B.addApproval('APRV', 1)
3688 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3689 self.waitUntilSettled()
3690
3691 self.assertEqual(A.data['status'], 'NEW')
3692 self.assertEqual(B.data['status'], 'NEW')
3693 self.assertEqual(A.reported, 0)
3694 self.assertEqual(B.reported, 0)
3695 self.assertEqual(len(self.history), 0)
3696
3697 # Enqueue and merge B alone.
3698 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3699 self.waitUntilSettled()
3700
3701 self.assertEqual(B.data['status'], 'MERGED')
3702 self.assertEqual(B.reported, 2)
3703
3704 # Now that B is merged, A should be able to be enqueued and
3705 # merged.
3706 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3707 self.waitUntilSettled()
3708
3709 self.assertEqual(A.data['status'], 'MERGED')
3710 self.assertEqual(A.reported, 2)
3711
James E. Blair96698e22015-04-02 07:48:21 -07003712 def test_crd_gate_reverse(self):
3713 "Test reverse cross-repo dependencies"
3714 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3715 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3716 A.addApproval('CRVW', 2)
3717 B.addApproval('CRVW', 2)
3718
3719 # A Depends-On: B
3720
3721 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3722 A.subject, B.data['id'])
3723
3724 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3725 self.waitUntilSettled()
3726
3727 self.assertEqual(A.data['status'], 'NEW')
3728 self.assertEqual(B.data['status'], 'NEW')
3729
3730 self.worker.hold_jobs_in_build = True
3731 A.addApproval('APRV', 1)
3732 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3733 self.waitUntilSettled()
3734
3735 self.worker.release('.*-merge')
3736 self.waitUntilSettled()
3737 self.worker.release('.*-merge')
3738 self.waitUntilSettled()
3739 self.worker.hold_jobs_in_build = False
3740 self.worker.release()
3741 self.waitUntilSettled()
3742
3743 self.assertEqual(A.data['status'], 'MERGED')
3744 self.assertEqual(B.data['status'], 'MERGED')
3745 self.assertEqual(A.reported, 2)
3746 self.assertEqual(B.reported, 2)
3747
3748 self.assertEqual(self.getJobFromHistory('project1-merge').changes,
3749 '2,1 1,1')
3750
James E. Blair5ee24252014-12-30 10:12:29 -08003751 def test_crd_cycle(self):
3752 "Test cross-repo dependency cycles"
3753 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3754 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3755 A.addApproval('CRVW', 2)
3756 B.addApproval('CRVW', 2)
3757
3758 # A -> B -> A (via commit-depends)
3759
3760 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3761 A.subject, B.data['id'])
3762 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3763 B.subject, A.data['id'])
3764
3765 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3766 self.waitUntilSettled()
3767
3768 self.assertEqual(A.reported, 0)
3769 self.assertEqual(B.reported, 0)
3770 self.assertEqual(A.data['status'], 'NEW')
3771 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003772
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003773 def test_crd_gate_unknown(self):
3774 "Test unknown projects in dependent pipeline"
3775 self.init_repo("org/unknown")
3776 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3777 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
3778 A.addApproval('CRVW', 2)
3779 B.addApproval('CRVW', 2)
3780
3781 # A Depends-On: B
3782 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3783 A.subject, B.data['id'])
3784
3785 B.addApproval('APRV', 1)
3786 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3787 self.waitUntilSettled()
3788
3789 # Unknown projects cannot share a queue with any other
3790 # since they don't have common jobs with any other (they have no jobs).
3791 # Changes which depend on unknown project changes
3792 # should not be processed in dependent pipeline
3793 self.assertEqual(A.data['status'], 'NEW')
3794 self.assertEqual(B.data['status'], 'NEW')
3795 self.assertEqual(A.reported, 0)
3796 self.assertEqual(B.reported, 0)
3797 self.assertEqual(len(self.history), 0)
3798
3799 # Simulate change B being gated outside this layout
3800 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3801 B.setMerged()
3802 self.waitUntilSettled()
3803 self.assertEqual(len(self.history), 0)
3804
3805 # Now that B is merged, A should be able to be enqueued and
3806 # merged.
3807 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3808 self.waitUntilSettled()
3809
3810 self.assertEqual(A.data['status'], 'MERGED')
3811 self.assertEqual(A.reported, 2)
3812 self.assertEqual(B.data['status'], 'MERGED')
3813 self.assertEqual(B.reported, 0)
3814
James E. Blairbfb8e042014-12-30 17:01:44 -08003815 def test_crd_check(self):
3816 "Test cross-repo dependencies in independent pipelines"
3817
3818 self.gearman_server.hold_jobs_in_queue = True
3819 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3820 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3821
3822 # A Depends-On: B
3823 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3824 A.subject, B.data['id'])
3825
3826 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3827 self.waitUntilSettled()
3828
3829 queue = self.gearman_server.getQueue()
3830 ref = self.getParameter(queue[-1], 'ZUUL_REF')
3831 self.gearman_server.hold_jobs_in_queue = False
3832 self.gearman_server.release()
3833 self.waitUntilSettled()
3834
3835 path = os.path.join(self.git_root, "org/project1")
3836 repo = git.Repo(path)
3837 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
3838 repo_messages.reverse()
3839 correct_messages = ['initial commit', 'A-1']
3840 self.assertEqual(repo_messages, correct_messages)
3841
3842 path = os.path.join(self.git_root, "org/project2")
3843 repo = git.Repo(path)
3844 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
3845 repo_messages.reverse()
3846 correct_messages = ['initial commit', 'B-1']
3847 self.assertEqual(repo_messages, correct_messages)
3848
3849 self.assertEqual(A.data['status'], 'NEW')
3850 self.assertEqual(B.data['status'], 'NEW')
3851 self.assertEqual(A.reported, 1)
3852 self.assertEqual(B.reported, 0)
3853
3854 self.assertEqual(self.history[0].changes, '2,1 1,1')
3855 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08003856
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003857 def test_crd_check_git_depends(self):
3858 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07003859 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003860 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3861 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3862
3863 # Add two git-dependent changes and make sure they both report
3864 # success.
3865 B.setDependsOn(A, 1)
3866 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3867 self.waitUntilSettled()
3868 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3869 self.waitUntilSettled()
3870
James E. Blairb8c16472015-05-05 14:55:26 -07003871 self.orderedRelease()
3872 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003873 self.waitUntilSettled()
3874
3875 self.assertEqual(A.data['status'], 'NEW')
3876 self.assertEqual(B.data['status'], 'NEW')
3877 self.assertEqual(A.reported, 1)
3878 self.assertEqual(B.reported, 1)
3879
3880 self.assertEqual(self.history[0].changes, '1,1')
3881 self.assertEqual(self.history[-1].changes, '1,1 2,1')
3882 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
3883
3884 self.assertIn('Build succeeded', A.messages[0])
3885 self.assertIn('Build succeeded', B.messages[0])
3886
3887 def test_crd_check_duplicate(self):
3888 "Test duplicate check in independent pipelines"
James E. Blair1e263032015-05-07 14:35:34 -07003889 self.worker.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003890 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3891 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3892 check_pipeline = self.sched.layout.pipelines['check']
3893
3894 # Add two git-dependent changes...
3895 B.setDependsOn(A, 1)
3896 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3897 self.waitUntilSettled()
3898 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3899
3900 # ...make sure the live one is not duplicated...
3901 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3902 self.waitUntilSettled()
3903 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3904
3905 # ...but the non-live one is able to be.
3906 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3907 self.waitUntilSettled()
3908 self.assertEqual(len(check_pipeline.getAllItems()), 3)
3909
Clark Boylandd849822015-03-02 12:38:14 -08003910 # Release jobs in order to avoid races with change A jobs
3911 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07003912 self.orderedRelease()
James E. Blair1e263032015-05-07 14:35:34 -07003913 self.worker.hold_jobs_in_build = False
3914 self.worker.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003915 self.waitUntilSettled()
3916
3917 self.assertEqual(A.data['status'], 'NEW')
3918 self.assertEqual(B.data['status'], 'NEW')
3919 self.assertEqual(A.reported, 1)
3920 self.assertEqual(B.reported, 1)
3921
3922 self.assertEqual(self.history[0].changes, '1,1 2,1')
3923 self.assertEqual(self.history[1].changes, '1,1')
3924 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
3925
3926 self.assertIn('Build succeeded', A.messages[0])
3927 self.assertIn('Build succeeded', B.messages[0])
3928
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003929 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08003930 "Test cross-repo dependencies re-enqueued in independent pipelines"
3931
3932 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003933 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
3934 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08003935
3936 # A Depends-On: B
3937 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3938 A.subject, B.data['id'])
3939
3940 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3941 self.waitUntilSettled()
3942
3943 self.sched.reconfigure(self.config)
3944
3945 # Make sure the items still share a change queue, and the
3946 # first one is not live.
3947 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 1)
3948 queue = self.sched.layout.pipelines['check'].queues[0]
3949 first_item = queue.queue[0]
3950 for item in queue.queue:
3951 self.assertEqual(item.queue, first_item.queue)
3952 self.assertFalse(first_item.live)
3953 self.assertTrue(queue.queue[1].live)
3954
3955 self.gearman_server.hold_jobs_in_queue = False
3956 self.gearman_server.release()
3957 self.waitUntilSettled()
3958
3959 self.assertEqual(A.data['status'], 'NEW')
3960 self.assertEqual(B.data['status'], 'NEW')
3961 self.assertEqual(A.reported, 1)
3962 self.assertEqual(B.reported, 0)
3963
3964 self.assertEqual(self.history[0].changes, '2,1 1,1')
3965 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08003966
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003967 def test_crd_check_reconfiguration(self):
3968 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
3969
3970 def test_crd_undefined_project(self):
3971 """Test that undefined projects in dependencies are handled for
3972 independent pipelines"""
3973 # It's a hack for fake gerrit,
3974 # as it implies repo creation upon the creation of any change
3975 self.init_repo("org/unknown")
3976 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
3977
James E. Blair17dd6772015-02-09 14:45:18 -08003978 def test_crd_check_ignore_dependencies(self):
3979 "Test cross-repo dependencies can be ignored"
James E. Blairf84026c2015-12-08 16:11:46 -08003980 self.updateConfigLayout(
3981 'tests/fixtures/layout-ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08003982 self.sched.reconfigure(self.config)
3983 self.registerJobs()
3984
3985 self.gearman_server.hold_jobs_in_queue = True
3986 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3987 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3988 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
3989
3990 # A Depends-On: B
3991 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3992 A.subject, B.data['id'])
3993 # C git-depends on B
3994 C.setDependsOn(B, 1)
3995 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3996 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3997 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
3998 self.waitUntilSettled()
3999
4000 # Make sure none of the items share a change queue, and all
4001 # are live.
4002 check_pipeline = self.sched.layout.pipelines['check']
4003 self.assertEqual(len(check_pipeline.queues), 3)
4004 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4005 for item in check_pipeline.getAllItems():
4006 self.assertTrue(item.live)
4007
4008 self.gearman_server.hold_jobs_in_queue = False
4009 self.gearman_server.release()
4010 self.waitUntilSettled()
4011
4012 self.assertEqual(A.data['status'], 'NEW')
4013 self.assertEqual(B.data['status'], 'NEW')
4014 self.assertEqual(C.data['status'], 'NEW')
4015 self.assertEqual(A.reported, 1)
4016 self.assertEqual(B.reported, 1)
4017 self.assertEqual(C.reported, 1)
4018
4019 # Each job should have tested exactly one change
4020 for job in self.history:
4021 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004022
4023 def test_crd_check_transitive(self):
4024 "Test transitive cross-repo dependencies"
4025 # Specifically, if A -> B -> C, and C gets a new patchset and
4026 # A gets a new patchset, ensure the test of A,2 includes B,1
4027 # and C,2 (not C,1 which would indicate stale data in the
4028 # cache for B).
4029 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4030 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4031 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4032
4033 # A Depends-On: B
4034 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4035 A.subject, B.data['id'])
4036
4037 # B Depends-On: C
4038 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4039 B.subject, C.data['id'])
4040
4041 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4042 self.waitUntilSettled()
4043 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4044
4045 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4046 self.waitUntilSettled()
4047 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4048
4049 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4050 self.waitUntilSettled()
4051 self.assertEqual(self.history[-1].changes, '3,1')
4052
4053 C.addPatchset()
4054 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4055 self.waitUntilSettled()
4056 self.assertEqual(self.history[-1].changes, '3,2')
4057
4058 A.addPatchset()
4059 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4060 self.waitUntilSettled()
4061 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004062
4063 def test_disable_at(self):
4064 "Test a pipeline will only report to the disabled trigger when failing"
4065
James E. Blairf84026c2015-12-08 16:11:46 -08004066 self.updateConfigLayout(
4067 'tests/fixtures/layout-disable-at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004068 self.sched.reconfigure(self.config)
4069
4070 self.assertEqual(3, self.sched.layout.pipelines['check'].disable_at)
4071 self.assertEqual(
4072 0, self.sched.layout.pipelines['check']._consecutive_failures)
4073 self.assertFalse(self.sched.layout.pipelines['check']._disabled)
4074
4075 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4076 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4077 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4078 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4079 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4080 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4081 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4082 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4083 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4084 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4085 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4086
4087 self.worker.addFailTest('project-test1', A)
4088 self.worker.addFailTest('project-test1', B)
4089 # Let C pass, resetting the counter
4090 self.worker.addFailTest('project-test1', D)
4091 self.worker.addFailTest('project-test1', E)
4092 self.worker.addFailTest('project-test1', F)
4093 self.worker.addFailTest('project-test1', G)
4094 self.worker.addFailTest('project-test1', H)
4095 # I also passes but should only report to the disabled reporters
4096 self.worker.addFailTest('project-test1', J)
4097 self.worker.addFailTest('project-test1', K)
4098
4099 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4100 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4101 self.waitUntilSettled()
4102
4103 self.assertEqual(
4104 2, self.sched.layout.pipelines['check']._consecutive_failures)
4105 self.assertFalse(self.sched.layout.pipelines['check']._disabled)
4106
4107 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4108 self.waitUntilSettled()
4109
4110 self.assertEqual(
4111 0, self.sched.layout.pipelines['check']._consecutive_failures)
4112 self.assertFalse(self.sched.layout.pipelines['check']._disabled)
4113
4114 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4115 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4116 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4117 self.waitUntilSettled()
4118
4119 # We should be disabled now
4120 self.assertEqual(
4121 3, self.sched.layout.pipelines['check']._consecutive_failures)
4122 self.assertTrue(self.sched.layout.pipelines['check']._disabled)
4123
4124 # We need to wait between each of these patches to make sure the
4125 # smtp messages come back in an expected order
4126 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4127 self.waitUntilSettled()
4128 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4129 self.waitUntilSettled()
4130 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4131 self.waitUntilSettled()
4132
4133 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4134 # leaving a message on each change
4135 self.assertEqual(1, len(A.messages))
4136 self.assertIn('Build failed.', A.messages[0])
4137 self.assertEqual(1, len(B.messages))
4138 self.assertIn('Build failed.', B.messages[0])
4139 self.assertEqual(1, len(C.messages))
4140 self.assertIn('Build succeeded.', C.messages[0])
4141 self.assertEqual(1, len(D.messages))
4142 self.assertIn('Build failed.', D.messages[0])
4143 self.assertEqual(1, len(E.messages))
4144 self.assertIn('Build failed.', E.messages[0])
4145 self.assertEqual(1, len(F.messages))
4146 self.assertIn('Build failed.', F.messages[0])
4147
4148 # The last 3 (GHI) would have only reported via smtp.
4149 self.assertEqual(3, len(self.smtp_messages))
4150 self.assertEqual(0, len(G.messages))
4151 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
4152 self.assertIn('/7/1/check', self.smtp_messages[0]['body'])
4153 self.assertEqual(0, len(H.messages))
4154 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
4155 self.assertIn('/8/1/check', self.smtp_messages[1]['body'])
4156 self.assertEqual(0, len(I.messages))
4157 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
4158 self.assertIn('/9/1/check', self.smtp_messages[2]['body'])
4159
4160 # Now reload the configuration (simulate a HUP) to check the pipeline
4161 # comes out of disabled
4162 self.sched.reconfigure(self.config)
4163
4164 self.assertEqual(3, self.sched.layout.pipelines['check'].disable_at)
4165 self.assertEqual(
4166 0, self.sched.layout.pipelines['check']._consecutive_failures)
4167 self.assertFalse(self.sched.layout.pipelines['check']._disabled)
4168
4169 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4170 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4171 self.waitUntilSettled()
4172
4173 self.assertEqual(
4174 2, self.sched.layout.pipelines['check']._consecutive_failures)
4175 self.assertFalse(self.sched.layout.pipelines['check']._disabled)
4176
4177 # J and K went back to gerrit
4178 self.assertEqual(1, len(J.messages))
4179 self.assertIn('Build failed.', J.messages[0])
4180 self.assertEqual(1, len(K.messages))
4181 self.assertIn('Build failed.', K.messages[0])
4182 # No more messages reported via smtp
4183 self.assertEqual(3, len(self.smtp_messages))