blob: 89056f4cd6209bbd75218fff8945af1c2fe95c60 [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...
Joshua Hesketh29d99b72014-08-19 16:27:42 +10001430 # 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))
Antoine Mussobd86a312014-01-08 14:51:33 +01001434
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()
James E. Blairf8058972014-08-15 16:09:16 -07002134 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002135 self.worker.release('.*')
2136 self.waitUntilSettled()
2137
James E. Blairad28e912013-11-27 10:43:22 -08002138 def test_client_enqueue(self):
2139 "Test that the RPC client can enqueue a change"
2140 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2141 A.addApproval('CRVW', 2)
2142 A.addApproval('APRV', 1)
2143
2144 client = zuul.rpcclient.RPCClient('127.0.0.1',
2145 self.gearman_server.port)
2146 r = client.enqueue(pipeline='gate',
2147 project='org/project',
2148 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002149 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002150 self.waitUntilSettled()
2151 self.assertEqual(self.getJobFromHistory('project-merge').result,
2152 'SUCCESS')
2153 self.assertEqual(self.getJobFromHistory('project-test1').result,
2154 'SUCCESS')
2155 self.assertEqual(self.getJobFromHistory('project-test2').result,
2156 'SUCCESS')
2157 self.assertEqual(A.data['status'], 'MERGED')
2158 self.assertEqual(A.reported, 2)
2159 self.assertEqual(r, True)
2160
2161 def test_client_enqueue_negative(self):
2162 "Test that the RPC client returns errors"
2163 client = zuul.rpcclient.RPCClient('127.0.0.1',
2164 self.gearman_server.port)
2165 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2166 "Invalid project"):
2167 r = client.enqueue(pipeline='gate',
2168 project='project-does-not-exist',
2169 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002170 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002171 client.shutdown()
2172 self.assertEqual(r, False)
2173
2174 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2175 "Invalid pipeline"):
2176 r = client.enqueue(pipeline='pipeline-does-not-exist',
2177 project='org/project',
2178 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002179 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002180 client.shutdown()
2181 self.assertEqual(r, False)
2182
2183 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2184 "Invalid trigger"):
2185 r = client.enqueue(pipeline='gate',
2186 project='org/project',
2187 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08002188 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002189 client.shutdown()
2190 self.assertEqual(r, False)
2191
2192 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2193 "Invalid change"):
2194 r = client.enqueue(pipeline='gate',
2195 project='org/project',
2196 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002197 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002198 client.shutdown()
2199 self.assertEqual(r, False)
2200
2201 self.waitUntilSettled()
2202 self.assertEqual(len(self.history), 0)
2203 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08002204
2205 def test_client_promote(self):
2206 "Test that the RPC client can promote a change"
2207 self.worker.hold_jobs_in_build = True
2208 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2209 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2210 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2211 A.addApproval('CRVW', 2)
2212 B.addApproval('CRVW', 2)
2213 C.addApproval('CRVW', 2)
2214
2215 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2216 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2217 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2218
2219 self.waitUntilSettled()
2220
Sean Daguef39b9ca2014-01-10 21:34:35 -05002221 items = self.sched.layout.pipelines['gate'].getAllItems()
2222 enqueue_times = {}
2223 for item in items:
2224 enqueue_times[str(item.change)] = item.enqueue_time
2225
James E. Blair36658cf2013-12-06 17:53:48 -08002226 client = zuul.rpcclient.RPCClient('127.0.0.1',
2227 self.gearman_server.port)
2228 r = client.promote(pipeline='gate',
2229 change_ids=['2,1', '3,1'])
2230
Sean Daguef39b9ca2014-01-10 21:34:35 -05002231 # ensure that enqueue times are durable
2232 items = self.sched.layout.pipelines['gate'].getAllItems()
2233 for item in items:
2234 self.assertEqual(
2235 enqueue_times[str(item.change)], item.enqueue_time)
2236
James E. Blair78acec92014-02-06 07:11:32 -08002237 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08002238 self.worker.release('.*-merge')
2239 self.waitUntilSettled()
2240 self.worker.release('.*-merge')
2241 self.waitUntilSettled()
2242 self.worker.release('.*-merge')
2243 self.waitUntilSettled()
2244
2245 self.assertEqual(len(self.builds), 6)
2246 self.assertEqual(self.builds[0].name, 'project-test1')
2247 self.assertEqual(self.builds[1].name, 'project-test2')
2248 self.assertEqual(self.builds[2].name, 'project-test1')
2249 self.assertEqual(self.builds[3].name, 'project-test2')
2250 self.assertEqual(self.builds[4].name, 'project-test1')
2251 self.assertEqual(self.builds[5].name, 'project-test2')
2252
2253 self.assertTrue(self.job_has_changes(self.builds[0], B))
2254 self.assertFalse(self.job_has_changes(self.builds[0], A))
2255 self.assertFalse(self.job_has_changes(self.builds[0], C))
2256
2257 self.assertTrue(self.job_has_changes(self.builds[2], B))
2258 self.assertTrue(self.job_has_changes(self.builds[2], C))
2259 self.assertFalse(self.job_has_changes(self.builds[2], A))
2260
2261 self.assertTrue(self.job_has_changes(self.builds[4], B))
2262 self.assertTrue(self.job_has_changes(self.builds[4], C))
2263 self.assertTrue(self.job_has_changes(self.builds[4], A))
2264
2265 self.worker.release()
2266 self.waitUntilSettled()
2267
2268 self.assertEqual(A.data['status'], 'MERGED')
2269 self.assertEqual(A.reported, 2)
2270 self.assertEqual(B.data['status'], 'MERGED')
2271 self.assertEqual(B.reported, 2)
2272 self.assertEqual(C.data['status'], 'MERGED')
2273 self.assertEqual(C.reported, 2)
2274
2275 client.shutdown()
2276 self.assertEqual(r, True)
2277
2278 def test_client_promote_dependent(self):
2279 "Test that the RPC client can promote a dependent change"
2280 # C (depends on B) -> B -> A ; then promote C to get:
2281 # A -> C (depends on B) -> B
2282 self.worker.hold_jobs_in_build = True
2283 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2284 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2285 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2286
2287 C.setDependsOn(B, 1)
2288
2289 A.addApproval('CRVW', 2)
2290 B.addApproval('CRVW', 2)
2291 C.addApproval('CRVW', 2)
2292
2293 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2294 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2295 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2296
2297 self.waitUntilSettled()
2298
2299 client = zuul.rpcclient.RPCClient('127.0.0.1',
2300 self.gearman_server.port)
2301 r = client.promote(pipeline='gate',
2302 change_ids=['3,1'])
2303
James E. Blair78acec92014-02-06 07:11:32 -08002304 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08002305 self.worker.release('.*-merge')
2306 self.waitUntilSettled()
2307 self.worker.release('.*-merge')
2308 self.waitUntilSettled()
2309 self.worker.release('.*-merge')
2310 self.waitUntilSettled()
2311
2312 self.assertEqual(len(self.builds), 6)
2313 self.assertEqual(self.builds[0].name, 'project-test1')
2314 self.assertEqual(self.builds[1].name, 'project-test2')
2315 self.assertEqual(self.builds[2].name, 'project-test1')
2316 self.assertEqual(self.builds[3].name, 'project-test2')
2317 self.assertEqual(self.builds[4].name, 'project-test1')
2318 self.assertEqual(self.builds[5].name, 'project-test2')
2319
2320 self.assertTrue(self.job_has_changes(self.builds[0], B))
2321 self.assertFalse(self.job_has_changes(self.builds[0], A))
2322 self.assertFalse(self.job_has_changes(self.builds[0], C))
2323
2324 self.assertTrue(self.job_has_changes(self.builds[2], B))
2325 self.assertTrue(self.job_has_changes(self.builds[2], C))
2326 self.assertFalse(self.job_has_changes(self.builds[2], A))
2327
2328 self.assertTrue(self.job_has_changes(self.builds[4], B))
2329 self.assertTrue(self.job_has_changes(self.builds[4], C))
2330 self.assertTrue(self.job_has_changes(self.builds[4], A))
2331
2332 self.worker.release()
2333 self.waitUntilSettled()
2334
2335 self.assertEqual(A.data['status'], 'MERGED')
2336 self.assertEqual(A.reported, 2)
2337 self.assertEqual(B.data['status'], 'MERGED')
2338 self.assertEqual(B.reported, 2)
2339 self.assertEqual(C.data['status'], 'MERGED')
2340 self.assertEqual(C.reported, 2)
2341
2342 client.shutdown()
2343 self.assertEqual(r, True)
2344
2345 def test_client_promote_negative(self):
2346 "Test that the RPC client returns errors for promotion"
2347 self.worker.hold_jobs_in_build = True
2348 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2349 A.addApproval('CRVW', 2)
2350 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2351 self.waitUntilSettled()
2352
2353 client = zuul.rpcclient.RPCClient('127.0.0.1',
2354 self.gearman_server.port)
2355
2356 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
2357 r = client.promote(pipeline='nonexistent',
2358 change_ids=['2,1', '3,1'])
2359 client.shutdown()
2360 self.assertEqual(r, False)
2361
2362 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
2363 r = client.promote(pipeline='gate',
2364 change_ids=['4,1'])
2365 client.shutdown()
2366 self.assertEqual(r, False)
2367
2368 self.worker.hold_jobs_in_build = False
2369 self.worker.release()
2370 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08002371
2372 def test_queue_rate_limiting(self):
2373 "Test that DependentPipelines are rate limited with dep across window"
2374 self.config.set('zuul', 'layout_config',
2375 'tests/fixtures/layout-rate-limit.yaml')
2376 self.sched.reconfigure(self.config)
2377 self.worker.hold_jobs_in_build = True
2378 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2379 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2380 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2381
2382 C.setDependsOn(B, 1)
2383 self.worker.addFailTest('project-test1', A)
2384
2385 A.addApproval('CRVW', 2)
2386 B.addApproval('CRVW', 2)
2387 C.addApproval('CRVW', 2)
2388
2389 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2390 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2391 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2392 self.waitUntilSettled()
2393
2394 # Only A and B will have their merge jobs queued because
2395 # window is 2.
2396 self.assertEqual(len(self.builds), 2)
2397 self.assertEqual(self.builds[0].name, 'project-merge')
2398 self.assertEqual(self.builds[1].name, 'project-merge')
2399
2400 self.worker.release('.*-merge')
2401 self.waitUntilSettled()
2402 self.worker.release('.*-merge')
2403 self.waitUntilSettled()
2404
2405 # Only A and B will have their test jobs queued because
2406 # window is 2.
2407 self.assertEqual(len(self.builds), 4)
2408 self.assertEqual(self.builds[0].name, 'project-test1')
2409 self.assertEqual(self.builds[1].name, 'project-test2')
2410 self.assertEqual(self.builds[2].name, 'project-test1')
2411 self.assertEqual(self.builds[3].name, 'project-test2')
2412
2413 self.worker.release('project-.*')
2414 self.waitUntilSettled()
2415
2416 queue = self.sched.layout.pipelines['gate'].queues[0]
2417 # A failed so window is reduced by 1 to 1.
2418 self.assertEqual(queue.window, 1)
2419 self.assertEqual(queue.window_floor, 1)
2420 self.assertEqual(A.data['status'], 'NEW')
2421
2422 # Gate is reset and only B's merge job is queued because
2423 # window shrunk to 1.
2424 self.assertEqual(len(self.builds), 1)
2425 self.assertEqual(self.builds[0].name, 'project-merge')
2426
2427 self.worker.release('.*-merge')
2428 self.waitUntilSettled()
2429
2430 # Only B's test jobs are queued because window is still 1.
2431 self.assertEqual(len(self.builds), 2)
2432 self.assertEqual(self.builds[0].name, 'project-test1')
2433 self.assertEqual(self.builds[1].name, 'project-test2')
2434
2435 self.worker.release('project-.*')
2436 self.waitUntilSettled()
2437
2438 # B was successfully merged so window is increased to 2.
2439 self.assertEqual(queue.window, 2)
2440 self.assertEqual(queue.window_floor, 1)
2441 self.assertEqual(B.data['status'], 'MERGED')
2442
2443 # Only C is left and its merge job is queued.
2444 self.assertEqual(len(self.builds), 1)
2445 self.assertEqual(self.builds[0].name, 'project-merge')
2446
2447 self.worker.release('.*-merge')
2448 self.waitUntilSettled()
2449
2450 # After successful merge job the test jobs for C are queued.
2451 self.assertEqual(len(self.builds), 2)
2452 self.assertEqual(self.builds[0].name, 'project-test1')
2453 self.assertEqual(self.builds[1].name, 'project-test2')
2454
2455 self.worker.release('project-.*')
2456 self.waitUntilSettled()
2457
2458 # C successfully merged so window is bumped to 3.
2459 self.assertEqual(queue.window, 3)
2460 self.assertEqual(queue.window_floor, 1)
2461 self.assertEqual(C.data['status'], 'MERGED')
2462
2463 def test_queue_rate_limiting_dependent(self):
2464 "Test that DependentPipelines are rate limited with dep in window"
2465 self.config.set('zuul', 'layout_config',
2466 'tests/fixtures/layout-rate-limit.yaml')
2467 self.sched.reconfigure(self.config)
2468 self.worker.hold_jobs_in_build = True
2469 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2470 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2471 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2472
2473 B.setDependsOn(A, 1)
2474
2475 self.worker.addFailTest('project-test1', A)
2476
2477 A.addApproval('CRVW', 2)
2478 B.addApproval('CRVW', 2)
2479 C.addApproval('CRVW', 2)
2480
2481 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2482 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2483 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2484 self.waitUntilSettled()
2485
2486 # Only A and B will have their merge jobs queued because
2487 # window is 2.
2488 self.assertEqual(len(self.builds), 2)
2489 self.assertEqual(self.builds[0].name, 'project-merge')
2490 self.assertEqual(self.builds[1].name, 'project-merge')
2491
2492 self.worker.release('.*-merge')
2493 self.waitUntilSettled()
2494 self.worker.release('.*-merge')
2495 self.waitUntilSettled()
2496
2497 # Only A and B will have their test jobs queued because
2498 # window is 2.
2499 self.assertEqual(len(self.builds), 4)
2500 self.assertEqual(self.builds[0].name, 'project-test1')
2501 self.assertEqual(self.builds[1].name, 'project-test2')
2502 self.assertEqual(self.builds[2].name, 'project-test1')
2503 self.assertEqual(self.builds[3].name, 'project-test2')
2504
2505 self.worker.release('project-.*')
2506 self.waitUntilSettled()
2507
2508 queue = self.sched.layout.pipelines['gate'].queues[0]
2509 # A failed so window is reduced by 1 to 1.
2510 self.assertEqual(queue.window, 1)
2511 self.assertEqual(queue.window_floor, 1)
2512 self.assertEqual(A.data['status'], 'NEW')
2513 self.assertEqual(B.data['status'], 'NEW')
2514
2515 # Gate is reset and only C's merge job is queued because
2516 # window shrunk to 1 and A and B were dequeued.
2517 self.assertEqual(len(self.builds), 1)
2518 self.assertEqual(self.builds[0].name, 'project-merge')
2519
2520 self.worker.release('.*-merge')
2521 self.waitUntilSettled()
2522
2523 # Only C's test jobs are queued because window is still 1.
2524 self.assertEqual(len(self.builds), 2)
2525 self.assertEqual(self.builds[0].name, 'project-test1')
2526 self.assertEqual(self.builds[1].name, 'project-test2')
2527
2528 self.worker.release('project-.*')
2529 self.waitUntilSettled()
2530
2531 # C was successfully merged so window is increased to 2.
2532 self.assertEqual(queue.window, 2)
2533 self.assertEqual(queue.window_floor, 1)
2534 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08002535
2536 def test_worker_update_metadata(self):
2537 "Test if a worker can send back metadata about itself"
2538 self.worker.hold_jobs_in_build = True
2539
2540 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2541 A.addApproval('CRVW', 2)
2542 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2543 self.waitUntilSettled()
2544
2545 self.assertEqual(len(self.launcher.builds), 1)
2546
2547 self.log.debug('Current builds:')
2548 self.log.debug(self.launcher.builds)
2549
2550 start = time.time()
2551 while True:
2552 if time.time() - start > 10:
2553 raise Exception("Timeout waiting for gearman server to report "
2554 + "back to the client")
2555 build = self.launcher.builds.values()[0]
2556 if build.worker.name == "My Worker":
2557 break
2558 else:
2559 time.sleep(0)
2560
2561 self.log.debug(build)
2562 self.assertEqual("My Worker", build.worker.name)
2563 self.assertEqual("localhost", build.worker.hostname)
2564 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
2565 self.assertEqual("zuul.example.org", build.worker.fqdn)
2566 self.assertEqual("FakeBuilder", build.worker.program)
2567 self.assertEqual("v1.1", build.worker.version)
2568 self.assertEqual({'something': 'else'}, build.worker.extra)
2569
2570 self.worker.hold_jobs_in_build = False
2571 self.worker.release()
2572 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11002573
2574 def test_footer_message(self):
2575 "Test a pipeline's footer message is correctly added to the report."
2576 self.config.set('zuul', 'layout_config',
2577 'tests/fixtures/layout-footer-message.yaml')
2578 self.sched.reconfigure(self.config)
2579 self.registerJobs()
2580
2581 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2582 A.addApproval('CRVW', 2)
2583 self.worker.addFailTest('test1', A)
2584 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2585 self.waitUntilSettled()
2586
2587 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2588 B.addApproval('CRVW', 2)
2589 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2590 self.waitUntilSettled()
2591
2592 self.assertEqual(2, len(self.smtp_messages))
2593
2594 failure_body = """\
2595Build failed. For information on how to proceed, see \
2596http://wiki.example.org/Test_Failures
2597
2598- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
2599- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
2600
2601For CI problems and help debugging, contact ci@example.org"""
2602
2603 success_body = """\
2604Build succeeded.
2605
2606- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
2607- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
2608
2609For CI problems and help debugging, contact ci@example.org"""
2610
2611 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
2612 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11002613
2614 def test_merge_failure_reporters(self):
2615 """Check that the config is set up correctly"""
2616
2617 self.config.set('zuul', 'layout_config',
2618 'tests/fixtures/layout-merge-failure.yaml')
2619 self.sched.reconfigure(self.config)
2620 self.registerJobs()
2621
2622 self.assertEqual(
2623 "Merge Failed.\n\nThis change was unable to be automatically "
2624 "merged with the current state of the repository. Please rebase "
2625 "your change and upload a new patchset.",
2626 self.sched.layout.pipelines['check'].merge_failure_message)
2627 self.assertEqual(
2628 "The merge failed! For more information...",
2629 self.sched.layout.pipelines['gate'].merge_failure_message)
2630
2631 self.assertEqual(
2632 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
2633 self.assertEqual(
2634 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
2635
2636 self.assertTrue(isinstance(
2637 self.sched.layout.pipelines['check'].merge_failure_actions[0].
2638 reporter, zuul.reporter.gerrit.Reporter))
2639
2640 self.assertTrue(
2641 (
2642 isinstance(self.sched.layout.pipelines['gate'].
2643 merge_failure_actions[0].reporter,
2644 zuul.reporter.smtp.Reporter) and
2645 isinstance(self.sched.layout.pipelines['gate'].
2646 merge_failure_actions[1].reporter,
2647 zuul.reporter.gerrit.Reporter)
2648 ) or (
2649 isinstance(self.sched.layout.pipelines['gate'].
2650 merge_failure_actions[0].reporter,
2651 zuul.reporter.gerrit.Reporter) and
2652 isinstance(self.sched.layout.pipelines['gate'].
2653 merge_failure_actions[1].reporter,
2654 zuul.reporter.smtp.Reporter)
2655 )
2656 )
2657
2658 def test_merge_failure_reports(self):
2659 """Check that when a change fails to merge the correct message is sent
2660 to the correct reporter"""
2661 self.config.set('zuul', 'layout_config',
2662 'tests/fixtures/layout-merge-failure.yaml')
2663 self.sched.reconfigure(self.config)
2664 self.registerJobs()
2665
2666 # Check a test failure isn't reported to SMTP
2667 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2668 A.addApproval('CRVW', 2)
2669 self.worker.addFailTest('project-test1', A)
2670 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2671 self.waitUntilSettled()
2672
2673 self.assertEqual(3, len(self.history)) # 3 jobs
2674 self.assertEqual(0, len(self.smtp_messages))
2675
2676 # Check a merge failure is reported to SMTP
2677 # B should be merged, but C will conflict with B
2678 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2679 B.addPatchset(['conflict'])
2680 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2681 C.addPatchset(['conflict'])
2682 B.addApproval('CRVW', 2)
2683 C.addApproval('CRVW', 2)
2684 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2685 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2686 self.waitUntilSettled()
2687
2688 self.assertEqual(6, len(self.history)) # A and B jobs
2689 self.assertEqual(1, len(self.smtp_messages))
2690 self.assertEqual('The merge failed! For more information...',
2691 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11002692
2693 def test_swift_instructions(self):
2694 "Test that the correct swift instructions are sent to the workers"
2695 self.config.set('zuul', 'layout_config',
2696 'tests/fixtures/layout-swift.yaml')
2697 self.sched.reconfigure(self.config)
2698 self.registerJobs()
2699
2700 self.worker.hold_jobs_in_build = True
2701 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2702
2703 A.addApproval('CRVW', 2)
2704 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2705 self.waitUntilSettled()
2706
2707 self.assertEqual(
2708 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
2709 "gate/test-merge/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10002710 self.builds[0].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11002711 self.assertEqual(5,
2712 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
2713 split('\n')))
2714 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
2715
2716 self.assertEqual(
2717 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
2718 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10002719 self.builds[1].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11002720 self.assertEqual(5,
2721 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
2722 split('\n')))
2723 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
2724
2725 self.assertEqual(
2726 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
2727 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10002728 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11002729 self.assertEqual(5,
2730 len(self.builds[1].
2731 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
2732 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
2733
2734 self.worker.hold_jobs_in_build = False
2735 self.worker.release()
2736 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08002737
2738 def test_client_get_running_jobs(self):
2739 "Test that the RPC client can get a list of running jobs"
2740 self.worker.hold_jobs_in_build = True
2741 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2742 A.addApproval('CRVW', 2)
2743 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2744 self.waitUntilSettled()
2745
2746 client = zuul.rpcclient.RPCClient('127.0.0.1',
2747 self.gearman_server.port)
2748
2749 # Wait for gearman server to send the initial workData back to zuul
2750 start = time.time()
2751 while True:
2752 if time.time() - start > 10:
2753 raise Exception("Timeout waiting for gearman server to report "
2754 + "back to the client")
2755 build = self.launcher.builds.values()[0]
2756 if build.worker.name == "My Worker":
2757 break
2758 else:
2759 time.sleep(0)
2760
2761 running_items = client.get_running_jobs()
2762
2763 self.assertEqual(1, len(running_items))
2764 running_item = running_items[0]
2765 self.assertEqual([], running_item['failing_reasons'])
2766 self.assertEqual([], running_item['items_behind'])
2767 self.assertEqual('https://hostname/1', running_item['url'])
2768 self.assertEqual(None, running_item['item_ahead'])
2769 self.assertEqual('org/project', running_item['project'])
2770 self.assertEqual(None, running_item['remaining_time'])
2771 self.assertEqual(True, running_item['active'])
2772 self.assertEqual('1,1', running_item['id'])
2773
2774 self.assertEqual(3, len(running_item['jobs']))
2775 for job in running_item['jobs']:
2776 if job['name'] == 'project-merge':
2777 self.assertEqual('project-merge', job['name'])
2778 self.assertEqual('gate', job['pipeline'])
2779 self.assertEqual(False, job['retry'])
2780 self.assertEqual(13, len(job['parameters']))
2781 self.assertEqual('https://server/job/project-merge/0/',
2782 job['url'])
2783 self.assertEqual(7, len(job['worker']))
2784 self.assertEqual(False, job['canceled'])
2785 self.assertEqual(True, job['voting'])
2786 self.assertEqual(None, job['result'])
2787 self.assertEqual('gate', job['pipeline'])
2788 break
2789
2790 self.worker.hold_jobs_in_build = False
2791 self.worker.release()
2792 self.waitUntilSettled()
2793
2794 running_items = client.get_running_jobs()
2795 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07002796
2797 def test_nonvoting_pipeline(self):
2798 "Test that a nonvoting pipeline (experimental) can still report"
2799
Joshua Heskethcc017ea2014-04-30 19:55:25 +10002800 A = self.fake_gerrit.addFakeChange('org/experimental-project',
2801 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07002802 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2803 self.waitUntilSettled()
Joshua Heskethcc017ea2014-04-30 19:55:25 +10002804 self.assertEqual(
2805 self.getJobFromHistory('experimental-project-test').result,
2806 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07002807 self.assertEqual(A.reported, 1)