blob: 9baa824cc8303f85987bd606a1fee37d4e46de4b [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
Monty Taylorbc758832013-06-17 17:22:42 -040025
James E. Blair4886cc12012-07-18 15:39:41 -070026import git
Monty Taylorbc758832013-06-17 17:22:42 -040027import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070028
James E. Blairb0fcae42012-07-17 11:12:10 -070029import zuul.scheduler
James E. Blairad28e912013-11-27 10:43:22 -080030import zuul.rpcclient
Joshua Hesketh1879cf72013-08-19 14:13:15 +100031import zuul.reporter.gerrit
Joshua Hesketh5fea8672013-08-19 17:32:01 +100032import zuul.reporter.smtp
James E. Blairb0fcae42012-07-17 11:12:10 -070033
Clark Boylanb640e052014-04-03 16:41:46 -070034from tests.base import ZuulTestCase, repack_repo
James E. Blairb0fcae42012-07-17 11:12:10 -070035
James E. Blair1f4c2bb2013-04-26 08:40:46 -070036logging.basicConfig(level=logging.DEBUG,
37 format='%(asctime)s %(name)-32s '
38 '%(levelname)-8s %(message)s')
James E. Blairb0fcae42012-07-17 11:12:10 -070039
40
Clark Boylanb640e052014-04-03 16:41:46 -070041class TestScheduler(ZuulTestCase):
Antoine Mussobd86a312014-01-08 14:51:33 +010042
James E. Blairb0fcae42012-07-17 11:12:10 -070043 def test_jobs_launched(self):
44 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070045
James E. Blairb0fcae42012-07-17 11:12:10 -070046 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -070047 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -070048 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
49 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040050 self.assertEqual(self.getJobFromHistory('project-merge').result,
51 'SUCCESS')
52 self.assertEqual(self.getJobFromHistory('project-test1').result,
53 'SUCCESS')
54 self.assertEqual(self.getJobFromHistory('project-test2').result,
55 'SUCCESS')
56 self.assertEqual(A.data['status'], 'MERGED')
57 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -070058
James E. Blair66eeebf2013-07-27 17:44:32 -070059 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
60 self.assertReportedStat('zuul.pipeline.gate.current_changes',
61 value='1|g')
62 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
63 kind='ms')
64 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
65 value='1|c')
66 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
67 self.assertReportedStat('zuul.pipeline.gate.total_changes',
68 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070069 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070070 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -070071 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070072 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070073
James E. Blair3cb10702013-08-24 08:56:03 -070074 def test_initial_pipeline_gauges(self):
75 "Test that each pipeline reported its length on start"
76 pipeline_names = self.sched.layout.pipelines.keys()
77 self.assertNotEqual(len(pipeline_names), 0)
78 for name in pipeline_names:
79 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
80 value='0|g')
81
James E. Blair42f74822013-05-14 15:18:03 -070082 def test_duplicate_pipelines(self):
83 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -070084
James E. Blair42f74822013-05-14 15:18:03 -070085 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
86 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
87 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -070088
Monty Taylor98f0f3e2013-07-06 16:02:31 -040089 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -040090 self.history[0].name == 'project-test1'
91 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -070092
Monty Taylor98f0f3e2013-07-06 16:02:31 -040093 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -070094 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -040095 self.assertIn('dup1/project-test1', A.messages[0])
96 self.assertNotIn('dup2/project-test1', A.messages[0])
97 self.assertNotIn('dup1/project-test1', A.messages[1])
98 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -070099 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400100 self.assertIn('dup1/project-test1', A.messages[1])
101 self.assertNotIn('dup2/project-test1', A.messages[1])
102 self.assertNotIn('dup1/project-test1', A.messages[0])
103 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -0700104
James E. Blairb0fcae42012-07-17 11:12:10 -0700105 def test_parallel_changes(self):
106 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700107
108 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700109 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
110 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
111 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700112 A.addApproval('CRVW', 2)
113 B.addApproval('CRVW', 2)
114 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700115
116 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
117 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
118 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
119
120 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400121 self.assertEqual(len(self.builds), 1)
122 self.assertEqual(self.builds[0].name, 'project-merge')
123 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700124
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700125 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700126 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400127 self.assertEqual(len(self.builds), 3)
128 self.assertEqual(self.builds[0].name, 'project-test1')
129 self.assertTrue(self.job_has_changes(self.builds[0], A))
130 self.assertEqual(self.builds[1].name, 'project-test2')
131 self.assertTrue(self.job_has_changes(self.builds[1], A))
132 self.assertEqual(self.builds[2].name, 'project-merge')
133 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700134
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700135 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700136 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400137 self.assertEqual(len(self.builds), 5)
138 self.assertEqual(self.builds[0].name, 'project-test1')
139 self.assertTrue(self.job_has_changes(self.builds[0], A))
140 self.assertEqual(self.builds[1].name, 'project-test2')
141 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700142
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400143 self.assertEqual(self.builds[2].name, 'project-test1')
144 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
145 self.assertEqual(self.builds[3].name, 'project-test2')
146 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700147
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400148 self.assertEqual(self.builds[4].name, 'project-merge')
149 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700150
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700151 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700152 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400153 self.assertEqual(len(self.builds), 6)
154 self.assertEqual(self.builds[0].name, 'project-test1')
155 self.assertTrue(self.job_has_changes(self.builds[0], A))
156 self.assertEqual(self.builds[1].name, 'project-test2')
157 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700158
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400159 self.assertEqual(self.builds[2].name, 'project-test1')
160 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
161 self.assertEqual(self.builds[3].name, 'project-test2')
162 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700163
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400164 self.assertEqual(self.builds[4].name, 'project-test1')
165 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
166 self.assertEqual(self.builds[5].name, 'project-test2')
167 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700168
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700169 self.worker.hold_jobs_in_build = False
170 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700171 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400172 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700173
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400174 self.assertEqual(len(self.history), 9)
175 self.assertEqual(A.data['status'], 'MERGED')
176 self.assertEqual(B.data['status'], 'MERGED')
177 self.assertEqual(C.data['status'], 'MERGED')
178 self.assertEqual(A.reported, 2)
179 self.assertEqual(B.reported, 2)
180 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700181
182 def test_failed_changes(self):
183 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -0400184 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700185
James E. Blairb02a3bb2012-07-30 17:49:55 -0700186 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
187 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -0700188 A.addApproval('CRVW', 2)
189 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700190
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700191 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700192
James E. Blaire2819012013-06-28 17:17:26 -0400193 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
194 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700195 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -0400196
197 self.worker.release('.*-merge')
198 self.waitUntilSettled()
199
200 self.worker.hold_jobs_in_build = False
201 self.worker.release()
202
203 self.waitUntilSettled()
204 # It's certain that the merge job for change 2 will run, but
205 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400206 self.assertTrue(len(self.history) > 6)
207 self.assertEqual(A.data['status'], 'NEW')
208 self.assertEqual(B.data['status'], 'MERGED')
209 self.assertEqual(A.reported, 2)
210 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700211
212 def test_independent_queues(self):
213 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700214
215 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900216 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700217 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
218 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700219 A.addApproval('CRVW', 2)
220 B.addApproval('CRVW', 2)
221 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700222
223 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
224 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
225 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
226
James E. Blairb02a3bb2012-07-30 17:49:55 -0700227 self.waitUntilSettled()
228
229 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400230 self.assertEqual(len(self.builds), 2)
231 self.assertEqual(self.builds[0].name, 'project-merge')
232 self.assertTrue(self.job_has_changes(self.builds[0], A))
233 self.assertEqual(self.builds[1].name, 'project1-merge')
234 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700235
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700236 # Release the current merge builds
237 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700238 self.waitUntilSettled()
239 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700240 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700241 self.waitUntilSettled()
242
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700243 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -0700244 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400245 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700246
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700247 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700248 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400249 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700250
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400251 self.assertEqual(len(self.history), 11)
252 self.assertEqual(A.data['status'], 'MERGED')
253 self.assertEqual(B.data['status'], 'MERGED')
254 self.assertEqual(C.data['status'], 'MERGED')
255 self.assertEqual(A.reported, 2)
256 self.assertEqual(B.reported, 2)
257 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700258
259 def test_failed_change_at_head(self):
260 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700261
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700262 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700263 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
264 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
265 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700266 A.addApproval('CRVW', 2)
267 B.addApproval('CRVW', 2)
268 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700269
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700270 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -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
276 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700277
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400278 self.assertEqual(len(self.builds), 1)
279 self.assertEqual(self.builds[0].name, 'project-merge')
280 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -0700281
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700282 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700283 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700284 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700285 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700286 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700287 self.waitUntilSettled()
288
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400289 self.assertEqual(len(self.builds), 6)
290 self.assertEqual(self.builds[0].name, 'project-test1')
291 self.assertEqual(self.builds[1].name, 'project-test2')
292 self.assertEqual(self.builds[2].name, 'project-test1')
293 self.assertEqual(self.builds[3].name, 'project-test2')
294 self.assertEqual(self.builds[4].name, 'project-test1')
295 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700296
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400297 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700298 self.waitUntilSettled()
299
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400300 # project-test2, project-merge for B
301 self.assertEqual(len(self.builds), 2)
302 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -0700303
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700304 self.worker.hold_jobs_in_build = False
305 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700306 self.waitUntilSettled()
307
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400308 self.assertEqual(len(self.builds), 0)
309 self.assertEqual(len(self.history), 15)
310 self.assertEqual(A.data['status'], 'NEW')
311 self.assertEqual(B.data['status'], 'MERGED')
312 self.assertEqual(C.data['status'], 'MERGED')
313 self.assertEqual(A.reported, 2)
314 self.assertEqual(B.reported, 2)
315 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700316
James E. Blair0aac4872013-08-23 14:02:38 -0700317 def test_failed_change_in_middle(self):
318 "Test a failed change in the middle of the queue"
319
320 self.worker.hold_jobs_in_build = True
321 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
322 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
323 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
324 A.addApproval('CRVW', 2)
325 B.addApproval('CRVW', 2)
326 C.addApproval('CRVW', 2)
327
328 self.worker.addFailTest('project-test1', B)
329
330 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
331 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
332 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
333
334 self.waitUntilSettled()
335
336 self.worker.release('.*-merge')
337 self.waitUntilSettled()
338 self.worker.release('.*-merge')
339 self.waitUntilSettled()
340 self.worker.release('.*-merge')
341 self.waitUntilSettled()
342
343 self.assertEqual(len(self.builds), 6)
344 self.assertEqual(self.builds[0].name, 'project-test1')
345 self.assertEqual(self.builds[1].name, 'project-test2')
346 self.assertEqual(self.builds[2].name, 'project-test1')
347 self.assertEqual(self.builds[3].name, 'project-test2')
348 self.assertEqual(self.builds[4].name, 'project-test1')
349 self.assertEqual(self.builds[5].name, 'project-test2')
350
351 self.release(self.builds[2])
352 self.waitUntilSettled()
353
James E. Blair972e3c72013-08-29 12:04:55 -0700354 # project-test1 and project-test2 for A
355 # project-test2 for B
356 # project-merge for C (without B)
357 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700358 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
359
James E. Blair972e3c72013-08-29 12:04:55 -0700360 self.worker.release('.*-merge')
361 self.waitUntilSettled()
362
363 # project-test1 and project-test2 for A
364 # project-test2 for B
365 # project-test1 and project-test2 for C
366 self.assertEqual(len(self.builds), 5)
367
James E. Blair0aac4872013-08-23 14:02:38 -0700368 items = self.sched.layout.pipelines['gate'].getAllItems()
369 builds = items[0].current_build_set.getBuilds()
370 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
371 self.assertEqual(self.countJobResults(builds, None), 2)
372 builds = items[1].current_build_set.getBuilds()
373 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
374 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
375 self.assertEqual(self.countJobResults(builds, None), 1)
376 builds = items[2].current_build_set.getBuilds()
377 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700378 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700379
380 self.worker.hold_jobs_in_build = False
381 self.worker.release()
382 self.waitUntilSettled()
383
384 self.assertEqual(len(self.builds), 0)
385 self.assertEqual(len(self.history), 12)
386 self.assertEqual(A.data['status'], 'MERGED')
387 self.assertEqual(B.data['status'], 'NEW')
388 self.assertEqual(C.data['status'], 'MERGED')
389 self.assertEqual(A.reported, 2)
390 self.assertEqual(B.reported, 2)
391 self.assertEqual(C.reported, 2)
392
James E. Blaird466dc42012-07-31 10:42:56 -0700393 def test_failed_change_at_head_with_queue(self):
394 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700395
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700396 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700397 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
398 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
399 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700400 A.addApproval('CRVW', 2)
401 B.addApproval('CRVW', 2)
402 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700403
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700404 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700405
406 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
407 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
408 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
409
410 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700411 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400412 self.assertEqual(len(self.builds), 0)
413 self.assertEqual(len(queue), 1)
414 self.assertEqual(queue[0].name, 'build:project-merge')
415 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -0700416
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700417 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700418 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700419 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700420 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700421 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700422 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700423 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700424
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400425 self.assertEqual(len(self.builds), 0)
426 self.assertEqual(len(queue), 6)
427 self.assertEqual(queue[0].name, 'build:project-test1')
428 self.assertEqual(queue[1].name, 'build:project-test2')
429 self.assertEqual(queue[2].name, 'build:project-test1')
430 self.assertEqual(queue[3].name, 'build:project-test2')
431 self.assertEqual(queue[4].name, 'build:project-test1')
432 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700433
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700434 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700435 self.waitUntilSettled()
436
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400437 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700438 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400439 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
440 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700441
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700442 self.gearman_server.hold_jobs_in_queue = False
443 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700444 self.waitUntilSettled()
445
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400446 self.assertEqual(len(self.builds), 0)
447 self.assertEqual(len(self.history), 11)
448 self.assertEqual(A.data['status'], 'NEW')
449 self.assertEqual(B.data['status'], 'MERGED')
450 self.assertEqual(C.data['status'], 'MERGED')
451 self.assertEqual(A.reported, 2)
452 self.assertEqual(B.reported, 2)
453 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700454
James E. Blairfef71632013-09-23 11:15:47 -0700455 def test_two_failed_changes_at_head(self):
456 "Test that changes are reparented correctly if 2 fail at head"
457
458 self.worker.hold_jobs_in_build = True
459 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
460 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
461 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
462 A.addApproval('CRVW', 2)
463 B.addApproval('CRVW', 2)
464 C.addApproval('CRVW', 2)
465
466 self.worker.addFailTest('project-test1', A)
467 self.worker.addFailTest('project-test1', B)
468
469 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
470 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
471 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
472 self.waitUntilSettled()
473
474 self.worker.release('.*-merge')
475 self.waitUntilSettled()
476 self.worker.release('.*-merge')
477 self.waitUntilSettled()
478 self.worker.release('.*-merge')
479 self.waitUntilSettled()
480
481 self.assertEqual(len(self.builds), 6)
482 self.assertEqual(self.builds[0].name, 'project-test1')
483 self.assertEqual(self.builds[1].name, 'project-test2')
484 self.assertEqual(self.builds[2].name, 'project-test1')
485 self.assertEqual(self.builds[3].name, 'project-test2')
486 self.assertEqual(self.builds[4].name, 'project-test1')
487 self.assertEqual(self.builds[5].name, 'project-test2')
488
489 self.assertTrue(self.job_has_changes(self.builds[0], A))
490 self.assertTrue(self.job_has_changes(self.builds[2], A))
491 self.assertTrue(self.job_has_changes(self.builds[2], B))
492 self.assertTrue(self.job_has_changes(self.builds[4], A))
493 self.assertTrue(self.job_has_changes(self.builds[4], B))
494 self.assertTrue(self.job_has_changes(self.builds[4], C))
495
496 # Fail change B first
497 self.release(self.builds[2])
498 self.waitUntilSettled()
499
500 # restart of C after B failure
501 self.worker.release('.*-merge')
502 self.waitUntilSettled()
503
504 self.assertEqual(len(self.builds), 5)
505 self.assertEqual(self.builds[0].name, 'project-test1')
506 self.assertEqual(self.builds[1].name, 'project-test2')
507 self.assertEqual(self.builds[2].name, 'project-test2')
508 self.assertEqual(self.builds[3].name, 'project-test1')
509 self.assertEqual(self.builds[4].name, 'project-test2')
510
511 self.assertTrue(self.job_has_changes(self.builds[1], A))
512 self.assertTrue(self.job_has_changes(self.builds[2], A))
513 self.assertTrue(self.job_has_changes(self.builds[2], B))
514 self.assertTrue(self.job_has_changes(self.builds[4], A))
515 self.assertFalse(self.job_has_changes(self.builds[4], B))
516 self.assertTrue(self.job_has_changes(self.builds[4], C))
517
518 # Finish running all passing jobs for change A
519 self.release(self.builds[1])
520 self.waitUntilSettled()
521 # Fail and report change A
522 self.release(self.builds[0])
523 self.waitUntilSettled()
524
525 # restart of B,C after A failure
526 self.worker.release('.*-merge')
527 self.waitUntilSettled()
528 self.worker.release('.*-merge')
529 self.waitUntilSettled()
530
531 self.assertEqual(len(self.builds), 4)
532 self.assertEqual(self.builds[0].name, 'project-test1') # B
533 self.assertEqual(self.builds[1].name, 'project-test2') # B
534 self.assertEqual(self.builds[2].name, 'project-test1') # C
535 self.assertEqual(self.builds[3].name, 'project-test2') # C
536
537 self.assertFalse(self.job_has_changes(self.builds[1], A))
538 self.assertTrue(self.job_has_changes(self.builds[1], B))
539 self.assertFalse(self.job_has_changes(self.builds[1], C))
540
541 self.assertFalse(self.job_has_changes(self.builds[2], A))
542 # After A failed and B and C restarted, B should be back in
543 # C's tests because it has not failed yet.
544 self.assertTrue(self.job_has_changes(self.builds[2], B))
545 self.assertTrue(self.job_has_changes(self.builds[2], C))
546
547 self.worker.hold_jobs_in_build = False
548 self.worker.release()
549 self.waitUntilSettled()
550
551 self.assertEqual(len(self.builds), 0)
552 self.assertEqual(len(self.history), 21)
553 self.assertEqual(A.data['status'], 'NEW')
554 self.assertEqual(B.data['status'], 'NEW')
555 self.assertEqual(C.data['status'], 'MERGED')
556 self.assertEqual(A.reported, 2)
557 self.assertEqual(B.reported, 2)
558 self.assertEqual(C.reported, 2)
559
James E. Blair8c803f82012-07-31 16:25:42 -0700560 def test_patch_order(self):
561 "Test that dependent patches are tested in the right order"
562 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
563 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
564 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
565 A.addApproval('CRVW', 2)
566 B.addApproval('CRVW', 2)
567 C.addApproval('CRVW', 2)
568
569 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
570 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
571 M2.setMerged()
572 M1.setMerged()
573
574 # C -> B -> A -> M1 -> M2
575 # M2 is here to make sure it is never queried. If it is, it
576 # means zuul is walking down the entire history of merged
577 # changes.
578
579 C.setDependsOn(B, 1)
580 B.setDependsOn(A, 1)
581 A.setDependsOn(M1, 1)
582 M1.setDependsOn(M2, 1)
583
584 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
585
586 self.waitUntilSettled()
587
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400588 self.assertEqual(A.data['status'], 'NEW')
589 self.assertEqual(B.data['status'], 'NEW')
590 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700591
592 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
593 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
594
595 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400596 self.assertEqual(M2.queried, 0)
597 self.assertEqual(A.data['status'], 'MERGED')
598 self.assertEqual(B.data['status'], 'MERGED')
599 self.assertEqual(C.data['status'], 'MERGED')
600 self.assertEqual(A.reported, 2)
601 self.assertEqual(B.reported, 2)
602 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700603
James E. Blair0e933c52013-07-11 10:18:52 -0700604 def test_trigger_cache(self):
605 "Test that the trigger cache operates correctly"
606 self.worker.hold_jobs_in_build = True
607
608 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
609 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
610 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
611 A.addApproval('CRVW', 2)
612 B.addApproval('CRVW', 2)
613
614 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
615 M1.setMerged()
616
617 B.setDependsOn(A, 1)
618 A.setDependsOn(M1, 1)
619
620 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
621 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
622
623 self.waitUntilSettled()
624
625 for build in self.builds:
626 if build.parameters['ZUUL_PIPELINE'] == 'check':
627 build.release()
628 self.waitUntilSettled()
629 for build in self.builds:
630 if build.parameters['ZUUL_PIPELINE'] == 'check':
631 build.release()
632 self.waitUntilSettled()
633
634 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
635 self.waitUntilSettled()
636
Antoine Mussof0506fa2014-06-03 15:03:38 +0200637 self.log.debug("len %s" % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700638 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -0700639 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700640
641 self.worker.hold_jobs_in_build = False
642 self.worker.release()
643 self.waitUntilSettled()
644
645 self.assertEqual(A.data['status'], 'MERGED')
646 self.assertEqual(B.data['status'], 'MERGED')
647 self.assertEqual(A.queried, 2) # Initial and isMerged
648 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
649
James E. Blair8c803f82012-07-31 16:25:42 -0700650 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700651 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700652 # TODO: move to test_gerrit (this is a unit test!)
653 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairc0dedf82014-08-06 09:37:52 -0700654 source = self.sched.layout.pipelines['gate'].source
655 a = source._getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -0400656 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700657 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700658
659 A.addApproval('CRVW', 2)
James E. Blairc0dedf82014-08-06 09:37:52 -0700660 a = source._getChange(1, 2, refresh=True)
661 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700662
663 A.addApproval('APRV', 1)
James E. Blairc0dedf82014-08-06 09:37:52 -0700664 a = source._getChange(1, 2, refresh=True)
665 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
666 source.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -0700667
668 def test_build_configuration(self):
669 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700670
671 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -0700672 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
673 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
674 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
675 A.addApproval('CRVW', 2)
676 B.addApproval('CRVW', 2)
677 C.addApproval('CRVW', 2)
678 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
679 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
680 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
681 self.waitUntilSettled()
682
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700683 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -0700684 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700685 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -0700686 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700687 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -0700688 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700689 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700690 ref = self.getParameter(queue[-1], 'ZUUL_REF')
691 self.gearman_server.hold_jobs_in_queue = False
692 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700693 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -0700694
Monty Taylorbc758832013-06-17 17:22:42 -0400695 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -0700696 repo = git.Repo(path)
697 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
698 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -0700699 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400700 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -0700701
702 def test_build_configuration_conflict(self):
703 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700704
705 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -0700706 A = self.fake_gerrit.addFakeChange('org/conflict-project',
707 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -0700708 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700709 B = self.fake_gerrit.addFakeChange('org/conflict-project',
710 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -0700711 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700712 C = self.fake_gerrit.addFakeChange('org/conflict-project',
713 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -0700714 A.addApproval('CRVW', 2)
715 B.addApproval('CRVW', 2)
716 C.addApproval('CRVW', 2)
717 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
718 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
719 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
720 self.waitUntilSettled()
721
James E. Blair6736beb2013-07-11 15:18:15 -0700722 self.assertEqual(A.reported, 1)
723 self.assertEqual(B.reported, 1)
724 self.assertEqual(C.reported, 1)
725
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700726 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700727 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700728 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700729 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700730 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700731 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700732
733 self.assertEqual(len(self.history), 2) # A and C merge jobs
734
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700735 self.gearman_server.hold_jobs_in_queue = False
736 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700737 self.waitUntilSettled()
738
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400739 self.assertEqual(A.data['status'], 'MERGED')
740 self.assertEqual(B.data['status'], 'NEW')
741 self.assertEqual(C.data['status'], 'MERGED')
742 self.assertEqual(A.reported, 2)
743 self.assertEqual(B.reported, 2)
744 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -0700745 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -0700746
James E. Blairdaabed22012-08-15 15:38:57 -0700747 def test_post(self):
748 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700749
Zhongyue Luo5d556072012-09-21 02:00:47 +0900750 e = {
751 "type": "ref-updated",
752 "submitter": {
753 "name": "User Name",
754 },
755 "refUpdate": {
756 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
757 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
758 "refName": "master",
759 "project": "org/project",
760 }
761 }
James E. Blairdaabed22012-08-15 15:38:57 -0700762 self.fake_gerrit.addEvent(e)
763 self.waitUntilSettled()
764
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400765 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400766 self.assertEqual(len(self.history), 1)
767 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -0700768
769 def test_build_configuration_branch(self):
770 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700771
772 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -0700773 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
774 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
775 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
776 A.addApproval('CRVW', 2)
777 B.addApproval('CRVW', 2)
778 C.addApproval('CRVW', 2)
779 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
780 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
781 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
782 self.waitUntilSettled()
783
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700784 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -0700785 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700786 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -0700787 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700788 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -0700789 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700790 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700791 ref = self.getParameter(queue[-1], 'ZUUL_REF')
792 self.gearman_server.hold_jobs_in_queue = False
793 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -0700794 self.waitUntilSettled()
795
Monty Taylorbc758832013-06-17 17:22:42 -0400796 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -0700797 repo = git.Repo(path)
798 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
799 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -0700800 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400801 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -0700802
803 def test_build_configuration_branch_interaction(self):
804 "Test that switching between branches works"
805 self.test_build_configuration()
806 self.test_build_configuration_branch()
807 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -0400808 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -0700809 repo = git.Repo(path)
810 repo.heads.master.commit = repo.commit('init')
811 self.test_build_configuration()
812
813 def test_build_configuration_multi_branch(self):
814 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700815
816 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -0700817 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
818 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
819 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
820 A.addApproval('CRVW', 2)
821 B.addApproval('CRVW', 2)
822 C.addApproval('CRVW', 2)
823 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
824 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
825 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
826 self.waitUntilSettled()
James E. Blairbb1fe502014-03-04 10:15:06 -0800827 queue = self.gearman_server.getQueue()
828 job_A = None
829 for job in queue:
830 if 'project-merge' in job.name:
831 job_A = job
832 ref_A = self.getParameter(job_A, 'ZUUL_REF')
833 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
834 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
835 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
James E. Blairc6294a52012-08-17 10:19:48 -0700836
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700837 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -0700838 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700839 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -0700840 job_B = None
841 for job in queue:
842 if 'project-merge' in job.name:
843 job_B = job
844 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -0800845 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -0700846 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blairbb1fe502014-03-04 10:15:06 -0800847 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
848
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700849 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -0700850 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700851 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -0700852 for job in queue:
853 if 'project-merge' in job.name:
854 job_C = job
855 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -0800856 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -0700857 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blairbb1fe502014-03-04 10:15:06 -0800858 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700859 self.gearman_server.hold_jobs_in_queue = False
860 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -0700861 self.waitUntilSettled()
862
Monty Taylorbc758832013-06-17 17:22:42 -0400863 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -0700864 repo = git.Repo(path)
865
866 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -0700867 for c in repo.iter_commits(ref_C)]
James E. Blairbb1fe502014-03-04 10:15:06 -0800868 repo_shas = [c.hexsha for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -0700869 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -0700870 correct_messages = ['initial commit', 'A-1', 'C-1']
James E. Blairbb1fe502014-03-04 10:15:06 -0800871 # Ensure the right commits are in the history for this ref
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400872 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -0800873 # Ensure ZUUL_REF -> ZUUL_COMMIT
874 self.assertEqual(repo_shas[0], commit_C)
James E. Blairc6294a52012-08-17 10:19:48 -0700875
876 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -0700877 for c in repo.iter_commits(ref_B)]
James E. Blairbb1fe502014-03-04 10:15:06 -0800878 repo_shas = [c.hexsha for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -0700879 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -0700880 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400881 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -0800882 self.assertEqual(repo_shas[0], commit_B)
883
884 repo_messages = [c.message.strip()
885 for c in repo.iter_commits(ref_A)]
886 repo_shas = [c.hexsha for c in repo.iter_commits(ref_A)]
887 repo_messages.reverse()
888 correct_messages = ['initial commit', 'A-1']
889 self.assertEqual(repo_messages, correct_messages)
890 self.assertEqual(repo_shas[0], commit_A)
891
892 self.assertNotEqual(ref_A, ref_B, ref_C)
893 self.assertNotEqual(commit_A, commit_B, commit_C)
James E. Blair7f71c802012-08-22 13:04:32 -0700894
895 def test_one_job_project(self):
896 "Test that queueing works with one job"
897 A = self.fake_gerrit.addFakeChange('org/one-job-project',
898 'master', 'A')
899 B = self.fake_gerrit.addFakeChange('org/one-job-project',
900 'master', 'B')
901 A.addApproval('CRVW', 2)
902 B.addApproval('CRVW', 2)
903 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
904 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
905 self.waitUntilSettled()
906
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400907 self.assertEqual(A.data['status'], 'MERGED')
908 self.assertEqual(A.reported, 2)
909 self.assertEqual(B.data['status'], 'MERGED')
910 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -0700911
Antoine Musso80edd5a2013-02-13 15:37:53 +0100912 def test_job_from_templates_launched(self):
913 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700914
Antoine Musso80edd5a2013-02-13 15:37:53 +0100915 A = self.fake_gerrit.addFakeChange(
916 'org/templated-project', 'master', 'A')
917 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
918 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +0100919
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400920 self.assertEqual(self.getJobFromHistory('project-test1').result,
921 'SUCCESS')
922 self.assertEqual(self.getJobFromHistory('project-test2').result,
923 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +0100924
James E. Blair3e98c022013-12-16 15:25:38 -0800925 def test_layered_templates(self):
926 "Test whether a job generated via a template can be launched"
927
928 A = self.fake_gerrit.addFakeChange(
929 'org/layered-project', 'master', 'A')
930 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
931 self.waitUntilSettled()
932
933 self.assertEqual(self.getJobFromHistory('project-test1').result,
934 'SUCCESS')
935 self.assertEqual(self.getJobFromHistory('project-test2').result,
936 'SUCCESS')
James E. Blairaea6cf62013-12-16 15:38:12 -0800937 self.assertEqual(self.getJobFromHistory('layered-project-test3'
938 ).result, 'SUCCESS')
939 self.assertEqual(self.getJobFromHistory('layered-project-test4'
940 ).result, 'SUCCESS')
James E. Blair12a92b12014-03-26 11:54:53 -0700941 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
942 ).result, 'SUCCESS')
James E. Blair3e98c022013-12-16 15:25:38 -0800943 self.assertEqual(self.getJobFromHistory('project-test6').result,
944 'SUCCESS')
945
James E. Blaircaec0c52012-08-22 14:52:22 -0700946 def test_dependent_changes_dequeue(self):
947 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700948
James E. Blaircaec0c52012-08-22 14:52:22 -0700949 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
950 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
951 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
952 A.addApproval('CRVW', 2)
953 B.addApproval('CRVW', 2)
954 C.addApproval('CRVW', 2)
955
956 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
957 M1.setMerged()
958
959 # C -> B -> A -> M1
960
961 C.setDependsOn(B, 1)
962 B.setDependsOn(A, 1)
963 A.setDependsOn(M1, 1)
964
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700965 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -0700966
967 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
968 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
969 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
970
971 self.waitUntilSettled()
972
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400973 self.assertEqual(A.data['status'], 'NEW')
974 self.assertEqual(A.reported, 2)
975 self.assertEqual(B.data['status'], 'NEW')
976 self.assertEqual(B.reported, 2)
977 self.assertEqual(C.data['status'], 'NEW')
978 self.assertEqual(C.reported, 2)
979 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -0700980
James E. Blair972e3c72013-08-29 12:04:55 -0700981 def test_failing_dependent_changes(self):
982 "Test that failing dependent patches are taken out of stream"
983 self.worker.hold_jobs_in_build = True
984 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
985 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
986 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
987 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
988 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
989 A.addApproval('CRVW', 2)
990 B.addApproval('CRVW', 2)
991 C.addApproval('CRVW', 2)
992 D.addApproval('CRVW', 2)
993 E.addApproval('CRVW', 2)
994
995 # E, D -> C -> B, A
996
997 D.setDependsOn(C, 1)
998 C.setDependsOn(B, 1)
999
1000 self.worker.addFailTest('project-test1', B)
1001
1002 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1003 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1004 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1005 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1006 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1007
1008 self.waitUntilSettled()
1009 self.worker.release('.*-merge')
1010 self.waitUntilSettled()
1011 self.worker.release('.*-merge')
1012 self.waitUntilSettled()
1013 self.worker.release('.*-merge')
1014 self.waitUntilSettled()
1015 self.worker.release('.*-merge')
1016 self.waitUntilSettled()
1017 self.worker.release('.*-merge')
1018 self.waitUntilSettled()
1019
1020 self.worker.hold_jobs_in_build = False
1021 for build in self.builds:
1022 if build.parameters['ZUUL_CHANGE'] != '1':
1023 build.release()
1024 self.waitUntilSettled()
1025
1026 self.worker.release()
1027 self.waitUntilSettled()
1028
1029 self.assertEqual(A.data['status'], 'MERGED')
1030 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001031 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001032 self.assertEqual(B.data['status'], 'NEW')
1033 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001034 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001035 self.assertEqual(C.data['status'], 'NEW')
1036 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001037 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001038 self.assertEqual(D.data['status'], 'NEW')
1039 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001040 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001041 self.assertEqual(E.data['status'], 'MERGED')
1042 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001043 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001044 self.assertEqual(len(self.history), 18)
1045
James E. Blairec590122012-08-22 15:19:31 -07001046 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001047 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001048 # If it's dequeued more than once, we should see extra
1049 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001050
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001051 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001052 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1053 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1054 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1055 A.addApproval('CRVW', 2)
1056 B.addApproval('CRVW', 2)
1057 C.addApproval('CRVW', 2)
1058
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001059 self.worker.addFailTest('project1-test1', A)
1060 self.worker.addFailTest('project1-test2', A)
1061 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001062
1063 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1064 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1065 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1066
1067 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001068
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001069 self.assertEqual(len(self.builds), 1)
1070 self.assertEqual(self.builds[0].name, 'project1-merge')
1071 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07001072
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001073 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001074 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001075 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001076 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001077 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001078 self.waitUntilSettled()
1079
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001080 self.assertEqual(len(self.builds), 9)
1081 self.assertEqual(self.builds[0].name, 'project1-test1')
1082 self.assertEqual(self.builds[1].name, 'project1-test2')
1083 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
1084 self.assertEqual(self.builds[3].name, 'project1-test1')
1085 self.assertEqual(self.builds[4].name, 'project1-test2')
1086 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
1087 self.assertEqual(self.builds[6].name, 'project1-test1')
1088 self.assertEqual(self.builds[7].name, 'project1-test2')
1089 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001090
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001091 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001092 self.waitUntilSettled()
1093
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001094 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1095 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001096
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001097 self.worker.hold_jobs_in_build = False
1098 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001099 self.waitUntilSettled()
1100
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001101 self.assertEqual(len(self.builds), 0)
1102 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001103
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001104 self.assertEqual(A.data['status'], 'NEW')
1105 self.assertEqual(B.data['status'], 'MERGED')
1106 self.assertEqual(C.data['status'], 'MERGED')
1107 self.assertEqual(A.reported, 2)
1108 self.assertEqual(B.reported, 2)
1109 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001110
1111 def test_nonvoting_job(self):
1112 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001113
James E. Blair4ec821f2012-08-23 15:28:28 -07001114 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1115 'master', 'A')
1116 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001117 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001118 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1119
1120 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001121
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001122 self.assertEqual(A.data['status'], 'MERGED')
1123 self.assertEqual(A.reported, 2)
1124 self.assertEqual(
1125 self.getJobFromHistory('nonvoting-project-merge').result,
1126 'SUCCESS')
1127 self.assertEqual(
1128 self.getJobFromHistory('nonvoting-project-test1').result,
1129 'SUCCESS')
1130 self.assertEqual(
1131 self.getJobFromHistory('nonvoting-project-test2').result,
1132 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001133
1134 def test_check_queue_success(self):
1135 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001136
James E. Blaire0487072012-08-29 17:38:31 -07001137 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1138 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1139
1140 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001141
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001142 self.assertEqual(A.data['status'], 'NEW')
1143 self.assertEqual(A.reported, 1)
1144 self.assertEqual(self.getJobFromHistory('project-merge').result,
1145 'SUCCESS')
1146 self.assertEqual(self.getJobFromHistory('project-test1').result,
1147 'SUCCESS')
1148 self.assertEqual(self.getJobFromHistory('project-test2').result,
1149 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001150
1151 def test_check_queue_failure(self):
1152 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001153
James E. Blaire0487072012-08-29 17:38:31 -07001154 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001155 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001156 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1157
1158 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001159
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001160 self.assertEqual(A.data['status'], 'NEW')
1161 self.assertEqual(A.reported, 1)
1162 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001163 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001164 self.assertEqual(self.getJobFromHistory('project-test1').result,
1165 'SUCCESS')
1166 self.assertEqual(self.getJobFromHistory('project-test2').result,
1167 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001168
1169 def test_dependent_behind_dequeue(self):
1170 "test that dependent changes behind dequeued changes work"
1171 # This complicated test is a reproduction of a real life bug
1172 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001173
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001174 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001175 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1176 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1177 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1178 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1179 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1180 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1181 D.setDependsOn(C, 1)
1182 E.setDependsOn(D, 1)
1183 A.addApproval('CRVW', 2)
1184 B.addApproval('CRVW', 2)
1185 C.addApproval('CRVW', 2)
1186 D.addApproval('CRVW', 2)
1187 E.addApproval('CRVW', 2)
1188 F.addApproval('CRVW', 2)
1189
1190 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001191
1192 # Change object re-use in the gerrit trigger is hidden if
1193 # changes are added in quick succession; waiting makes it more
1194 # like real life.
1195 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1196 self.waitUntilSettled()
1197 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1198 self.waitUntilSettled()
1199
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001200 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001201 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001202 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001203 self.waitUntilSettled()
1204
1205 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1206 self.waitUntilSettled()
1207 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1208 self.waitUntilSettled()
1209 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1210 self.waitUntilSettled()
1211 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1212 self.waitUntilSettled()
1213
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001214 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001215 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001216 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001217 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001218 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001219 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001220 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001221 self.waitUntilSettled()
1222
1223 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001224
1225 # Grab pointers to the jobs we want to release before
1226 # releasing any, because list indexes may change as
1227 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001228 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001229 a.release()
1230 b.release()
1231 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001232 self.waitUntilSettled()
1233
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001234 self.worker.hold_jobs_in_build = False
1235 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07001236 self.waitUntilSettled()
1237
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001238 self.assertEqual(A.data['status'], 'NEW')
1239 self.assertEqual(B.data['status'], 'MERGED')
1240 self.assertEqual(C.data['status'], 'MERGED')
1241 self.assertEqual(D.data['status'], 'MERGED')
1242 self.assertEqual(E.data['status'], 'MERGED')
1243 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001244
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001245 self.assertEqual(A.reported, 2)
1246 self.assertEqual(B.reported, 2)
1247 self.assertEqual(C.reported, 2)
1248 self.assertEqual(D.reported, 2)
1249 self.assertEqual(E.reported, 2)
1250 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001251
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001252 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1253 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001254
1255 def test_merger_repack(self):
1256 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001257
James E. Blair05fed602012-09-07 12:45:24 -07001258 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1259 A.addApproval('CRVW', 2)
1260 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1261 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001262 self.assertEqual(self.getJobFromHistory('project-merge').result,
1263 'SUCCESS')
1264 self.assertEqual(self.getJobFromHistory('project-test1').result,
1265 'SUCCESS')
1266 self.assertEqual(self.getJobFromHistory('project-test2').result,
1267 'SUCCESS')
1268 self.assertEqual(A.data['status'], 'MERGED')
1269 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001270 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07001271 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001272
Monty Taylorbc758832013-06-17 17:22:42 -04001273 path = os.path.join(self.git_root, "org/project")
1274 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001275
1276 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1277 A.addApproval('CRVW', 2)
1278 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1279 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001280 self.assertEqual(self.getJobFromHistory('project-merge').result,
1281 'SUCCESS')
1282 self.assertEqual(self.getJobFromHistory('project-test1').result,
1283 'SUCCESS')
1284 self.assertEqual(self.getJobFromHistory('project-test2').result,
1285 'SUCCESS')
1286 self.assertEqual(A.data['status'], 'MERGED')
1287 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001288
James E. Blair4886f282012-11-15 09:27:33 -08001289 def test_merger_repack_large_change(self):
1290 "Test that the merger works with large changes after a repack"
1291 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001292 # This test assumes the repo is already cloned; make sure it is
1293 url = self.sched.triggers['gerrit'].getGitUrl(
1294 self.sched.layout.projects['org/project1'])
James E. Blair4076e2b2014-01-28 12:42:20 -08001295 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08001296 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1297 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001298 path = os.path.join(self.upstream_root, "org/project1")
1299 print repack_repo(path)
1300 path = os.path.join(self.git_root, "org/project1")
1301 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001302
1303 A.addApproval('CRVW', 2)
1304 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1305 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001306 self.assertEqual(self.getJobFromHistory('project1-merge').result,
1307 'SUCCESS')
1308 self.assertEqual(self.getJobFromHistory('project1-test1').result,
1309 'SUCCESS')
1310 self.assertEqual(self.getJobFromHistory('project1-test2').result,
1311 'SUCCESS')
1312 self.assertEqual(A.data['status'], 'MERGED')
1313 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001314
James E. Blair7ee88a22012-09-12 18:59:31 +02001315 def test_nonexistent_job(self):
1316 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001317 # Set to the state immediately after a restart
1318 self.resetGearmanServer()
1319 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02001320
1321 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1322 A.addApproval('CRVW', 2)
1323 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1324 # There may be a thread about to report a lost change
1325 while A.reported < 2:
1326 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001327 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001328 self.assertFalse(job_names)
1329 self.assertEqual(A.data['status'], 'NEW')
1330 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001331 self.assertEmptyQueues()
1332
1333 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001334 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02001335 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1336 A.addApproval('CRVW', 2)
1337 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1338 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001339 self.assertEqual(self.getJobFromHistory('project-merge').result,
1340 'SUCCESS')
1341 self.assertEqual(self.getJobFromHistory('project-test1').result,
1342 'SUCCESS')
1343 self.assertEqual(self.getJobFromHistory('project-test2').result,
1344 'SUCCESS')
1345 self.assertEqual(A.data['status'], 'MERGED')
1346 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08001347
1348 def test_single_nonexistent_post_job(self):
1349 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08001350 e = {
1351 "type": "ref-updated",
1352 "submitter": {
1353 "name": "User Name",
1354 },
1355 "refUpdate": {
1356 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1357 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1358 "refName": "master",
1359 "project": "org/project",
1360 }
1361 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001362 # Set to the state immediately after a restart
1363 self.resetGearmanServer()
1364 self.launcher.negative_function_cache_ttl = 0
1365
James E. Blairf62d4282012-12-31 17:01:50 -08001366 self.fake_gerrit.addEvent(e)
1367 self.waitUntilSettled()
1368
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001369 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08001370
1371 def test_new_patchset_dequeues_old(self):
1372 "Test that a new patchset causes the old to be dequeued"
1373 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001374 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001375 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1376 M.setMerged()
1377
1378 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1379 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1380 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1381 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1382 A.addApproval('CRVW', 2)
1383 B.addApproval('CRVW', 2)
1384 C.addApproval('CRVW', 2)
1385 D.addApproval('CRVW', 2)
1386
1387 C.setDependsOn(B, 1)
1388 B.setDependsOn(A, 1)
1389 A.setDependsOn(M, 1)
1390
1391 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1392 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1393 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1394 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1395 self.waitUntilSettled()
1396
1397 B.addPatchset()
1398 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1399 self.waitUntilSettled()
1400
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001401 self.worker.hold_jobs_in_build = False
1402 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001403 self.waitUntilSettled()
1404
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001405 self.assertEqual(A.data['status'], 'MERGED')
1406 self.assertEqual(A.reported, 2)
1407 self.assertEqual(B.data['status'], 'NEW')
1408 self.assertEqual(B.reported, 2)
1409 self.assertEqual(C.data['status'], 'NEW')
1410 self.assertEqual(C.reported, 2)
1411 self.assertEqual(D.data['status'], 'MERGED')
1412 self.assertEqual(D.reported, 2)
1413 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001414
Antoine Mussobd86a312014-01-08 14:51:33 +01001415 def test_abandoned_change_dequeues(self):
1416 "Test that an abandoned change is dequeued"
1417
1418 self.worker.hold_jobs_in_build = True
1419
1420 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1421 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1422 self.waitUntilSettled()
1423 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1424 self.assertEqual(self.builds[0].name, 'project-merge')
1425
1426 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1427 self.waitUntilSettled()
1428
1429 # For debugging purposes...
1430 #for pipeline in self.sched.layout.pipelines.values():
1431 # for queue in pipeline.queues:
1432 # self.log.info("pipepline %s queue %s contents %s" % (
1433 # pipeline.name, queue.name, queue.queue))
1434
1435 self.worker.release('.*-merge')
1436 self.waitUntilSettled()
1437
1438 self.assertEqual(len(self.builds), 0, "No job running")
1439 self.assertEmptyQueues()
1440 self.assertEqual(len(self.history), 1, "Only one build in history")
1441 self.assertEqual(self.history[0].result, 'ABORTED',
1442 'Build should have been aborted')
1443 self.assertEqual(A.reported, 0, "Abandoned change should not report")
1444
Arx Cruzb1b010d2013-10-28 19:49:59 -02001445 def test_zuul_url_return(self):
1446 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001447 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001448 self.worker.hold_jobs_in_build = True
1449
1450 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1451 A.addApproval('CRVW', 2)
1452 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1453 self.waitUntilSettled()
1454
1455 self.assertEqual(len(self.builds), 1)
1456 for build in self.builds:
1457 self.assertTrue('ZUUL_URL' in build.parameters)
1458
1459 self.worker.hold_jobs_in_build = False
1460 self.worker.release()
1461 self.waitUntilSettled()
1462
James E. Blair2fa50962013-01-30 21:50:41 -08001463 def test_new_patchset_dequeues_old_on_head(self):
1464 "Test that a new patchset causes the old to be dequeued (at head)"
1465 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001466 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001467 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1468 M.setMerged()
1469 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1470 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1471 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1472 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1473 A.addApproval('CRVW', 2)
1474 B.addApproval('CRVW', 2)
1475 C.addApproval('CRVW', 2)
1476 D.addApproval('CRVW', 2)
1477
1478 C.setDependsOn(B, 1)
1479 B.setDependsOn(A, 1)
1480 A.setDependsOn(M, 1)
1481
1482 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1483 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1484 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1485 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1486 self.waitUntilSettled()
1487
1488 A.addPatchset()
1489 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1490 self.waitUntilSettled()
1491
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001492 self.worker.hold_jobs_in_build = False
1493 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001494 self.waitUntilSettled()
1495
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001496 self.assertEqual(A.data['status'], 'NEW')
1497 self.assertEqual(A.reported, 2)
1498 self.assertEqual(B.data['status'], 'NEW')
1499 self.assertEqual(B.reported, 2)
1500 self.assertEqual(C.data['status'], 'NEW')
1501 self.assertEqual(C.reported, 2)
1502 self.assertEqual(D.data['status'], 'MERGED')
1503 self.assertEqual(D.reported, 2)
1504 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001505
1506 def test_new_patchset_dequeues_old_without_dependents(self):
1507 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001508 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001509 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1510 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1511 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1512 A.addApproval('CRVW', 2)
1513 B.addApproval('CRVW', 2)
1514 C.addApproval('CRVW', 2)
1515
1516 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1517 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1518 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1519 self.waitUntilSettled()
1520
1521 B.addPatchset()
1522 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1523 self.waitUntilSettled()
1524
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001525 self.worker.hold_jobs_in_build = False
1526 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001527 self.waitUntilSettled()
1528
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001529 self.assertEqual(A.data['status'], 'MERGED')
1530 self.assertEqual(A.reported, 2)
1531 self.assertEqual(B.data['status'], 'NEW')
1532 self.assertEqual(B.reported, 2)
1533 self.assertEqual(C.data['status'], 'MERGED')
1534 self.assertEqual(C.reported, 2)
1535 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001536
1537 def test_new_patchset_dequeues_old_independent_queue(self):
1538 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001539 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001540 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1541 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1542 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1543 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1544 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1545 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1546 self.waitUntilSettled()
1547
1548 B.addPatchset()
1549 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1550 self.waitUntilSettled()
1551
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001552 self.worker.hold_jobs_in_build = False
1553 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001554 self.waitUntilSettled()
1555
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001556 self.assertEqual(A.data['status'], 'NEW')
1557 self.assertEqual(A.reported, 1)
1558 self.assertEqual(B.data['status'], 'NEW')
1559 self.assertEqual(B.reported, 1)
1560 self.assertEqual(C.data['status'], 'NEW')
1561 self.assertEqual(C.reported, 1)
1562 self.assertEqual(len(self.history), 10)
1563 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001564
James E. Blair18c64442014-03-18 10:14:45 -07001565 def test_noop_job(self):
1566 "Test that the internal noop job works"
1567 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
1568 A.addApproval('CRVW', 2)
1569 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1570 self.waitUntilSettled()
1571
1572 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1573 self.assertTrue(self.sched._areAllBuildsComplete())
1574 self.assertEqual(len(self.history), 0)
1575 self.assertEqual(A.data['status'], 'MERGED')
1576 self.assertEqual(A.reported, 2)
1577
James E. Blair7d0dedc2013-02-21 17:26:09 -08001578 def test_zuul_refs(self):
1579 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001580 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001581 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1582 M1.setMerged()
1583 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1584 M2.setMerged()
1585
1586 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1587 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1588 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1589 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1590 A.addApproval('CRVW', 2)
1591 B.addApproval('CRVW', 2)
1592 C.addApproval('CRVW', 2)
1593 D.addApproval('CRVW', 2)
1594 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1595 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1596 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1597 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1598
1599 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001600 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001601 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001602 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001603 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001604 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001605 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001606 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001607 self.waitUntilSettled()
1608
James E. Blair7d0dedc2013-02-21 17:26:09 -08001609 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001610 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001611 if x.parameters['ZUUL_CHANGE'] == '3':
1612 a_zref = x.parameters['ZUUL_REF']
1613 if x.parameters['ZUUL_CHANGE'] == '4':
1614 b_zref = x.parameters['ZUUL_REF']
1615 if x.parameters['ZUUL_CHANGE'] == '5':
1616 c_zref = x.parameters['ZUUL_REF']
1617 if x.parameters['ZUUL_CHANGE'] == '6':
1618 d_zref = x.parameters['ZUUL_REF']
1619
1620 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001621 self.assertIsNotNone(a_zref)
1622 self.assertIsNotNone(b_zref)
1623 self.assertIsNotNone(c_zref)
1624 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001625
1626 # And they should all be different
1627 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001628 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001629
1630 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001631 self.assertTrue(self.ref_has_change(a_zref, A))
1632 self.assertFalse(self.ref_has_change(a_zref, B))
1633 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001634
1635 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001636 self.assertTrue(self.ref_has_change(b_zref, A))
1637 self.assertTrue(self.ref_has_change(b_zref, B))
1638 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001639
1640 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001641 self.assertTrue(self.ref_has_change(c_zref, A))
1642 self.assertTrue(self.ref_has_change(c_zref, B))
1643 self.assertTrue(self.ref_has_change(c_zref, C))
1644 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001645
1646 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001647 self.assertTrue(self.ref_has_change(d_zref, A))
1648 self.assertTrue(self.ref_has_change(d_zref, B))
1649 self.assertTrue(self.ref_has_change(d_zref, C))
1650 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001651
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001652 self.worker.hold_jobs_in_build = False
1653 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08001654 self.waitUntilSettled()
1655
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001656 self.assertEqual(A.data['status'], 'MERGED')
1657 self.assertEqual(A.reported, 2)
1658 self.assertEqual(B.data['status'], 'MERGED')
1659 self.assertEqual(B.reported, 2)
1660 self.assertEqual(C.data['status'], 'MERGED')
1661 self.assertEqual(C.reported, 2)
1662 self.assertEqual(D.data['status'], 'MERGED')
1663 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08001664
James E. Blair4a28a882013-08-23 15:17:33 -07001665 def test_rerun_on_error(self):
1666 "Test that if a worker fails to run a job, it is run again"
1667 self.worker.hold_jobs_in_build = True
1668 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1669 A.addApproval('CRVW', 2)
1670 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1671 self.waitUntilSettled()
1672
1673 self.builds[0].run_error = True
1674 self.worker.hold_jobs_in_build = False
1675 self.worker.release()
1676 self.waitUntilSettled()
1677 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
1678 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
1679
James E. Blair412e5582013-04-22 15:50:12 -07001680 def test_statsd(self):
1681 "Test each of the statsd methods used in the scheduler"
1682 import extras
1683 statsd = extras.try_import('statsd.statsd')
1684 statsd.incr('test-incr')
1685 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07001686 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07001687 self.assertReportedStat('test-incr', '1|c')
1688 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07001689 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07001690
James E. Blairdad52252014-02-07 16:59:17 -08001691 def test_stuck_job_cleanup(self):
1692 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07001693 # This job won't be registered at startup because it is not in
1694 # the standard layout, but we need it to already be registerd
1695 # for when we reconfigure, as that is when Zuul will attempt
1696 # to run the new job.
1697 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001698 self.gearman_server.hold_jobs_in_queue = True
1699 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1700 A.addApproval('CRVW', 2)
1701 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1702 self.waitUntilSettled()
1703 self.assertEqual(len(self.gearman_server.getQueue()), 1)
1704
1705 self.config.set('zuul', 'layout_config',
1706 'tests/fixtures/layout-no-jobs.yaml')
1707 self.sched.reconfigure(self.config)
1708 self.waitUntilSettled()
1709
James E. Blair18c64442014-03-18 10:14:45 -07001710 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001711 self.waitUntilSettled()
1712 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1713 self.assertTrue(self.sched._areAllBuildsComplete())
1714
1715 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07001716 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001717 self.assertEqual(self.history[0].result, 'SUCCESS')
1718
James E. Blair70c71582013-03-06 08:50:50 -08001719 def test_file_jobs(self):
1720 "Test that file jobs run only when appropriate"
1721 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1722 A.addPatchset(['pip-requires'])
1723 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1724 A.addApproval('CRVW', 2)
1725 B.addApproval('CRVW', 2)
1726 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1727 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1728 self.waitUntilSettled()
1729
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001730 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08001731 if x.name == 'project-testfile']
1732
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001733 self.assertEqual(len(testfile_jobs), 1)
1734 self.assertEqual(testfile_jobs[0].changes, '1,2')
1735 self.assertEqual(A.data['status'], 'MERGED')
1736 self.assertEqual(A.reported, 2)
1737 self.assertEqual(B.data['status'], 'MERGED')
1738 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07001739
1740 def test_test_config(self):
1741 "Test that we can test the config"
1742 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07001743 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07001744 sched.registerTrigger(None, 'timer')
James E. Blairc494d542014-08-06 09:23:52 -07001745 sched.registerTrigger(None, 'zuul')
Clark Boylanb640e052014-04-03 16:41:46 -07001746 sched.testConfig(self.config.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001747
1748 def test_build_description(self):
1749 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001750 self.worker.registerFunction('set_description:' +
1751 self.worker.worker_id)
1752
1753 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1754 A.addApproval('CRVW', 2)
1755 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1756 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001757 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001758 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001759 self.assertTrue(re.search("Branch.*master", desc))
1760 self.assertTrue(re.search("Pipeline.*gate", desc))
1761 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
1762 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
1763 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
1764 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001765
James E. Blairc8a1e052014-02-25 09:29:26 -08001766 def test_queue_names(self):
1767 "Test shared change queue names"
1768 project1 = self.sched.layout.projects['org/project1']
1769 project2 = self.sched.layout.projects['org/project2']
1770 q1 = self.sched.layout.pipelines['gate'].getQueue(project1)
1771 q2 = self.sched.layout.pipelines['gate'].getQueue(project2)
1772 self.assertEqual(q1.name, 'integration')
1773 self.assertEqual(q2.name, 'integration')
1774
1775 self.config.set('zuul', 'layout_config',
1776 'tests/fixtures/layout-bad-queue.yaml')
1777 with testtools.ExpectedException(
1778 Exception, "More than one name assigned to change queue"):
1779 self.sched.reconfigure(self.config)
1780
James E. Blair64ed6f22013-07-10 14:07:23 -07001781 def test_queue_precedence(self):
1782 "Test that queue precedence works"
1783
1784 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07001785 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07001786 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1787 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1788 A.addApproval('CRVW', 2)
1789 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1790
1791 self.waitUntilSettled()
1792 self.gearman_server.hold_jobs_in_queue = False
1793 self.gearman_server.release()
1794 self.waitUntilSettled()
1795
James E. Blair8de58bd2013-07-18 16:23:33 -07001796 # Run one build at a time to ensure non-race order:
1797 for x in range(6):
1798 self.release(self.builds[0])
1799 self.waitUntilSettled()
1800 self.worker.hold_jobs_in_build = False
1801 self.waitUntilSettled()
1802
James E. Blair64ed6f22013-07-10 14:07:23 -07001803 self.log.debug(self.history)
1804 self.assertEqual(self.history[0].pipeline, 'gate')
1805 self.assertEqual(self.history[1].pipeline, 'check')
1806 self.assertEqual(self.history[2].pipeline, 'gate')
1807 self.assertEqual(self.history[3].pipeline, 'gate')
1808 self.assertEqual(self.history[4].pipeline, 'check')
1809 self.assertEqual(self.history[5].pipeline, 'check')
1810
Clark Boylana5edbe42014-06-03 16:39:10 -07001811 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07001812 "Test that we can retrieve JSON status info"
1813 self.worker.hold_jobs_in_build = True
1814 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1815 A.addApproval('CRVW', 2)
1816 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1817 self.waitUntilSettled()
1818
1819 port = self.webapp.server.socket.getsockname()[1]
1820
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04001821 req = urllib2.Request("http://localhost:%s/status.json" % port)
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04001822 f = urllib2.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07001823 headers = f.info()
1824 self.assertIn('Content-Length', headers)
1825 self.assertIn('Content-Type', headers)
1826 self.assertEqual(headers['Content-Type'],
1827 'application/json; charset=UTF-8')
1828 self.assertIn('Last-Modified', headers)
James E. Blair1843a552013-07-03 14:19:52 -07001829 data = f.read()
1830
1831 self.worker.hold_jobs_in_build = False
1832 self.worker.release()
1833 self.waitUntilSettled()
1834
1835 data = json.loads(data)
1836 status_jobs = set()
1837 for p in data['pipelines']:
1838 for q in p['change_queues']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08001839 if q['dependent']:
1840 self.assertEqual(q['window'], 20)
1841 else:
1842 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07001843 for head in q['heads']:
1844 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08001845 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001846 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07001847 for job in change['jobs']:
1848 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001849 self.assertIn('project-merge', status_jobs)
1850 self.assertIn('project-test1', status_jobs)
1851 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07001852
James E. Blairc3d428e2013-12-03 15:06:48 -08001853 def test_merging_queues(self):
1854 "Test that transitively-connected change queues are merged"
1855 self.config.set('zuul', 'layout_config',
1856 'tests/fixtures/layout-merge-queues.yaml')
1857 self.sched.reconfigure(self.config)
1858 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
1859
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001860 def test_node_label(self):
1861 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001862 self.worker.registerFunction('build:node-project-test1:debian')
1863
1864 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
1865 A.addApproval('CRVW', 2)
1866 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1867 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001868
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001869 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
1870 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
1871 'debian')
1872 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07001873
1874 def test_live_reconfiguration(self):
1875 "Test that live reconfiguration works"
1876 self.worker.hold_jobs_in_build = True
1877 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1878 A.addApproval('CRVW', 2)
1879 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1880 self.waitUntilSettled()
1881
1882 self.sched.reconfigure(self.config)
1883
1884 self.worker.hold_jobs_in_build = False
1885 self.worker.release()
1886 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001887 self.assertEqual(self.getJobFromHistory('project-merge').result,
1888 'SUCCESS')
1889 self.assertEqual(self.getJobFromHistory('project-test1').result,
1890 'SUCCESS')
1891 self.assertEqual(self.getJobFromHistory('project-test2').result,
1892 'SUCCESS')
1893 self.assertEqual(A.data['status'], 'MERGED')
1894 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07001895
James E. Blaire712d9f2013-07-31 11:40:11 -07001896 def test_live_reconfiguration_functions(self):
1897 "Test live reconfiguration with a custom function"
1898 self.worker.registerFunction('build:node-project-test1:debian')
1899 self.worker.registerFunction('build:node-project-test1:wheezy')
1900 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
1901 A.addApproval('CRVW', 2)
1902 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1903 self.waitUntilSettled()
1904
1905 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
1906 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
1907 'debian')
1908 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
1909
1910 self.config.set('zuul', 'layout_config',
1911 'tests/fixtures/layout-live-'
1912 'reconfiguration-functions.yaml')
1913 self.sched.reconfigure(self.config)
1914 self.worker.build_history = []
1915
1916 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
1917 B.addApproval('CRVW', 2)
1918 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1919 self.waitUntilSettled()
1920
1921 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
1922 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
1923 'wheezy')
1924 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
1925
James E. Blair287c06d2013-07-24 10:39:30 -07001926 def test_delayed_repo_init(self):
1927 self.config.set('zuul', 'layout_config',
1928 'tests/fixtures/layout-delayed-repo-init.yaml')
1929 self.sched.reconfigure(self.config)
1930
1931 self.init_repo("org/new-project")
1932 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
1933
1934 A.addApproval('CRVW', 2)
1935 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1936 self.waitUntilSettled()
1937 self.assertEqual(self.getJobFromHistory('project-merge').result,
1938 'SUCCESS')
1939 self.assertEqual(self.getJobFromHistory('project-test1').result,
1940 'SUCCESS')
1941 self.assertEqual(self.getJobFromHistory('project-test2').result,
1942 'SUCCESS')
1943 self.assertEqual(A.data['status'], 'MERGED')
1944 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07001945
Clark Boylan6dbbc482013-10-18 10:57:31 -07001946 def test_repo_deleted(self):
1947 self.config.set('zuul', 'layout_config',
1948 'tests/fixtures/layout-repo-deleted.yaml')
1949 self.sched.reconfigure(self.config)
1950
1951 self.init_repo("org/delete-project")
1952 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
1953
1954 A.addApproval('CRVW', 2)
1955 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1956 self.waitUntilSettled()
1957 self.assertEqual(self.getJobFromHistory('project-merge').result,
1958 'SUCCESS')
1959 self.assertEqual(self.getJobFromHistory('project-test1').result,
1960 'SUCCESS')
1961 self.assertEqual(self.getJobFromHistory('project-test2').result,
1962 'SUCCESS')
1963 self.assertEqual(A.data['status'], 'MERGED')
1964 self.assertEqual(A.reported, 2)
1965
1966 # Delete org/new-project zuul repo. Should be recloned.
1967 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
1968
1969 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
1970
1971 B.addApproval('CRVW', 2)
1972 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1973 self.waitUntilSettled()
1974 self.assertEqual(self.getJobFromHistory('project-merge').result,
1975 'SUCCESS')
1976 self.assertEqual(self.getJobFromHistory('project-test1').result,
1977 'SUCCESS')
1978 self.assertEqual(self.getJobFromHistory('project-test2').result,
1979 'SUCCESS')
1980 self.assertEqual(B.data['status'], 'MERGED')
1981 self.assertEqual(B.reported, 2)
1982
James E. Blair63bb0ef2013-07-29 17:14:51 -07001983 def test_timer(self):
1984 "Test that a periodic job is triggered"
1985 self.worker.hold_jobs_in_build = True
1986 self.config.set('zuul', 'layout_config',
1987 'tests/fixtures/layout-timer.yaml')
1988 self.sched.reconfigure(self.config)
1989 self.registerJobs()
1990
Clark Boylan3ee090a2014-04-03 20:55:09 -07001991 # The pipeline triggers every second, so we should have seen
1992 # several by now.
1993 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07001994 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07001995
1996 self.assertEqual(len(self.builds), 2)
1997
James E. Blair63bb0ef2013-07-29 17:14:51 -07001998 port = self.webapp.server.socket.getsockname()[1]
1999
2000 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2001 data = f.read()
2002
2003 self.worker.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002004 # Stop queuing timer triggered jobs so that the assertions
2005 # below don't race against more jobs being queued.
2006 self.config.set('zuul', 'layout_config',
2007 'tests/fixtures/layout-no-timer.yaml')
2008 self.sched.reconfigure(self.config)
2009 self.registerJobs()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002010 self.worker.release()
2011 self.waitUntilSettled()
2012
2013 self.assertEqual(self.getJobFromHistory(
2014 'project-bitrot-stable-old').result, 'SUCCESS')
2015 self.assertEqual(self.getJobFromHistory(
2016 'project-bitrot-stable-older').result, 'SUCCESS')
2017
2018 data = json.loads(data)
2019 status_jobs = set()
2020 for p in data['pipelines']:
2021 for q in p['change_queues']:
2022 for head in q['heads']:
2023 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07002024 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002025 for job in change['jobs']:
2026 status_jobs.add(job['name'])
2027 self.assertIn('project-bitrot-stable-old', status_jobs)
2028 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002029
James E. Blair4f6033c2014-03-27 15:49:09 -07002030 def test_idle(self):
2031 "Test that frequent periodic jobs work"
2032 self.worker.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002033
Clark Boylan3ee090a2014-04-03 20:55:09 -07002034 for x in range(1, 3):
2035 # Test that timer triggers periodic jobs even across
2036 # layout config reloads.
2037 # Start timer trigger
2038 self.config.set('zuul', 'layout_config',
2039 'tests/fixtures/layout-idle.yaml')
2040 self.sched.reconfigure(self.config)
2041 self.registerJobs()
James E. Blair4f6033c2014-03-27 15:49:09 -07002042
Clark Boylan3ee090a2014-04-03 20:55:09 -07002043 # The pipeline triggers every second, so we should have seen
2044 # several by now.
2045 time.sleep(5)
2046 self.waitUntilSettled()
2047
2048 # Stop queuing timer triggered jobs so that the assertions
2049 # below don't race against more jobs being queued.
2050 self.config.set('zuul', 'layout_config',
2051 'tests/fixtures/layout-no-timer.yaml')
2052 self.sched.reconfigure(self.config)
2053 self.registerJobs()
2054
2055 self.assertEqual(len(self.builds), 2)
2056 self.worker.release('.*')
2057 self.waitUntilSettled()
2058 self.assertEqual(len(self.builds), 0)
2059 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002060
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002061 def test_check_smtp_pool(self):
2062 self.config.set('zuul', 'layout_config',
2063 'tests/fixtures/layout-smtp.yaml')
2064 self.sched.reconfigure(self.config)
2065
2066 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2067 self.waitUntilSettled()
2068
2069 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2070 self.waitUntilSettled()
2071
James E. Blairff80a2f2013-12-27 13:24:06 -08002072 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002073
2074 # A.messages only holds what FakeGerrit places in it. Thus we
2075 # work on the knowledge of what the first message should be as
2076 # it is only configured to go to SMTP.
2077
2078 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002079 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002080 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002081 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002082 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002083 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002084
2085 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002086 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002087 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002088 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002089 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002090 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002091
James E. Blaire5910202013-12-27 09:50:31 -08002092 def test_timer_smtp(self):
2093 "Test that a periodic job is triggered"
Clark Boylan3ee090a2014-04-03 20:55:09 -07002094 self.worker.hold_jobs_in_build = True
James E. Blaire5910202013-12-27 09:50:31 -08002095 self.config.set('zuul', 'layout_config',
2096 'tests/fixtures/layout-timer-smtp.yaml')
2097 self.sched.reconfigure(self.config)
2098 self.registerJobs()
2099
Clark Boylan3ee090a2014-04-03 20:55:09 -07002100 # The pipeline triggers every second, so we should have seen
2101 # several by now.
2102 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002103 self.waitUntilSettled()
2104
Clark Boylan3ee090a2014-04-03 20:55:09 -07002105 self.assertEqual(len(self.builds), 2)
2106 self.worker.release('.*')
2107 self.waitUntilSettled()
2108 self.assertEqual(len(self.history), 2)
2109
James E. Blaire5910202013-12-27 09:50:31 -08002110 self.assertEqual(self.getJobFromHistory(
2111 'project-bitrot-stable-old').result, 'SUCCESS')
2112 self.assertEqual(self.getJobFromHistory(
2113 'project-bitrot-stable-older').result, 'SUCCESS')
2114
James E. Blairff80a2f2013-12-27 13:24:06 -08002115 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002116
2117 # A.messages only holds what FakeGerrit places in it. Thus we
2118 # work on the knowledge of what the first message should be as
2119 # it is only configured to go to SMTP.
2120
2121 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002122 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002123 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002124 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002125 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002126 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002127
Clark Boylan3ee090a2014-04-03 20:55:09 -07002128 # Stop queuing timer triggered jobs and let any that may have
2129 # queued through so that end of test assertions pass.
2130 self.config.set('zuul', 'layout_config',
2131 'tests/fixtures/layout-no-timer.yaml')
2132 self.sched.reconfigure(self.config)
2133 self.registerJobs()
2134 self.worker.release('.*')
2135 self.waitUntilSettled()
2136
James E. Blairad28e912013-11-27 10:43:22 -08002137 def test_client_enqueue(self):
2138 "Test that the RPC client can enqueue a change"
2139 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2140 A.addApproval('CRVW', 2)
2141 A.addApproval('APRV', 1)
2142
2143 client = zuul.rpcclient.RPCClient('127.0.0.1',
2144 self.gearman_server.port)
2145 r = client.enqueue(pipeline='gate',
2146 project='org/project',
2147 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002148 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002149 self.waitUntilSettled()
2150 self.assertEqual(self.getJobFromHistory('project-merge').result,
2151 'SUCCESS')
2152 self.assertEqual(self.getJobFromHistory('project-test1').result,
2153 'SUCCESS')
2154 self.assertEqual(self.getJobFromHistory('project-test2').result,
2155 'SUCCESS')
2156 self.assertEqual(A.data['status'], 'MERGED')
2157 self.assertEqual(A.reported, 2)
2158 self.assertEqual(r, True)
2159
2160 def test_client_enqueue_negative(self):
2161 "Test that the RPC client returns errors"
2162 client = zuul.rpcclient.RPCClient('127.0.0.1',
2163 self.gearman_server.port)
2164 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2165 "Invalid project"):
2166 r = client.enqueue(pipeline='gate',
2167 project='project-does-not-exist',
2168 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002169 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002170 client.shutdown()
2171 self.assertEqual(r, False)
2172
2173 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2174 "Invalid pipeline"):
2175 r = client.enqueue(pipeline='pipeline-does-not-exist',
2176 project='org/project',
2177 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002178 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002179 client.shutdown()
2180 self.assertEqual(r, False)
2181
2182 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2183 "Invalid trigger"):
2184 r = client.enqueue(pipeline='gate',
2185 project='org/project',
2186 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08002187 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002188 client.shutdown()
2189 self.assertEqual(r, False)
2190
2191 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2192 "Invalid change"):
2193 r = client.enqueue(pipeline='gate',
2194 project='org/project',
2195 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002196 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002197 client.shutdown()
2198 self.assertEqual(r, False)
2199
2200 self.waitUntilSettled()
2201 self.assertEqual(len(self.history), 0)
2202 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08002203
2204 def test_client_promote(self):
2205 "Test that the RPC client can promote a change"
2206 self.worker.hold_jobs_in_build = True
2207 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2208 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2209 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2210 A.addApproval('CRVW', 2)
2211 B.addApproval('CRVW', 2)
2212 C.addApproval('CRVW', 2)
2213
2214 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2215 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2216 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2217
2218 self.waitUntilSettled()
2219
Sean Daguef39b9ca2014-01-10 21:34:35 -05002220 items = self.sched.layout.pipelines['gate'].getAllItems()
2221 enqueue_times = {}
2222 for item in items:
2223 enqueue_times[str(item.change)] = item.enqueue_time
2224
James E. Blair36658cf2013-12-06 17:53:48 -08002225 client = zuul.rpcclient.RPCClient('127.0.0.1',
2226 self.gearman_server.port)
2227 r = client.promote(pipeline='gate',
2228 change_ids=['2,1', '3,1'])
2229
Sean Daguef39b9ca2014-01-10 21:34:35 -05002230 # ensure that enqueue times are durable
2231 items = self.sched.layout.pipelines['gate'].getAllItems()
2232 for item in items:
2233 self.assertEqual(
2234 enqueue_times[str(item.change)], item.enqueue_time)
2235
James E. Blair78acec92014-02-06 07:11:32 -08002236 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08002237 self.worker.release('.*-merge')
2238 self.waitUntilSettled()
2239 self.worker.release('.*-merge')
2240 self.waitUntilSettled()
2241 self.worker.release('.*-merge')
2242 self.waitUntilSettled()
2243
2244 self.assertEqual(len(self.builds), 6)
2245 self.assertEqual(self.builds[0].name, 'project-test1')
2246 self.assertEqual(self.builds[1].name, 'project-test2')
2247 self.assertEqual(self.builds[2].name, 'project-test1')
2248 self.assertEqual(self.builds[3].name, 'project-test2')
2249 self.assertEqual(self.builds[4].name, 'project-test1')
2250 self.assertEqual(self.builds[5].name, 'project-test2')
2251
2252 self.assertTrue(self.job_has_changes(self.builds[0], B))
2253 self.assertFalse(self.job_has_changes(self.builds[0], A))
2254 self.assertFalse(self.job_has_changes(self.builds[0], C))
2255
2256 self.assertTrue(self.job_has_changes(self.builds[2], B))
2257 self.assertTrue(self.job_has_changes(self.builds[2], C))
2258 self.assertFalse(self.job_has_changes(self.builds[2], A))
2259
2260 self.assertTrue(self.job_has_changes(self.builds[4], B))
2261 self.assertTrue(self.job_has_changes(self.builds[4], C))
2262 self.assertTrue(self.job_has_changes(self.builds[4], A))
2263
2264 self.worker.release()
2265 self.waitUntilSettled()
2266
2267 self.assertEqual(A.data['status'], 'MERGED')
2268 self.assertEqual(A.reported, 2)
2269 self.assertEqual(B.data['status'], 'MERGED')
2270 self.assertEqual(B.reported, 2)
2271 self.assertEqual(C.data['status'], 'MERGED')
2272 self.assertEqual(C.reported, 2)
2273
2274 client.shutdown()
2275 self.assertEqual(r, True)
2276
2277 def test_client_promote_dependent(self):
2278 "Test that the RPC client can promote a dependent change"
2279 # C (depends on B) -> B -> A ; then promote C to get:
2280 # A -> C (depends on B) -> B
2281 self.worker.hold_jobs_in_build = True
2282 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2283 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2284 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2285
2286 C.setDependsOn(B, 1)
2287
2288 A.addApproval('CRVW', 2)
2289 B.addApproval('CRVW', 2)
2290 C.addApproval('CRVW', 2)
2291
2292 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2293 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2294 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2295
2296 self.waitUntilSettled()
2297
2298 client = zuul.rpcclient.RPCClient('127.0.0.1',
2299 self.gearman_server.port)
2300 r = client.promote(pipeline='gate',
2301 change_ids=['3,1'])
2302
James E. Blair78acec92014-02-06 07:11:32 -08002303 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08002304 self.worker.release('.*-merge')
2305 self.waitUntilSettled()
2306 self.worker.release('.*-merge')
2307 self.waitUntilSettled()
2308 self.worker.release('.*-merge')
2309 self.waitUntilSettled()
2310
2311 self.assertEqual(len(self.builds), 6)
2312 self.assertEqual(self.builds[0].name, 'project-test1')
2313 self.assertEqual(self.builds[1].name, 'project-test2')
2314 self.assertEqual(self.builds[2].name, 'project-test1')
2315 self.assertEqual(self.builds[3].name, 'project-test2')
2316 self.assertEqual(self.builds[4].name, 'project-test1')
2317 self.assertEqual(self.builds[5].name, 'project-test2')
2318
2319 self.assertTrue(self.job_has_changes(self.builds[0], B))
2320 self.assertFalse(self.job_has_changes(self.builds[0], A))
2321 self.assertFalse(self.job_has_changes(self.builds[0], C))
2322
2323 self.assertTrue(self.job_has_changes(self.builds[2], B))
2324 self.assertTrue(self.job_has_changes(self.builds[2], C))
2325 self.assertFalse(self.job_has_changes(self.builds[2], A))
2326
2327 self.assertTrue(self.job_has_changes(self.builds[4], B))
2328 self.assertTrue(self.job_has_changes(self.builds[4], C))
2329 self.assertTrue(self.job_has_changes(self.builds[4], A))
2330
2331 self.worker.release()
2332 self.waitUntilSettled()
2333
2334 self.assertEqual(A.data['status'], 'MERGED')
2335 self.assertEqual(A.reported, 2)
2336 self.assertEqual(B.data['status'], 'MERGED')
2337 self.assertEqual(B.reported, 2)
2338 self.assertEqual(C.data['status'], 'MERGED')
2339 self.assertEqual(C.reported, 2)
2340
2341 client.shutdown()
2342 self.assertEqual(r, True)
2343
2344 def test_client_promote_negative(self):
2345 "Test that the RPC client returns errors for promotion"
2346 self.worker.hold_jobs_in_build = True
2347 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2348 A.addApproval('CRVW', 2)
2349 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2350 self.waitUntilSettled()
2351
2352 client = zuul.rpcclient.RPCClient('127.0.0.1',
2353 self.gearman_server.port)
2354
2355 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
2356 r = client.promote(pipeline='nonexistent',
2357 change_ids=['2,1', '3,1'])
2358 client.shutdown()
2359 self.assertEqual(r, False)
2360
2361 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
2362 r = client.promote(pipeline='gate',
2363 change_ids=['4,1'])
2364 client.shutdown()
2365 self.assertEqual(r, False)
2366
2367 self.worker.hold_jobs_in_build = False
2368 self.worker.release()
2369 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08002370
2371 def test_queue_rate_limiting(self):
2372 "Test that DependentPipelines are rate limited with dep across window"
2373 self.config.set('zuul', 'layout_config',
2374 'tests/fixtures/layout-rate-limit.yaml')
2375 self.sched.reconfigure(self.config)
2376 self.worker.hold_jobs_in_build = True
2377 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2378 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2379 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2380
2381 C.setDependsOn(B, 1)
2382 self.worker.addFailTest('project-test1', A)
2383
2384 A.addApproval('CRVW', 2)
2385 B.addApproval('CRVW', 2)
2386 C.addApproval('CRVW', 2)
2387
2388 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2389 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2390 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2391 self.waitUntilSettled()
2392
2393 # Only A and B will have their merge jobs queued because
2394 # window is 2.
2395 self.assertEqual(len(self.builds), 2)
2396 self.assertEqual(self.builds[0].name, 'project-merge')
2397 self.assertEqual(self.builds[1].name, 'project-merge')
2398
2399 self.worker.release('.*-merge')
2400 self.waitUntilSettled()
2401 self.worker.release('.*-merge')
2402 self.waitUntilSettled()
2403
2404 # Only A and B will have their test jobs queued because
2405 # window is 2.
2406 self.assertEqual(len(self.builds), 4)
2407 self.assertEqual(self.builds[0].name, 'project-test1')
2408 self.assertEqual(self.builds[1].name, 'project-test2')
2409 self.assertEqual(self.builds[2].name, 'project-test1')
2410 self.assertEqual(self.builds[3].name, 'project-test2')
2411
2412 self.worker.release('project-.*')
2413 self.waitUntilSettled()
2414
2415 queue = self.sched.layout.pipelines['gate'].queues[0]
2416 # A failed so window is reduced by 1 to 1.
2417 self.assertEqual(queue.window, 1)
2418 self.assertEqual(queue.window_floor, 1)
2419 self.assertEqual(A.data['status'], 'NEW')
2420
2421 # Gate is reset and only B's merge job is queued because
2422 # window shrunk to 1.
2423 self.assertEqual(len(self.builds), 1)
2424 self.assertEqual(self.builds[0].name, 'project-merge')
2425
2426 self.worker.release('.*-merge')
2427 self.waitUntilSettled()
2428
2429 # Only B's test jobs are queued because window is still 1.
2430 self.assertEqual(len(self.builds), 2)
2431 self.assertEqual(self.builds[0].name, 'project-test1')
2432 self.assertEqual(self.builds[1].name, 'project-test2')
2433
2434 self.worker.release('project-.*')
2435 self.waitUntilSettled()
2436
2437 # B was successfully merged so window is increased to 2.
2438 self.assertEqual(queue.window, 2)
2439 self.assertEqual(queue.window_floor, 1)
2440 self.assertEqual(B.data['status'], 'MERGED')
2441
2442 # Only C is left and its merge job is queued.
2443 self.assertEqual(len(self.builds), 1)
2444 self.assertEqual(self.builds[0].name, 'project-merge')
2445
2446 self.worker.release('.*-merge')
2447 self.waitUntilSettled()
2448
2449 # After successful merge job the test jobs for C are queued.
2450 self.assertEqual(len(self.builds), 2)
2451 self.assertEqual(self.builds[0].name, 'project-test1')
2452 self.assertEqual(self.builds[1].name, 'project-test2')
2453
2454 self.worker.release('project-.*')
2455 self.waitUntilSettled()
2456
2457 # C successfully merged so window is bumped to 3.
2458 self.assertEqual(queue.window, 3)
2459 self.assertEqual(queue.window_floor, 1)
2460 self.assertEqual(C.data['status'], 'MERGED')
2461
2462 def test_queue_rate_limiting_dependent(self):
2463 "Test that DependentPipelines are rate limited with dep in window"
2464 self.config.set('zuul', 'layout_config',
2465 'tests/fixtures/layout-rate-limit.yaml')
2466 self.sched.reconfigure(self.config)
2467 self.worker.hold_jobs_in_build = True
2468 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2469 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2470 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2471
2472 B.setDependsOn(A, 1)
2473
2474 self.worker.addFailTest('project-test1', A)
2475
2476 A.addApproval('CRVW', 2)
2477 B.addApproval('CRVW', 2)
2478 C.addApproval('CRVW', 2)
2479
2480 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2481 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2482 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2483 self.waitUntilSettled()
2484
2485 # Only A and B will have their merge jobs queued because
2486 # window is 2.
2487 self.assertEqual(len(self.builds), 2)
2488 self.assertEqual(self.builds[0].name, 'project-merge')
2489 self.assertEqual(self.builds[1].name, 'project-merge')
2490
2491 self.worker.release('.*-merge')
2492 self.waitUntilSettled()
2493 self.worker.release('.*-merge')
2494 self.waitUntilSettled()
2495
2496 # Only A and B will have their test jobs queued because
2497 # window is 2.
2498 self.assertEqual(len(self.builds), 4)
2499 self.assertEqual(self.builds[0].name, 'project-test1')
2500 self.assertEqual(self.builds[1].name, 'project-test2')
2501 self.assertEqual(self.builds[2].name, 'project-test1')
2502 self.assertEqual(self.builds[3].name, 'project-test2')
2503
2504 self.worker.release('project-.*')
2505 self.waitUntilSettled()
2506
2507 queue = self.sched.layout.pipelines['gate'].queues[0]
2508 # A failed so window is reduced by 1 to 1.
2509 self.assertEqual(queue.window, 1)
2510 self.assertEqual(queue.window_floor, 1)
2511 self.assertEqual(A.data['status'], 'NEW')
2512 self.assertEqual(B.data['status'], 'NEW')
2513
2514 # Gate is reset and only C's merge job is queued because
2515 # window shrunk to 1 and A and B were dequeued.
2516 self.assertEqual(len(self.builds), 1)
2517 self.assertEqual(self.builds[0].name, 'project-merge')
2518
2519 self.worker.release('.*-merge')
2520 self.waitUntilSettled()
2521
2522 # Only C's test jobs are queued because window is still 1.
2523 self.assertEqual(len(self.builds), 2)
2524 self.assertEqual(self.builds[0].name, 'project-test1')
2525 self.assertEqual(self.builds[1].name, 'project-test2')
2526
2527 self.worker.release('project-.*')
2528 self.waitUntilSettled()
2529
2530 # C was successfully merged so window is increased to 2.
2531 self.assertEqual(queue.window, 2)
2532 self.assertEqual(queue.window_floor, 1)
2533 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08002534
2535 def test_worker_update_metadata(self):
2536 "Test if a worker can send back metadata about itself"
2537 self.worker.hold_jobs_in_build = True
2538
2539 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2540 A.addApproval('CRVW', 2)
2541 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2542 self.waitUntilSettled()
2543
2544 self.assertEqual(len(self.launcher.builds), 1)
2545
2546 self.log.debug('Current builds:')
2547 self.log.debug(self.launcher.builds)
2548
2549 start = time.time()
2550 while True:
2551 if time.time() - start > 10:
2552 raise Exception("Timeout waiting for gearman server to report "
2553 + "back to the client")
2554 build = self.launcher.builds.values()[0]
2555 if build.worker.name == "My Worker":
2556 break
2557 else:
2558 time.sleep(0)
2559
2560 self.log.debug(build)
2561 self.assertEqual("My Worker", build.worker.name)
2562 self.assertEqual("localhost", build.worker.hostname)
2563 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
2564 self.assertEqual("zuul.example.org", build.worker.fqdn)
2565 self.assertEqual("FakeBuilder", build.worker.program)
2566 self.assertEqual("v1.1", build.worker.version)
2567 self.assertEqual({'something': 'else'}, build.worker.extra)
2568
2569 self.worker.hold_jobs_in_build = False
2570 self.worker.release()
2571 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11002572
2573 def test_footer_message(self):
2574 "Test a pipeline's footer message is correctly added to the report."
2575 self.config.set('zuul', 'layout_config',
2576 'tests/fixtures/layout-footer-message.yaml')
2577 self.sched.reconfigure(self.config)
2578 self.registerJobs()
2579
2580 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2581 A.addApproval('CRVW', 2)
2582 self.worker.addFailTest('test1', A)
2583 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2584 self.waitUntilSettled()
2585
2586 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2587 B.addApproval('CRVW', 2)
2588 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2589 self.waitUntilSettled()
2590
2591 self.assertEqual(2, len(self.smtp_messages))
2592
2593 failure_body = """\
2594Build failed. For information on how to proceed, see \
2595http://wiki.example.org/Test_Failures
2596
2597- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
2598- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
2599
2600For CI problems and help debugging, contact ci@example.org"""
2601
2602 success_body = """\
2603Build succeeded.
2604
2605- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
2606- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
2607
2608For CI problems and help debugging, contact ci@example.org"""
2609
2610 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
2611 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11002612
2613 def test_merge_failure_reporters(self):
2614 """Check that the config is set up correctly"""
2615
2616 self.config.set('zuul', 'layout_config',
2617 'tests/fixtures/layout-merge-failure.yaml')
2618 self.sched.reconfigure(self.config)
2619 self.registerJobs()
2620
2621 self.assertEqual(
2622 "Merge Failed.\n\nThis change was unable to be automatically "
2623 "merged with the current state of the repository. Please rebase "
2624 "your change and upload a new patchset.",
2625 self.sched.layout.pipelines['check'].merge_failure_message)
2626 self.assertEqual(
2627 "The merge failed! For more information...",
2628 self.sched.layout.pipelines['gate'].merge_failure_message)
2629
2630 self.assertEqual(
2631 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
2632 self.assertEqual(
2633 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
2634
2635 self.assertTrue(isinstance(
2636 self.sched.layout.pipelines['check'].merge_failure_actions[0].
2637 reporter, zuul.reporter.gerrit.Reporter))
2638
2639 self.assertTrue(
2640 (
2641 isinstance(self.sched.layout.pipelines['gate'].
2642 merge_failure_actions[0].reporter,
2643 zuul.reporter.smtp.Reporter) and
2644 isinstance(self.sched.layout.pipelines['gate'].
2645 merge_failure_actions[1].reporter,
2646 zuul.reporter.gerrit.Reporter)
2647 ) or (
2648 isinstance(self.sched.layout.pipelines['gate'].
2649 merge_failure_actions[0].reporter,
2650 zuul.reporter.gerrit.Reporter) and
2651 isinstance(self.sched.layout.pipelines['gate'].
2652 merge_failure_actions[1].reporter,
2653 zuul.reporter.smtp.Reporter)
2654 )
2655 )
2656
2657 def test_merge_failure_reports(self):
2658 """Check that when a change fails to merge the correct message is sent
2659 to the correct reporter"""
2660 self.config.set('zuul', 'layout_config',
2661 'tests/fixtures/layout-merge-failure.yaml')
2662 self.sched.reconfigure(self.config)
2663 self.registerJobs()
2664
2665 # Check a test failure isn't reported to SMTP
2666 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2667 A.addApproval('CRVW', 2)
2668 self.worker.addFailTest('project-test1', A)
2669 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2670 self.waitUntilSettled()
2671
2672 self.assertEqual(3, len(self.history)) # 3 jobs
2673 self.assertEqual(0, len(self.smtp_messages))
2674
2675 # Check a merge failure is reported to SMTP
2676 # B should be merged, but C will conflict with B
2677 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2678 B.addPatchset(['conflict'])
2679 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2680 C.addPatchset(['conflict'])
2681 B.addApproval('CRVW', 2)
2682 C.addApproval('CRVW', 2)
2683 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2684 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2685 self.waitUntilSettled()
2686
2687 self.assertEqual(6, len(self.history)) # A and B jobs
2688 self.assertEqual(1, len(self.smtp_messages))
2689 self.assertEqual('The merge failed! For more information...',
2690 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11002691
2692 def test_swift_instructions(self):
2693 "Test that the correct swift instructions are sent to the workers"
2694 self.config.set('zuul', 'layout_config',
2695 'tests/fixtures/layout-swift.yaml')
2696 self.sched.reconfigure(self.config)
2697 self.registerJobs()
2698
2699 self.worker.hold_jobs_in_build = True
2700 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2701
2702 A.addApproval('CRVW', 2)
2703 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2704 self.waitUntilSettled()
2705
2706 self.assertEqual(
2707 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
2708 "gate/test-merge/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10002709 self.builds[0].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11002710 self.assertEqual(5,
2711 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
2712 split('\n')))
2713 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
2714
2715 self.assertEqual(
2716 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
2717 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10002718 self.builds[1].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11002719 self.assertEqual(5,
2720 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
2721 split('\n')))
2722 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
2723
2724 self.assertEqual(
2725 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
2726 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10002727 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11002728 self.assertEqual(5,
2729 len(self.builds[1].
2730 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
2731 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
2732
2733 self.worker.hold_jobs_in_build = False
2734 self.worker.release()
2735 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08002736
2737 def test_client_get_running_jobs(self):
2738 "Test that the RPC client can get a list of running jobs"
2739 self.worker.hold_jobs_in_build = True
2740 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2741 A.addApproval('CRVW', 2)
2742 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2743 self.waitUntilSettled()
2744
2745 client = zuul.rpcclient.RPCClient('127.0.0.1',
2746 self.gearman_server.port)
2747
2748 # Wait for gearman server to send the initial workData back to zuul
2749 start = time.time()
2750 while True:
2751 if time.time() - start > 10:
2752 raise Exception("Timeout waiting for gearman server to report "
2753 + "back to the client")
2754 build = self.launcher.builds.values()[0]
2755 if build.worker.name == "My Worker":
2756 break
2757 else:
2758 time.sleep(0)
2759
2760 running_items = client.get_running_jobs()
2761
2762 self.assertEqual(1, len(running_items))
2763 running_item = running_items[0]
2764 self.assertEqual([], running_item['failing_reasons'])
2765 self.assertEqual([], running_item['items_behind'])
2766 self.assertEqual('https://hostname/1', running_item['url'])
2767 self.assertEqual(None, running_item['item_ahead'])
2768 self.assertEqual('org/project', running_item['project'])
2769 self.assertEqual(None, running_item['remaining_time'])
2770 self.assertEqual(True, running_item['active'])
2771 self.assertEqual('1,1', running_item['id'])
2772
2773 self.assertEqual(3, len(running_item['jobs']))
2774 for job in running_item['jobs']:
2775 if job['name'] == 'project-merge':
2776 self.assertEqual('project-merge', job['name'])
2777 self.assertEqual('gate', job['pipeline'])
2778 self.assertEqual(False, job['retry'])
2779 self.assertEqual(13, len(job['parameters']))
2780 self.assertEqual('https://server/job/project-merge/0/',
2781 job['url'])
2782 self.assertEqual(7, len(job['worker']))
2783 self.assertEqual(False, job['canceled'])
2784 self.assertEqual(True, job['voting'])
2785 self.assertEqual(None, job['result'])
2786 self.assertEqual('gate', job['pipeline'])
2787 break
2788
2789 self.worker.hold_jobs_in_build = False
2790 self.worker.release()
2791 self.waitUntilSettled()
2792
2793 running_items = client.get_running_jobs()
2794 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07002795
2796 def test_nonvoting_pipeline(self):
2797 "Test that a nonvoting pipeline (experimental) can still report"
2798
Joshua Heskethcc017ea2014-04-30 19:55:25 +10002799 A = self.fake_gerrit.addFakeChange('org/experimental-project',
2800 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07002801 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2802 self.waitUntilSettled()
Joshua Heskethcc017ea2014-04-30 19:55:25 +10002803 self.assertEqual(
2804 self.getJobFromHistory('experimental-project-test').result,
2805 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07002806 self.assertEqual(A.reported, 1)