blob: ceb15887be6c61ccee12086deef231aaccceaf34 [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. Blair6c358e72013-07-29 17:06:47 -0700654 trigger = self.sched.layout.pipelines['gate'].trigger
655 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -0400656 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -0700657 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700658
659 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -0700660 a = trigger.getChange(1, 2, refresh=True)
661 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700662
663 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -0700664 a = trigger.getChange(1, 2, refresh=True)
665 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
666 trigger.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)
1031 self.assertEqual(B.data['status'], 'NEW')
1032 self.assertEqual(B.reported, 2)
1033 self.assertEqual(C.data['status'], 'NEW')
1034 self.assertEqual(C.reported, 2)
1035 self.assertEqual(D.data['status'], 'NEW')
1036 self.assertEqual(D.reported, 2)
1037 self.assertEqual(E.data['status'], 'MERGED')
1038 self.assertEqual(E.reported, 2)
1039 self.assertEqual(len(self.history), 18)
1040
James E. Blairec590122012-08-22 15:19:31 -07001041 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001042 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001043 # If it's dequeued more than once, we should see extra
1044 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001045
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001046 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001047 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1048 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1049 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1050 A.addApproval('CRVW', 2)
1051 B.addApproval('CRVW', 2)
1052 C.addApproval('CRVW', 2)
1053
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001054 self.worker.addFailTest('project1-test1', A)
1055 self.worker.addFailTest('project1-test2', A)
1056 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001057
1058 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1059 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1060 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1061
1062 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001063
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001064 self.assertEqual(len(self.builds), 1)
1065 self.assertEqual(self.builds[0].name, 'project1-merge')
1066 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07001067
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001068 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001069 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001070 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001071 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001072 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001073 self.waitUntilSettled()
1074
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001075 self.assertEqual(len(self.builds), 9)
1076 self.assertEqual(self.builds[0].name, 'project1-test1')
1077 self.assertEqual(self.builds[1].name, 'project1-test2')
1078 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
1079 self.assertEqual(self.builds[3].name, 'project1-test1')
1080 self.assertEqual(self.builds[4].name, 'project1-test2')
1081 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
1082 self.assertEqual(self.builds[6].name, 'project1-test1')
1083 self.assertEqual(self.builds[7].name, 'project1-test2')
1084 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001085
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001086 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001087 self.waitUntilSettled()
1088
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001089 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1090 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001091
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001092 self.worker.hold_jobs_in_build = False
1093 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001094 self.waitUntilSettled()
1095
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001096 self.assertEqual(len(self.builds), 0)
1097 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001098
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001099 self.assertEqual(A.data['status'], 'NEW')
1100 self.assertEqual(B.data['status'], 'MERGED')
1101 self.assertEqual(C.data['status'], 'MERGED')
1102 self.assertEqual(A.reported, 2)
1103 self.assertEqual(B.reported, 2)
1104 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001105
1106 def test_nonvoting_job(self):
1107 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001108
James E. Blair4ec821f2012-08-23 15:28:28 -07001109 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1110 'master', 'A')
1111 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001112 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001113 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1114
1115 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001116
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001117 self.assertEqual(A.data['status'], 'MERGED')
1118 self.assertEqual(A.reported, 2)
1119 self.assertEqual(
1120 self.getJobFromHistory('nonvoting-project-merge').result,
1121 'SUCCESS')
1122 self.assertEqual(
1123 self.getJobFromHistory('nonvoting-project-test1').result,
1124 'SUCCESS')
1125 self.assertEqual(
1126 self.getJobFromHistory('nonvoting-project-test2').result,
1127 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001128
1129 def test_check_queue_success(self):
1130 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001131
James E. Blaire0487072012-08-29 17:38:31 -07001132 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1133 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1134
1135 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001136
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001137 self.assertEqual(A.data['status'], 'NEW')
1138 self.assertEqual(A.reported, 1)
1139 self.assertEqual(self.getJobFromHistory('project-merge').result,
1140 'SUCCESS')
1141 self.assertEqual(self.getJobFromHistory('project-test1').result,
1142 'SUCCESS')
1143 self.assertEqual(self.getJobFromHistory('project-test2').result,
1144 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001145
1146 def test_check_queue_failure(self):
1147 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001148
James E. Blaire0487072012-08-29 17:38:31 -07001149 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001150 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001151 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1152
1153 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001154
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001155 self.assertEqual(A.data['status'], 'NEW')
1156 self.assertEqual(A.reported, 1)
1157 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001158 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001159 self.assertEqual(self.getJobFromHistory('project-test1').result,
1160 'SUCCESS')
1161 self.assertEqual(self.getJobFromHistory('project-test2').result,
1162 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001163
1164 def test_dependent_behind_dequeue(self):
1165 "test that dependent changes behind dequeued changes work"
1166 # This complicated test is a reproduction of a real life bug
1167 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001168
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001169 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001170 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1171 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1172 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1173 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1174 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1175 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1176 D.setDependsOn(C, 1)
1177 E.setDependsOn(D, 1)
1178 A.addApproval('CRVW', 2)
1179 B.addApproval('CRVW', 2)
1180 C.addApproval('CRVW', 2)
1181 D.addApproval('CRVW', 2)
1182 E.addApproval('CRVW', 2)
1183 F.addApproval('CRVW', 2)
1184
1185 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001186
1187 # Change object re-use in the gerrit trigger is hidden if
1188 # changes are added in quick succession; waiting makes it more
1189 # like real life.
1190 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1191 self.waitUntilSettled()
1192 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1193 self.waitUntilSettled()
1194
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001195 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001196 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001197 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001198 self.waitUntilSettled()
1199
1200 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1201 self.waitUntilSettled()
1202 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1203 self.waitUntilSettled()
1204 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1205 self.waitUntilSettled()
1206 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1207 self.waitUntilSettled()
1208
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001209 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001210 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001211 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001212 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001213 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001214 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001215 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001216 self.waitUntilSettled()
1217
1218 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001219
1220 # Grab pointers to the jobs we want to release before
1221 # releasing any, because list indexes may change as
1222 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001223 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001224 a.release()
1225 b.release()
1226 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001227 self.waitUntilSettled()
1228
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001229 self.worker.hold_jobs_in_build = False
1230 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07001231 self.waitUntilSettled()
1232
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001233 self.assertEqual(A.data['status'], 'NEW')
1234 self.assertEqual(B.data['status'], 'MERGED')
1235 self.assertEqual(C.data['status'], 'MERGED')
1236 self.assertEqual(D.data['status'], 'MERGED')
1237 self.assertEqual(E.data['status'], 'MERGED')
1238 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001239
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001240 self.assertEqual(A.reported, 2)
1241 self.assertEqual(B.reported, 2)
1242 self.assertEqual(C.reported, 2)
1243 self.assertEqual(D.reported, 2)
1244 self.assertEqual(E.reported, 2)
1245 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001246
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001247 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1248 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001249
1250 def test_merger_repack(self):
1251 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001252
James E. Blair05fed602012-09-07 12:45:24 -07001253 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1254 A.addApproval('CRVW', 2)
1255 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1256 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001257 self.assertEqual(self.getJobFromHistory('project-merge').result,
1258 'SUCCESS')
1259 self.assertEqual(self.getJobFromHistory('project-test1').result,
1260 'SUCCESS')
1261 self.assertEqual(self.getJobFromHistory('project-test2').result,
1262 'SUCCESS')
1263 self.assertEqual(A.data['status'], 'MERGED')
1264 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001265 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07001266 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001267
Monty Taylorbc758832013-06-17 17:22:42 -04001268 path = os.path.join(self.git_root, "org/project")
1269 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001270
1271 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1272 A.addApproval('CRVW', 2)
1273 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1274 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001275 self.assertEqual(self.getJobFromHistory('project-merge').result,
1276 'SUCCESS')
1277 self.assertEqual(self.getJobFromHistory('project-test1').result,
1278 'SUCCESS')
1279 self.assertEqual(self.getJobFromHistory('project-test2').result,
1280 'SUCCESS')
1281 self.assertEqual(A.data['status'], 'MERGED')
1282 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001283
James E. Blair4886f282012-11-15 09:27:33 -08001284 def test_merger_repack_large_change(self):
1285 "Test that the merger works with large changes after a repack"
1286 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001287 # This test assumes the repo is already cloned; make sure it is
1288 url = self.sched.triggers['gerrit'].getGitUrl(
1289 self.sched.layout.projects['org/project1'])
James E. Blair4076e2b2014-01-28 12:42:20 -08001290 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08001291 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1292 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001293 path = os.path.join(self.upstream_root, "org/project1")
1294 print repack_repo(path)
1295 path = os.path.join(self.git_root, "org/project1")
1296 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001297
1298 A.addApproval('CRVW', 2)
1299 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1300 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001301 self.assertEqual(self.getJobFromHistory('project1-merge').result,
1302 'SUCCESS')
1303 self.assertEqual(self.getJobFromHistory('project1-test1').result,
1304 'SUCCESS')
1305 self.assertEqual(self.getJobFromHistory('project1-test2').result,
1306 'SUCCESS')
1307 self.assertEqual(A.data['status'], 'MERGED')
1308 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001309
James E. Blair7ee88a22012-09-12 18:59:31 +02001310 def test_nonexistent_job(self):
1311 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001312 # Set to the state immediately after a restart
1313 self.resetGearmanServer()
1314 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02001315
1316 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1317 A.addApproval('CRVW', 2)
1318 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1319 # There may be a thread about to report a lost change
1320 while A.reported < 2:
1321 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001322 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001323 self.assertFalse(job_names)
1324 self.assertEqual(A.data['status'], 'NEW')
1325 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001326 self.assertEmptyQueues()
1327
1328 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001329 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02001330 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1331 A.addApproval('CRVW', 2)
1332 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1333 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001334 self.assertEqual(self.getJobFromHistory('project-merge').result,
1335 'SUCCESS')
1336 self.assertEqual(self.getJobFromHistory('project-test1').result,
1337 'SUCCESS')
1338 self.assertEqual(self.getJobFromHistory('project-test2').result,
1339 'SUCCESS')
1340 self.assertEqual(A.data['status'], 'MERGED')
1341 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08001342
1343 def test_single_nonexistent_post_job(self):
1344 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08001345 e = {
1346 "type": "ref-updated",
1347 "submitter": {
1348 "name": "User Name",
1349 },
1350 "refUpdate": {
1351 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1352 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1353 "refName": "master",
1354 "project": "org/project",
1355 }
1356 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001357 # Set to the state immediately after a restart
1358 self.resetGearmanServer()
1359 self.launcher.negative_function_cache_ttl = 0
1360
James E. Blairf62d4282012-12-31 17:01:50 -08001361 self.fake_gerrit.addEvent(e)
1362 self.waitUntilSettled()
1363
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001364 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08001365
1366 def test_new_patchset_dequeues_old(self):
1367 "Test that a new patchset causes the old to be dequeued"
1368 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001369 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001370 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1371 M.setMerged()
1372
1373 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1374 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1375 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1376 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1377 A.addApproval('CRVW', 2)
1378 B.addApproval('CRVW', 2)
1379 C.addApproval('CRVW', 2)
1380 D.addApproval('CRVW', 2)
1381
1382 C.setDependsOn(B, 1)
1383 B.setDependsOn(A, 1)
1384 A.setDependsOn(M, 1)
1385
1386 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1387 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1388 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1389 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1390 self.waitUntilSettled()
1391
1392 B.addPatchset()
1393 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1394 self.waitUntilSettled()
1395
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001396 self.worker.hold_jobs_in_build = False
1397 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001398 self.waitUntilSettled()
1399
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001400 self.assertEqual(A.data['status'], 'MERGED')
1401 self.assertEqual(A.reported, 2)
1402 self.assertEqual(B.data['status'], 'NEW')
1403 self.assertEqual(B.reported, 2)
1404 self.assertEqual(C.data['status'], 'NEW')
1405 self.assertEqual(C.reported, 2)
1406 self.assertEqual(D.data['status'], 'MERGED')
1407 self.assertEqual(D.reported, 2)
1408 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001409
Antoine Mussobd86a312014-01-08 14:51:33 +01001410 def test_abandoned_change_dequeues(self):
1411 "Test that an abandoned change is dequeued"
1412
1413 self.worker.hold_jobs_in_build = True
1414
1415 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1416 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1417 self.waitUntilSettled()
1418 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1419 self.assertEqual(self.builds[0].name, 'project-merge')
1420
1421 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1422 self.waitUntilSettled()
1423
1424 # For debugging purposes...
1425 #for pipeline in self.sched.layout.pipelines.values():
1426 # for queue in pipeline.queues:
1427 # self.log.info("pipepline %s queue %s contents %s" % (
1428 # pipeline.name, queue.name, queue.queue))
1429
1430 self.worker.release('.*-merge')
1431 self.waitUntilSettled()
1432
1433 self.assertEqual(len(self.builds), 0, "No job running")
1434 self.assertEmptyQueues()
1435 self.assertEqual(len(self.history), 1, "Only one build in history")
1436 self.assertEqual(self.history[0].result, 'ABORTED',
1437 'Build should have been aborted')
1438 self.assertEqual(A.reported, 0, "Abandoned change should not report")
1439
Arx Cruzb1b010d2013-10-28 19:49:59 -02001440 def test_zuul_url_return(self):
1441 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001442 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001443 self.worker.hold_jobs_in_build = True
1444
1445 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1446 A.addApproval('CRVW', 2)
1447 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1448 self.waitUntilSettled()
1449
1450 self.assertEqual(len(self.builds), 1)
1451 for build in self.builds:
1452 self.assertTrue('ZUUL_URL' in build.parameters)
1453
1454 self.worker.hold_jobs_in_build = False
1455 self.worker.release()
1456 self.waitUntilSettled()
1457
James E. Blair2fa50962013-01-30 21:50:41 -08001458 def test_new_patchset_dequeues_old_on_head(self):
1459 "Test that a new patchset causes the old to be dequeued (at head)"
1460 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001461 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001462 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1463 M.setMerged()
1464 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1465 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1466 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1467 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1468 A.addApproval('CRVW', 2)
1469 B.addApproval('CRVW', 2)
1470 C.addApproval('CRVW', 2)
1471 D.addApproval('CRVW', 2)
1472
1473 C.setDependsOn(B, 1)
1474 B.setDependsOn(A, 1)
1475 A.setDependsOn(M, 1)
1476
1477 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1478 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1479 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1480 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1481 self.waitUntilSettled()
1482
1483 A.addPatchset()
1484 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1485 self.waitUntilSettled()
1486
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001487 self.worker.hold_jobs_in_build = False
1488 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001489 self.waitUntilSettled()
1490
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001491 self.assertEqual(A.data['status'], 'NEW')
1492 self.assertEqual(A.reported, 2)
1493 self.assertEqual(B.data['status'], 'NEW')
1494 self.assertEqual(B.reported, 2)
1495 self.assertEqual(C.data['status'], 'NEW')
1496 self.assertEqual(C.reported, 2)
1497 self.assertEqual(D.data['status'], 'MERGED')
1498 self.assertEqual(D.reported, 2)
1499 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001500
1501 def test_new_patchset_dequeues_old_without_dependents(self):
1502 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001503 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001504 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1505 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1506 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1507 A.addApproval('CRVW', 2)
1508 B.addApproval('CRVW', 2)
1509 C.addApproval('CRVW', 2)
1510
1511 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1512 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1513 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1514 self.waitUntilSettled()
1515
1516 B.addPatchset()
1517 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1518 self.waitUntilSettled()
1519
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001520 self.worker.hold_jobs_in_build = False
1521 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001522 self.waitUntilSettled()
1523
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001524 self.assertEqual(A.data['status'], 'MERGED')
1525 self.assertEqual(A.reported, 2)
1526 self.assertEqual(B.data['status'], 'NEW')
1527 self.assertEqual(B.reported, 2)
1528 self.assertEqual(C.data['status'], 'MERGED')
1529 self.assertEqual(C.reported, 2)
1530 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001531
1532 def test_new_patchset_dequeues_old_independent_queue(self):
1533 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001534 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001535 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1536 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1537 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1538 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1539 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1540 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1541 self.waitUntilSettled()
1542
1543 B.addPatchset()
1544 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1545 self.waitUntilSettled()
1546
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001547 self.worker.hold_jobs_in_build = False
1548 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001549 self.waitUntilSettled()
1550
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001551 self.assertEqual(A.data['status'], 'NEW')
1552 self.assertEqual(A.reported, 1)
1553 self.assertEqual(B.data['status'], 'NEW')
1554 self.assertEqual(B.reported, 1)
1555 self.assertEqual(C.data['status'], 'NEW')
1556 self.assertEqual(C.reported, 1)
1557 self.assertEqual(len(self.history), 10)
1558 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001559
James E. Blair18c64442014-03-18 10:14:45 -07001560 def test_noop_job(self):
1561 "Test that the internal noop job works"
1562 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
1563 A.addApproval('CRVW', 2)
1564 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1565 self.waitUntilSettled()
1566
1567 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1568 self.assertTrue(self.sched._areAllBuildsComplete())
1569 self.assertEqual(len(self.history), 0)
1570 self.assertEqual(A.data['status'], 'MERGED')
1571 self.assertEqual(A.reported, 2)
1572
James E. Blair7d0dedc2013-02-21 17:26:09 -08001573 def test_zuul_refs(self):
1574 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001575 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001576 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1577 M1.setMerged()
1578 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1579 M2.setMerged()
1580
1581 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1582 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1583 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1584 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1585 A.addApproval('CRVW', 2)
1586 B.addApproval('CRVW', 2)
1587 C.addApproval('CRVW', 2)
1588 D.addApproval('CRVW', 2)
1589 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1590 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1591 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1592 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1593
1594 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001595 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001596 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001597 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001598 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001599 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001600 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001601 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001602 self.waitUntilSettled()
1603
James E. Blair7d0dedc2013-02-21 17:26:09 -08001604 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001605 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001606 if x.parameters['ZUUL_CHANGE'] == '3':
1607 a_zref = x.parameters['ZUUL_REF']
1608 if x.parameters['ZUUL_CHANGE'] == '4':
1609 b_zref = x.parameters['ZUUL_REF']
1610 if x.parameters['ZUUL_CHANGE'] == '5':
1611 c_zref = x.parameters['ZUUL_REF']
1612 if x.parameters['ZUUL_CHANGE'] == '6':
1613 d_zref = x.parameters['ZUUL_REF']
1614
1615 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001616 self.assertIsNotNone(a_zref)
1617 self.assertIsNotNone(b_zref)
1618 self.assertIsNotNone(c_zref)
1619 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001620
1621 # And they should all be different
1622 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001623 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001624
1625 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001626 self.assertTrue(self.ref_has_change(a_zref, A))
1627 self.assertFalse(self.ref_has_change(a_zref, B))
1628 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001629
1630 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001631 self.assertTrue(self.ref_has_change(b_zref, A))
1632 self.assertTrue(self.ref_has_change(b_zref, B))
1633 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001634
1635 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001636 self.assertTrue(self.ref_has_change(c_zref, A))
1637 self.assertTrue(self.ref_has_change(c_zref, B))
1638 self.assertTrue(self.ref_has_change(c_zref, C))
1639 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001640
1641 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001642 self.assertTrue(self.ref_has_change(d_zref, A))
1643 self.assertTrue(self.ref_has_change(d_zref, B))
1644 self.assertTrue(self.ref_has_change(d_zref, C))
1645 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001646
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001647 self.worker.hold_jobs_in_build = False
1648 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08001649 self.waitUntilSettled()
1650
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001651 self.assertEqual(A.data['status'], 'MERGED')
1652 self.assertEqual(A.reported, 2)
1653 self.assertEqual(B.data['status'], 'MERGED')
1654 self.assertEqual(B.reported, 2)
1655 self.assertEqual(C.data['status'], 'MERGED')
1656 self.assertEqual(C.reported, 2)
1657 self.assertEqual(D.data['status'], 'MERGED')
1658 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08001659
James E. Blair4a28a882013-08-23 15:17:33 -07001660 def test_rerun_on_error(self):
1661 "Test that if a worker fails to run a job, it is run again"
1662 self.worker.hold_jobs_in_build = True
1663 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1664 A.addApproval('CRVW', 2)
1665 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1666 self.waitUntilSettled()
1667
1668 self.builds[0].run_error = True
1669 self.worker.hold_jobs_in_build = False
1670 self.worker.release()
1671 self.waitUntilSettled()
1672 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
1673 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
1674
James E. Blair412e5582013-04-22 15:50:12 -07001675 def test_statsd(self):
1676 "Test each of the statsd methods used in the scheduler"
1677 import extras
1678 statsd = extras.try_import('statsd.statsd')
1679 statsd.incr('test-incr')
1680 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07001681 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07001682 self.assertReportedStat('test-incr', '1|c')
1683 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07001684 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07001685
James E. Blairdad52252014-02-07 16:59:17 -08001686 def test_stuck_job_cleanup(self):
1687 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07001688 # This job won't be registered at startup because it is not in
1689 # the standard layout, but we need it to already be registerd
1690 # for when we reconfigure, as that is when Zuul will attempt
1691 # to run the new job.
1692 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001693 self.gearman_server.hold_jobs_in_queue = True
1694 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1695 A.addApproval('CRVW', 2)
1696 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1697 self.waitUntilSettled()
1698 self.assertEqual(len(self.gearman_server.getQueue()), 1)
1699
1700 self.config.set('zuul', 'layout_config',
1701 'tests/fixtures/layout-no-jobs.yaml')
1702 self.sched.reconfigure(self.config)
1703 self.waitUntilSettled()
1704
James E. Blair18c64442014-03-18 10:14:45 -07001705 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001706 self.waitUntilSettled()
1707 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1708 self.assertTrue(self.sched._areAllBuildsComplete())
1709
1710 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07001711 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001712 self.assertEqual(self.history[0].result, 'SUCCESS')
1713
James E. Blair70c71582013-03-06 08:50:50 -08001714 def test_file_jobs(self):
1715 "Test that file jobs run only when appropriate"
1716 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1717 A.addPatchset(['pip-requires'])
1718 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1719 A.addApproval('CRVW', 2)
1720 B.addApproval('CRVW', 2)
1721 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1722 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1723 self.waitUntilSettled()
1724
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001725 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08001726 if x.name == 'project-testfile']
1727
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001728 self.assertEqual(len(testfile_jobs), 1)
1729 self.assertEqual(testfile_jobs[0].changes, '1,2')
1730 self.assertEqual(A.data['status'], 'MERGED')
1731 self.assertEqual(A.reported, 2)
1732 self.assertEqual(B.data['status'], 'MERGED')
1733 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07001734
1735 def test_test_config(self):
1736 "Test that we can test the config"
1737 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07001738 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07001739 sched.registerTrigger(None, 'timer')
Clark Boylanb640e052014-04-03 16:41:46 -07001740 sched.testConfig(self.config.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001741
1742 def test_build_description(self):
1743 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001744 self.worker.registerFunction('set_description:' +
1745 self.worker.worker_id)
1746
1747 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1748 A.addApproval('CRVW', 2)
1749 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1750 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001751 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001752 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001753 self.assertTrue(re.search("Branch.*master", desc))
1754 self.assertTrue(re.search("Pipeline.*gate", desc))
1755 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
1756 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
1757 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
1758 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001759
James E. Blairc8a1e052014-02-25 09:29:26 -08001760 def test_queue_names(self):
1761 "Test shared change queue names"
1762 project1 = self.sched.layout.projects['org/project1']
1763 project2 = self.sched.layout.projects['org/project2']
1764 q1 = self.sched.layout.pipelines['gate'].getQueue(project1)
1765 q2 = self.sched.layout.pipelines['gate'].getQueue(project2)
1766 self.assertEqual(q1.name, 'integration')
1767 self.assertEqual(q2.name, 'integration')
1768
1769 self.config.set('zuul', 'layout_config',
1770 'tests/fixtures/layout-bad-queue.yaml')
1771 with testtools.ExpectedException(
1772 Exception, "More than one name assigned to change queue"):
1773 self.sched.reconfigure(self.config)
1774
James E. Blair64ed6f22013-07-10 14:07:23 -07001775 def test_queue_precedence(self):
1776 "Test that queue precedence works"
1777
1778 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07001779 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07001780 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1781 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1782 A.addApproval('CRVW', 2)
1783 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1784
1785 self.waitUntilSettled()
1786 self.gearman_server.hold_jobs_in_queue = False
1787 self.gearman_server.release()
1788 self.waitUntilSettled()
1789
James E. Blair8de58bd2013-07-18 16:23:33 -07001790 # Run one build at a time to ensure non-race order:
1791 for x in range(6):
1792 self.release(self.builds[0])
1793 self.waitUntilSettled()
1794 self.worker.hold_jobs_in_build = False
1795 self.waitUntilSettled()
1796
James E. Blair64ed6f22013-07-10 14:07:23 -07001797 self.log.debug(self.history)
1798 self.assertEqual(self.history[0].pipeline, 'gate')
1799 self.assertEqual(self.history[1].pipeline, 'check')
1800 self.assertEqual(self.history[2].pipeline, 'gate')
1801 self.assertEqual(self.history[3].pipeline, 'gate')
1802 self.assertEqual(self.history[4].pipeline, 'check')
1803 self.assertEqual(self.history[5].pipeline, 'check')
1804
Clark Boylana5edbe42014-06-03 16:39:10 -07001805 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07001806 "Test that we can retrieve JSON status info"
1807 self.worker.hold_jobs_in_build = True
1808 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1809 A.addApproval('CRVW', 2)
1810 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1811 self.waitUntilSettled()
1812
1813 port = self.webapp.server.socket.getsockname()[1]
1814
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04001815 req = urllib2.Request("http://localhost:%s/status.json" % port)
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04001816 f = urllib2.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07001817 headers = f.info()
1818 self.assertIn('Content-Length', headers)
1819 self.assertIn('Content-Type', headers)
1820 self.assertEqual(headers['Content-Type'],
1821 'application/json; charset=UTF-8')
1822 self.assertIn('Last-Modified', headers)
James E. Blair1843a552013-07-03 14:19:52 -07001823 data = f.read()
1824
1825 self.worker.hold_jobs_in_build = False
1826 self.worker.release()
1827 self.waitUntilSettled()
1828
1829 data = json.loads(data)
1830 status_jobs = set()
1831 for p in data['pipelines']:
1832 for q in p['change_queues']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08001833 if q['dependent']:
1834 self.assertEqual(q['window'], 20)
1835 else:
1836 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07001837 for head in q['heads']:
1838 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08001839 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001840 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07001841 for job in change['jobs']:
1842 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001843 self.assertIn('project-merge', status_jobs)
1844 self.assertIn('project-test1', status_jobs)
1845 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07001846
James E. Blairc3d428e2013-12-03 15:06:48 -08001847 def test_merging_queues(self):
1848 "Test that transitively-connected change queues are merged"
1849 self.config.set('zuul', 'layout_config',
1850 'tests/fixtures/layout-merge-queues.yaml')
1851 self.sched.reconfigure(self.config)
1852 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
1853
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001854 def test_node_label(self):
1855 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001856 self.worker.registerFunction('build:node-project-test1:debian')
1857
1858 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
1859 A.addApproval('CRVW', 2)
1860 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1861 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001862
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001863 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
1864 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
1865 'debian')
1866 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07001867
1868 def test_live_reconfiguration(self):
1869 "Test that live reconfiguration works"
1870 self.worker.hold_jobs_in_build = True
1871 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1872 A.addApproval('CRVW', 2)
1873 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1874 self.waitUntilSettled()
1875
1876 self.sched.reconfigure(self.config)
1877
1878 self.worker.hold_jobs_in_build = False
1879 self.worker.release()
1880 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001881 self.assertEqual(self.getJobFromHistory('project-merge').result,
1882 'SUCCESS')
1883 self.assertEqual(self.getJobFromHistory('project-test1').result,
1884 'SUCCESS')
1885 self.assertEqual(self.getJobFromHistory('project-test2').result,
1886 'SUCCESS')
1887 self.assertEqual(A.data['status'], 'MERGED')
1888 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07001889
James E. Blaire712d9f2013-07-31 11:40:11 -07001890 def test_live_reconfiguration_functions(self):
1891 "Test live reconfiguration with a custom function"
1892 self.worker.registerFunction('build:node-project-test1:debian')
1893 self.worker.registerFunction('build:node-project-test1:wheezy')
1894 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
1895 A.addApproval('CRVW', 2)
1896 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1897 self.waitUntilSettled()
1898
1899 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
1900 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
1901 'debian')
1902 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
1903
1904 self.config.set('zuul', 'layout_config',
1905 'tests/fixtures/layout-live-'
1906 'reconfiguration-functions.yaml')
1907 self.sched.reconfigure(self.config)
1908 self.worker.build_history = []
1909
1910 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
1911 B.addApproval('CRVW', 2)
1912 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1913 self.waitUntilSettled()
1914
1915 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
1916 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
1917 'wheezy')
1918 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
1919
James E. Blair287c06d2013-07-24 10:39:30 -07001920 def test_delayed_repo_init(self):
1921 self.config.set('zuul', 'layout_config',
1922 'tests/fixtures/layout-delayed-repo-init.yaml')
1923 self.sched.reconfigure(self.config)
1924
1925 self.init_repo("org/new-project")
1926 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
1927
1928 A.addApproval('CRVW', 2)
1929 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1930 self.waitUntilSettled()
1931 self.assertEqual(self.getJobFromHistory('project-merge').result,
1932 'SUCCESS')
1933 self.assertEqual(self.getJobFromHistory('project-test1').result,
1934 'SUCCESS')
1935 self.assertEqual(self.getJobFromHistory('project-test2').result,
1936 'SUCCESS')
1937 self.assertEqual(A.data['status'], 'MERGED')
1938 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07001939
Clark Boylan6dbbc482013-10-18 10:57:31 -07001940 def test_repo_deleted(self):
1941 self.config.set('zuul', 'layout_config',
1942 'tests/fixtures/layout-repo-deleted.yaml')
1943 self.sched.reconfigure(self.config)
1944
1945 self.init_repo("org/delete-project")
1946 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
1947
1948 A.addApproval('CRVW', 2)
1949 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1950 self.waitUntilSettled()
1951 self.assertEqual(self.getJobFromHistory('project-merge').result,
1952 'SUCCESS')
1953 self.assertEqual(self.getJobFromHistory('project-test1').result,
1954 'SUCCESS')
1955 self.assertEqual(self.getJobFromHistory('project-test2').result,
1956 'SUCCESS')
1957 self.assertEqual(A.data['status'], 'MERGED')
1958 self.assertEqual(A.reported, 2)
1959
1960 # Delete org/new-project zuul repo. Should be recloned.
1961 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
1962
1963 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
1964
1965 B.addApproval('CRVW', 2)
1966 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1967 self.waitUntilSettled()
1968 self.assertEqual(self.getJobFromHistory('project-merge').result,
1969 'SUCCESS')
1970 self.assertEqual(self.getJobFromHistory('project-test1').result,
1971 'SUCCESS')
1972 self.assertEqual(self.getJobFromHistory('project-test2').result,
1973 'SUCCESS')
1974 self.assertEqual(B.data['status'], 'MERGED')
1975 self.assertEqual(B.reported, 2)
1976
James E. Blair63bb0ef2013-07-29 17:14:51 -07001977 def test_timer(self):
1978 "Test that a periodic job is triggered"
1979 self.worker.hold_jobs_in_build = True
1980 self.config.set('zuul', 'layout_config',
1981 'tests/fixtures/layout-timer.yaml')
1982 self.sched.reconfigure(self.config)
1983 self.registerJobs()
1984
Clark Boylan3ee090a2014-04-03 20:55:09 -07001985 # The pipeline triggers every second, so we should have seen
1986 # several by now.
1987 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07001988 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07001989
1990 self.assertEqual(len(self.builds), 2)
1991
James E. Blair63bb0ef2013-07-29 17:14:51 -07001992 port = self.webapp.server.socket.getsockname()[1]
1993
1994 f = urllib.urlopen("http://localhost:%s/status.json" % port)
1995 data = f.read()
1996
1997 self.worker.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07001998 # Stop queuing timer triggered jobs so that the assertions
1999 # below don't race against more jobs being queued.
2000 self.config.set('zuul', 'layout_config',
2001 'tests/fixtures/layout-no-timer.yaml')
2002 self.sched.reconfigure(self.config)
2003 self.registerJobs()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002004 self.worker.release()
2005 self.waitUntilSettled()
2006
2007 self.assertEqual(self.getJobFromHistory(
2008 'project-bitrot-stable-old').result, 'SUCCESS')
2009 self.assertEqual(self.getJobFromHistory(
2010 'project-bitrot-stable-older').result, 'SUCCESS')
2011
2012 data = json.loads(data)
2013 status_jobs = set()
2014 for p in data['pipelines']:
2015 for q in p['change_queues']:
2016 for head in q['heads']:
2017 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07002018 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002019 for job in change['jobs']:
2020 status_jobs.add(job['name'])
2021 self.assertIn('project-bitrot-stable-old', status_jobs)
2022 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002023
James E. Blair4f6033c2014-03-27 15:49:09 -07002024 def test_idle(self):
2025 "Test that frequent periodic jobs work"
2026 self.worker.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002027
Clark Boylan3ee090a2014-04-03 20:55:09 -07002028 for x in range(1, 3):
2029 # Test that timer triggers periodic jobs even across
2030 # layout config reloads.
2031 # Start timer trigger
2032 self.config.set('zuul', 'layout_config',
2033 'tests/fixtures/layout-idle.yaml')
2034 self.sched.reconfigure(self.config)
2035 self.registerJobs()
James E. Blair4f6033c2014-03-27 15:49:09 -07002036
Clark Boylan3ee090a2014-04-03 20:55:09 -07002037 # The pipeline triggers every second, so we should have seen
2038 # several by now.
2039 time.sleep(5)
2040 self.waitUntilSettled()
2041
2042 # Stop queuing timer triggered jobs so that the assertions
2043 # below don't race against more jobs being queued.
2044 self.config.set('zuul', 'layout_config',
2045 'tests/fixtures/layout-no-timer.yaml')
2046 self.sched.reconfigure(self.config)
2047 self.registerJobs()
2048
2049 self.assertEqual(len(self.builds), 2)
2050 self.worker.release('.*')
2051 self.waitUntilSettled()
2052 self.assertEqual(len(self.builds), 0)
2053 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002054
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002055 def test_check_smtp_pool(self):
2056 self.config.set('zuul', 'layout_config',
2057 'tests/fixtures/layout-smtp.yaml')
2058 self.sched.reconfigure(self.config)
2059
2060 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2061 self.waitUntilSettled()
2062
2063 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2064 self.waitUntilSettled()
2065
James E. Blairff80a2f2013-12-27 13:24:06 -08002066 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002067
2068 # A.messages only holds what FakeGerrit places in it. Thus we
2069 # work on the knowledge of what the first message should be as
2070 # it is only configured to go to SMTP.
2071
2072 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002073 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002074 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002075 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002076 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002077 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002078
2079 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002080 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002081 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002082 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002083 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002084 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002085
James E. Blaire5910202013-12-27 09:50:31 -08002086 def test_timer_smtp(self):
2087 "Test that a periodic job is triggered"
Clark Boylan3ee090a2014-04-03 20:55:09 -07002088 self.worker.hold_jobs_in_build = True
James E. Blaire5910202013-12-27 09:50:31 -08002089 self.config.set('zuul', 'layout_config',
2090 'tests/fixtures/layout-timer-smtp.yaml')
2091 self.sched.reconfigure(self.config)
2092 self.registerJobs()
2093
Clark Boylan3ee090a2014-04-03 20:55:09 -07002094 # The pipeline triggers every second, so we should have seen
2095 # several by now.
2096 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002097 self.waitUntilSettled()
2098
Clark Boylan3ee090a2014-04-03 20:55:09 -07002099 self.assertEqual(len(self.builds), 2)
2100 self.worker.release('.*')
2101 self.waitUntilSettled()
2102 self.assertEqual(len(self.history), 2)
2103
James E. Blaire5910202013-12-27 09:50:31 -08002104 self.assertEqual(self.getJobFromHistory(
2105 'project-bitrot-stable-old').result, 'SUCCESS')
2106 self.assertEqual(self.getJobFromHistory(
2107 'project-bitrot-stable-older').result, 'SUCCESS')
2108
James E. Blairff80a2f2013-12-27 13:24:06 -08002109 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002110
2111 # A.messages only holds what FakeGerrit places in it. Thus we
2112 # work on the knowledge of what the first message should be as
2113 # it is only configured to go to SMTP.
2114
2115 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002116 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002117 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002118 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002119 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002120 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002121
Clark Boylan3ee090a2014-04-03 20:55:09 -07002122 # Stop queuing timer triggered jobs and let any that may have
2123 # queued through so that end of test assertions pass.
2124 self.config.set('zuul', 'layout_config',
2125 'tests/fixtures/layout-no-timer.yaml')
2126 self.sched.reconfigure(self.config)
2127 self.registerJobs()
2128 self.worker.release('.*')
2129 self.waitUntilSettled()
2130
James E. Blairad28e912013-11-27 10:43:22 -08002131 def test_client_enqueue(self):
2132 "Test that the RPC client can enqueue a change"
2133 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2134 A.addApproval('CRVW', 2)
2135 A.addApproval('APRV', 1)
2136
2137 client = zuul.rpcclient.RPCClient('127.0.0.1',
2138 self.gearman_server.port)
2139 r = client.enqueue(pipeline='gate',
2140 project='org/project',
2141 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002142 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002143 self.waitUntilSettled()
2144 self.assertEqual(self.getJobFromHistory('project-merge').result,
2145 'SUCCESS')
2146 self.assertEqual(self.getJobFromHistory('project-test1').result,
2147 'SUCCESS')
2148 self.assertEqual(self.getJobFromHistory('project-test2').result,
2149 'SUCCESS')
2150 self.assertEqual(A.data['status'], 'MERGED')
2151 self.assertEqual(A.reported, 2)
2152 self.assertEqual(r, True)
2153
2154 def test_client_enqueue_negative(self):
2155 "Test that the RPC client returns errors"
2156 client = zuul.rpcclient.RPCClient('127.0.0.1',
2157 self.gearman_server.port)
2158 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2159 "Invalid project"):
2160 r = client.enqueue(pipeline='gate',
2161 project='project-does-not-exist',
2162 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002163 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002164 client.shutdown()
2165 self.assertEqual(r, False)
2166
2167 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2168 "Invalid pipeline"):
2169 r = client.enqueue(pipeline='pipeline-does-not-exist',
2170 project='org/project',
2171 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002172 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002173 client.shutdown()
2174 self.assertEqual(r, False)
2175
2176 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2177 "Invalid trigger"):
2178 r = client.enqueue(pipeline='gate',
2179 project='org/project',
2180 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08002181 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002182 client.shutdown()
2183 self.assertEqual(r, False)
2184
2185 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2186 "Invalid change"):
2187 r = client.enqueue(pipeline='gate',
2188 project='org/project',
2189 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002190 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002191 client.shutdown()
2192 self.assertEqual(r, False)
2193
2194 self.waitUntilSettled()
2195 self.assertEqual(len(self.history), 0)
2196 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08002197
2198 def test_client_promote(self):
2199 "Test that the RPC client can promote a change"
2200 self.worker.hold_jobs_in_build = True
2201 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2202 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2203 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2204 A.addApproval('CRVW', 2)
2205 B.addApproval('CRVW', 2)
2206 C.addApproval('CRVW', 2)
2207
2208 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2209 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2210 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2211
2212 self.waitUntilSettled()
2213
Sean Daguef39b9ca2014-01-10 21:34:35 -05002214 items = self.sched.layout.pipelines['gate'].getAllItems()
2215 enqueue_times = {}
2216 for item in items:
2217 enqueue_times[str(item.change)] = item.enqueue_time
2218
James E. Blair36658cf2013-12-06 17:53:48 -08002219 client = zuul.rpcclient.RPCClient('127.0.0.1',
2220 self.gearman_server.port)
2221 r = client.promote(pipeline='gate',
2222 change_ids=['2,1', '3,1'])
2223
Sean Daguef39b9ca2014-01-10 21:34:35 -05002224 # ensure that enqueue times are durable
2225 items = self.sched.layout.pipelines['gate'].getAllItems()
2226 for item in items:
2227 self.assertEqual(
2228 enqueue_times[str(item.change)], item.enqueue_time)
2229
James E. Blair78acec92014-02-06 07:11:32 -08002230 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08002231 self.worker.release('.*-merge')
2232 self.waitUntilSettled()
2233 self.worker.release('.*-merge')
2234 self.waitUntilSettled()
2235 self.worker.release('.*-merge')
2236 self.waitUntilSettled()
2237
2238 self.assertEqual(len(self.builds), 6)
2239 self.assertEqual(self.builds[0].name, 'project-test1')
2240 self.assertEqual(self.builds[1].name, 'project-test2')
2241 self.assertEqual(self.builds[2].name, 'project-test1')
2242 self.assertEqual(self.builds[3].name, 'project-test2')
2243 self.assertEqual(self.builds[4].name, 'project-test1')
2244 self.assertEqual(self.builds[5].name, 'project-test2')
2245
2246 self.assertTrue(self.job_has_changes(self.builds[0], B))
2247 self.assertFalse(self.job_has_changes(self.builds[0], A))
2248 self.assertFalse(self.job_has_changes(self.builds[0], C))
2249
2250 self.assertTrue(self.job_has_changes(self.builds[2], B))
2251 self.assertTrue(self.job_has_changes(self.builds[2], C))
2252 self.assertFalse(self.job_has_changes(self.builds[2], A))
2253
2254 self.assertTrue(self.job_has_changes(self.builds[4], B))
2255 self.assertTrue(self.job_has_changes(self.builds[4], C))
2256 self.assertTrue(self.job_has_changes(self.builds[4], A))
2257
2258 self.worker.release()
2259 self.waitUntilSettled()
2260
2261 self.assertEqual(A.data['status'], 'MERGED')
2262 self.assertEqual(A.reported, 2)
2263 self.assertEqual(B.data['status'], 'MERGED')
2264 self.assertEqual(B.reported, 2)
2265 self.assertEqual(C.data['status'], 'MERGED')
2266 self.assertEqual(C.reported, 2)
2267
2268 client.shutdown()
2269 self.assertEqual(r, True)
2270
2271 def test_client_promote_dependent(self):
2272 "Test that the RPC client can promote a dependent change"
2273 # C (depends on B) -> B -> A ; then promote C to get:
2274 # A -> C (depends on B) -> B
2275 self.worker.hold_jobs_in_build = True
2276 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2277 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2278 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2279
2280 C.setDependsOn(B, 1)
2281
2282 A.addApproval('CRVW', 2)
2283 B.addApproval('CRVW', 2)
2284 C.addApproval('CRVW', 2)
2285
2286 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2287 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2288 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2289
2290 self.waitUntilSettled()
2291
2292 client = zuul.rpcclient.RPCClient('127.0.0.1',
2293 self.gearman_server.port)
2294 r = client.promote(pipeline='gate',
2295 change_ids=['3,1'])
2296
James E. Blair78acec92014-02-06 07:11:32 -08002297 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08002298 self.worker.release('.*-merge')
2299 self.waitUntilSettled()
2300 self.worker.release('.*-merge')
2301 self.waitUntilSettled()
2302 self.worker.release('.*-merge')
2303 self.waitUntilSettled()
2304
2305 self.assertEqual(len(self.builds), 6)
2306 self.assertEqual(self.builds[0].name, 'project-test1')
2307 self.assertEqual(self.builds[1].name, 'project-test2')
2308 self.assertEqual(self.builds[2].name, 'project-test1')
2309 self.assertEqual(self.builds[3].name, 'project-test2')
2310 self.assertEqual(self.builds[4].name, 'project-test1')
2311 self.assertEqual(self.builds[5].name, 'project-test2')
2312
2313 self.assertTrue(self.job_has_changes(self.builds[0], B))
2314 self.assertFalse(self.job_has_changes(self.builds[0], A))
2315 self.assertFalse(self.job_has_changes(self.builds[0], C))
2316
2317 self.assertTrue(self.job_has_changes(self.builds[2], B))
2318 self.assertTrue(self.job_has_changes(self.builds[2], C))
2319 self.assertFalse(self.job_has_changes(self.builds[2], A))
2320
2321 self.assertTrue(self.job_has_changes(self.builds[4], B))
2322 self.assertTrue(self.job_has_changes(self.builds[4], C))
2323 self.assertTrue(self.job_has_changes(self.builds[4], A))
2324
2325 self.worker.release()
2326 self.waitUntilSettled()
2327
2328 self.assertEqual(A.data['status'], 'MERGED')
2329 self.assertEqual(A.reported, 2)
2330 self.assertEqual(B.data['status'], 'MERGED')
2331 self.assertEqual(B.reported, 2)
2332 self.assertEqual(C.data['status'], 'MERGED')
2333 self.assertEqual(C.reported, 2)
2334
2335 client.shutdown()
2336 self.assertEqual(r, True)
2337
2338 def test_client_promote_negative(self):
2339 "Test that the RPC client returns errors for promotion"
2340 self.worker.hold_jobs_in_build = True
2341 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2342 A.addApproval('CRVW', 2)
2343 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2344 self.waitUntilSettled()
2345
2346 client = zuul.rpcclient.RPCClient('127.0.0.1',
2347 self.gearman_server.port)
2348
2349 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
2350 r = client.promote(pipeline='nonexistent',
2351 change_ids=['2,1', '3,1'])
2352 client.shutdown()
2353 self.assertEqual(r, False)
2354
2355 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
2356 r = client.promote(pipeline='gate',
2357 change_ids=['4,1'])
2358 client.shutdown()
2359 self.assertEqual(r, False)
2360
2361 self.worker.hold_jobs_in_build = False
2362 self.worker.release()
2363 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08002364
2365 def test_queue_rate_limiting(self):
2366 "Test that DependentPipelines are rate limited with dep across window"
2367 self.config.set('zuul', 'layout_config',
2368 'tests/fixtures/layout-rate-limit.yaml')
2369 self.sched.reconfigure(self.config)
2370 self.worker.hold_jobs_in_build = True
2371 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2372 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2373 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2374
2375 C.setDependsOn(B, 1)
2376 self.worker.addFailTest('project-test1', A)
2377
2378 A.addApproval('CRVW', 2)
2379 B.addApproval('CRVW', 2)
2380 C.addApproval('CRVW', 2)
2381
2382 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2383 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2384 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2385 self.waitUntilSettled()
2386
2387 # Only A and B will have their merge jobs queued because
2388 # window is 2.
2389 self.assertEqual(len(self.builds), 2)
2390 self.assertEqual(self.builds[0].name, 'project-merge')
2391 self.assertEqual(self.builds[1].name, 'project-merge')
2392
2393 self.worker.release('.*-merge')
2394 self.waitUntilSettled()
2395 self.worker.release('.*-merge')
2396 self.waitUntilSettled()
2397
2398 # Only A and B will have their test jobs queued because
2399 # window is 2.
2400 self.assertEqual(len(self.builds), 4)
2401 self.assertEqual(self.builds[0].name, 'project-test1')
2402 self.assertEqual(self.builds[1].name, 'project-test2')
2403 self.assertEqual(self.builds[2].name, 'project-test1')
2404 self.assertEqual(self.builds[3].name, 'project-test2')
2405
2406 self.worker.release('project-.*')
2407 self.waitUntilSettled()
2408
2409 queue = self.sched.layout.pipelines['gate'].queues[0]
2410 # A failed so window is reduced by 1 to 1.
2411 self.assertEqual(queue.window, 1)
2412 self.assertEqual(queue.window_floor, 1)
2413 self.assertEqual(A.data['status'], 'NEW')
2414
2415 # Gate is reset and only B's merge job is queued because
2416 # window shrunk to 1.
2417 self.assertEqual(len(self.builds), 1)
2418 self.assertEqual(self.builds[0].name, 'project-merge')
2419
2420 self.worker.release('.*-merge')
2421 self.waitUntilSettled()
2422
2423 # Only B's test jobs are queued because window is still 1.
2424 self.assertEqual(len(self.builds), 2)
2425 self.assertEqual(self.builds[0].name, 'project-test1')
2426 self.assertEqual(self.builds[1].name, 'project-test2')
2427
2428 self.worker.release('project-.*')
2429 self.waitUntilSettled()
2430
2431 # B was successfully merged so window is increased to 2.
2432 self.assertEqual(queue.window, 2)
2433 self.assertEqual(queue.window_floor, 1)
2434 self.assertEqual(B.data['status'], 'MERGED')
2435
2436 # Only C is left and its merge job is queued.
2437 self.assertEqual(len(self.builds), 1)
2438 self.assertEqual(self.builds[0].name, 'project-merge')
2439
2440 self.worker.release('.*-merge')
2441 self.waitUntilSettled()
2442
2443 # After successful merge job the test jobs for C are queued.
2444 self.assertEqual(len(self.builds), 2)
2445 self.assertEqual(self.builds[0].name, 'project-test1')
2446 self.assertEqual(self.builds[1].name, 'project-test2')
2447
2448 self.worker.release('project-.*')
2449 self.waitUntilSettled()
2450
2451 # C successfully merged so window is bumped to 3.
2452 self.assertEqual(queue.window, 3)
2453 self.assertEqual(queue.window_floor, 1)
2454 self.assertEqual(C.data['status'], 'MERGED')
2455
2456 def test_queue_rate_limiting_dependent(self):
2457 "Test that DependentPipelines are rate limited with dep in window"
2458 self.config.set('zuul', 'layout_config',
2459 'tests/fixtures/layout-rate-limit.yaml')
2460 self.sched.reconfigure(self.config)
2461 self.worker.hold_jobs_in_build = True
2462 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2463 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2464 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2465
2466 B.setDependsOn(A, 1)
2467
2468 self.worker.addFailTest('project-test1', A)
2469
2470 A.addApproval('CRVW', 2)
2471 B.addApproval('CRVW', 2)
2472 C.addApproval('CRVW', 2)
2473
2474 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2475 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2476 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2477 self.waitUntilSettled()
2478
2479 # Only A and B will have their merge jobs queued because
2480 # window is 2.
2481 self.assertEqual(len(self.builds), 2)
2482 self.assertEqual(self.builds[0].name, 'project-merge')
2483 self.assertEqual(self.builds[1].name, 'project-merge')
2484
2485 self.worker.release('.*-merge')
2486 self.waitUntilSettled()
2487 self.worker.release('.*-merge')
2488 self.waitUntilSettled()
2489
2490 # Only A and B will have their test jobs queued because
2491 # window is 2.
2492 self.assertEqual(len(self.builds), 4)
2493 self.assertEqual(self.builds[0].name, 'project-test1')
2494 self.assertEqual(self.builds[1].name, 'project-test2')
2495 self.assertEqual(self.builds[2].name, 'project-test1')
2496 self.assertEqual(self.builds[3].name, 'project-test2')
2497
2498 self.worker.release('project-.*')
2499 self.waitUntilSettled()
2500
2501 queue = self.sched.layout.pipelines['gate'].queues[0]
2502 # A failed so window is reduced by 1 to 1.
2503 self.assertEqual(queue.window, 1)
2504 self.assertEqual(queue.window_floor, 1)
2505 self.assertEqual(A.data['status'], 'NEW')
2506 self.assertEqual(B.data['status'], 'NEW')
2507
2508 # Gate is reset and only C's merge job is queued because
2509 # window shrunk to 1 and A and B were dequeued.
2510 self.assertEqual(len(self.builds), 1)
2511 self.assertEqual(self.builds[0].name, 'project-merge')
2512
2513 self.worker.release('.*-merge')
2514 self.waitUntilSettled()
2515
2516 # Only C's test jobs are queued because window is still 1.
2517 self.assertEqual(len(self.builds), 2)
2518 self.assertEqual(self.builds[0].name, 'project-test1')
2519 self.assertEqual(self.builds[1].name, 'project-test2')
2520
2521 self.worker.release('project-.*')
2522 self.waitUntilSettled()
2523
2524 # C was successfully merged so window is increased to 2.
2525 self.assertEqual(queue.window, 2)
2526 self.assertEqual(queue.window_floor, 1)
2527 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08002528
2529 def test_worker_update_metadata(self):
2530 "Test if a worker can send back metadata about itself"
2531 self.worker.hold_jobs_in_build = True
2532
2533 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2534 A.addApproval('CRVW', 2)
2535 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2536 self.waitUntilSettled()
2537
2538 self.assertEqual(len(self.launcher.builds), 1)
2539
2540 self.log.debug('Current builds:')
2541 self.log.debug(self.launcher.builds)
2542
2543 start = time.time()
2544 while True:
2545 if time.time() - start > 10:
2546 raise Exception("Timeout waiting for gearman server to report "
2547 + "back to the client")
2548 build = self.launcher.builds.values()[0]
2549 if build.worker.name == "My Worker":
2550 break
2551 else:
2552 time.sleep(0)
2553
2554 self.log.debug(build)
2555 self.assertEqual("My Worker", build.worker.name)
2556 self.assertEqual("localhost", build.worker.hostname)
2557 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
2558 self.assertEqual("zuul.example.org", build.worker.fqdn)
2559 self.assertEqual("FakeBuilder", build.worker.program)
2560 self.assertEqual("v1.1", build.worker.version)
2561 self.assertEqual({'something': 'else'}, build.worker.extra)
2562
2563 self.worker.hold_jobs_in_build = False
2564 self.worker.release()
2565 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11002566
2567 def test_footer_message(self):
2568 "Test a pipeline's footer message is correctly added to the report."
2569 self.config.set('zuul', 'layout_config',
2570 'tests/fixtures/layout-footer-message.yaml')
2571 self.sched.reconfigure(self.config)
2572 self.registerJobs()
2573
2574 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2575 A.addApproval('CRVW', 2)
2576 self.worker.addFailTest('test1', A)
2577 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2578 self.waitUntilSettled()
2579
2580 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2581 B.addApproval('CRVW', 2)
2582 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2583 self.waitUntilSettled()
2584
2585 self.assertEqual(2, len(self.smtp_messages))
2586
2587 failure_body = """\
2588Build failed. For information on how to proceed, see \
2589http://wiki.example.org/Test_Failures
2590
2591- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
2592- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
2593
2594For CI problems and help debugging, contact ci@example.org"""
2595
2596 success_body = """\
2597Build succeeded.
2598
2599- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
2600- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
2601
2602For CI problems and help debugging, contact ci@example.org"""
2603
2604 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
2605 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11002606
2607 def test_merge_failure_reporters(self):
2608 """Check that the config is set up correctly"""
2609
2610 self.config.set('zuul', 'layout_config',
2611 'tests/fixtures/layout-merge-failure.yaml')
2612 self.sched.reconfigure(self.config)
2613 self.registerJobs()
2614
2615 self.assertEqual(
2616 "Merge Failed.\n\nThis change was unable to be automatically "
2617 "merged with the current state of the repository. Please rebase "
2618 "your change and upload a new patchset.",
2619 self.sched.layout.pipelines['check'].merge_failure_message)
2620 self.assertEqual(
2621 "The merge failed! For more information...",
2622 self.sched.layout.pipelines['gate'].merge_failure_message)
2623
2624 self.assertEqual(
2625 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
2626 self.assertEqual(
2627 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
2628
2629 self.assertTrue(isinstance(
2630 self.sched.layout.pipelines['check'].merge_failure_actions[0].
2631 reporter, zuul.reporter.gerrit.Reporter))
2632
2633 self.assertTrue(
2634 (
2635 isinstance(self.sched.layout.pipelines['gate'].
2636 merge_failure_actions[0].reporter,
2637 zuul.reporter.smtp.Reporter) and
2638 isinstance(self.sched.layout.pipelines['gate'].
2639 merge_failure_actions[1].reporter,
2640 zuul.reporter.gerrit.Reporter)
2641 ) or (
2642 isinstance(self.sched.layout.pipelines['gate'].
2643 merge_failure_actions[0].reporter,
2644 zuul.reporter.gerrit.Reporter) and
2645 isinstance(self.sched.layout.pipelines['gate'].
2646 merge_failure_actions[1].reporter,
2647 zuul.reporter.smtp.Reporter)
2648 )
2649 )
2650
2651 def test_merge_failure_reports(self):
2652 """Check that when a change fails to merge the correct message is sent
2653 to the correct reporter"""
2654 self.config.set('zuul', 'layout_config',
2655 'tests/fixtures/layout-merge-failure.yaml')
2656 self.sched.reconfigure(self.config)
2657 self.registerJobs()
2658
2659 # Check a test failure isn't reported to SMTP
2660 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2661 A.addApproval('CRVW', 2)
2662 self.worker.addFailTest('project-test1', A)
2663 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2664 self.waitUntilSettled()
2665
2666 self.assertEqual(3, len(self.history)) # 3 jobs
2667 self.assertEqual(0, len(self.smtp_messages))
2668
2669 # Check a merge failure is reported to SMTP
2670 # B should be merged, but C will conflict with B
2671 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2672 B.addPatchset(['conflict'])
2673 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2674 C.addPatchset(['conflict'])
2675 B.addApproval('CRVW', 2)
2676 C.addApproval('CRVW', 2)
2677 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2678 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2679 self.waitUntilSettled()
2680
2681 self.assertEqual(6, len(self.history)) # A and B jobs
2682 self.assertEqual(1, len(self.smtp_messages))
2683 self.assertEqual('The merge failed! For more information...',
2684 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11002685
2686 def test_swift_instructions(self):
2687 "Test that the correct swift instructions are sent to the workers"
2688 self.config.set('zuul', 'layout_config',
2689 'tests/fixtures/layout-swift.yaml')
2690 self.sched.reconfigure(self.config)
2691 self.registerJobs()
2692
2693 self.worker.hold_jobs_in_build = True
2694 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2695
2696 A.addApproval('CRVW', 2)
2697 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2698 self.waitUntilSettled()
2699
2700 self.assertEqual(
2701 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
2702 "gate/test-merge/",
2703 self.builds[0].parameters['SWIFT_logs_URL'][:-32])
2704 self.assertEqual(5,
2705 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
2706 split('\n')))
2707 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
2708
2709 self.assertEqual(
2710 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
2711 "gate/test-test/",
2712 self.builds[1].parameters['SWIFT_logs_URL'][:-32])
2713 self.assertEqual(5,
2714 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
2715 split('\n')))
2716 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
2717
2718 self.assertEqual(
2719 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
2720 "gate/test-test/",
2721 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-32])
2722 self.assertEqual(5,
2723 len(self.builds[1].
2724 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
2725 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
2726
2727 self.worker.hold_jobs_in_build = False
2728 self.worker.release()
2729 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08002730
2731 def test_client_get_running_jobs(self):
2732 "Test that the RPC client can get a list of running jobs"
2733 self.worker.hold_jobs_in_build = True
2734 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2735 A.addApproval('CRVW', 2)
2736 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2737 self.waitUntilSettled()
2738
2739 client = zuul.rpcclient.RPCClient('127.0.0.1',
2740 self.gearman_server.port)
2741
2742 # Wait for gearman server to send the initial workData back to zuul
2743 start = time.time()
2744 while True:
2745 if time.time() - start > 10:
2746 raise Exception("Timeout waiting for gearman server to report "
2747 + "back to the client")
2748 build = self.launcher.builds.values()[0]
2749 if build.worker.name == "My Worker":
2750 break
2751 else:
2752 time.sleep(0)
2753
2754 running_items = client.get_running_jobs()
2755
2756 self.assertEqual(1, len(running_items))
2757 running_item = running_items[0]
2758 self.assertEqual([], running_item['failing_reasons'])
2759 self.assertEqual([], running_item['items_behind'])
2760 self.assertEqual('https://hostname/1', running_item['url'])
2761 self.assertEqual(None, running_item['item_ahead'])
2762 self.assertEqual('org/project', running_item['project'])
2763 self.assertEqual(None, running_item['remaining_time'])
2764 self.assertEqual(True, running_item['active'])
2765 self.assertEqual('1,1', running_item['id'])
2766
2767 self.assertEqual(3, len(running_item['jobs']))
2768 for job in running_item['jobs']:
2769 if job['name'] == 'project-merge':
2770 self.assertEqual('project-merge', job['name'])
2771 self.assertEqual('gate', job['pipeline'])
2772 self.assertEqual(False, job['retry'])
2773 self.assertEqual(13, len(job['parameters']))
2774 self.assertEqual('https://server/job/project-merge/0/',
2775 job['url'])
2776 self.assertEqual(7, len(job['worker']))
2777 self.assertEqual(False, job['canceled'])
2778 self.assertEqual(True, job['voting'])
2779 self.assertEqual(None, job['result'])
2780 self.assertEqual('gate', job['pipeline'])
2781 break
2782
2783 self.worker.hold_jobs_in_build = False
2784 self.worker.release()
2785 self.waitUntilSettled()
2786
2787 running_items = client.get_running_jobs()
2788 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07002789
2790 def test_nonvoting_pipeline(self):
2791 "Test that a nonvoting pipeline (experimental) can still report"
2792
Joshua Heskethcc017ea2014-04-30 19:55:25 +10002793 A = self.fake_gerrit.addFakeChange('org/experimental-project',
2794 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07002795 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2796 self.waitUntilSettled()
Joshua Heskethcc017ea2014-04-30 19:55:25 +10002797 self.assertEqual(
2798 self.getJobFromHistory('experimental-project-test').result,
2799 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07002800 self.assertEqual(A.reported, 1)