blob: 99f6311c3f980796d4cb784f1409af65d7ee105f [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. Blairec056492016-07-22 09:45:56 -070023from unittest import skip
Monty Taylorbc758832013-06-17 17:22:42 -040024
James E. Blair4886cc12012-07-18 15:39:41 -070025import git
Morgan Fainberg293f7f82016-05-30 14:01:22 -070026from six.moves import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040027import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070028
Maru Newby3fe5f852015-01-13 04:22:14 +000029import zuul.change_matcher
James E. Blairb0fcae42012-07-17 11:12:10 -070030import zuul.scheduler
James E. Blairad28e912013-11-27 10:43:22 -080031import zuul.rpcclient
Joshua Hesketh1879cf72013-08-19 14:13:15 +100032import zuul.reporter.gerrit
Joshua Hesketh5fea8672013-08-19 17:32:01 +100033import zuul.reporter.smtp
Paul Belanger9bba4902016-11-02 16:07:33 -040034import zuul.model
James E. Blairb0fcae42012-07-17 11:12:10 -070035
Maru Newby3fe5f852015-01-13 04:22:14 +000036from tests.base import (
Maru Newby3fe5f852015-01-13 04:22:14 +000037 ZuulTestCase,
38 repack_repo,
39)
James E. Blairb0fcae42012-07-17 11:12:10 -070040
James E. Blair1f4c2bb2013-04-26 08:40:46 -070041logging.basicConfig(level=logging.DEBUG,
42 format='%(asctime)s %(name)-32s '
43 '%(levelname)-8s %(message)s')
James E. Blairb0fcae42012-07-17 11:12:10 -070044
45
Clark Boylanb640e052014-04-03 16:41:46 -070046class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070047 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010048
James E. Blairb0fcae42012-07-17 11:12:10 -070049 def test_jobs_launched(self):
50 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070051
James E. Blairb0fcae42012-07-17 11:12:10 -070052 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070053 A.addApproval('code-review', 2)
54 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070055 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040056 self.assertEqual(self.getJobFromHistory('project-merge').result,
57 'SUCCESS')
58 self.assertEqual(self.getJobFromHistory('project-test1').result,
59 'SUCCESS')
60 self.assertEqual(self.getJobFromHistory('project-test2').result,
61 'SUCCESS')
62 self.assertEqual(A.data['status'], 'MERGED')
63 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070064 self.assertEqual(self.getJobFromHistory('project-test1').node,
65 'image1')
James E. Blairb0fcae42012-07-17 11:12:10 -070066
James E. Blair552b54f2016-07-22 13:55:32 -070067 # TODOv3(jeblair): we may want to report stats by tenant (also?).
James E. Blair66eeebf2013-07-27 17:44:32 -070068 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
69 self.assertReportedStat('zuul.pipeline.gate.current_changes',
70 value='1|g')
71 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
72 kind='ms')
73 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
74 value='1|c')
75 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
76 self.assertReportedStat('zuul.pipeline.gate.total_changes',
77 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070078 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070079 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -070080 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070081 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070082
James E. Blair5821bd92015-09-16 08:48:15 -070083 for build in self.builds:
84 self.assertEqual(build.parameters['ZUUL_VOTING'], '1')
85
James E. Blair3cb10702013-08-24 08:56:03 -070086 def test_initial_pipeline_gauges(self):
87 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -070088 self.assertReportedStat('zuul.pipeline.gate.current_changes',
89 value='0|g')
90 self.assertReportedStat('zuul.pipeline.check.current_changes',
91 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -070092
James E. Blair34776ee2016-08-25 13:53:54 -070093 def test_job_branch(self):
94 "Test the correct variant of a job runs on a branch"
95 self.create_branch('org/project', 'stable')
96 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
97 A.addApproval('code-review', 2)
98 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
99 self.waitUntilSettled()
100 self.assertEqual(self.getJobFromHistory('project-test1').result,
101 'SUCCESS')
102 self.assertEqual(self.getJobFromHistory('project-test2').result,
103 'SUCCESS')
104 self.assertEqual(A.data['status'], 'MERGED')
105 self.assertEqual(A.reported, 2,
106 "A should report start and success")
107 self.assertIn('gate', A.messages[1],
108 "A should transit gate")
109 self.assertEqual(self.getJobFromHistory('project-test1').node,
110 'image2')
111
James E. Blairb0fcae42012-07-17 11:12:10 -0700112 def test_parallel_changes(self):
113 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700114
James E. Blair791b5392016-08-03 11:25:56 -0700115 self.launch_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700116 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
117 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
118 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700119 A.addApproval('code-review', 2)
120 B.addApproval('code-review', 2)
121 C.addApproval('code-review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700122
James E. Blair8b5408c2016-08-08 15:37:46 -0700123 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
124 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
125 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700126
127 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400128 self.assertEqual(len(self.builds), 1)
129 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700130 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700131
James E. Blair791b5392016-08-03 11:25:56 -0700132 self.launch_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700133 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400134 self.assertEqual(len(self.builds), 3)
135 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700136 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400137 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700138 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400139 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700140 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700141
James E. Blair791b5392016-08-03 11:25:56 -0700142 self.launch_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700143 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400144 self.assertEqual(len(self.builds), 5)
145 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700146 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400147 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700148 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700149
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400150 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700151 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400152 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700153 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700154
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400155 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700156 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700157
James E. Blair791b5392016-08-03 11:25:56 -0700158 self.launch_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700159 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400160 self.assertEqual(len(self.builds), 6)
161 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700162 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400163 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700164 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700165
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400166 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700167 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400168 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700169 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700170
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400171 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700172 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400173 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700174 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700175
James E. Blair791b5392016-08-03 11:25:56 -0700176 self.launch_server.hold_jobs_in_build = False
177 self.launch_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700178 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400179 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700180
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400181 self.assertEqual(len(self.history), 9)
182 self.assertEqual(A.data['status'], 'MERGED')
183 self.assertEqual(B.data['status'], 'MERGED')
184 self.assertEqual(C.data['status'], 'MERGED')
185 self.assertEqual(A.reported, 2)
186 self.assertEqual(B.reported, 2)
187 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700188
189 def test_failed_changes(self):
190 "Test that a change behind a failed change is retested"
James E. Blair08d19992016-08-10 15:25:31 -0700191 self.launch_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700192
James E. Blairb02a3bb2012-07-30 17:49:55 -0700193 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
194 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -0700195 A.addApproval('code-review', 2)
196 B.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700197
James E. Blair08d19992016-08-10 15:25:31 -0700198 self.launch_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700199
James E. Blair8b5408c2016-08-08 15:37:46 -0700200 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
201 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700202 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700203 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400204
James E. Blair08d19992016-08-10 15:25:31 -0700205 self.launch_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400206 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700207 # A/project-merge is complete
208 self.assertBuilds([
209 dict(name='project-test1', changes='1,1'),
210 dict(name='project-test2', changes='1,1'),
211 dict(name='project-merge', changes='1,1 2,1'),
212 ])
James E. Blaire2819012013-06-28 17:17:26 -0400213
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700214 self.launch_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400215 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700216 # A/project-merge is complete
217 # B/project-merge is complete
218 self.assertBuilds([
219 dict(name='project-test1', changes='1,1'),
220 dict(name='project-test2', changes='1,1'),
221 dict(name='project-test1', changes='1,1 2,1'),
222 dict(name='project-test2', changes='1,1 2,1'),
223 ])
224
225 # Release project-test1 for A which will fail. This will
226 # abort both running B jobs and relaunch project-merge for B.
227 self.builds[0].release()
228 self.waitUntilSettled()
229
230 self.orderedRelease()
231 self.assertHistory([
232 dict(name='project-merge', result='SUCCESS', changes='1,1'),
233 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
234 dict(name='project-test1', result='FAILURE', changes='1,1'),
235 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
236 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
237 dict(name='project-test2', result='SUCCESS', changes='1,1'),
238 dict(name='project-merge', result='SUCCESS', changes='2,1'),
239 dict(name='project-test1', result='SUCCESS', changes='2,1'),
240 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700241 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700242
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400243 self.assertEqual(A.data['status'], 'NEW')
244 self.assertEqual(B.data['status'], 'MERGED')
245 self.assertEqual(A.reported, 2)
246 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700247
248 def test_independent_queues(self):
249 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700250
James E. Blair08d19992016-08-10 15:25:31 -0700251 self.launch_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900252 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700253 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
254 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700255 A.addApproval('code-review', 2)
256 B.addApproval('code-review', 2)
257 C.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700258
James E. Blair8b5408c2016-08-08 15:37:46 -0700259 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
260 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
261 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700262
James E. Blairb02a3bb2012-07-30 17:49:55 -0700263 self.waitUntilSettled()
264
265 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700266 self.assertBuilds([
267 dict(name='project-merge', changes='1,1'),
268 dict(name='project-merge', changes='2,1'),
269 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700270
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700271 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700272 self.builds[0].release()
273 self.waitUntilSettled()
274 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700275 self.waitUntilSettled()
276 # Release the merge job for project2 which is behind project1
James E. Blair08d19992016-08-10 15:25:31 -0700277 self.launch_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700278 self.waitUntilSettled()
279
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700280 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700281 self.assertBuilds([
282 dict(name='project-test1', changes='1,1'),
283 dict(name='project-test2', changes='1,1'),
284 dict(name='project-test1', changes='2,1'),
285 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500286 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700287 dict(name='project-test1', changes='2,1 3,1'),
288 dict(name='project-test2', changes='2,1 3,1'),
289 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700290
James E. Blair5f0d49e2016-08-19 09:36:50 -0700291 self.orderedRelease()
292 self.assertHistory([
293 dict(name='project-merge', result='SUCCESS', changes='1,1'),
294 dict(name='project-merge', result='SUCCESS', changes='2,1'),
295 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
296 dict(name='project-test1', result='SUCCESS', changes='1,1'),
297 dict(name='project-test2', result='SUCCESS', changes='1,1'),
298 dict(name='project-test1', result='SUCCESS', changes='2,1'),
299 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500300 dict(
301 name='project1-project2-integration',
302 result='SUCCESS',
303 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700304 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
305 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
306 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700307
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400308 self.assertEqual(A.data['status'], 'MERGED')
309 self.assertEqual(B.data['status'], 'MERGED')
310 self.assertEqual(C.data['status'], 'MERGED')
311 self.assertEqual(A.reported, 2)
312 self.assertEqual(B.reported, 2)
313 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700314
315 def test_failed_change_at_head(self):
316 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700317
James E. Blair08d19992016-08-10 15:25:31 -0700318 self.launch_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700319 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
320 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
321 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700322 A.addApproval('code-review', 2)
323 B.addApproval('code-review', 2)
324 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700325
James E. Blair08d19992016-08-10 15:25:31 -0700326 self.launch_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700327
James E. Blair8b5408c2016-08-08 15:37:46 -0700328 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
329 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
330 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700331
332 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700333
James E. Blairb536ecc2016-08-31 10:11:42 -0700334 self.assertBuilds([
335 dict(name='project-merge', changes='1,1'),
336 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700337
James E. Blair08d19992016-08-10 15:25:31 -0700338 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700339 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700340 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700341 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700342 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700343 self.waitUntilSettled()
344
James E. Blairb536ecc2016-08-31 10:11:42 -0700345 self.assertBuilds([
346 dict(name='project-test1', changes='1,1'),
347 dict(name='project-test2', changes='1,1'),
348 dict(name='project-test1', changes='1,1 2,1'),
349 dict(name='project-test2', changes='1,1 2,1'),
350 dict(name='project-test1', changes='1,1 2,1 3,1'),
351 dict(name='project-test2', changes='1,1 2,1 3,1'),
352 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700353
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400354 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700355 self.waitUntilSettled()
356
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400357 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700358 self.assertBuilds([
359 dict(name='project-test2', changes='1,1'),
360 dict(name='project-merge', changes='2,1'),
361 ])
362 # Unordered history comparison because the aborts can finish
363 # in any order.
364 self.assertHistory([
365 dict(name='project-merge', result='SUCCESS',
366 changes='1,1'),
367 dict(name='project-merge', result='SUCCESS',
368 changes='1,1 2,1'),
369 dict(name='project-merge', result='SUCCESS',
370 changes='1,1 2,1 3,1'),
371 dict(name='project-test1', result='FAILURE',
372 changes='1,1'),
373 dict(name='project-test1', result='ABORTED',
374 changes='1,1 2,1'),
375 dict(name='project-test2', result='ABORTED',
376 changes='1,1 2,1'),
377 dict(name='project-test1', result='ABORTED',
378 changes='1,1 2,1 3,1'),
379 dict(name='project-test2', result='ABORTED',
380 changes='1,1 2,1 3,1'),
381 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700382
James E. Blairb536ecc2016-08-31 10:11:42 -0700383 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700384 self.waitUntilSettled()
James E. Blairb536ecc2016-08-31 10:11:42 -0700385 self.launch_server.release('.*-merge')
386 self.waitUntilSettled()
387 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700388
James E. Blairb536ecc2016-08-31 10:11:42 -0700389 self.assertBuilds([])
390 self.assertHistory([
391 dict(name='project-merge', result='SUCCESS',
392 changes='1,1'),
393 dict(name='project-merge', result='SUCCESS',
394 changes='1,1 2,1'),
395 dict(name='project-merge', result='SUCCESS',
396 changes='1,1 2,1 3,1'),
397 dict(name='project-test1', result='FAILURE',
398 changes='1,1'),
399 dict(name='project-test1', result='ABORTED',
400 changes='1,1 2,1'),
401 dict(name='project-test2', result='ABORTED',
402 changes='1,1 2,1'),
403 dict(name='project-test1', result='ABORTED',
404 changes='1,1 2,1 3,1'),
405 dict(name='project-test2', result='ABORTED',
406 changes='1,1 2,1 3,1'),
407 dict(name='project-merge', result='SUCCESS',
408 changes='2,1'),
409 dict(name='project-merge', result='SUCCESS',
410 changes='2,1 3,1'),
411 dict(name='project-test2', result='SUCCESS',
412 changes='1,1'),
413 dict(name='project-test1', result='SUCCESS',
414 changes='2,1'),
415 dict(name='project-test2', result='SUCCESS',
416 changes='2,1'),
417 dict(name='project-test1', result='SUCCESS',
418 changes='2,1 3,1'),
419 dict(name='project-test2', result='SUCCESS',
420 changes='2,1 3,1'),
421 ], ordered=False)
422
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400423 self.assertEqual(A.data['status'], 'NEW')
424 self.assertEqual(B.data['status'], 'MERGED')
425 self.assertEqual(C.data['status'], 'MERGED')
426 self.assertEqual(A.reported, 2)
427 self.assertEqual(B.reported, 2)
428 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700429
James E. Blair0aac4872013-08-23 14:02:38 -0700430 def test_failed_change_in_middle(self):
431 "Test a failed change in the middle of the queue"
432
James E. Blair08d19992016-08-10 15:25:31 -0700433 self.launch_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700434 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
435 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
436 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700437 A.addApproval('code-review', 2)
438 B.addApproval('code-review', 2)
439 C.addApproval('code-review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700440
James E. Blair08d19992016-08-10 15:25:31 -0700441 self.launch_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700442
James E. Blair8b5408c2016-08-08 15:37:46 -0700443 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
444 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
445 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700446
447 self.waitUntilSettled()
448
James E. Blair08d19992016-08-10 15:25:31 -0700449 self.launch_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700450 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700451 self.launch_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700452 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700453 self.launch_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700454 self.waitUntilSettled()
455
456 self.assertEqual(len(self.builds), 6)
457 self.assertEqual(self.builds[0].name, 'project-test1')
458 self.assertEqual(self.builds[1].name, 'project-test2')
459 self.assertEqual(self.builds[2].name, 'project-test1')
460 self.assertEqual(self.builds[3].name, 'project-test2')
461 self.assertEqual(self.builds[4].name, 'project-test1')
462 self.assertEqual(self.builds[5].name, 'project-test2')
463
464 self.release(self.builds[2])
465 self.waitUntilSettled()
466
James E. Blair972e3c72013-08-29 12:04:55 -0700467 # project-test1 and project-test2 for A
468 # project-test2 for B
469 # project-merge for C (without B)
470 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700471 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
472
James E. Blair08d19992016-08-10 15:25:31 -0700473 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700474 self.waitUntilSettled()
475
476 # project-test1 and project-test2 for A
477 # project-test2 for B
478 # project-test1 and project-test2 for C
479 self.assertEqual(len(self.builds), 5)
480
Paul Belanger2e2a0492016-10-30 18:33:37 -0400481 tenant = self.sched.abide.tenants.get('tenant-one')
482 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700483 builds = items[0].current_build_set.getBuilds()
484 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
485 self.assertEqual(self.countJobResults(builds, None), 2)
486 builds = items[1].current_build_set.getBuilds()
487 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
488 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
489 self.assertEqual(self.countJobResults(builds, None), 1)
490 builds = items[2].current_build_set.getBuilds()
491 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700492 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700493
James E. Blair08d19992016-08-10 15:25:31 -0700494 self.launch_server.hold_jobs_in_build = False
495 self.launch_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700496 self.waitUntilSettled()
497
498 self.assertEqual(len(self.builds), 0)
499 self.assertEqual(len(self.history), 12)
500 self.assertEqual(A.data['status'], 'MERGED')
501 self.assertEqual(B.data['status'], 'NEW')
502 self.assertEqual(C.data['status'], 'MERGED')
503 self.assertEqual(A.reported, 2)
504 self.assertEqual(B.reported, 2)
505 self.assertEqual(C.reported, 2)
506
James E. Blaird466dc42012-07-31 10:42:56 -0700507 def test_failed_change_at_head_with_queue(self):
508 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700509
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700510 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700511 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
512 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
513 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700514 A.addApproval('code-review', 2)
515 B.addApproval('code-review', 2)
516 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700517
James E. Blair08d19992016-08-10 15:25:31 -0700518 self.launch_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700519
James E. Blair8b5408c2016-08-08 15:37:46 -0700520 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
521 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
522 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700523
524 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700525 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400526 self.assertEqual(len(self.builds), 0)
527 self.assertEqual(len(queue), 1)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800528 self.assertEqual(queue[0].name, 'launcher:launch')
529 job_args = json.loads(queue[0].arguments)
530 self.assertEqual(job_args['job'], 'project-merge')
531 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700532
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700533 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700534 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700535 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700536 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700537 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700538 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700539 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700540
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400541 self.assertEqual(len(self.builds), 0)
542 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800543
544 self.assertEqual(
545 json.loads(queue[0].arguments)['job'], 'project-test1')
546 self.assertEqual(
547 json.loads(queue[1].arguments)['job'], 'project-test2')
548 self.assertEqual(
549 json.loads(queue[2].arguments)['job'], 'project-test1')
550 self.assertEqual(
551 json.loads(queue[3].arguments)['job'], 'project-test2')
552 self.assertEqual(
553 json.loads(queue[4].arguments)['job'], 'project-test1')
554 self.assertEqual(
555 json.loads(queue[5].arguments)['job'], 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700556
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700557 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700558 self.waitUntilSettled()
559
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400560 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700561 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400562 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
563 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700564
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700565 self.gearman_server.hold_jobs_in_queue = False
566 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700567 self.waitUntilSettled()
568
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400569 self.assertEqual(len(self.builds), 0)
570 self.assertEqual(len(self.history), 11)
571 self.assertEqual(A.data['status'], 'NEW')
572 self.assertEqual(B.data['status'], 'MERGED')
573 self.assertEqual(C.data['status'], 'MERGED')
574 self.assertEqual(A.reported, 2)
575 self.assertEqual(B.reported, 2)
576 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700577
James E. Blairec056492016-07-22 09:45:56 -0700578 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700579 def _test_time_database(self, iteration):
James E. Blair08d19992016-08-10 15:25:31 -0700580 self.launch_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700581 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -0700582 A.addApproval('code-review', 2)
583 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700584 self.waitUntilSettled()
585 time.sleep(2)
586
587 data = json.loads(self.sched.formatStatusJSON())
588 found_job = None
589 for pipeline in data['pipelines']:
590 if pipeline['name'] != 'gate':
591 continue
592 for queue in pipeline['change_queues']:
593 for head in queue['heads']:
594 for item in head:
595 for job in item['jobs']:
596 if job['name'] == 'project-merge':
597 found_job = job
598 break
599
600 self.assertIsNotNone(found_job)
601 if iteration == 1:
602 self.assertIsNotNone(found_job['estimated_time'])
603 self.assertIsNone(found_job['remaining_time'])
604 else:
605 self.assertIsNotNone(found_job['estimated_time'])
606 self.assertTrue(found_job['estimated_time'] >= 2)
607 self.assertIsNotNone(found_job['remaining_time'])
608
James E. Blair08d19992016-08-10 15:25:31 -0700609 self.launch_server.hold_jobs_in_build = False
610 self.launch_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700611 self.waitUntilSettled()
612
James E. Blairec056492016-07-22 09:45:56 -0700613 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700614 def test_time_database(self):
615 "Test the time database"
616
617 self._test_time_database(1)
618 self._test_time_database(2)
619
James E. Blairfef71632013-09-23 11:15:47 -0700620 def test_two_failed_changes_at_head(self):
621 "Test that changes are reparented correctly if 2 fail at head"
622
James E. Blair08d19992016-08-10 15:25:31 -0700623 self.launch_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700624 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
625 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
626 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700627 A.addApproval('code-review', 2)
628 B.addApproval('code-review', 2)
629 C.addApproval('code-review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700630
James E. Blair08d19992016-08-10 15:25:31 -0700631 self.launch_server.failJob('project-test1', A)
632 self.launch_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700633
James E. Blair8b5408c2016-08-08 15:37:46 -0700634 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
635 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
636 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700637 self.waitUntilSettled()
638
James E. Blair08d19992016-08-10 15:25:31 -0700639 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700640 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700641 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700642 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700643 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700644 self.waitUntilSettled()
645
646 self.assertEqual(len(self.builds), 6)
647 self.assertEqual(self.builds[0].name, 'project-test1')
648 self.assertEqual(self.builds[1].name, 'project-test2')
649 self.assertEqual(self.builds[2].name, 'project-test1')
650 self.assertEqual(self.builds[3].name, 'project-test2')
651 self.assertEqual(self.builds[4].name, 'project-test1')
652 self.assertEqual(self.builds[5].name, 'project-test2')
653
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400654 self.assertTrue(self.builds[0].hasChanges(A))
655 self.assertTrue(self.builds[2].hasChanges(A))
656 self.assertTrue(self.builds[2].hasChanges(B))
657 self.assertTrue(self.builds[4].hasChanges(A))
658 self.assertTrue(self.builds[4].hasChanges(B))
659 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700660
661 # Fail change B first
662 self.release(self.builds[2])
663 self.waitUntilSettled()
664
665 # restart of C after B failure
James E. Blair08d19992016-08-10 15:25:31 -0700666 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700667 self.waitUntilSettled()
668
669 self.assertEqual(len(self.builds), 5)
670 self.assertEqual(self.builds[0].name, 'project-test1')
671 self.assertEqual(self.builds[1].name, 'project-test2')
672 self.assertEqual(self.builds[2].name, 'project-test2')
673 self.assertEqual(self.builds[3].name, 'project-test1')
674 self.assertEqual(self.builds[4].name, 'project-test2')
675
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400676 self.assertTrue(self.builds[1].hasChanges(A))
677 self.assertTrue(self.builds[2].hasChanges(A))
678 self.assertTrue(self.builds[2].hasChanges(B))
679 self.assertTrue(self.builds[4].hasChanges(A))
680 self.assertFalse(self.builds[4].hasChanges(B))
681 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700682
683 # Finish running all passing jobs for change A
684 self.release(self.builds[1])
685 self.waitUntilSettled()
686 # Fail and report change A
687 self.release(self.builds[0])
688 self.waitUntilSettled()
689
690 # restart of B,C after A failure
James E. Blair08d19992016-08-10 15:25:31 -0700691 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700692 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700693 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700694 self.waitUntilSettled()
695
696 self.assertEqual(len(self.builds), 4)
697 self.assertEqual(self.builds[0].name, 'project-test1') # B
698 self.assertEqual(self.builds[1].name, 'project-test2') # B
699 self.assertEqual(self.builds[2].name, 'project-test1') # C
700 self.assertEqual(self.builds[3].name, 'project-test2') # C
701
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400702 self.assertFalse(self.builds[1].hasChanges(A))
703 self.assertTrue(self.builds[1].hasChanges(B))
704 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700705
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400706 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700707 # After A failed and B and C restarted, B should be back in
708 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400709 self.assertTrue(self.builds[2].hasChanges(B))
710 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700711
James E. Blair08d19992016-08-10 15:25:31 -0700712 self.launch_server.hold_jobs_in_build = False
713 self.launch_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700714 self.waitUntilSettled()
715
716 self.assertEqual(len(self.builds), 0)
717 self.assertEqual(len(self.history), 21)
718 self.assertEqual(A.data['status'], 'NEW')
719 self.assertEqual(B.data['status'], 'NEW')
720 self.assertEqual(C.data['status'], 'MERGED')
721 self.assertEqual(A.reported, 2)
722 self.assertEqual(B.reported, 2)
723 self.assertEqual(C.reported, 2)
724
James E. Blair8c803f82012-07-31 16:25:42 -0700725 def test_patch_order(self):
726 "Test that dependent patches are tested in the right order"
727 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
728 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
729 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700730 A.addApproval('code-review', 2)
731 B.addApproval('code-review', 2)
732 C.addApproval('code-review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700733
734 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
735 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
736 M2.setMerged()
737 M1.setMerged()
738
739 # C -> B -> A -> M1 -> M2
740 # M2 is here to make sure it is never queried. If it is, it
741 # means zuul is walking down the entire history of merged
742 # changes.
743
744 C.setDependsOn(B, 1)
745 B.setDependsOn(A, 1)
746 A.setDependsOn(M1, 1)
747 M1.setDependsOn(M2, 1)
748
James E. Blair8b5408c2016-08-08 15:37:46 -0700749 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700750
751 self.waitUntilSettled()
752
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400753 self.assertEqual(A.data['status'], 'NEW')
754 self.assertEqual(B.data['status'], 'NEW')
755 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700756
James E. Blair8b5408c2016-08-08 15:37:46 -0700757 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
758 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700759
760 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400761 self.assertEqual(M2.queried, 0)
762 self.assertEqual(A.data['status'], 'MERGED')
763 self.assertEqual(B.data['status'], 'MERGED')
764 self.assertEqual(C.data['status'], 'MERGED')
765 self.assertEqual(A.reported, 2)
766 self.assertEqual(B.reported, 2)
767 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700768
James E. Blair063672f2015-01-29 13:09:12 -0800769 def test_needed_changes_enqueue(self):
770 "Test that a needed change is enqueued ahead"
771 # A Given a git tree like this, if we enqueue
772 # / \ change C, we should walk up and down the tree
773 # B G and enqueue changes in the order ABCDEFG.
774 # /|\ This is also the order that you would get if
775 # *C E F you enqueued changes in the order ABCDEFG, so
776 # / the ordering is stable across re-enqueue events.
777 # D
778
779 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
780 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
781 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
782 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
783 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
784 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
785 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
786 B.setDependsOn(A, 1)
787 C.setDependsOn(B, 1)
788 D.setDependsOn(C, 1)
789 E.setDependsOn(B, 1)
790 F.setDependsOn(B, 1)
791 G.setDependsOn(A, 1)
792
James E. Blair8b5408c2016-08-08 15:37:46 -0700793 A.addApproval('code-review', 2)
794 B.addApproval('code-review', 2)
795 C.addApproval('code-review', 2)
796 D.addApproval('code-review', 2)
797 E.addApproval('code-review', 2)
798 F.addApproval('code-review', 2)
799 G.addApproval('code-review', 2)
800 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800801
802 self.waitUntilSettled()
803
804 self.assertEqual(A.data['status'], 'NEW')
805 self.assertEqual(B.data['status'], 'NEW')
806 self.assertEqual(C.data['status'], 'NEW')
807 self.assertEqual(D.data['status'], 'NEW')
808 self.assertEqual(E.data['status'], 'NEW')
809 self.assertEqual(F.data['status'], 'NEW')
810 self.assertEqual(G.data['status'], 'NEW')
811
812 # We're about to add approvals to changes without adding the
813 # triggering events to Zuul, so that we can be sure that it is
814 # enqueing the changes based on dependencies, not because of
815 # triggering events. Since it will have the changes cached
816 # already (without approvals), we need to clear the cache
817 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400818 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100819 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800820
James E. Blair08d19992016-08-10 15:25:31 -0700821 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -0700822 A.addApproval('approved', 1)
823 B.addApproval('approved', 1)
824 D.addApproval('approved', 1)
825 E.addApproval('approved', 1)
826 F.addApproval('approved', 1)
827 G.addApproval('approved', 1)
828 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800829
830 for x in range(8):
James E. Blair08d19992016-08-10 15:25:31 -0700831 self.launch_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800832 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700833 self.launch_server.hold_jobs_in_build = False
834 self.launch_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800835 self.waitUntilSettled()
836
837 self.assertEqual(A.data['status'], 'MERGED')
838 self.assertEqual(B.data['status'], 'MERGED')
839 self.assertEqual(C.data['status'], 'MERGED')
840 self.assertEqual(D.data['status'], 'MERGED')
841 self.assertEqual(E.data['status'], 'MERGED')
842 self.assertEqual(F.data['status'], 'MERGED')
843 self.assertEqual(G.data['status'], 'MERGED')
844 self.assertEqual(A.reported, 2)
845 self.assertEqual(B.reported, 2)
846 self.assertEqual(C.reported, 2)
847 self.assertEqual(D.reported, 2)
848 self.assertEqual(E.reported, 2)
849 self.assertEqual(F.reported, 2)
850 self.assertEqual(G.reported, 2)
851 self.assertEqual(self.history[6].changes,
852 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
853
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100854 def test_source_cache(self):
855 "Test that the source cache operates correctly"
James E. Blair08d19992016-08-10 15:25:31 -0700856 self.launch_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700857
858 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
859 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
860 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
James E. Blair8b5408c2016-08-08 15:37:46 -0700861 A.addApproval('code-review', 2)
862 B.addApproval('code-review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700863
864 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
865 M1.setMerged()
866
867 B.setDependsOn(A, 1)
868 A.setDependsOn(M1, 1)
869
James E. Blair8b5408c2016-08-08 15:37:46 -0700870 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700871 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
872
873 self.waitUntilSettled()
874
875 for build in self.builds:
876 if build.parameters['ZUUL_PIPELINE'] == 'check':
877 build.release()
878 self.waitUntilSettled()
879 for build in self.builds:
880 if build.parameters['ZUUL_PIPELINE'] == 'check':
881 build.release()
882 self.waitUntilSettled()
883
James E. Blair8b5408c2016-08-08 15:37:46 -0700884 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700885 self.waitUntilSettled()
886
Joshua Hesketh352264b2015-08-11 23:42:08 +1000887 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700888 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000889 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700890
James E. Blair08d19992016-08-10 15:25:31 -0700891 self.launch_server.hold_jobs_in_build = False
892 self.launch_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700893 self.waitUntilSettled()
894
895 self.assertEqual(A.data['status'], 'MERGED')
896 self.assertEqual(B.data['status'], 'MERGED')
897 self.assertEqual(A.queried, 2) # Initial and isMerged
898 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
899
James E. Blair8c803f82012-07-31 16:25:42 -0700900 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700901 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700902 # TODO: move to test_gerrit (this is a unit test!)
903 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400904 tenant = self.sched.abide.tenants.get('tenant-one')
905 source = tenant.layout.pipelines['gate'].source
906
907 # TODO(pabelanger): As we add more source / trigger APIs we should make
908 # it easier for users to create events for testing.
909 event = zuul.model.TriggerEvent()
910 event.trigger_name = 'gerrit'
911 event.change_number = '1'
912 event.patch_number = '2'
913
914 a = source.getChange(event)
915 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700916 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700917
James E. Blair8b5408c2016-08-08 15:37:46 -0700918 A.addApproval('code-review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400919 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700920 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700921
James E. Blair8b5408c2016-08-08 15:37:46 -0700922 A.addApproval('approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400923 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700924 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700925
James E. Blairec056492016-07-22 09:45:56 -0700926 @skip("Disabled for early v3 development")
James E. Blair973721f2012-08-15 10:19:43 -0700927 def test_build_configuration_conflict(self):
928 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700929
930 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -0700931 A = self.fake_gerrit.addFakeChange('org/conflict-project',
932 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -0700933 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700934 B = self.fake_gerrit.addFakeChange('org/conflict-project',
935 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -0700936 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700937 C = self.fake_gerrit.addFakeChange('org/conflict-project',
938 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700939 A.addApproval('code-review', 2)
940 B.addApproval('code-review', 2)
941 C.addApproval('code-review', 2)
942 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
943 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
944 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700945 self.waitUntilSettled()
946
James E. Blair6736beb2013-07-11 15:18:15 -0700947 self.assertEqual(A.reported, 1)
948 self.assertEqual(B.reported, 1)
949 self.assertEqual(C.reported, 1)
950
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700951 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700952 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700953 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700954 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700955 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700956 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700957
958 self.assertEqual(len(self.history), 2) # A and C merge jobs
959
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700960 self.gearman_server.hold_jobs_in_queue = False
961 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700962 self.waitUntilSettled()
963
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400964 self.assertEqual(A.data['status'], 'MERGED')
965 self.assertEqual(B.data['status'], 'NEW')
966 self.assertEqual(C.data['status'], 'MERGED')
967 self.assertEqual(A.reported, 2)
968 self.assertEqual(B.reported, 2)
969 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -0700970 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -0700971
James E. Blairdaabed22012-08-15 15:38:57 -0700972 def test_post(self):
973 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700974
Zhongyue Luo5d556072012-09-21 02:00:47 +0900975 e = {
976 "type": "ref-updated",
977 "submitter": {
978 "name": "User Name",
979 },
980 "refUpdate": {
981 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
982 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
983 "refName": "master",
984 "project": "org/project",
985 }
986 }
James E. Blairdaabed22012-08-15 15:38:57 -0700987 self.fake_gerrit.addEvent(e)
988 self.waitUntilSettled()
989
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400990 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400991 self.assertEqual(len(self.history), 1)
992 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -0700993
K Jonathan Harkerf95e7232015-04-29 13:33:16 -0700994 def test_post_ignore_deletes(self):
995 "Test that deleting refs does not trigger post jobs"
996
997 e = {
998 "type": "ref-updated",
999 "submitter": {
1000 "name": "User Name",
1001 },
1002 "refUpdate": {
1003 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1004 "newRev": "0000000000000000000000000000000000000000",
1005 "refName": "master",
1006 "project": "org/project",
1007 }
1008 }
1009 self.fake_gerrit.addEvent(e)
1010 self.waitUntilSettled()
1011
1012 job_names = [x.name for x in self.history]
1013 self.assertEqual(len(self.history), 0)
1014 self.assertNotIn('project-post', job_names)
1015
1016 def test_post_ignore_deletes_negative(self):
1017 "Test that deleting refs does trigger post jobs"
1018
Adam Gandelmanc5e4f1d2016-11-29 14:27:17 -08001019 self.updateConfigLayout('layout-dont-ignore-ref-deletes')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001020 self.sched.reconfigure(self.config)
1021
1022 e = {
1023 "type": "ref-updated",
1024 "submitter": {
1025 "name": "User Name",
1026 },
1027 "refUpdate": {
1028 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1029 "newRev": "0000000000000000000000000000000000000000",
1030 "refName": "master",
1031 "project": "org/project",
1032 }
1033 }
1034 self.fake_gerrit.addEvent(e)
1035 self.waitUntilSettled()
1036
1037 job_names = [x.name for x in self.history]
1038 self.assertEqual(len(self.history), 1)
1039 self.assertIn('project-post', job_names)
1040
James E. Blairec056492016-07-22 09:45:56 -07001041 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001042 def test_build_configuration_branch(self):
1043 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001044
1045 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001046 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1047 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1048 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001049 A.addApproval('code-review', 2)
1050 B.addApproval('code-review', 2)
1051 C.addApproval('code-review', 2)
1052 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1053 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1054 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairc6294a52012-08-17 10:19:48 -07001055 self.waitUntilSettled()
1056
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001057 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001058 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001059 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001060 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001061 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001062 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001063 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001064 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1065 self.gearman_server.hold_jobs_in_queue = False
1066 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001067 self.waitUntilSettled()
1068
Monty Taylorbc758832013-06-17 17:22:42 -04001069 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001070 repo = git.Repo(path)
1071 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1072 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001073 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001074 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001075
James E. Blairec056492016-07-22 09:45:56 -07001076 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001077 def test_build_configuration_branch_interaction(self):
1078 "Test that switching between branches works"
1079 self.test_build_configuration()
1080 self.test_build_configuration_branch()
1081 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001082 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001083 repo = git.Repo(path)
1084 repo.heads.master.commit = repo.commit('init')
1085 self.test_build_configuration()
1086
James E. Blairec056492016-07-22 09:45:56 -07001087 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001088 def test_build_configuration_multi_branch(self):
1089 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001090
1091 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001092 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1093 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1094 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001095 A.addApproval('code-review', 2)
1096 B.addApproval('code-review', 2)
1097 C.addApproval('code-review', 2)
1098 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1099 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1100 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairc6294a52012-08-17 10:19:48 -07001101 self.waitUntilSettled()
James E. Blairbb1fe502014-03-04 10:15:06 -08001102 queue = self.gearman_server.getQueue()
1103 job_A = None
1104 for job in queue:
1105 if 'project-merge' in job.name:
1106 job_A = job
1107 ref_A = self.getParameter(job_A, 'ZUUL_REF')
1108 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
1109 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
1110 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
James E. Blairc6294a52012-08-17 10:19:48 -07001111
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001112 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001113 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001114 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001115 job_B = None
1116 for job in queue:
1117 if 'project-merge' in job.name:
1118 job_B = job
1119 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001120 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001121 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blairbb1fe502014-03-04 10:15:06 -08001122 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
1123
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001124 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001125 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001126 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001127 for job in queue:
1128 if 'project-merge' in job.name:
1129 job_C = job
1130 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001131 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001132 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blairbb1fe502014-03-04 10:15:06 -08001133 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001134 self.gearman_server.hold_jobs_in_queue = False
1135 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001136 self.waitUntilSettled()
1137
Monty Taylorbc758832013-06-17 17:22:42 -04001138 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001139 repo = git.Repo(path)
1140
1141 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001142 for c in repo.iter_commits(ref_C)]
James E. Blairbb1fe502014-03-04 10:15:06 -08001143 repo_shas = [c.hexsha for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001144 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001145 correct_messages = ['initial commit', 'A-1', 'C-1']
James E. Blairbb1fe502014-03-04 10:15:06 -08001146 # Ensure the right commits are in the history for this ref
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001147 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08001148 # Ensure ZUUL_REF -> ZUUL_COMMIT
1149 self.assertEqual(repo_shas[0], commit_C)
James E. Blairc6294a52012-08-17 10:19:48 -07001150
1151 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001152 for c in repo.iter_commits(ref_B)]
James E. Blairbb1fe502014-03-04 10:15:06 -08001153 repo_shas = [c.hexsha for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001154 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001155 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001156 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08001157 self.assertEqual(repo_shas[0], commit_B)
1158
1159 repo_messages = [c.message.strip()
1160 for c in repo.iter_commits(ref_A)]
1161 repo_shas = [c.hexsha for c in repo.iter_commits(ref_A)]
1162 repo_messages.reverse()
1163 correct_messages = ['initial commit', 'A-1']
1164 self.assertEqual(repo_messages, correct_messages)
1165 self.assertEqual(repo_shas[0], commit_A)
1166
1167 self.assertNotEqual(ref_A, ref_B, ref_C)
1168 self.assertNotEqual(commit_A, commit_B, commit_C)
James E. Blair7f71c802012-08-22 13:04:32 -07001169
James E. Blaircaec0c52012-08-22 14:52:22 -07001170 def test_dependent_changes_dequeue(self):
1171 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001172
James E. Blaircaec0c52012-08-22 14:52:22 -07001173 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1174 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1175 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001176 A.addApproval('code-review', 2)
1177 B.addApproval('code-review', 2)
1178 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001179
1180 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1181 M1.setMerged()
1182
1183 # C -> B -> A -> M1
1184
1185 C.setDependsOn(B, 1)
1186 B.setDependsOn(A, 1)
1187 A.setDependsOn(M1, 1)
1188
James E. Blair08d19992016-08-10 15:25:31 -07001189 self.launch_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001190
James E. Blair8b5408c2016-08-08 15:37:46 -07001191 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1192 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1193 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001194
1195 self.waitUntilSettled()
1196
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001197 self.assertEqual(A.data['status'], 'NEW')
1198 self.assertEqual(A.reported, 2)
1199 self.assertEqual(B.data['status'], 'NEW')
1200 self.assertEqual(B.reported, 2)
1201 self.assertEqual(C.data['status'], 'NEW')
1202 self.assertEqual(C.reported, 2)
1203 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001204
James E. Blair972e3c72013-08-29 12:04:55 -07001205 def test_failing_dependent_changes(self):
1206 "Test that failing dependent patches are taken out of stream"
James E. Blair08d19992016-08-10 15:25:31 -07001207 self.launch_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001208 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1209 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1210 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1211 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1212 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001213 A.addApproval('code-review', 2)
1214 B.addApproval('code-review', 2)
1215 C.addApproval('code-review', 2)
1216 D.addApproval('code-review', 2)
1217 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001218
1219 # E, D -> C -> B, A
1220
1221 D.setDependsOn(C, 1)
1222 C.setDependsOn(B, 1)
1223
James E. Blair08d19992016-08-10 15:25:31 -07001224 self.launch_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001225
James E. Blair8b5408c2016-08-08 15:37:46 -07001226 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1227 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1228 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1229 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1230 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001231
1232 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001233 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001234 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001235 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001236 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001237 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001238 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001239 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001240 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001241 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001242 self.waitUntilSettled()
1243
James E. Blair08d19992016-08-10 15:25:31 -07001244 self.launch_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001245 for build in self.builds:
1246 if build.parameters['ZUUL_CHANGE'] != '1':
1247 build.release()
1248 self.waitUntilSettled()
1249
James E. Blair08d19992016-08-10 15:25:31 -07001250 self.launch_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001251 self.waitUntilSettled()
1252
1253 self.assertEqual(A.data['status'], 'MERGED')
1254 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001255 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001256 self.assertEqual(B.data['status'], 'NEW')
1257 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001258 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001259 self.assertEqual(C.data['status'], 'NEW')
1260 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001261 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001262 self.assertEqual(D.data['status'], 'NEW')
1263 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001264 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001265 self.assertEqual(E.data['status'], 'MERGED')
1266 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001267 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001268 self.assertEqual(len(self.history), 18)
1269
James E. Blairec590122012-08-22 15:19:31 -07001270 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001271 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001272 # If it's dequeued more than once, we should see extra
1273 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001274
James E. Blair08d19992016-08-10 15:25:31 -07001275 self.launch_server.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001276 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1277 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1278 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001279 A.addApproval('code-review', 2)
1280 B.addApproval('code-review', 2)
1281 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001282
Paul Belanger58bf6912016-11-11 19:36:01 -05001283 self.launch_server.failJob('project-test1', A)
1284 self.launch_server.failJob('project-test2', A)
James E. Blair08d19992016-08-10 15:25:31 -07001285 self.launch_server.failJob('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001286
James E. Blair8b5408c2016-08-08 15:37:46 -07001287 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1288 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1289 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001290
1291 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001292
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001293 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001294 self.assertEqual(self.builds[0].name, 'project-merge')
1295 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001296
James E. Blair08d19992016-08-10 15:25:31 -07001297 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001298 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001299 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001300 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001301 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001302 self.waitUntilSettled()
1303
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001304 self.assertEqual(len(self.builds), 9)
Paul Belanger58bf6912016-11-11 19:36:01 -05001305 self.assertEqual(self.builds[0].name, 'project-test1')
1306 self.assertEqual(self.builds[1].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001307 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001308 self.assertEqual(self.builds[3].name, 'project-test1')
1309 self.assertEqual(self.builds[4].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001310 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001311 self.assertEqual(self.builds[6].name, 'project-test1')
1312 self.assertEqual(self.builds[7].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001313 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001314
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001315 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001316 self.waitUntilSettled()
1317
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001318 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1319 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001320
James E. Blair08d19992016-08-10 15:25:31 -07001321 self.launch_server.hold_jobs_in_build = False
1322 self.launch_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001323 self.waitUntilSettled()
1324
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001325 self.assertEqual(len(self.builds), 0)
1326 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001327
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001328 self.assertEqual(A.data['status'], 'NEW')
1329 self.assertEqual(B.data['status'], 'MERGED')
1330 self.assertEqual(C.data['status'], 'MERGED')
1331 self.assertEqual(A.reported, 2)
1332 self.assertEqual(B.reported, 2)
1333 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001334
1335 def test_nonvoting_job(self):
1336 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001337
James E. Blair4ec821f2012-08-23 15:28:28 -07001338 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1339 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001340 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07001341 self.launch_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001342 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001343
1344 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001345
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001346 self.assertEqual(A.data['status'], 'MERGED')
1347 self.assertEqual(A.reported, 2)
1348 self.assertEqual(
1349 self.getJobFromHistory('nonvoting-project-merge').result,
1350 'SUCCESS')
1351 self.assertEqual(
1352 self.getJobFromHistory('nonvoting-project-test1').result,
1353 'SUCCESS')
1354 self.assertEqual(
1355 self.getJobFromHistory('nonvoting-project-test2').result,
1356 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001357
James E. Blair5821bd92015-09-16 08:48:15 -07001358 for build in self.builds:
1359 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1360
James E. Blaire0487072012-08-29 17:38:31 -07001361 def test_check_queue_success(self):
1362 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001363
James E. Blaire0487072012-08-29 17:38:31 -07001364 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1365 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1366
1367 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001368
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001369 self.assertEqual(A.data['status'], 'NEW')
1370 self.assertEqual(A.reported, 1)
1371 self.assertEqual(self.getJobFromHistory('project-merge').result,
1372 'SUCCESS')
1373 self.assertEqual(self.getJobFromHistory('project-test1').result,
1374 'SUCCESS')
1375 self.assertEqual(self.getJobFromHistory('project-test2').result,
1376 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001377
1378 def test_check_queue_failure(self):
1379 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001380
James E. Blaire0487072012-08-29 17:38:31 -07001381 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair08d19992016-08-10 15:25:31 -07001382 self.launch_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001383 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1384
1385 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001386
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001387 self.assertEqual(A.data['status'], 'NEW')
1388 self.assertEqual(A.reported, 1)
1389 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001390 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001391 self.assertEqual(self.getJobFromHistory('project-test1').result,
1392 'SUCCESS')
1393 self.assertEqual(self.getJobFromHistory('project-test2').result,
1394 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001395
James E. Blairec056492016-07-22 09:45:56 -07001396 @skip("Disabled for early v3 development")
James E. Blair127bc182012-08-28 15:55:15 -07001397 def test_dependent_behind_dequeue(self):
1398 "test that dependent changes behind dequeued changes work"
1399 # This complicated test is a reproduction of a real life bug
1400 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001401
James E. Blair08d19992016-08-10 15:25:31 -07001402 self.launch_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001403 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1404 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1405 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1406 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1407 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1408 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1409 D.setDependsOn(C, 1)
1410 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001411 A.addApproval('code-review', 2)
1412 B.addApproval('code-review', 2)
1413 C.addApproval('code-review', 2)
1414 D.addApproval('code-review', 2)
1415 E.addApproval('code-review', 2)
1416 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001417
1418 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001419
1420 # Change object re-use in the gerrit trigger is hidden if
1421 # changes are added in quick succession; waiting makes it more
1422 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001423 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001424 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001425 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001426 self.waitUntilSettled()
1427
James E. Blair08d19992016-08-10 15:25:31 -07001428 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001429 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001430 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001431 self.waitUntilSettled()
1432
James E. Blair8b5408c2016-08-08 15:37:46 -07001433 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001434 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001435 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001436 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001437 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001438 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001439 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001440 self.waitUntilSettled()
1441
James E. Blair08d19992016-08-10 15:25:31 -07001442 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001443 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001444 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001445 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001446 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001447 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001448 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001449 self.waitUntilSettled()
1450
1451 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001452
1453 # Grab pointers to the jobs we want to release before
1454 # releasing any, because list indexes may change as
1455 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001456 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001457 a.release()
1458 b.release()
1459 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001460 self.waitUntilSettled()
1461
James E. Blair08d19992016-08-10 15:25:31 -07001462 self.launch_server.hold_jobs_in_build = False
1463 self.launch_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001464 self.waitUntilSettled()
1465
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001466 self.assertEqual(A.data['status'], 'NEW')
1467 self.assertEqual(B.data['status'], 'MERGED')
1468 self.assertEqual(C.data['status'], 'MERGED')
1469 self.assertEqual(D.data['status'], 'MERGED')
1470 self.assertEqual(E.data['status'], 'MERGED')
1471 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001472
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001473 self.assertEqual(A.reported, 2)
1474 self.assertEqual(B.reported, 2)
1475 self.assertEqual(C.reported, 2)
1476 self.assertEqual(D.reported, 2)
1477 self.assertEqual(E.reported, 2)
1478 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001479
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001480 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1481 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001482
1483 def test_merger_repack(self):
1484 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001485
James E. Blair05fed602012-09-07 12:45:24 -07001486 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001487 A.addApproval('code-review', 2)
1488 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001489 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001490 self.assertEqual(self.getJobFromHistory('project-merge').result,
1491 'SUCCESS')
1492 self.assertEqual(self.getJobFromHistory('project-test1').result,
1493 'SUCCESS')
1494 self.assertEqual(self.getJobFromHistory('project-test2').result,
1495 'SUCCESS')
1496 self.assertEqual(A.data['status'], 'MERGED')
1497 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001498 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001499 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001500
Monty Taylorbc758832013-06-17 17:22:42 -04001501 path = os.path.join(self.git_root, "org/project")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001502 print(repack_repo(path))
James E. Blair05fed602012-09-07 12:45:24 -07001503
1504 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001505 A.addApproval('code-review', 2)
1506 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001507 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001508 self.assertEqual(self.getJobFromHistory('project-merge').result,
1509 'SUCCESS')
1510 self.assertEqual(self.getJobFromHistory('project-test1').result,
1511 'SUCCESS')
1512 self.assertEqual(self.getJobFromHistory('project-test2').result,
1513 'SUCCESS')
1514 self.assertEqual(A.data['status'], 'MERGED')
1515 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001516
James E. Blair4886f282012-11-15 09:27:33 -08001517 def test_merger_repack_large_change(self):
1518 "Test that the merger works with large changes after a repack"
1519 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001520 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001521 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Hesketh352264b2015-08-11 23:42:08 +10001522 url = self.fake_gerrit.getGitUrl(
Paul Belanger503bded2016-11-11 19:43:38 -05001523 tenant.layout.project_configs.get('org/project1'))
James E. Blair4076e2b2014-01-28 12:42:20 -08001524 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08001525 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1526 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001527 path = os.path.join(self.upstream_root, "org/project1")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001528 print(repack_repo(path))
Monty Taylorbc758832013-06-17 17:22:42 -04001529 path = os.path.join(self.git_root, "org/project1")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001530 print(repack_repo(path))
James E. Blair4886f282012-11-15 09:27:33 -08001531
James E. Blair8b5408c2016-08-08 15:37:46 -07001532 A.addApproval('code-review', 2)
1533 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001534 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001535 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001536 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001537 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001538 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001539 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001540 'SUCCESS')
1541 self.assertEqual(A.data['status'], 'MERGED')
1542 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001543
James E. Blairec056492016-07-22 09:45:56 -07001544 @skip("Disabled for early v3 development")
James E. Blair7ee88a22012-09-12 18:59:31 +02001545 def test_nonexistent_job(self):
1546 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001547 # Set to the state immediately after a restart
1548 self.resetGearmanServer()
1549 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02001550
1551 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001552 A.addApproval('code-review', 2)
1553 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair7ee88a22012-09-12 18:59:31 +02001554 # There may be a thread about to report a lost change
1555 while A.reported < 2:
1556 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001557 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001558 self.assertFalse(job_names)
1559 self.assertEqual(A.data['status'], 'NEW')
1560 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001561 self.assertEmptyQueues()
1562
1563 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001564 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02001565 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001566 A.addApproval('code-review', 2)
1567 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair7ee88a22012-09-12 18:59:31 +02001568 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001569 self.assertEqual(self.getJobFromHistory('project-merge').result,
1570 'SUCCESS')
1571 self.assertEqual(self.getJobFromHistory('project-test1').result,
1572 'SUCCESS')
1573 self.assertEqual(self.getJobFromHistory('project-test2').result,
1574 'SUCCESS')
1575 self.assertEqual(A.data['status'], 'MERGED')
1576 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08001577
James E. Blair2fa50962013-01-30 21:50:41 -08001578 def test_new_patchset_dequeues_old(self):
1579 "Test that a new patchset causes the old to be dequeued"
1580 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair08d19992016-08-10 15:25:31 -07001581 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001582 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1583 M.setMerged()
1584
1585 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1586 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1587 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1588 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001589 A.addApproval('code-review', 2)
1590 B.addApproval('code-review', 2)
1591 C.addApproval('code-review', 2)
1592 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001593
1594 C.setDependsOn(B, 1)
1595 B.setDependsOn(A, 1)
1596 A.setDependsOn(M, 1)
1597
James E. Blair8b5408c2016-08-08 15:37:46 -07001598 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1599 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1600 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1601 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001602 self.waitUntilSettled()
1603
1604 B.addPatchset()
1605 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1606 self.waitUntilSettled()
1607
James E. Blair08d19992016-08-10 15:25:31 -07001608 self.launch_server.hold_jobs_in_build = False
1609 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001610 self.waitUntilSettled()
1611
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001612 self.assertEqual(A.data['status'], 'MERGED')
1613 self.assertEqual(A.reported, 2)
1614 self.assertEqual(B.data['status'], 'NEW')
1615 self.assertEqual(B.reported, 2)
1616 self.assertEqual(C.data['status'], 'NEW')
1617 self.assertEqual(C.reported, 2)
1618 self.assertEqual(D.data['status'], 'MERGED')
1619 self.assertEqual(D.reported, 2)
1620 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001621
James E. Blairba437362015-02-07 11:41:52 -08001622 def test_new_patchset_check(self):
1623 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001624
James E. Blair08d19992016-08-10 15:25:31 -07001625 self.launch_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001626
1627 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001628 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001629 tenant = self.sched.abide.tenants.get('tenant-one')
1630 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001631
1632 # Add two git-dependent changes
1633 B.setDependsOn(A, 1)
1634 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1635 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001636 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1637 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001638
1639 # A live item, and a non-live/live pair
1640 items = check_pipeline.getAllItems()
1641 self.assertEqual(len(items), 3)
1642
1643 self.assertEqual(items[0].change.number, '1')
1644 self.assertEqual(items[0].change.patchset, '1')
1645 self.assertFalse(items[0].live)
1646
1647 self.assertEqual(items[1].change.number, '2')
1648 self.assertEqual(items[1].change.patchset, '1')
1649 self.assertTrue(items[1].live)
1650
1651 self.assertEqual(items[2].change.number, '1')
1652 self.assertEqual(items[2].change.patchset, '1')
1653 self.assertTrue(items[2].live)
1654
1655 # Add a new patchset to A
1656 A.addPatchset()
1657 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1658 self.waitUntilSettled()
1659
1660 # The live copy of A,1 should be gone, but the non-live and B
1661 # should continue, and we should have a new A,2
1662 items = check_pipeline.getAllItems()
1663 self.assertEqual(len(items), 3)
1664
1665 self.assertEqual(items[0].change.number, '1')
1666 self.assertEqual(items[0].change.patchset, '1')
1667 self.assertFalse(items[0].live)
1668
1669 self.assertEqual(items[1].change.number, '2')
1670 self.assertEqual(items[1].change.patchset, '1')
1671 self.assertTrue(items[1].live)
1672
1673 self.assertEqual(items[2].change.number, '1')
1674 self.assertEqual(items[2].change.patchset, '2')
1675 self.assertTrue(items[2].live)
1676
1677 # Add a new patchset to B
1678 B.addPatchset()
1679 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1680 self.waitUntilSettled()
1681
1682 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1683 # but we should have a new B,2 (still based on A,1)
1684 items = check_pipeline.getAllItems()
1685 self.assertEqual(len(items), 3)
1686
1687 self.assertEqual(items[0].change.number, '1')
1688 self.assertEqual(items[0].change.patchset, '2')
1689 self.assertTrue(items[0].live)
1690
1691 self.assertEqual(items[1].change.number, '1')
1692 self.assertEqual(items[1].change.patchset, '1')
1693 self.assertFalse(items[1].live)
1694
1695 self.assertEqual(items[2].change.number, '2')
1696 self.assertEqual(items[2].change.patchset, '2')
1697 self.assertTrue(items[2].live)
1698
1699 self.builds[0].release()
1700 self.waitUntilSettled()
1701 self.builds[0].release()
1702 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001703 self.launch_server.hold_jobs_in_build = False
1704 self.launch_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001705 self.waitUntilSettled()
1706
1707 self.assertEqual(A.reported, 1)
1708 self.assertEqual(B.reported, 1)
1709 self.assertEqual(self.history[0].result, 'ABORTED')
1710 self.assertEqual(self.history[0].changes, '1,1')
1711 self.assertEqual(self.history[1].result, 'ABORTED')
1712 self.assertEqual(self.history[1].changes, '1,1 2,1')
1713 self.assertEqual(self.history[2].result, 'SUCCESS')
1714 self.assertEqual(self.history[2].changes, '1,2')
1715 self.assertEqual(self.history[3].result, 'SUCCESS')
1716 self.assertEqual(self.history[3].changes, '1,1 2,2')
1717
1718 def test_abandoned_gate(self):
1719 "Test that an abandoned change is dequeued from gate"
1720
James E. Blair08d19992016-08-10 15:25:31 -07001721 self.launch_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001722
1723 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001724 A.addApproval('code-review', 2)
1725 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001726 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001727 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1728 self.assertEqual(self.builds[0].name, 'project-merge')
1729
1730 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1731 self.waitUntilSettled()
1732
James E. Blair08d19992016-08-10 15:25:31 -07001733 self.launch_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001734 self.waitUntilSettled()
1735
Arie5b4048c2016-10-05 16:12:06 +03001736 self.assertBuilds([])
1737 self.assertHistory([
1738 dict(name='project-merge', result='ABORTED', changes='1,1')],
1739 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001740 self.assertEqual(A.reported, 1,
1741 "Abandoned gate change should report only start")
1742
1743 def test_abandoned_check(self):
1744 "Test that an abandoned change is dequeued from check"
1745
James E. Blair08d19992016-08-10 15:25:31 -07001746 self.launch_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001747
1748 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1749 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001750 tenant = self.sched.abide.tenants.get('tenant-one')
1751 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001752
1753 # Add two git-dependent changes
1754 B.setDependsOn(A, 1)
1755 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1756 self.waitUntilSettled()
1757 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1758 self.waitUntilSettled()
1759 # A live item, and a non-live/live pair
1760 items = check_pipeline.getAllItems()
1761 self.assertEqual(len(items), 3)
1762
1763 self.assertEqual(items[0].change.number, '1')
1764 self.assertFalse(items[0].live)
1765
1766 self.assertEqual(items[1].change.number, '2')
1767 self.assertTrue(items[1].live)
1768
1769 self.assertEqual(items[2].change.number, '1')
1770 self.assertTrue(items[2].live)
1771
1772 # Abandon A
1773 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1774 self.waitUntilSettled()
1775
1776 # The live copy of A should be gone, but the non-live and B
1777 # should continue
1778 items = check_pipeline.getAllItems()
1779 self.assertEqual(len(items), 2)
1780
1781 self.assertEqual(items[0].change.number, '1')
1782 self.assertFalse(items[0].live)
1783
1784 self.assertEqual(items[1].change.number, '2')
1785 self.assertTrue(items[1].live)
1786
James E. Blair08d19992016-08-10 15:25:31 -07001787 self.launch_server.hold_jobs_in_build = False
1788 self.launch_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001789 self.waitUntilSettled()
1790
1791 self.assertEqual(len(self.history), 4)
1792 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001793 'Build should have been aborted')
1794 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001795 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001796
James E. Blairec056492016-07-22 09:45:56 -07001797 @skip("Disabled for early v3 development")
Steve Varnau7b78b312015-04-03 14:49:46 -07001798 def test_abandoned_not_timer(self):
1799 "Test that an abandoned change does not cancel timer jobs"
1800
James E. Blair08d19992016-08-10 15:25:31 -07001801 self.launch_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001802
1803 # Start timer trigger - also org/project
James E. Blairf84026c2015-12-08 16:11:46 -08001804 self.updateConfigLayout(
1805 'tests/fixtures/layout-idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001806 self.sched.reconfigure(self.config)
1807 self.registerJobs()
1808 # The pipeline triggers every second, so we should have seen
1809 # several by now.
1810 time.sleep(5)
1811 self.waitUntilSettled()
1812 # Stop queuing timer triggered jobs so that the assertions
1813 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08001814 self.updateConfigLayout(
1815 'tests/fixtures/layout-no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001816 self.sched.reconfigure(self.config)
1817 self.registerJobs()
1818 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1819
1820 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1821 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1822 self.waitUntilSettled()
1823 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1824
1825 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1826 self.waitUntilSettled()
1827
1828 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1829
James E. Blair08d19992016-08-10 15:25:31 -07001830 self.launch_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001831 self.waitUntilSettled()
1832
Arx Cruzb1b010d2013-10-28 19:49:59 -02001833 def test_zuul_url_return(self):
1834 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001835 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
James E. Blair08d19992016-08-10 15:25:31 -07001836 self.launch_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001837
1838 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001839 A.addApproval('code-review', 2)
1840 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001841 self.waitUntilSettled()
1842
1843 self.assertEqual(len(self.builds), 1)
1844 for build in self.builds:
1845 self.assertTrue('ZUUL_URL' in build.parameters)
1846
James E. Blair08d19992016-08-10 15:25:31 -07001847 self.launch_server.hold_jobs_in_build = False
1848 self.launch_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001849 self.waitUntilSettled()
1850
James E. Blair2fa50962013-01-30 21:50:41 -08001851 def test_new_patchset_dequeues_old_on_head(self):
1852 "Test that a new patchset causes the old to be dequeued (at head)"
1853 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair08d19992016-08-10 15:25:31 -07001854 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001855 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1856 M.setMerged()
1857 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1858 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1859 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1860 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001861 A.addApproval('code-review', 2)
1862 B.addApproval('code-review', 2)
1863 C.addApproval('code-review', 2)
1864 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001865
1866 C.setDependsOn(B, 1)
1867 B.setDependsOn(A, 1)
1868 A.setDependsOn(M, 1)
1869
James E. Blair8b5408c2016-08-08 15:37:46 -07001870 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1871 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1872 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1873 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001874 self.waitUntilSettled()
1875
1876 A.addPatchset()
1877 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1878 self.waitUntilSettled()
1879
James E. Blair08d19992016-08-10 15:25:31 -07001880 self.launch_server.hold_jobs_in_build = False
1881 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001882 self.waitUntilSettled()
1883
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001884 self.assertEqual(A.data['status'], 'NEW')
1885 self.assertEqual(A.reported, 2)
1886 self.assertEqual(B.data['status'], 'NEW')
1887 self.assertEqual(B.reported, 2)
1888 self.assertEqual(C.data['status'], 'NEW')
1889 self.assertEqual(C.reported, 2)
1890 self.assertEqual(D.data['status'], 'MERGED')
1891 self.assertEqual(D.reported, 2)
1892 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001893
1894 def test_new_patchset_dequeues_old_without_dependents(self):
1895 "Test that a new patchset causes only the old to be dequeued"
James E. Blair08d19992016-08-10 15:25:31 -07001896 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001897 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1898 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1899 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001900 A.addApproval('code-review', 2)
1901 B.addApproval('code-review', 2)
1902 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001903
James E. Blair8b5408c2016-08-08 15:37:46 -07001904 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1905 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1906 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001907 self.waitUntilSettled()
1908
1909 B.addPatchset()
1910 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1911 self.waitUntilSettled()
1912
James E. Blair08d19992016-08-10 15:25:31 -07001913 self.launch_server.hold_jobs_in_build = False
1914 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001915 self.waitUntilSettled()
1916
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001917 self.assertEqual(A.data['status'], 'MERGED')
1918 self.assertEqual(A.reported, 2)
1919 self.assertEqual(B.data['status'], 'NEW')
1920 self.assertEqual(B.reported, 2)
1921 self.assertEqual(C.data['status'], 'MERGED')
1922 self.assertEqual(C.reported, 2)
1923 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001924
1925 def test_new_patchset_dequeues_old_independent_queue(self):
1926 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair08d19992016-08-10 15:25:31 -07001927 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001928 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1929 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1930 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1931 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1932 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1933 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1934 self.waitUntilSettled()
1935
1936 B.addPatchset()
1937 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1938 self.waitUntilSettled()
1939
James E. Blair08d19992016-08-10 15:25:31 -07001940 self.launch_server.hold_jobs_in_build = False
1941 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001942 self.waitUntilSettled()
1943
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001944 self.assertEqual(A.data['status'], 'NEW')
1945 self.assertEqual(A.reported, 1)
1946 self.assertEqual(B.data['status'], 'NEW')
1947 self.assertEqual(B.reported, 1)
1948 self.assertEqual(C.data['status'], 'NEW')
1949 self.assertEqual(C.reported, 1)
1950 self.assertEqual(len(self.history), 10)
1951 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001952
James E. Blair18c64442014-03-18 10:14:45 -07001953 def test_noop_job(self):
1954 "Test that the internal noop job works"
1955 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001956 A.addApproval('code-review', 2)
1957 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001958 self.waitUntilSettled()
1959
1960 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1961 self.assertTrue(self.sched._areAllBuildsComplete())
1962 self.assertEqual(len(self.history), 0)
1963 self.assertEqual(A.data['status'], 'MERGED')
1964 self.assertEqual(A.reported, 2)
1965
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001966 def test_no_job_project(self):
1967 "Test that reports with no jobs don't get sent"
1968 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1969 'master', 'A')
1970 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1971 self.waitUntilSettled()
1972
1973 # Change wasn't reported to
1974 self.assertEqual(A.reported, False)
1975
1976 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001977 tenant = self.sched.abide.tenants.get('tenant-one')
1978 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001979 items = check_pipeline.getAllItems()
1980 self.assertEqual(len(items), 0)
1981
1982 self.assertEqual(len(self.history), 0)
1983
James E. Blair7d0dedc2013-02-21 17:26:09 -08001984 def test_zuul_refs(self):
1985 "Test that zuul refs exist and have the right changes"
James E. Blair08d19992016-08-10 15:25:31 -07001986 self.launch_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001987 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1988 M1.setMerged()
1989 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1990 M2.setMerged()
1991
1992 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1993 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1994 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1995 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001996 A.addApproval('code-review', 2)
1997 B.addApproval('code-review', 2)
1998 C.addApproval('code-review', 2)
1999 D.addApproval('code-review', 2)
2000 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2001 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
2002 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
2003 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002004
2005 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002006 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002007 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002008 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002009 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002010 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002011 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002012 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002013 self.waitUntilSettled()
2014
James E. Blair7d0dedc2013-02-21 17:26:09 -08002015 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08002016 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002017 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002018 if x.parameters['ZUUL_CHANGE'] == '3':
2019 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002020 a_build = x
2021 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002022 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002023 b_build = x
2024 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002025 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002026 c_build = x
2027 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002028 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002029 d_build = x
2030 if a_build and b_build and c_build and d_build:
2031 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002032
2033 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002034 self.assertIsNotNone(a_zref)
2035 self.assertIsNotNone(b_zref)
2036 self.assertIsNotNone(c_zref)
2037 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002038
2039 # And they should all be different
2040 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002041 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002042
Clint Byrum3343e3e2016-11-15 16:05:03 -08002043 # should have a, not b, and should not be in project2
2044 self.assertTrue(a_build.hasChanges(A))
2045 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002046
Clint Byrum3343e3e2016-11-15 16:05:03 -08002047 # should have a and b, and should not be in project2
2048 self.assertTrue(b_build.hasChanges(A, B))
2049 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002050
Clint Byrum3343e3e2016-11-15 16:05:03 -08002051 # should have a and b in 1, c in 2
2052 self.assertTrue(c_build.hasChanges(A, B, C))
2053 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002054
Clint Byrum3343e3e2016-11-15 16:05:03 -08002055 # should have a and b in 1, c and d in 2
2056 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002057
James E. Blair08d19992016-08-10 15:25:31 -07002058 self.launch_server.hold_jobs_in_build = False
2059 self.launch_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002060 self.waitUntilSettled()
2061
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002062 self.assertEqual(A.data['status'], 'MERGED')
2063 self.assertEqual(A.reported, 2)
2064 self.assertEqual(B.data['status'], 'MERGED')
2065 self.assertEqual(B.reported, 2)
2066 self.assertEqual(C.data['status'], 'MERGED')
2067 self.assertEqual(C.reported, 2)
2068 self.assertEqual(D.data['status'], 'MERGED')
2069 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002070
James E. Blair4a28a882013-08-23 15:17:33 -07002071 def test_rerun_on_error(self):
2072 "Test that if a worker fails to run a job, it is run again"
James E. Blair08d19992016-08-10 15:25:31 -07002073 self.launch_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002074 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002075 A.addApproval('code-review', 2)
2076 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002077 self.waitUntilSettled()
2078
2079 self.builds[0].run_error = True
James E. Blair08d19992016-08-10 15:25:31 -07002080 self.launch_server.hold_jobs_in_build = False
2081 self.launch_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002082 self.waitUntilSettled()
2083 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2084 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2085
James E. Blair412e5582013-04-22 15:50:12 -07002086 def test_statsd(self):
2087 "Test each of the statsd methods used in the scheduler"
2088 import extras
2089 statsd = extras.try_import('statsd.statsd')
2090 statsd.incr('test-incr')
2091 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002092 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002093 self.assertReportedStat('test-incr', '1|c')
2094 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002095 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002096
James E. Blairec056492016-07-22 09:45:56 -07002097 @skip("Disabled for early v3 development")
James E. Blairdad52252014-02-07 16:59:17 -08002098 def test_stuck_job_cleanup(self):
2099 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07002100 # This job won't be registered at startup because it is not in
2101 # the standard layout, but we need it to already be registerd
2102 # for when we reconfigure, as that is when Zuul will attempt
2103 # to run the new job.
2104 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002105 self.gearman_server.hold_jobs_in_queue = True
2106 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002107 A.addApproval('code-review', 2)
2108 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002109 self.waitUntilSettled()
2110 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2111
James E. Blairf84026c2015-12-08 16:11:46 -08002112 self.updateConfigLayout(
2113 'tests/fixtures/layout-no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002114 self.sched.reconfigure(self.config)
2115 self.waitUntilSettled()
2116
James E. Blair18c64442014-03-18 10:14:45 -07002117 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002118 self.waitUntilSettled()
2119 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2120 self.assertTrue(self.sched._areAllBuildsComplete())
2121
2122 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002123 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002124 self.assertEqual(self.history[0].result, 'SUCCESS')
2125
James E. Blair879dafb2015-07-17 14:04:49 -07002126 def test_file_head(self):
2127 # This is a regression test for an observed bug. A change
2128 # with a file named "HEAD" in the root directory of the repo
2129 # was processed by a merger. It then was unable to reset the
2130 # repo because of:
2131 # GitCommandError: 'git reset --hard HEAD' returned
2132 # with exit code 128
2133 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2134 # and filename
2135 # Use '--' to separate filenames from revisions'
2136
2137 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002138 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002139 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2140
2141 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2142 self.waitUntilSettled()
2143
2144 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2145 self.waitUntilSettled()
2146
2147 self.assertIn('Build succeeded', A.messages[0])
2148 self.assertIn('Build succeeded', B.messages[0])
2149
James E. Blairec056492016-07-22 09:45:56 -07002150 @skip("Disabled for early v3 development")
James E. Blair70c71582013-03-06 08:50:50 -08002151 def test_file_jobs(self):
2152 "Test that file jobs run only when appropriate"
2153 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2154 A.addPatchset(['pip-requires'])
2155 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002156 A.addApproval('code-review', 2)
2157 B.addApproval('code-review', 2)
2158 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2159 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002160 self.waitUntilSettled()
2161
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002162 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002163 if x.name == 'project-testfile']
2164
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002165 self.assertEqual(len(testfile_jobs), 1)
2166 self.assertEqual(testfile_jobs[0].changes, '1,2')
2167 self.assertEqual(A.data['status'], 'MERGED')
2168 self.assertEqual(A.reported, 2)
2169 self.assertEqual(B.data['status'], 'MERGED')
2170 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002171
Clint Byrum3ee39f32016-11-17 23:45:07 -08002172 def _test_irrelevant_files_jobs(self, should_skip):
2173 "Test that jobs with irrelevant-files filter run only when appropriate"
2174 self.updateConfigLayout('layout-irrelevant-files')
Maru Newby3fe5f852015-01-13 04:22:14 +00002175 self.sched.reconfigure(self.config)
Clint Byrum3ee39f32016-11-17 23:45:07 -08002176
2177 if should_skip:
2178 files = {'ignoreme': 'ignored\n'}
2179 else:
2180 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002181
2182 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002183 'master',
2184 'test irrelevant-files',
2185 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002186 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2187 self.waitUntilSettled()
2188
2189 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002190 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002191
2192 if should_skip:
2193 self.assertEqual([], tested_change_ids)
2194 else:
2195 self.assertIn(change.data['number'], tested_change_ids)
2196
Clint Byrum3ee39f32016-11-17 23:45:07 -08002197 def test_irrelevant_files_match_skips_job(self):
2198 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002199
Clint Byrum3ee39f32016-11-17 23:45:07 -08002200 def test_irrelevant_files_no_match_runs_job(self):
2201 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002202
Clint Byrum85493602016-11-18 11:59:47 -08002203 def test_inherited_jobs_keep_matchers(self):
2204 self.updateConfigLayout('layout-inheritance')
2205 self.sched.reconfigure(self.config)
2206
2207 files = {'ignoreme': 'ignored\n'}
2208
2209 change = self.fake_gerrit.addFakeChange('org/project',
2210 'master',
2211 'test irrelevant-files',
2212 files=files)
2213 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2214 self.waitUntilSettled()
2215
2216 run_jobs = set([build.name for build in self.history])
2217
2218 self.assertEqual(set(['project-test-nomatch-starts-empty',
2219 'project-test-nomatch-starts-full']), run_jobs)
2220
James E. Blairec056492016-07-22 09:45:56 -07002221 @skip("Disabled for early v3 development")
James E. Blair3c5e5b52013-04-26 11:17:03 -07002222 def test_test_config(self):
2223 "Test that we can test the config"
James E. Blairf84026c2015-12-08 16:11:46 -08002224 self.sched.testConfig(self.config.get('zuul', 'tenant_config'),
Joshua Hesketh352264b2015-08-11 23:42:08 +10002225 self.connections)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002226
James E. Blairec056492016-07-22 09:45:56 -07002227 @skip("Disabled for early v3 development")
James E. Blairc8a1e052014-02-25 09:29:26 -08002228 def test_queue_names(self):
2229 "Test shared change queue names"
2230 project1 = self.sched.layout.projects['org/project1']
2231 project2 = self.sched.layout.projects['org/project2']
2232 q1 = self.sched.layout.pipelines['gate'].getQueue(project1)
2233 q2 = self.sched.layout.pipelines['gate'].getQueue(project2)
2234 self.assertEqual(q1.name, 'integration')
2235 self.assertEqual(q2.name, 'integration')
2236
James E. Blairf84026c2015-12-08 16:11:46 -08002237 self.updateConfigLayout(
2238 'tests/fixtures/layout-bad-queue.yaml')
James E. Blairc8a1e052014-02-25 09:29:26 -08002239 with testtools.ExpectedException(
2240 Exception, "More than one name assigned to change queue"):
2241 self.sched.reconfigure(self.config)
2242
James E. Blair64ed6f22013-07-10 14:07:23 -07002243 def test_queue_precedence(self):
2244 "Test that queue precedence works"
2245
2246 self.gearman_server.hold_jobs_in_queue = True
James E. Blair08d19992016-08-10 15:25:31 -07002247 self.launch_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002248 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2249 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002250 A.addApproval('code-review', 2)
2251 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002252
2253 self.waitUntilSettled()
2254 self.gearman_server.hold_jobs_in_queue = False
2255 self.gearman_server.release()
2256 self.waitUntilSettled()
2257
James E. Blair8de58bd2013-07-18 16:23:33 -07002258 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002259 self.orderedRelease()
James E. Blair08d19992016-08-10 15:25:31 -07002260 self.launch_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002261 self.waitUntilSettled()
2262
James E. Blair64ed6f22013-07-10 14:07:23 -07002263 self.log.debug(self.history)
2264 self.assertEqual(self.history[0].pipeline, 'gate')
2265 self.assertEqual(self.history[1].pipeline, 'check')
2266 self.assertEqual(self.history[2].pipeline, 'gate')
2267 self.assertEqual(self.history[3].pipeline, 'gate')
2268 self.assertEqual(self.history[4].pipeline, 'check')
2269 self.assertEqual(self.history[5].pipeline, 'check')
2270
James E. Blairec056492016-07-22 09:45:56 -07002271 @skip("Disabled for early v3 development")
Clark Boylana5edbe42014-06-03 16:39:10 -07002272 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002273 "Test that we can retrieve JSON status info"
James E. Blair08d19992016-08-10 15:25:31 -07002274 self.launch_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002275 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002276 A.addApproval('code-review', 2)
2277 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002278 self.waitUntilSettled()
2279
James E. Blair08d19992016-08-10 15:25:31 -07002280 self.launch_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002281 self.waitUntilSettled()
2282
James E. Blair1843a552013-07-03 14:19:52 -07002283 port = self.webapp.server.socket.getsockname()[1]
2284
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002285 req = urllib.request.Request("http://localhost:%s/status.json" % port)
2286 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002287 headers = f.info()
2288 self.assertIn('Content-Length', headers)
2289 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002290 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2291 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002292 self.assertIn('Access-Control-Allow-Origin', headers)
2293 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002294 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002295 self.assertIn('Expires', headers)
James E. Blair1843a552013-07-03 14:19:52 -07002296 data = f.read()
2297
James E. Blair08d19992016-08-10 15:25:31 -07002298 self.launch_server.hold_jobs_in_build = False
2299 self.launch_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002300 self.waitUntilSettled()
2301
2302 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002303 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002304 for p in data['pipelines']:
2305 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002306 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002307 self.assertEqual(q['window'], 20)
2308 else:
2309 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002310 for head in q['heads']:
2311 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002312 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002313 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002314 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002315 status_jobs.append(job)
2316 self.assertEqual('project-merge', status_jobs[0]['name'])
2317 self.assertEqual('https://server/job/project-merge/0/',
2318 status_jobs[0]['url'])
2319 self.assertEqual('http://logs.example.com/1/1/gate/project-merge/0',
2320 status_jobs[0]['report_url'])
2321
2322 self.assertEqual('project-test1', status_jobs[1]['name'])
2323 self.assertEqual('https://server/job/project-test1/1/',
2324 status_jobs[1]['url'])
2325 self.assertEqual('http://logs.example.com/1/1/gate/project-test1/1',
2326 status_jobs[1]['report_url'])
2327
2328 self.assertEqual('project-test2', status_jobs[2]['name'])
2329 self.assertEqual('https://server/job/project-test2/2/',
2330 status_jobs[2]['url'])
2331 self.assertEqual('http://logs.example.com/1/1/gate/project-test2/2',
2332 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002333
James E. Blairec056492016-07-22 09:45:56 -07002334 @skip("Disabled for early v3 development")
James E. Blairc3d428e2013-12-03 15:06:48 -08002335 def test_merging_queues(self):
2336 "Test that transitively-connected change queues are merged"
James E. Blairf84026c2015-12-08 16:11:46 -08002337 self.updateConfigLayout(
2338 'tests/fixtures/layout-merge-queues.yaml')
James E. Blairc3d428e2013-12-03 15:06:48 -08002339 self.sched.reconfigure(self.config)
2340 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
2341
James E. Blairec056492016-07-22 09:45:56 -07002342 @skip("Disabled for early v3 development")
James E. Blairaf17a972016-02-03 15:07:18 -08002343 def test_mutex(self):
2344 "Test job mutexes"
2345 self.config.set('zuul', 'layout_config',
2346 'tests/fixtures/layout-mutex.yaml')
2347 self.sched.reconfigure(self.config)
2348
James E. Blair08d19992016-08-10 15:25:31 -07002349 self.launch_server.hold_jobs_in_build = True
James E. Blairaf17a972016-02-03 15:07:18 -08002350 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2351 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2352 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2353
2354 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2355 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2356 self.waitUntilSettled()
2357 self.assertEqual(len(self.builds), 3)
2358 self.assertEqual(self.builds[0].name, 'project-test1')
2359 self.assertEqual(self.builds[1].name, 'mutex-one')
2360 self.assertEqual(self.builds[2].name, 'project-test1')
2361
James E. Blair08d19992016-08-10 15:25:31 -07002362 self.launch_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002363 self.waitUntilSettled()
2364
2365 self.assertEqual(len(self.builds), 3)
2366 self.assertEqual(self.builds[0].name, 'project-test1')
2367 self.assertEqual(self.builds[1].name, 'project-test1')
2368 self.assertEqual(self.builds[2].name, 'mutex-two')
2369 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2370
James E. Blair08d19992016-08-10 15:25:31 -07002371 self.launch_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002372 self.waitUntilSettled()
2373
2374 self.assertEqual(len(self.builds), 3)
2375 self.assertEqual(self.builds[0].name, 'project-test1')
2376 self.assertEqual(self.builds[1].name, 'project-test1')
2377 self.assertEqual(self.builds[2].name, 'mutex-one')
2378 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2379
James E. Blair08d19992016-08-10 15:25:31 -07002380 self.launch_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002381 self.waitUntilSettled()
2382
2383 self.assertEqual(len(self.builds), 3)
2384 self.assertEqual(self.builds[0].name, 'project-test1')
2385 self.assertEqual(self.builds[1].name, 'project-test1')
2386 self.assertEqual(self.builds[2].name, 'mutex-two')
2387 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2388
James E. Blair08d19992016-08-10 15:25:31 -07002389 self.launch_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002390 self.waitUntilSettled()
2391
2392 self.assertEqual(len(self.builds), 2)
2393 self.assertEqual(self.builds[0].name, 'project-test1')
2394 self.assertEqual(self.builds[1].name, 'project-test1')
2395 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2396
James E. Blair08d19992016-08-10 15:25:31 -07002397 self.launch_server.hold_jobs_in_build = False
2398 self.launch_server.release()
James E. Blairaf17a972016-02-03 15:07:18 -08002399
2400 self.waitUntilSettled()
2401 self.assertEqual(len(self.builds), 0)
2402
2403 self.assertEqual(A.reported, 1)
2404 self.assertEqual(B.reported, 1)
2405 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2406
James E. Blairec056492016-07-22 09:45:56 -07002407 @skip("Disabled for early v3 development")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002408 def test_node_label(self):
2409 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002410 self.worker.registerFunction('build:node-project-test1:debian')
2411
2412 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002413 A.addApproval('code-review', 2)
2414 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002415 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002416
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002417 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2418 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2419 'debian')
2420 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002421
2422 def test_live_reconfiguration(self):
2423 "Test that live reconfiguration works"
James E. Blair08d19992016-08-10 15:25:31 -07002424 self.launch_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002425 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002426 A.addApproval('code-review', 2)
2427 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002428 self.waitUntilSettled()
2429
2430 self.sched.reconfigure(self.config)
2431
James E. Blair08d19992016-08-10 15:25:31 -07002432 self.launch_server.hold_jobs_in_build = False
2433 self.launch_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002434 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002435 self.assertEqual(self.getJobFromHistory('project-merge').result,
2436 'SUCCESS')
2437 self.assertEqual(self.getJobFromHistory('project-test1').result,
2438 'SUCCESS')
2439 self.assertEqual(self.getJobFromHistory('project-test2').result,
2440 'SUCCESS')
2441 self.assertEqual(A.data['status'], 'MERGED')
2442 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002443
James E. Blairec056492016-07-22 09:45:56 -07002444 @skip("Disabled for early v3 development")
James E. Blair6bc782d2015-07-17 16:20:21 -07002445 def test_live_reconfiguration_merge_conflict(self):
2446 # A real-world bug: a change in a gate queue has a merge
2447 # conflict and a job is added to its project while it's
2448 # sitting in the queue. The job gets added to the change and
2449 # enqueued and the change gets stuck.
2450 self.worker.registerFunction('build:project-test3')
James E. Blair08d19992016-08-10 15:25:31 -07002451 self.launch_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002452
2453 # This change is fine. It's here to stop the queue long
2454 # enough for the next change to be subject to the
2455 # reconfiguration, as well as to provide a conflict for the
2456 # next change. This change will succeed and merge.
2457 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2458 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002459 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002460
2461 # This change will be in merge conflict. During the
2462 # reconfiguration, we will add a job. We want to make sure
2463 # that doesn't cause it to get stuck.
2464 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2465 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002466 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002467
James E. Blair8b5408c2016-08-08 15:37:46 -07002468 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2469 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002470
2471 self.waitUntilSettled()
2472
2473 # No jobs have run yet
2474 self.assertEqual(A.data['status'], 'NEW')
2475 self.assertEqual(A.reported, 1)
2476 self.assertEqual(B.data['status'], 'NEW')
2477 self.assertEqual(B.reported, 1)
2478 self.assertEqual(len(self.history), 0)
2479
2480 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002481 self.updateConfigLayout(
2482 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002483 self.sched.reconfigure(self.config)
2484 self.waitUntilSettled()
2485
James E. Blair08d19992016-08-10 15:25:31 -07002486 self.launch_server.hold_jobs_in_build = False
2487 self.launch_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002488 self.waitUntilSettled()
2489
2490 self.assertEqual(A.data['status'], 'MERGED')
2491 self.assertEqual(A.reported, 2)
2492 self.assertEqual(B.data['status'], 'NEW')
2493 self.assertEqual(B.reported, 2)
2494 self.assertEqual(self.getJobFromHistory('project-merge').result,
2495 'SUCCESS')
2496 self.assertEqual(self.getJobFromHistory('project-test1').result,
2497 'SUCCESS')
2498 self.assertEqual(self.getJobFromHistory('project-test2').result,
2499 'SUCCESS')
2500 self.assertEqual(self.getJobFromHistory('project-test3').result,
2501 'SUCCESS')
2502 self.assertEqual(len(self.history), 4)
2503
James E. Blairec056492016-07-22 09:45:56 -07002504 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002505 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002506 # An extrapolation of test_live_reconfiguration_merge_conflict
2507 # that tests a job added to a job tree with a failed root does
2508 # not run.
2509 self.worker.registerFunction('build:project-test3')
James E. Blair08d19992016-08-10 15:25:31 -07002510 self.launch_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002511
2512 # This change is fine. It's here to stop the queue long
2513 # enough for the next change to be subject to the
2514 # reconfiguration. This change will succeed and merge.
2515 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2516 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002517 A.addApproval('code-review', 2)
2518 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002519 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002520 self.launch_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002521 self.waitUntilSettled()
2522
2523 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair08d19992016-08-10 15:25:31 -07002524 self.launch_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002525 B.addApproval('code-review', 2)
2526 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002527 self.waitUntilSettled()
2528
James E. Blair08d19992016-08-10 15:25:31 -07002529 self.launch_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002530 self.waitUntilSettled()
2531
2532 # Both -merge jobs have run, but no others.
2533 self.assertEqual(A.data['status'], 'NEW')
2534 self.assertEqual(A.reported, 1)
2535 self.assertEqual(B.data['status'], 'NEW')
2536 self.assertEqual(B.reported, 1)
2537 self.assertEqual(self.history[0].result, 'SUCCESS')
2538 self.assertEqual(self.history[0].name, 'project-merge')
2539 self.assertEqual(self.history[1].result, 'FAILURE')
2540 self.assertEqual(self.history[1].name, 'project-merge')
2541 self.assertEqual(len(self.history), 2)
2542
2543 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002544 self.updateConfigLayout(
2545 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002546 self.sched.reconfigure(self.config)
2547 self.waitUntilSettled()
2548
James E. Blair08d19992016-08-10 15:25:31 -07002549 self.launch_server.hold_jobs_in_build = False
2550 self.launch_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002551 self.waitUntilSettled()
2552
2553 self.assertEqual(A.data['status'], 'MERGED')
2554 self.assertEqual(A.reported, 2)
2555 self.assertEqual(B.data['status'], 'NEW')
2556 self.assertEqual(B.reported, 2)
2557 self.assertEqual(self.history[0].result, 'SUCCESS')
2558 self.assertEqual(self.history[0].name, 'project-merge')
2559 self.assertEqual(self.history[1].result, 'FAILURE')
2560 self.assertEqual(self.history[1].name, 'project-merge')
2561 self.assertEqual(self.history[2].result, 'SUCCESS')
2562 self.assertEqual(self.history[3].result, 'SUCCESS')
2563 self.assertEqual(self.history[4].result, 'SUCCESS')
2564 self.assertEqual(len(self.history), 5)
2565
James E. Blairec056492016-07-22 09:45:56 -07002566 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002567 def test_live_reconfiguration_failed_job(self):
2568 # Test that a change with a removed failing job does not
2569 # disrupt reconfiguration. If a change has a failed job and
2570 # that job is removed during a reconfiguration, we observed a
2571 # bug where the code to re-set build statuses would run on
2572 # that build and raise an exception because the job no longer
2573 # existed.
James E. Blair08d19992016-08-10 15:25:31 -07002574 self.launch_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002575
2576 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2577
2578 # This change will fail and later be removed by the reconfiguration.
James E. Blair08d19992016-08-10 15:25:31 -07002579 self.launch_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002580
2581 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2582 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002583 self.launch_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002584 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002585 self.launch_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002586 self.waitUntilSettled()
2587
2588 self.assertEqual(A.data['status'], 'NEW')
2589 self.assertEqual(A.reported, 0)
2590
2591 self.assertEqual(self.getJobFromHistory('project-merge').result,
2592 'SUCCESS')
2593 self.assertEqual(self.getJobFromHistory('project-test1').result,
2594 'FAILURE')
2595 self.assertEqual(len(self.history), 2)
2596
2597 # Remove the test1 job.
James E. Blairf84026c2015-12-08 16:11:46 -08002598 self.updateConfigLayout(
2599 'tests/fixtures/layout-live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002600 self.sched.reconfigure(self.config)
2601 self.waitUntilSettled()
2602
James E. Blair08d19992016-08-10 15:25:31 -07002603 self.launch_server.hold_jobs_in_build = False
2604 self.launch_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002605 self.waitUntilSettled()
2606
2607 self.assertEqual(self.getJobFromHistory('project-test2').result,
2608 'SUCCESS')
2609 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2610 'SUCCESS')
2611 self.assertEqual(len(self.history), 4)
2612
2613 self.assertEqual(A.data['status'], 'NEW')
2614 self.assertEqual(A.reported, 1)
2615 self.assertIn('Build succeeded', A.messages[0])
2616 # Ensure the removed job was not included in the report.
2617 self.assertNotIn('project-test1', A.messages[0])
2618
James E. Blairec056492016-07-22 09:45:56 -07002619 @skip("Disabled for early v3 development")
James E. Blairfe707d12015-08-05 15:18:15 -07002620 def test_live_reconfiguration_shared_queue(self):
2621 # Test that a change with a failing job which was removed from
2622 # this project but otherwise still exists in the system does
2623 # not disrupt reconfiguration.
2624
James E. Blair08d19992016-08-10 15:25:31 -07002625 self.launch_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002626
2627 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2628
James E. Blair08d19992016-08-10 15:25:31 -07002629 self.launch_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002630
2631 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2632 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002633 self.launch_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002634 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002635 self.launch_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002636 self.waitUntilSettled()
2637
2638 self.assertEqual(A.data['status'], 'NEW')
2639 self.assertEqual(A.reported, 0)
2640
2641 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2642 'SUCCESS')
2643 self.assertEqual(self.getJobFromHistory(
2644 'project1-project2-integration').result, 'FAILURE')
2645 self.assertEqual(len(self.history), 2)
2646
2647 # Remove the integration job.
James E. Blairf84026c2015-12-08 16:11:46 -08002648 self.updateConfigLayout(
2649 'tests/fixtures/layout-live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002650 self.sched.reconfigure(self.config)
2651 self.waitUntilSettled()
2652
James E. Blair08d19992016-08-10 15:25:31 -07002653 self.launch_server.hold_jobs_in_build = False
2654 self.launch_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002655 self.waitUntilSettled()
2656
2657 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2658 'SUCCESS')
2659 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2660 'SUCCESS')
2661 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2662 'SUCCESS')
2663 self.assertEqual(self.getJobFromHistory(
2664 'project1-project2-integration').result, 'FAILURE')
2665 self.assertEqual(len(self.history), 4)
2666
2667 self.assertEqual(A.data['status'], 'NEW')
2668 self.assertEqual(A.reported, 1)
2669 self.assertIn('Build succeeded', A.messages[0])
2670 # Ensure the removed job was not included in the report.
2671 self.assertNotIn('project1-project2-integration', A.messages[0])
2672
James E. Blairec056492016-07-22 09:45:56 -07002673 @skip("Disabled for early v3 development")
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002674 def test_double_live_reconfiguration_shared_queue(self):
2675 # This was a real-world regression. A change is added to
2676 # gate; a reconfigure happens, a second change which depends
2677 # on the first is added, and a second reconfiguration happens.
2678 # Ensure that both changes merge.
2679
2680 # A failure may indicate incorrect caching or cleaning up of
2681 # references during a reconfiguration.
James E. Blair08d19992016-08-10 15:25:31 -07002682 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002683
2684 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2685 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2686 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002687 A.addApproval('code-review', 2)
2688 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002689
2690 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002691 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002692 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002693 self.launch_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002694 self.waitUntilSettled()
2695
2696 # Reconfigure (with only one change in the pipeline).
2697 self.sched.reconfigure(self.config)
2698 self.waitUntilSettled()
2699
2700 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002701 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002702 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002703 self.launch_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002704 self.waitUntilSettled()
2705
2706 # Reconfigure (with both in the pipeline).
2707 self.sched.reconfigure(self.config)
2708 self.waitUntilSettled()
2709
James E. Blair08d19992016-08-10 15:25:31 -07002710 self.launch_server.hold_jobs_in_build = False
2711 self.launch_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002712 self.waitUntilSettled()
2713
2714 self.assertEqual(len(self.history), 8)
2715
2716 self.assertEqual(A.data['status'], 'MERGED')
2717 self.assertEqual(A.reported, 2)
2718 self.assertEqual(B.data['status'], 'MERGED')
2719 self.assertEqual(B.reported, 2)
2720
James E. Blairec056492016-07-22 09:45:56 -07002721 @skip("Disabled for early v3 development")
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002722 def test_live_reconfiguration_del_project(self):
2723 # Test project deletion from layout
2724 # while changes are enqueued
2725
James E. Blair08d19992016-08-10 15:25:31 -07002726 self.launch_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002727 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2728 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2729 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2730
2731 # A Depends-On: B
2732 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2733 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002734 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002735
2736 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2737 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2738 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002739 self.launch_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002740 self.waitUntilSettled()
2741 self.assertEqual(len(self.builds), 5)
2742
2743 # This layout defines only org/project, not org/project1
James E. Blairf84026c2015-12-08 16:11:46 -08002744 self.updateConfigLayout(
2745 'tests/fixtures/layout-live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002746 self.sched.reconfigure(self.config)
2747 self.waitUntilSettled()
2748
2749 # Builds for C aborted, builds for A succeed,
2750 # and have change B applied ahead
2751 job_c = self.getJobFromHistory('project1-test1')
2752 self.assertEqual(job_c.changes, '3,1')
2753 self.assertEqual(job_c.result, 'ABORTED')
2754
James E. Blair08d19992016-08-10 15:25:31 -07002755 self.launch_server.hold_jobs_in_build = False
2756 self.launch_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002757 self.waitUntilSettled()
2758
2759 self.assertEqual(self.getJobFromHistory('project-test1').changes,
2760 '2,1 1,1')
2761
2762 self.assertEqual(A.data['status'], 'NEW')
2763 self.assertEqual(B.data['status'], 'NEW')
2764 self.assertEqual(C.data['status'], 'NEW')
2765 self.assertEqual(A.reported, 1)
2766 self.assertEqual(B.reported, 0)
2767 self.assertEqual(C.reported, 0)
2768
2769 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
2770 self.assertIn('Build succeeded', A.messages[0])
2771
James E. Blairec056492016-07-22 09:45:56 -07002772 @skip("Disabled for early v3 development")
James E. Blaire712d9f2013-07-31 11:40:11 -07002773 def test_live_reconfiguration_functions(self):
2774 "Test live reconfiguration with a custom function"
2775 self.worker.registerFunction('build:node-project-test1:debian')
2776 self.worker.registerFunction('build:node-project-test1:wheezy')
2777 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002778 A.addApproval('code-review', 2)
2779 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002780 self.waitUntilSettled()
2781
2782 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2783 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2784 'debian')
2785 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2786
James E. Blairf84026c2015-12-08 16:11:46 -08002787 self.updateConfigLayout(
2788 'tests/fixtures/layout-live-reconfiguration-functions.yaml')
James E. Blaire712d9f2013-07-31 11:40:11 -07002789 self.sched.reconfigure(self.config)
2790 self.worker.build_history = []
2791
2792 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002793 B.addApproval('code-review', 2)
2794 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002795 self.waitUntilSettled()
2796
2797 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2798 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2799 'wheezy')
2800 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2801
James E. Blairec056492016-07-22 09:45:56 -07002802 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002803 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002804 self.updateConfigLayout(
2805 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002806 self.sched.reconfigure(self.config)
2807
2808 self.init_repo("org/new-project")
2809 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2810
James E. Blair8b5408c2016-08-08 15:37:46 -07002811 A.addApproval('code-review', 2)
2812 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002813 self.waitUntilSettled()
2814 self.assertEqual(self.getJobFromHistory('project-merge').result,
2815 'SUCCESS')
2816 self.assertEqual(self.getJobFromHistory('project-test1').result,
2817 'SUCCESS')
2818 self.assertEqual(self.getJobFromHistory('project-test2').result,
2819 'SUCCESS')
2820 self.assertEqual(A.data['status'], 'MERGED')
2821 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002822
Clark Boylan6dbbc482013-10-18 10:57:31 -07002823 def test_repo_deleted(self):
Paul Belanger66e95962016-11-11 12:11:06 -05002824 self.updateConfigLayout('layout-repo-deleted')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002825 self.sched.reconfigure(self.config)
2826
2827 self.init_repo("org/delete-project")
2828 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2829
James E. Blair8b5408c2016-08-08 15:37:46 -07002830 A.addApproval('code-review', 2)
2831 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002832 self.waitUntilSettled()
2833 self.assertEqual(self.getJobFromHistory('project-merge').result,
2834 'SUCCESS')
2835 self.assertEqual(self.getJobFromHistory('project-test1').result,
2836 'SUCCESS')
2837 self.assertEqual(self.getJobFromHistory('project-test2').result,
2838 'SUCCESS')
2839 self.assertEqual(A.data['status'], 'MERGED')
2840 self.assertEqual(A.reported, 2)
2841
2842 # Delete org/new-project zuul repo. Should be recloned.
2843 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
2844
2845 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2846
James E. Blair8b5408c2016-08-08 15:37:46 -07002847 B.addApproval('code-review', 2)
2848 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002849 self.waitUntilSettled()
2850 self.assertEqual(self.getJobFromHistory('project-merge').result,
2851 'SUCCESS')
2852 self.assertEqual(self.getJobFromHistory('project-test1').result,
2853 'SUCCESS')
2854 self.assertEqual(self.getJobFromHistory('project-test2').result,
2855 'SUCCESS')
2856 self.assertEqual(B.data['status'], 'MERGED')
2857 self.assertEqual(B.reported, 2)
2858
James E. Blairec056492016-07-22 09:45:56 -07002859 @skip("Disabled for early v3 development")
James E. Blair456f2fb2016-02-09 09:29:33 -08002860 def test_tags(self):
2861 "Test job tags"
2862 self.config.set('zuul', 'layout_config',
2863 'tests/fixtures/layout-tags.yaml')
2864 self.sched.reconfigure(self.config)
2865
2866 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2867 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2868 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2869 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2870 self.waitUntilSettled()
2871
2872 results = {'project1-merge': 'extratag merge project1',
2873 'project2-merge': 'merge'}
2874
2875 for build in self.history:
2876 self.assertEqual(results.get(build.name, ''),
2877 build.parameters.get('BUILD_TAGS'))
2878
James E. Blairec056492016-07-22 09:45:56 -07002879 @skip("Disabled for early v3 development")
James E. Blair63bb0ef2013-07-29 17:14:51 -07002880 def test_timer(self):
2881 "Test that a periodic job is triggered"
James E. Blair08d19992016-08-10 15:25:31 -07002882 self.launch_server.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08002883 self.updateConfigLayout(
2884 'tests/fixtures/layout-timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002885 self.sched.reconfigure(self.config)
2886 self.registerJobs()
2887
Clark Boylan3ee090a2014-04-03 20:55:09 -07002888 # The pipeline triggers every second, so we should have seen
2889 # several by now.
2890 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002891 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002892
2893 self.assertEqual(len(self.builds), 2)
2894
James E. Blair63bb0ef2013-07-29 17:14:51 -07002895 port = self.webapp.server.socket.getsockname()[1]
2896
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002897 req = urllib.request.Request("http://localhost:%s/status.json" % port)
2898 f = urllib.request.urlopen(req)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002899 data = f.read()
2900
James E. Blair08d19992016-08-10 15:25:31 -07002901 self.launch_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002902 # Stop queuing timer triggered jobs so that the assertions
2903 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002904 self.updateConfigLayout(
2905 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002906 self.sched.reconfigure(self.config)
2907 self.registerJobs()
James E. Blair08d19992016-08-10 15:25:31 -07002908 self.launch_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002909 self.waitUntilSettled()
2910
2911 self.assertEqual(self.getJobFromHistory(
2912 'project-bitrot-stable-old').result, 'SUCCESS')
2913 self.assertEqual(self.getJobFromHistory(
2914 'project-bitrot-stable-older').result, 'SUCCESS')
2915
2916 data = json.loads(data)
2917 status_jobs = set()
2918 for p in data['pipelines']:
2919 for q in p['change_queues']:
2920 for head in q['heads']:
2921 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07002922 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002923 for job in change['jobs']:
2924 status_jobs.add(job['name'])
2925 self.assertIn('project-bitrot-stable-old', status_jobs)
2926 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002927
James E. Blairec056492016-07-22 09:45:56 -07002928 @skip("Disabled for early v3 development")
James E. Blair4f6033c2014-03-27 15:49:09 -07002929 def test_idle(self):
2930 "Test that frequent periodic jobs work"
James E. Blair08d19992016-08-10 15:25:31 -07002931 self.launch_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002932
Clark Boylan3ee090a2014-04-03 20:55:09 -07002933 for x in range(1, 3):
2934 # Test that timer triggers periodic jobs even across
2935 # layout config reloads.
2936 # Start timer trigger
James E. Blairf84026c2015-12-08 16:11:46 -08002937 self.updateConfigLayout(
2938 'tests/fixtures/layout-idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002939 self.sched.reconfigure(self.config)
2940 self.registerJobs()
James E. Blair995fc0f2016-02-04 16:48:31 -08002941 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002942
Clark Boylan3ee090a2014-04-03 20:55:09 -07002943 # The pipeline triggers every second, so we should have seen
2944 # several by now.
2945 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002946
2947 # Stop queuing timer triggered jobs so that the assertions
2948 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002949 self.updateConfigLayout(
2950 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002951 self.sched.reconfigure(self.config)
2952 self.registerJobs()
James E. Blair995fc0f2016-02-04 16:48:31 -08002953 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002954
2955 self.assertEqual(len(self.builds), 2)
James E. Blair08d19992016-08-10 15:25:31 -07002956 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002957 self.waitUntilSettled()
2958 self.assertEqual(len(self.builds), 0)
2959 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002960
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002961 def test_check_smtp_pool(self):
Paul Belanger01be8ca2016-11-11 12:21:06 -05002962 self.updateConfigLayout('layout-smtp')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002963 self.sched.reconfigure(self.config)
2964
2965 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2966 self.waitUntilSettled()
2967
2968 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2969 self.waitUntilSettled()
2970
James E. Blairff80a2f2013-12-27 13:24:06 -08002971 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002972
2973 # A.messages only holds what FakeGerrit places in it. Thus we
2974 # work on the knowledge of what the first message should be as
2975 # it is only configured to go to SMTP.
2976
2977 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002978 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002979 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002980 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002981 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002982 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002983
2984 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002985 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002986 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002987 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002988 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002989 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002990
James E. Blairec056492016-07-22 09:45:56 -07002991 @skip("Disabled for early v3 development")
James E. Blaire5910202013-12-27 09:50:31 -08002992 def test_timer_smtp(self):
2993 "Test that a periodic job is triggered"
James E. Blair08d19992016-08-10 15:25:31 -07002994 self.launch_server.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08002995 self.updateConfigLayout(
2996 'tests/fixtures/layout-timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002997 self.sched.reconfigure(self.config)
2998 self.registerJobs()
2999
Clark Boylan3ee090a2014-04-03 20:55:09 -07003000 # The pipeline triggers every second, so we should have seen
3001 # several by now.
3002 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003003 self.waitUntilSettled()
3004
Clark Boylan3ee090a2014-04-03 20:55:09 -07003005 self.assertEqual(len(self.builds), 2)
James E. Blair08d19992016-08-10 15:25:31 -07003006 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003007 self.waitUntilSettled()
3008 self.assertEqual(len(self.history), 2)
3009
James E. Blaire5910202013-12-27 09:50:31 -08003010 self.assertEqual(self.getJobFromHistory(
3011 'project-bitrot-stable-old').result, 'SUCCESS')
3012 self.assertEqual(self.getJobFromHistory(
3013 'project-bitrot-stable-older').result, 'SUCCESS')
3014
James E. Blairff80a2f2013-12-27 13:24:06 -08003015 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003016
3017 # A.messages only holds what FakeGerrit places in it. Thus we
3018 # work on the knowledge of what the first message should be as
3019 # it is only configured to go to SMTP.
3020
3021 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003022 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003023 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003024 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003025 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003026 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003027
Clark Boylan3ee090a2014-04-03 20:55:09 -07003028 # Stop queuing timer triggered jobs and let any that may have
3029 # queued through so that end of test assertions pass.
James E. Blairf84026c2015-12-08 16:11:46 -08003030 self.updateConfigLayout(
3031 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003032 self.sched.reconfigure(self.config)
3033 self.registerJobs()
James E. Blairf8058972014-08-15 16:09:16 -07003034 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003035 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003036 self.waitUntilSettled()
3037
James E. Blair91e34592015-07-31 16:45:59 -07003038 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003039 "Test that the RPC client can enqueue a change"
3040 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003041 A.addApproval('code-review', 2)
3042 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003043
3044 client = zuul.rpcclient.RPCClient('127.0.0.1',
3045 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003046 r = client.enqueue(tenant='tenant-one',
3047 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003048 project='org/project',
3049 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003050 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003051 self.waitUntilSettled()
3052 self.assertEqual(self.getJobFromHistory('project-merge').result,
3053 'SUCCESS')
3054 self.assertEqual(self.getJobFromHistory('project-test1').result,
3055 'SUCCESS')
3056 self.assertEqual(self.getJobFromHistory('project-test2').result,
3057 'SUCCESS')
3058 self.assertEqual(A.data['status'], 'MERGED')
3059 self.assertEqual(A.reported, 2)
3060 self.assertEqual(r, True)
3061
James E. Blairec056492016-07-22 09:45:56 -07003062 @skip("Disabled for early v3 development")
James E. Blair91e34592015-07-31 16:45:59 -07003063 def test_client_enqueue_ref(self):
3064 "Test that the RPC client can enqueue a ref"
3065
3066 client = zuul.rpcclient.RPCClient('127.0.0.1',
3067 self.gearman_server.port)
3068 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003069 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003070 pipeline='post',
3071 project='org/project',
3072 trigger='gerrit',
3073 ref='master',
3074 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
3075 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
3076 self.waitUntilSettled()
3077 job_names = [x.name for x in self.history]
3078 self.assertEqual(len(self.history), 1)
3079 self.assertIn('project-post', job_names)
3080 self.assertEqual(r, True)
3081
James E. Blairad28e912013-11-27 10:43:22 -08003082 def test_client_enqueue_negative(self):
3083 "Test that the RPC client returns errors"
3084 client = zuul.rpcclient.RPCClient('127.0.0.1',
3085 self.gearman_server.port)
3086 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003087 "Invalid tenant"):
3088 r = client.enqueue(tenant='tenant-foo',
3089 pipeline='gate',
3090 project='org/project',
3091 trigger='gerrit',
3092 change='1,1')
3093 client.shutdown()
3094 self.assertEqual(r, False)
3095
3096 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003097 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003098 r = client.enqueue(tenant='tenant-one',
3099 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003100 project='project-does-not-exist',
3101 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003102 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003103 client.shutdown()
3104 self.assertEqual(r, False)
3105
3106 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3107 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003108 r = client.enqueue(tenant='tenant-one',
3109 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003110 project='org/project',
3111 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003112 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003113 client.shutdown()
3114 self.assertEqual(r, False)
3115
3116 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3117 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003118 r = client.enqueue(tenant='tenant-one',
3119 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003120 project='org/project',
3121 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003122 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003123 client.shutdown()
3124 self.assertEqual(r, False)
3125
3126 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3127 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003128 r = client.enqueue(tenant='tenant-one',
3129 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003130 project='org/project',
3131 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003132 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003133 client.shutdown()
3134 self.assertEqual(r, False)
3135
3136 self.waitUntilSettled()
3137 self.assertEqual(len(self.history), 0)
3138 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003139
3140 def test_client_promote(self):
3141 "Test that the RPC client can promote a change"
James E. Blair08d19992016-08-10 15:25:31 -07003142 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003143 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3144 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3145 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003146 A.addApproval('code-review', 2)
3147 B.addApproval('code-review', 2)
3148 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003149
James E. Blair8b5408c2016-08-08 15:37:46 -07003150 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3151 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3152 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003153
3154 self.waitUntilSettled()
3155
Paul Belangerbaca3132016-11-04 12:49:54 -04003156 tenant = self.sched.abide.tenants.get('tenant-one')
3157 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003158 enqueue_times = {}
3159 for item in items:
3160 enqueue_times[str(item.change)] = item.enqueue_time
3161
James E. Blair36658cf2013-12-06 17:53:48 -08003162 client = zuul.rpcclient.RPCClient('127.0.0.1',
3163 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003164 r = client.promote(tenant='tenant-one',
3165 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003166 change_ids=['2,1', '3,1'])
3167
Sean Daguef39b9ca2014-01-10 21:34:35 -05003168 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003169 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003170 for item in items:
3171 self.assertEqual(
3172 enqueue_times[str(item.change)], item.enqueue_time)
3173
James E. Blair78acec92014-02-06 07:11:32 -08003174 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003175 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003176 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003177 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003178 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003179 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003180 self.waitUntilSettled()
3181
3182 self.assertEqual(len(self.builds), 6)
3183 self.assertEqual(self.builds[0].name, 'project-test1')
3184 self.assertEqual(self.builds[1].name, 'project-test2')
3185 self.assertEqual(self.builds[2].name, 'project-test1')
3186 self.assertEqual(self.builds[3].name, 'project-test2')
3187 self.assertEqual(self.builds[4].name, 'project-test1')
3188 self.assertEqual(self.builds[5].name, 'project-test2')
3189
Paul Belangerbaca3132016-11-04 12:49:54 -04003190 self.assertTrue(self.builds[0].hasChanges(B))
3191 self.assertFalse(self.builds[0].hasChanges(A))
3192 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003193
Paul Belangerbaca3132016-11-04 12:49:54 -04003194 self.assertTrue(self.builds[2].hasChanges(B))
3195 self.assertTrue(self.builds[2].hasChanges(C))
3196 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003197
Paul Belangerbaca3132016-11-04 12:49:54 -04003198 self.assertTrue(self.builds[4].hasChanges(B))
3199 self.assertTrue(self.builds[4].hasChanges(C))
3200 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003201
James E. Blair08d19992016-08-10 15:25:31 -07003202 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003203 self.waitUntilSettled()
3204
3205 self.assertEqual(A.data['status'], 'MERGED')
3206 self.assertEqual(A.reported, 2)
3207 self.assertEqual(B.data['status'], 'MERGED')
3208 self.assertEqual(B.reported, 2)
3209 self.assertEqual(C.data['status'], 'MERGED')
3210 self.assertEqual(C.reported, 2)
3211
3212 client.shutdown()
3213 self.assertEqual(r, True)
3214
3215 def test_client_promote_dependent(self):
3216 "Test that the RPC client can promote a dependent change"
3217 # C (depends on B) -> B -> A ; then promote C to get:
3218 # A -> C (depends on B) -> B
James E. Blair08d19992016-08-10 15:25:31 -07003219 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003220 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3221 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3222 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3223
3224 C.setDependsOn(B, 1)
3225
James E. Blair8b5408c2016-08-08 15:37:46 -07003226 A.addApproval('code-review', 2)
3227 B.addApproval('code-review', 2)
3228 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003229
James E. Blair8b5408c2016-08-08 15:37:46 -07003230 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3231 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3232 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003233
3234 self.waitUntilSettled()
3235
3236 client = zuul.rpcclient.RPCClient('127.0.0.1',
3237 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003238 r = client.promote(tenant='tenant-one',
3239 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003240 change_ids=['3,1'])
3241
James E. Blair78acec92014-02-06 07:11:32 -08003242 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003243 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003244 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003245 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003246 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003247 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003248 self.waitUntilSettled()
3249
3250 self.assertEqual(len(self.builds), 6)
3251 self.assertEqual(self.builds[0].name, 'project-test1')
3252 self.assertEqual(self.builds[1].name, 'project-test2')
3253 self.assertEqual(self.builds[2].name, 'project-test1')
3254 self.assertEqual(self.builds[3].name, 'project-test2')
3255 self.assertEqual(self.builds[4].name, 'project-test1')
3256 self.assertEqual(self.builds[5].name, 'project-test2')
3257
Paul Belangerbaca3132016-11-04 12:49:54 -04003258 self.assertTrue(self.builds[0].hasChanges(B))
3259 self.assertFalse(self.builds[0].hasChanges(A))
3260 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003261
Paul Belangerbaca3132016-11-04 12:49:54 -04003262 self.assertTrue(self.builds[2].hasChanges(B))
3263 self.assertTrue(self.builds[2].hasChanges(C))
3264 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003265
Paul Belangerbaca3132016-11-04 12:49:54 -04003266 self.assertTrue(self.builds[4].hasChanges(B))
3267 self.assertTrue(self.builds[4].hasChanges(C))
3268 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003269
James E. Blair08d19992016-08-10 15:25:31 -07003270 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003271 self.waitUntilSettled()
3272
3273 self.assertEqual(A.data['status'], 'MERGED')
3274 self.assertEqual(A.reported, 2)
3275 self.assertEqual(B.data['status'], 'MERGED')
3276 self.assertEqual(B.reported, 2)
3277 self.assertEqual(C.data['status'], 'MERGED')
3278 self.assertEqual(C.reported, 2)
3279
3280 client.shutdown()
3281 self.assertEqual(r, True)
3282
3283 def test_client_promote_negative(self):
3284 "Test that the RPC client returns errors for promotion"
James E. Blair08d19992016-08-10 15:25:31 -07003285 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003286 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003287 A.addApproval('code-review', 2)
3288 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003289 self.waitUntilSettled()
3290
3291 client = zuul.rpcclient.RPCClient('127.0.0.1',
3292 self.gearman_server.port)
3293
3294 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003295 r = client.promote(tenant='tenant-one',
3296 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003297 change_ids=['2,1', '3,1'])
3298 client.shutdown()
3299 self.assertEqual(r, False)
3300
3301 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003302 r = client.promote(tenant='tenant-one',
3303 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003304 change_ids=['4,1'])
3305 client.shutdown()
3306 self.assertEqual(r, False)
3307
James E. Blair08d19992016-08-10 15:25:31 -07003308 self.launch_server.hold_jobs_in_build = False
3309 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003310 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003311
James E. Blairec056492016-07-22 09:45:56 -07003312 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003313 def test_queue_rate_limiting(self):
3314 "Test that DependentPipelines are rate limited with dep across window"
James E. Blairf84026c2015-12-08 16:11:46 -08003315 self.updateConfigLayout(
3316 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003317 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07003318 self.launch_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003319 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3320 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3321 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3322
3323 C.setDependsOn(B, 1)
James E. Blair08d19992016-08-10 15:25:31 -07003324 self.launch_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003325
James E. Blair8b5408c2016-08-08 15:37:46 -07003326 A.addApproval('code-review', 2)
3327 B.addApproval('code-review', 2)
3328 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003329
James E. Blair8b5408c2016-08-08 15:37:46 -07003330 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3331 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3332 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003333 self.waitUntilSettled()
3334
3335 # Only A and B will have their merge jobs queued because
3336 # window is 2.
3337 self.assertEqual(len(self.builds), 2)
3338 self.assertEqual(self.builds[0].name, 'project-merge')
3339 self.assertEqual(self.builds[1].name, 'project-merge')
3340
James E. Blair08d19992016-08-10 15:25:31 -07003341 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003342 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003343 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003344 self.waitUntilSettled()
3345
3346 # Only A and B will have their test jobs queued because
3347 # window is 2.
3348 self.assertEqual(len(self.builds), 4)
3349 self.assertEqual(self.builds[0].name, 'project-test1')
3350 self.assertEqual(self.builds[1].name, 'project-test2')
3351 self.assertEqual(self.builds[2].name, 'project-test1')
3352 self.assertEqual(self.builds[3].name, 'project-test2')
3353
James E. Blair08d19992016-08-10 15:25:31 -07003354 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003355 self.waitUntilSettled()
3356
3357 queue = self.sched.layout.pipelines['gate'].queues[0]
3358 # A failed so window is reduced by 1 to 1.
3359 self.assertEqual(queue.window, 1)
3360 self.assertEqual(queue.window_floor, 1)
3361 self.assertEqual(A.data['status'], 'NEW')
3362
3363 # Gate is reset and only B's merge job is queued because
3364 # window shrunk to 1.
3365 self.assertEqual(len(self.builds), 1)
3366 self.assertEqual(self.builds[0].name, 'project-merge')
3367
James E. Blair08d19992016-08-10 15:25:31 -07003368 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003369 self.waitUntilSettled()
3370
3371 # Only B's test jobs are queued because window is still 1.
3372 self.assertEqual(len(self.builds), 2)
3373 self.assertEqual(self.builds[0].name, 'project-test1')
3374 self.assertEqual(self.builds[1].name, 'project-test2')
3375
James E. Blair08d19992016-08-10 15:25:31 -07003376 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003377 self.waitUntilSettled()
3378
3379 # B was successfully merged so window is increased to 2.
3380 self.assertEqual(queue.window, 2)
3381 self.assertEqual(queue.window_floor, 1)
3382 self.assertEqual(B.data['status'], 'MERGED')
3383
3384 # Only C is left and its merge job is queued.
3385 self.assertEqual(len(self.builds), 1)
3386 self.assertEqual(self.builds[0].name, 'project-merge')
3387
James E. Blair08d19992016-08-10 15:25:31 -07003388 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003389 self.waitUntilSettled()
3390
3391 # After successful merge job the test jobs for C are queued.
3392 self.assertEqual(len(self.builds), 2)
3393 self.assertEqual(self.builds[0].name, 'project-test1')
3394 self.assertEqual(self.builds[1].name, 'project-test2')
3395
James E. Blair08d19992016-08-10 15:25:31 -07003396 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003397 self.waitUntilSettled()
3398
3399 # C successfully merged so window is bumped to 3.
3400 self.assertEqual(queue.window, 3)
3401 self.assertEqual(queue.window_floor, 1)
3402 self.assertEqual(C.data['status'], 'MERGED')
3403
James E. Blairec056492016-07-22 09:45:56 -07003404 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003405 def test_queue_rate_limiting_dependent(self):
3406 "Test that DependentPipelines are rate limited with dep in window"
James E. Blairf84026c2015-12-08 16:11:46 -08003407 self.updateConfigLayout(
3408 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003409 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07003410 self.launch_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003411 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3412 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3413 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3414
3415 B.setDependsOn(A, 1)
3416
James E. Blair08d19992016-08-10 15:25:31 -07003417 self.launch_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003418
James E. Blair8b5408c2016-08-08 15:37:46 -07003419 A.addApproval('code-review', 2)
3420 B.addApproval('code-review', 2)
3421 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003422
James E. Blair8b5408c2016-08-08 15:37:46 -07003423 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3424 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3425 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003426 self.waitUntilSettled()
3427
3428 # Only A and B will have their merge jobs queued because
3429 # window is 2.
3430 self.assertEqual(len(self.builds), 2)
3431 self.assertEqual(self.builds[0].name, 'project-merge')
3432 self.assertEqual(self.builds[1].name, 'project-merge')
3433
James E. Blair08d19992016-08-10 15:25:31 -07003434 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003435 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003436 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003437 self.waitUntilSettled()
3438
3439 # Only A and B will have their test jobs queued because
3440 # window is 2.
3441 self.assertEqual(len(self.builds), 4)
3442 self.assertEqual(self.builds[0].name, 'project-test1')
3443 self.assertEqual(self.builds[1].name, 'project-test2')
3444 self.assertEqual(self.builds[2].name, 'project-test1')
3445 self.assertEqual(self.builds[3].name, 'project-test2')
3446
James E. Blair08d19992016-08-10 15:25:31 -07003447 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003448 self.waitUntilSettled()
3449
3450 queue = self.sched.layout.pipelines['gate'].queues[0]
3451 # A failed so window is reduced by 1 to 1.
3452 self.assertEqual(queue.window, 1)
3453 self.assertEqual(queue.window_floor, 1)
3454 self.assertEqual(A.data['status'], 'NEW')
3455 self.assertEqual(B.data['status'], 'NEW')
3456
3457 # Gate is reset and only C's merge job is queued because
3458 # window shrunk to 1 and A and B were dequeued.
3459 self.assertEqual(len(self.builds), 1)
3460 self.assertEqual(self.builds[0].name, 'project-merge')
3461
James E. Blair08d19992016-08-10 15:25:31 -07003462 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003463 self.waitUntilSettled()
3464
3465 # Only C's test jobs are queued because window is still 1.
3466 self.assertEqual(len(self.builds), 2)
3467 self.assertEqual(self.builds[0].name, 'project-test1')
3468 self.assertEqual(self.builds[1].name, 'project-test2')
3469
James E. Blair08d19992016-08-10 15:25:31 -07003470 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003471 self.waitUntilSettled()
3472
3473 # C was successfully merged so window is increased to 2.
3474 self.assertEqual(queue.window, 2)
3475 self.assertEqual(queue.window_floor, 1)
3476 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003477
James E. Blairec056492016-07-22 09:45:56 -07003478 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003479 def test_worker_update_metadata(self):
3480 "Test if a worker can send back metadata about itself"
James E. Blair08d19992016-08-10 15:25:31 -07003481 self.launch_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003482
3483 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003484 A.addApproval('code-review', 2)
3485 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003486 self.waitUntilSettled()
3487
3488 self.assertEqual(len(self.launcher.builds), 1)
3489
3490 self.log.debug('Current builds:')
3491 self.log.debug(self.launcher.builds)
3492
3493 start = time.time()
3494 while True:
3495 if time.time() - start > 10:
3496 raise Exception("Timeout waiting for gearman server to report "
3497 + "back to the client")
3498 build = self.launcher.builds.values()[0]
3499 if build.worker.name == "My Worker":
3500 break
3501 else:
3502 time.sleep(0)
3503
3504 self.log.debug(build)
3505 self.assertEqual("My Worker", build.worker.name)
3506 self.assertEqual("localhost", build.worker.hostname)
3507 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3508 self.assertEqual("zuul.example.org", build.worker.fqdn)
3509 self.assertEqual("FakeBuilder", build.worker.program)
3510 self.assertEqual("v1.1", build.worker.version)
3511 self.assertEqual({'something': 'else'}, build.worker.extra)
3512
James E. Blair08d19992016-08-10 15:25:31 -07003513 self.launch_server.hold_jobs_in_build = False
3514 self.launch_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003515 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003516
James E. Blairec056492016-07-22 09:45:56 -07003517 @skip("Disabled for early v3 development")
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003518 def test_footer_message(self):
3519 "Test a pipeline's footer message is correctly added to the report."
James E. Blairf84026c2015-12-08 16:11:46 -08003520 self.updateConfigLayout(
3521 'tests/fixtures/layout-footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003522 self.sched.reconfigure(self.config)
3523 self.registerJobs()
3524
3525 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003526 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07003527 self.launch_server.failJob('test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003528 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003529 self.waitUntilSettled()
3530
3531 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003532 B.addApproval('code-review', 2)
3533 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003534 self.waitUntilSettled()
3535
3536 self.assertEqual(2, len(self.smtp_messages))
3537
3538 failure_body = """\
3539Build failed. For information on how to proceed, see \
3540http://wiki.example.org/Test_Failures
3541
3542- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
3543- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
3544
3545For CI problems and help debugging, contact ci@example.org"""
3546
3547 success_body = """\
3548Build succeeded.
3549
3550- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
3551- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
3552
3553For CI problems and help debugging, contact ci@example.org"""
3554
3555 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
3556 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11003557
James E. Blairec056492016-07-22 09:45:56 -07003558 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003559 def test_merge_failure_reporters(self):
3560 """Check that the config is set up correctly"""
3561
James E. Blairf84026c2015-12-08 16:11:46 -08003562 self.updateConfigLayout(
3563 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003564 self.sched.reconfigure(self.config)
3565 self.registerJobs()
3566
3567 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003568 "Merge Failed.\n\nThis change or one of its cross-repo "
3569 "dependencies was unable to be automatically merged with the "
3570 "current state of its repository. Please rebase the change and "
3571 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003572 self.sched.layout.pipelines['check'].merge_failure_message)
3573 self.assertEqual(
3574 "The merge failed! For more information...",
3575 self.sched.layout.pipelines['gate'].merge_failure_message)
3576
3577 self.assertEqual(
3578 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3579 self.assertEqual(
3580 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3581
3582 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003583 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3584 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003585
3586 self.assertTrue(
3587 (
3588 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003589 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003590 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003591 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003592 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003593 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003594 ) or (
3595 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003596 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003597 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003598 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003599 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003600 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003601 )
3602 )
3603
James E. Blairec056492016-07-22 09:45:56 -07003604 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003605 def test_merge_failure_reports(self):
3606 """Check that when a change fails to merge the correct message is sent
3607 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003608 self.updateConfigLayout(
3609 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003610 self.sched.reconfigure(self.config)
3611 self.registerJobs()
3612
3613 # Check a test failure isn't reported to SMTP
3614 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003615 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07003616 self.launch_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003617 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003618 self.waitUntilSettled()
3619
3620 self.assertEqual(3, len(self.history)) # 3 jobs
3621 self.assertEqual(0, len(self.smtp_messages))
3622
3623 # Check a merge failure is reported to SMTP
3624 # B should be merged, but C will conflict with B
3625 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3626 B.addPatchset(['conflict'])
3627 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3628 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003629 B.addApproval('code-review', 2)
3630 C.addApproval('code-review', 2)
3631 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3632 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003633 self.waitUntilSettled()
3634
3635 self.assertEqual(6, len(self.history)) # A and B jobs
3636 self.assertEqual(1, len(self.smtp_messages))
3637 self.assertEqual('The merge failed! For more information...',
3638 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003639
James E. Blairec056492016-07-22 09:45:56 -07003640 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003641 def test_default_merge_failure_reports(self):
3642 """Check that the default merge failure reports are correct."""
3643
3644 # A should report success, B should report merge failure.
3645 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3646 A.addPatchset(['conflict'])
3647 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3648 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003649 A.addApproval('code-review', 2)
3650 B.addApproval('code-review', 2)
3651 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3652 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003653 self.waitUntilSettled()
3654
3655 self.assertEqual(3, len(self.history)) # A jobs
3656 self.assertEqual(A.reported, 2)
3657 self.assertEqual(B.reported, 2)
3658 self.assertEqual(A.data['status'], 'MERGED')
3659 self.assertEqual(B.data['status'], 'NEW')
3660 self.assertIn('Build succeeded', A.messages[1])
3661 self.assertIn('Merge Failed', B.messages[1])
3662 self.assertIn('automatically merged', B.messages[1])
3663 self.assertNotIn('logs.example.com', B.messages[1])
3664 self.assertNotIn('SKIPPED', B.messages[1])
3665
James E. Blairec056492016-07-22 09:45:56 -07003666 @skip("Disabled for early v3 development")
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003667 def test_swift_instructions(self):
3668 "Test that the correct swift instructions are sent to the workers"
James E. Blairf84026c2015-12-08 16:11:46 -08003669 self.updateConfigLayout(
3670 'tests/fixtures/layout-swift.yaml')
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003671 self.sched.reconfigure(self.config)
3672 self.registerJobs()
3673
James E. Blair08d19992016-08-10 15:25:31 -07003674 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003675 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3676
James E. Blair8b5408c2016-08-08 15:37:46 -07003677 A.addApproval('code-review', 2)
3678 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003679 self.waitUntilSettled()
3680
3681 self.assertEqual(
3682 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
3683 "gate/test-merge/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003684 self.builds[0].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003685 self.assertEqual(5,
3686 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
3687 split('\n')))
3688 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
3689
3690 self.assertEqual(
3691 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
3692 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003693 self.builds[1].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003694 self.assertEqual(5,
3695 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
3696 split('\n')))
3697 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
3698
3699 self.assertEqual(
3700 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
3701 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003702 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003703 self.assertEqual(5,
3704 len(self.builds[1].
3705 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
3706 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
3707
James E. Blair08d19992016-08-10 15:25:31 -07003708 self.launch_server.hold_jobs_in_build = False
3709 self.launch_server.release()
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003710 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003711
3712 def test_client_get_running_jobs(self):
3713 "Test that the RPC client can get a list of running jobs"
James E. Blair08d19992016-08-10 15:25:31 -07003714 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003715 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003716 A.addApproval('code-review', 2)
3717 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003718 self.waitUntilSettled()
3719
3720 client = zuul.rpcclient.RPCClient('127.0.0.1',
3721 self.gearman_server.port)
3722
3723 # Wait for gearman server to send the initial workData back to zuul
3724 start = time.time()
3725 while True:
3726 if time.time() - start > 10:
3727 raise Exception("Timeout waiting for gearman server to report "
3728 + "back to the client")
Clint Byrum78a69772016-12-05 10:52:07 -08003729 build = self.launch_client.builds.values()[0]
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003730 if build.worker.name == "My Worker":
3731 break
3732 else:
3733 time.sleep(0)
3734
3735 running_items = client.get_running_jobs()
3736
3737 self.assertEqual(1, len(running_items))
3738 running_item = running_items[0]
3739 self.assertEqual([], running_item['failing_reasons'])
3740 self.assertEqual([], running_item['items_behind'])
3741 self.assertEqual('https://hostname/1', running_item['url'])
3742 self.assertEqual(None, running_item['item_ahead'])
3743 self.assertEqual('org/project', running_item['project'])
3744 self.assertEqual(None, running_item['remaining_time'])
3745 self.assertEqual(True, running_item['active'])
3746 self.assertEqual('1,1', running_item['id'])
3747
3748 self.assertEqual(3, len(running_item['jobs']))
3749 for job in running_item['jobs']:
3750 if job['name'] == 'project-merge':
3751 self.assertEqual('project-merge', job['name'])
3752 self.assertEqual('gate', job['pipeline'])
3753 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003754 self.assertEqual('https://server/job/project-merge/0/',
3755 job['url'])
3756 self.assertEqual(7, len(job['worker']))
3757 self.assertEqual(False, job['canceled'])
3758 self.assertEqual(True, job['voting'])
3759 self.assertEqual(None, job['result'])
3760 self.assertEqual('gate', job['pipeline'])
3761 break
3762
James E. Blair08d19992016-08-10 15:25:31 -07003763 self.launch_server.hold_jobs_in_build = False
3764 self.launch_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003765 self.waitUntilSettled()
3766
3767 running_items = client.get_running_jobs()
3768 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003769
3770 def test_nonvoting_pipeline(self):
3771 "Test that a nonvoting pipeline (experimental) can still report"
3772
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003773 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3774 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003775 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3776 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003777
3778 self.assertEqual(self.getJobFromHistory('project-merge').result,
3779 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003780 self.assertEqual(
3781 self.getJobFromHistory('experimental-project-test').result,
3782 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003783 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003784
3785 def test_crd_gate(self):
3786 "Test cross-repo dependencies"
3787 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3788 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003789 A.addApproval('code-review', 2)
3790 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003791
3792 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3793 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3794 AM2.setMerged()
3795 AM1.setMerged()
3796
3797 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3798 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3799 BM2.setMerged()
3800 BM1.setMerged()
3801
3802 # A -> AM1 -> AM2
3803 # B -> BM1 -> BM2
3804 # A Depends-On: B
3805 # M2 is here to make sure it is never queried. If it is, it
3806 # means zuul is walking down the entire history of merged
3807 # changes.
3808
3809 B.setDependsOn(BM1, 1)
3810 BM1.setDependsOn(BM2, 1)
3811
3812 A.setDependsOn(AM1, 1)
3813 AM1.setDependsOn(AM2, 1)
3814
3815 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3816 A.subject, B.data['id'])
3817
James E. Blair8b5408c2016-08-08 15:37:46 -07003818 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003819 self.waitUntilSettled()
3820
3821 self.assertEqual(A.data['status'], 'NEW')
3822 self.assertEqual(B.data['status'], 'NEW')
3823
Paul Belanger5dccbe72016-11-14 11:17:37 -05003824 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003825 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003826
James E. Blair08d19992016-08-10 15:25:31 -07003827 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003828 B.addApproval('approved', 1)
3829 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003830 self.waitUntilSettled()
3831
James E. Blair08d19992016-08-10 15:25:31 -07003832 self.launch_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003833 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003834 self.launch_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003835 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003836 self.launch_server.hold_jobs_in_build = False
3837 self.launch_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003838 self.waitUntilSettled()
3839
3840 self.assertEqual(AM2.queried, 0)
3841 self.assertEqual(BM2.queried, 0)
3842 self.assertEqual(A.data['status'], 'MERGED')
3843 self.assertEqual(B.data['status'], 'MERGED')
3844 self.assertEqual(A.reported, 2)
3845 self.assertEqual(B.reported, 2)
3846
Paul Belanger5dccbe72016-11-14 11:17:37 -05003847 changes = self.getJobFromHistory(
3848 'project-merge', 'org/project1').changes
3849 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003850
3851 def test_crd_branch(self):
3852 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003853
3854 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003855 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3856 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3857 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3858 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003859 A.addApproval('code-review', 2)
3860 B.addApproval('code-review', 2)
3861 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003862
3863 # A Depends-On: B+C
3864 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3865 A.subject, B.data['id'])
3866
James E. Blair08d19992016-08-10 15:25:31 -07003867 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003868 B.addApproval('approved', 1)
3869 C.addApproval('approved', 1)
3870 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003871 self.waitUntilSettled()
3872
James E. Blair08d19992016-08-10 15:25:31 -07003873 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003874 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003875 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003876 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003877 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003878 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003879 self.launch_server.hold_jobs_in_build = False
3880 self.launch_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003881 self.waitUntilSettled()
3882
3883 self.assertEqual(A.data['status'], 'MERGED')
3884 self.assertEqual(B.data['status'], 'MERGED')
3885 self.assertEqual(C.data['status'], 'MERGED')
3886 self.assertEqual(A.reported, 2)
3887 self.assertEqual(B.reported, 2)
3888 self.assertEqual(C.reported, 2)
3889
Paul Belanger6379db12016-11-14 13:57:54 -05003890 changes = self.getJobFromHistory(
3891 'project-merge', 'org/project1').changes
3892 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003893
3894 def test_crd_multiline(self):
3895 "Test multiple depends-on lines in commit"
3896 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3897 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3898 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003899 A.addApproval('code-review', 2)
3900 B.addApproval('code-review', 2)
3901 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003902
3903 # A Depends-On: B+C
3904 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3905 A.subject, B.data['id'], C.data['id'])
3906
James E. Blair08d19992016-08-10 15:25:31 -07003907 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003908 B.addApproval('approved', 1)
3909 C.addApproval('approved', 1)
3910 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003911 self.waitUntilSettled()
3912
James E. Blair08d19992016-08-10 15:25:31 -07003913 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003914 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003915 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003916 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003917 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003918 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003919 self.launch_server.hold_jobs_in_build = False
3920 self.launch_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003921 self.waitUntilSettled()
3922
3923 self.assertEqual(A.data['status'], 'MERGED')
3924 self.assertEqual(B.data['status'], 'MERGED')
3925 self.assertEqual(C.data['status'], 'MERGED')
3926 self.assertEqual(A.reported, 2)
3927 self.assertEqual(B.reported, 2)
3928 self.assertEqual(C.reported, 2)
3929
Paul Belanger5dccbe72016-11-14 11:17:37 -05003930 changes = self.getJobFromHistory(
3931 'project-merge', 'org/project1').changes
3932 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003933
3934 def test_crd_unshared_gate(self):
3935 "Test cross-repo dependencies in unshared gate queues"
3936 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3937 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003938 A.addApproval('code-review', 2)
3939 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003940
3941 # A Depends-On: B
3942 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3943 A.subject, B.data['id'])
3944
3945 # A and B do not share a queue, make sure that A is unable to
3946 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003947 B.addApproval('approved', 1)
3948 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003949 self.waitUntilSettled()
3950
3951 self.assertEqual(A.data['status'], 'NEW')
3952 self.assertEqual(B.data['status'], 'NEW')
3953 self.assertEqual(A.reported, 0)
3954 self.assertEqual(B.reported, 0)
3955 self.assertEqual(len(self.history), 0)
3956
3957 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003958 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003959 self.waitUntilSettled()
3960
3961 self.assertEqual(B.data['status'], 'MERGED')
3962 self.assertEqual(B.reported, 2)
3963
3964 # Now that B is merged, A should be able to be enqueued and
3965 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003966 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003967 self.waitUntilSettled()
3968
3969 self.assertEqual(A.data['status'], 'MERGED')
3970 self.assertEqual(A.reported, 2)
3971
James E. Blair96698e22015-04-02 07:48:21 -07003972 def test_crd_gate_reverse(self):
3973 "Test reverse cross-repo dependencies"
3974 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3975 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003976 A.addApproval('code-review', 2)
3977 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003978
3979 # A Depends-On: B
3980
3981 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3982 A.subject, B.data['id'])
3983
James E. Blair8b5408c2016-08-08 15:37:46 -07003984 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003985 self.waitUntilSettled()
3986
3987 self.assertEqual(A.data['status'], 'NEW')
3988 self.assertEqual(B.data['status'], 'NEW')
3989
James E. Blair08d19992016-08-10 15:25:31 -07003990 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003991 A.addApproval('approved', 1)
3992 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003993 self.waitUntilSettled()
3994
James E. Blair08d19992016-08-10 15:25:31 -07003995 self.launch_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003996 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003997 self.launch_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003998 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003999 self.launch_server.hold_jobs_in_build = False
4000 self.launch_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07004001 self.waitUntilSettled()
4002
4003 self.assertEqual(A.data['status'], 'MERGED')
4004 self.assertEqual(B.data['status'], 'MERGED')
4005 self.assertEqual(A.reported, 2)
4006 self.assertEqual(B.reported, 2)
4007
Paul Belanger5dccbe72016-11-14 11:17:37 -05004008 changes = self.getJobFromHistory(
4009 'project-merge', 'org/project1').changes
4010 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07004011
James E. Blair5ee24252014-12-30 10:12:29 -08004012 def test_crd_cycle(self):
4013 "Test cross-repo dependency cycles"
4014 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4015 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004016 A.addApproval('code-review', 2)
4017 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004018
4019 # A -> B -> A (via commit-depends)
4020
4021 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4022 A.subject, B.data['id'])
4023 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4024 B.subject, A.data['id'])
4025
James E. Blair8b5408c2016-08-08 15:37:46 -07004026 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004027 self.waitUntilSettled()
4028
4029 self.assertEqual(A.reported, 0)
4030 self.assertEqual(B.reported, 0)
4031 self.assertEqual(A.data['status'], 'NEW')
4032 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004033
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004034 def test_crd_gate_unknown(self):
4035 "Test unknown projects in dependent pipeline"
4036 self.init_repo("org/unknown")
4037 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4038 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004039 A.addApproval('code-review', 2)
4040 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004041
4042 # A Depends-On: B
4043 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4044 A.subject, B.data['id'])
4045
James E. Blair8b5408c2016-08-08 15:37:46 -07004046 B.addApproval('approved', 1)
4047 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004048 self.waitUntilSettled()
4049
4050 # Unknown projects cannot share a queue with any other
4051 # since they don't have common jobs with any other (they have no jobs).
4052 # Changes which depend on unknown project changes
4053 # should not be processed in dependent pipeline
4054 self.assertEqual(A.data['status'], 'NEW')
4055 self.assertEqual(B.data['status'], 'NEW')
4056 self.assertEqual(A.reported, 0)
4057 self.assertEqual(B.reported, 0)
4058 self.assertEqual(len(self.history), 0)
4059
4060 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07004061 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004062 B.setMerged()
4063 self.waitUntilSettled()
4064 self.assertEqual(len(self.history), 0)
4065
4066 # Now that B is merged, A should be able to be enqueued and
4067 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07004068 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004069 self.waitUntilSettled()
4070
4071 self.assertEqual(A.data['status'], 'MERGED')
4072 self.assertEqual(A.reported, 2)
4073 self.assertEqual(B.data['status'], 'MERGED')
4074 self.assertEqual(B.reported, 0)
4075
James E. Blairbfb8e042014-12-30 17:01:44 -08004076 def test_crd_check(self):
4077 "Test cross-repo dependencies in independent pipelines"
4078
Paul Belangerb30342b2016-11-14 12:30:43 -05004079 self.launch_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004080 self.gearman_server.hold_jobs_in_queue = True
4081 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4082 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4083
4084 # A Depends-On: B
4085 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4086 A.subject, B.data['id'])
4087
4088 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4089 self.waitUntilSettled()
4090
4091 queue = self.gearman_server.getQueue()
4092 ref = self.getParameter(queue[-1], 'ZUUL_REF')
4093 self.gearman_server.hold_jobs_in_queue = False
4094 self.gearman_server.release()
4095 self.waitUntilSettled()
4096
Paul Belangerb30342b2016-11-14 12:30:43 -05004097 self.launch_server.release('.*-merge')
4098 self.waitUntilSettled()
4099
4100 path = os.path.join(self.builds[0].jobdir.git_root, "org/project1")
James E. Blairbfb8e042014-12-30 17:01:44 -08004101 repo = git.Repo(path)
4102 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4103 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004104 correct_messages = [
4105 'initial commit', 'add content from fixture', 'A-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004106 self.assertEqual(repo_messages, correct_messages)
4107
Paul Belangerb30342b2016-11-14 12:30:43 -05004108 path = os.path.join(self.builds[0].jobdir.git_root, "org/project2")
James E. Blairbfb8e042014-12-30 17:01:44 -08004109 repo = git.Repo(path)
4110 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4111 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004112 correct_messages = [
4113 'initial commit', 'add content from fixture', 'B-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004114 self.assertEqual(repo_messages, correct_messages)
4115
Paul Belangerb30342b2016-11-14 12:30:43 -05004116 self.launch_server.hold_jobs_in_build = False
4117 self.launch_server.release()
4118 self.waitUntilSettled()
4119
James E. Blairbfb8e042014-12-30 17:01:44 -08004120 self.assertEqual(A.data['status'], 'NEW')
4121 self.assertEqual(B.data['status'], 'NEW')
4122 self.assertEqual(A.reported, 1)
4123 self.assertEqual(B.reported, 0)
4124
4125 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004126 tenant = self.sched.abide.tenants.get('tenant-one')
4127 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004128
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004129 def test_crd_check_git_depends(self):
4130 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004131 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004132 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4133 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4134
4135 # Add two git-dependent changes and make sure they both report
4136 # success.
4137 B.setDependsOn(A, 1)
4138 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4139 self.waitUntilSettled()
4140 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4141 self.waitUntilSettled()
4142
James E. Blairb8c16472015-05-05 14:55:26 -07004143 self.orderedRelease()
4144 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004145 self.waitUntilSettled()
4146
4147 self.assertEqual(A.data['status'], 'NEW')
4148 self.assertEqual(B.data['status'], 'NEW')
4149 self.assertEqual(A.reported, 1)
4150 self.assertEqual(B.reported, 1)
4151
4152 self.assertEqual(self.history[0].changes, '1,1')
4153 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004154 tenant = self.sched.abide.tenants.get('tenant-one')
4155 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004156
4157 self.assertIn('Build succeeded', A.messages[0])
4158 self.assertIn('Build succeeded', B.messages[0])
4159
4160 def test_crd_check_duplicate(self):
4161 "Test duplicate check in independent pipelines"
James E. Blair08d19992016-08-10 15:25:31 -07004162 self.launch_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004163 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4164 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004165 tenant = self.sched.abide.tenants.get('tenant-one')
4166 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004167
4168 # Add two git-dependent changes...
4169 B.setDependsOn(A, 1)
4170 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4171 self.waitUntilSettled()
4172 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4173
4174 # ...make sure the live one is not duplicated...
4175 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4176 self.waitUntilSettled()
4177 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4178
4179 # ...but the non-live one is able to be.
4180 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4181 self.waitUntilSettled()
4182 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4183
Clark Boylandd849822015-03-02 12:38:14 -08004184 # Release jobs in order to avoid races with change A jobs
4185 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004186 self.orderedRelease()
James E. Blair08d19992016-08-10 15:25:31 -07004187 self.launch_server.hold_jobs_in_build = False
4188 self.launch_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004189 self.waitUntilSettled()
4190
4191 self.assertEqual(A.data['status'], 'NEW')
4192 self.assertEqual(B.data['status'], 'NEW')
4193 self.assertEqual(A.reported, 1)
4194 self.assertEqual(B.reported, 1)
4195
4196 self.assertEqual(self.history[0].changes, '1,1 2,1')
4197 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004198 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004199
4200 self.assertIn('Build succeeded', A.messages[0])
4201 self.assertIn('Build succeeded', B.messages[0])
4202
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004203 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004204 "Test cross-repo dependencies re-enqueued in independent pipelines"
4205
4206 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004207 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4208 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004209
4210 # A Depends-On: B
4211 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4212 A.subject, B.data['id'])
4213
4214 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4215 self.waitUntilSettled()
4216
4217 self.sched.reconfigure(self.config)
4218
4219 # Make sure the items still share a change queue, and the
4220 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004221 tenant = self.sched.abide.tenants.get('tenant-one')
4222 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4223 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004224 first_item = queue.queue[0]
4225 for item in queue.queue:
4226 self.assertEqual(item.queue, first_item.queue)
4227 self.assertFalse(first_item.live)
4228 self.assertTrue(queue.queue[1].live)
4229
4230 self.gearman_server.hold_jobs_in_queue = False
4231 self.gearman_server.release()
4232 self.waitUntilSettled()
4233
4234 self.assertEqual(A.data['status'], 'NEW')
4235 self.assertEqual(B.data['status'], 'NEW')
4236 self.assertEqual(A.reported, 1)
4237 self.assertEqual(B.reported, 0)
4238
4239 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004240 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004241
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004242 def test_crd_check_reconfiguration(self):
4243 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4244
4245 def test_crd_undefined_project(self):
4246 """Test that undefined projects in dependencies are handled for
4247 independent pipelines"""
4248 # It's a hack for fake gerrit,
4249 # as it implies repo creation upon the creation of any change
4250 self.init_repo("org/unknown")
4251 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4252
James E. Blairec056492016-07-22 09:45:56 -07004253 @skip("Disabled for early v3 development")
James E. Blair17dd6772015-02-09 14:45:18 -08004254 def test_crd_check_ignore_dependencies(self):
4255 "Test cross-repo dependencies can be ignored"
James E. Blairf84026c2015-12-08 16:11:46 -08004256 self.updateConfigLayout(
4257 'tests/fixtures/layout-ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004258 self.sched.reconfigure(self.config)
4259 self.registerJobs()
4260
4261 self.gearman_server.hold_jobs_in_queue = True
4262 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4263 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4264 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4265
4266 # A Depends-On: B
4267 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4268 A.subject, B.data['id'])
4269 # C git-depends on B
4270 C.setDependsOn(B, 1)
4271 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4272 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4273 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4274 self.waitUntilSettled()
4275
4276 # Make sure none of the items share a change queue, and all
4277 # are live.
4278 check_pipeline = self.sched.layout.pipelines['check']
4279 self.assertEqual(len(check_pipeline.queues), 3)
4280 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4281 for item in check_pipeline.getAllItems():
4282 self.assertTrue(item.live)
4283
4284 self.gearman_server.hold_jobs_in_queue = False
4285 self.gearman_server.release()
4286 self.waitUntilSettled()
4287
4288 self.assertEqual(A.data['status'], 'NEW')
4289 self.assertEqual(B.data['status'], 'NEW')
4290 self.assertEqual(C.data['status'], 'NEW')
4291 self.assertEqual(A.reported, 1)
4292 self.assertEqual(B.reported, 1)
4293 self.assertEqual(C.reported, 1)
4294
4295 # Each job should have tested exactly one change
4296 for job in self.history:
4297 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004298
James E. Blairec056492016-07-22 09:45:56 -07004299 @skip("Disabled for early v3 development")
James E. Blair96698e22015-04-02 07:48:21 -07004300 def test_crd_check_transitive(self):
4301 "Test transitive cross-repo dependencies"
4302 # Specifically, if A -> B -> C, and C gets a new patchset and
4303 # A gets a new patchset, ensure the test of A,2 includes B,1
4304 # and C,2 (not C,1 which would indicate stale data in the
4305 # cache for B).
4306 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4307 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4308 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4309
4310 # A Depends-On: B
4311 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4312 A.subject, B.data['id'])
4313
4314 # B Depends-On: C
4315 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4316 B.subject, C.data['id'])
4317
4318 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4319 self.waitUntilSettled()
4320 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4321
4322 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4323 self.waitUntilSettled()
4324 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4325
4326 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4327 self.waitUntilSettled()
4328 self.assertEqual(self.history[-1].changes, '3,1')
4329
4330 C.addPatchset()
4331 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4332 self.waitUntilSettled()
4333 self.assertEqual(self.history[-1].changes, '3,2')
4334
4335 A.addPatchset()
4336 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4337 self.waitUntilSettled()
4338 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004339
James E. Blaira8b90b32016-08-24 15:18:50 -07004340 def test_crd_check_unknown(self):
4341 "Test unknown projects in independent pipeline"
4342 self.init_repo("org/unknown")
4343 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4344 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4345 # A Depends-On: B
4346 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4347 A.subject, B.data['id'])
4348
4349 # Make sure zuul has seen an event on B.
4350 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4351 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4352 self.waitUntilSettled()
4353
4354 self.assertEqual(A.data['status'], 'NEW')
4355 self.assertEqual(A.reported, 1)
4356 self.assertEqual(B.data['status'], 'NEW')
4357 self.assertEqual(B.reported, 0)
4358
James E. Blair92464a22016-04-05 10:21:26 -07004359 def test_crd_cycle_join(self):
4360 "Test an updated change creates a cycle"
4361 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4362
4363 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4364 self.waitUntilSettled()
4365
4366 # Create B->A
4367 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4368 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4369 B.subject, A.data['id'])
4370 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4371 self.waitUntilSettled()
4372
4373 # Update A to add A->B (a cycle).
4374 A.addPatchset()
4375 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4376 A.subject, B.data['id'])
4377 # Normally we would submit the patchset-created event for
4378 # processing here, however, we have no way of noting whether
4379 # the dependency cycle detection correctly raised an
4380 # exception, so instead, we reach into the source driver and
4381 # call the method that would ultimately be called by the event
4382 # processing.
4383
Paul Belanger0e155e22016-11-14 14:12:23 -05004384 tenant = self.sched.abide.tenants.get('tenant-one')
4385 source = tenant.layout.pipelines['gate'].source
4386
4387 # TODO(pabelanger): As we add more source / trigger APIs we should make
4388 # it easier for users to create events for testing.
4389 event = zuul.model.TriggerEvent()
4390 event.trigger_name = 'gerrit'
4391 event.change_number = '1'
4392 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004393 with testtools.ExpectedException(
4394 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004395 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004396 self.log.debug("Got expected dependency cycle exception")
4397
4398 # Now if we update B to remove the depends-on, everything
4399 # should be okay. B; A->B
4400
4401 B.addPatchset()
4402 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004403
4404 source.getChange(event, True)
4405 event.change_number = '2'
4406 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004407
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004408 def test_disable_at(self):
4409 "Test a pipeline will only report to the disabled trigger when failing"
4410
Paul Belanger7dc76e82016-11-11 16:51:08 -05004411 self.updateConfigLayout('layout-disabled-at')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004412 self.sched.reconfigure(self.config)
4413
Paul Belanger7dc76e82016-11-11 16:51:08 -05004414 tenant = self.sched.abide.tenants.get('openstack')
4415 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004416 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004417 0, tenant.layout.pipelines['check']._consecutive_failures)
4418 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004419
4420 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4421 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4422 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4423 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4424 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4425 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4426 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4427 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4428 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4429 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4430 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4431
James E. Blair08d19992016-08-10 15:25:31 -07004432 self.launch_server.failJob('project-test1', A)
4433 self.launch_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004434 # Let C pass, resetting the counter
James E. Blair08d19992016-08-10 15:25:31 -07004435 self.launch_server.failJob('project-test1', D)
4436 self.launch_server.failJob('project-test1', E)
4437 self.launch_server.failJob('project-test1', F)
4438 self.launch_server.failJob('project-test1', G)
4439 self.launch_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004440 # I also passes but should only report to the disabled reporters
James E. Blair08d19992016-08-10 15:25:31 -07004441 self.launch_server.failJob('project-test1', J)
4442 self.launch_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004443
4444 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4445 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4446 self.waitUntilSettled()
4447
4448 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004449 2, tenant.layout.pipelines['check']._consecutive_failures)
4450 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004451
4452 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4453 self.waitUntilSettled()
4454
4455 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004456 0, tenant.layout.pipelines['check']._consecutive_failures)
4457 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004458
4459 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4460 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4461 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4462 self.waitUntilSettled()
4463
4464 # We should be disabled now
4465 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004466 3, tenant.layout.pipelines['check']._consecutive_failures)
4467 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004468
4469 # We need to wait between each of these patches to make sure the
4470 # smtp messages come back in an expected order
4471 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4472 self.waitUntilSettled()
4473 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4474 self.waitUntilSettled()
4475 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4476 self.waitUntilSettled()
4477
4478 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4479 # leaving a message on each change
4480 self.assertEqual(1, len(A.messages))
4481 self.assertIn('Build failed.', A.messages[0])
4482 self.assertEqual(1, len(B.messages))
4483 self.assertIn('Build failed.', B.messages[0])
4484 self.assertEqual(1, len(C.messages))
4485 self.assertIn('Build succeeded.', C.messages[0])
4486 self.assertEqual(1, len(D.messages))
4487 self.assertIn('Build failed.', D.messages[0])
4488 self.assertEqual(1, len(E.messages))
4489 self.assertIn('Build failed.', E.messages[0])
4490 self.assertEqual(1, len(F.messages))
4491 self.assertIn('Build failed.', F.messages[0])
4492
4493 # The last 3 (GHI) would have only reported via smtp.
4494 self.assertEqual(3, len(self.smtp_messages))
4495 self.assertEqual(0, len(G.messages))
4496 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004497 self.assertIn(
4498 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004499 self.assertEqual(0, len(H.messages))
4500 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004501 self.assertIn(
4502 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004503 self.assertEqual(0, len(I.messages))
4504 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004505 self.assertIn(
4506 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004507
4508 # Now reload the configuration (simulate a HUP) to check the pipeline
4509 # comes out of disabled
4510 self.sched.reconfigure(self.config)
4511
Paul Belanger7dc76e82016-11-11 16:51:08 -05004512 tenant = self.sched.abide.tenants.get('openstack')
4513
4514 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004515 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004516 0, tenant.layout.pipelines['check']._consecutive_failures)
4517 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004518
4519 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4520 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4521 self.waitUntilSettled()
4522
4523 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004524 2, tenant.layout.pipelines['check']._consecutive_failures)
4525 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004526
4527 # J and K went back to gerrit
4528 self.assertEqual(1, len(J.messages))
4529 self.assertIn('Build failed.', J.messages[0])
4530 self.assertEqual(1, len(K.messages))
4531 self.assertIn('Build failed.', K.messages[0])
4532 # No more messages reported via smtp
4533 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004534
Paul Belanger71d98172016-11-08 10:56:31 -05004535 def test_rerun_on_abort(self):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004536 "Test that if a launch server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004537
4538 self.config.set('zuul', 'layout_config',
4539 'tests/fixtures/layout-abort-attempts.yaml')
4540 self.sched.reconfigure(self.config)
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004541 self.launch_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004542 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4543 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4544 self.waitUntilSettled()
4545
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004546 self.launch_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004547 self.waitUntilSettled()
4548
4549 self.assertEqual(len(self.builds), 2)
4550 self.builds[0].requeue = True
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004551 self.launch_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004552 self.waitUntilSettled()
4553
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004554 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004555 self.assertEqual(len(self.builds), 1,
4556 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004557 self.builds[0].requeue = True
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004558 self.launch_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004559 self.waitUntilSettled()
4560
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004561 self.launch_server.hold_jobs_in_build = False
4562 self.launch_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004563 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004564 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004565 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4566 self.assertEqual(A.reported, 1)
4567 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004568
Arieb6f068c2016-10-09 13:11:06 +03004569
4570class TestDuplicatePipeline(ZuulTestCase):
4571 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4572
4573 def test_duplicate_pipelines(self):
4574 "Test that a change matching multiple pipelines works"
4575
4576 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4577 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4578 self.waitUntilSettled()
4579
4580 self.assertHistory([
4581 dict(name='project-test1', result='SUCCESS', changes='1,1',
4582 pipeline='dup1'),
4583 dict(name='project-test1', result='SUCCESS', changes='1,1',
4584 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004585 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004586
4587 self.assertEqual(len(A.messages), 2)
4588
Paul Belangera46a3742016-11-09 14:23:03 -05004589 if 'dup1' in A.messages[0]:
4590 self.assertIn('dup1', A.messages[0])
4591 self.assertNotIn('dup2', A.messages[0])
4592 self.assertIn('project-test1', A.messages[0])
4593 self.assertIn('dup2', A.messages[1])
4594 self.assertNotIn('dup1', A.messages[1])
4595 self.assertIn('project-test1', A.messages[1])
4596 else:
4597 self.assertIn('dup1', A.messages[1])
4598 self.assertNotIn('dup2', A.messages[1])
4599 self.assertIn('project-test1', A.messages[1])
4600 self.assertIn('dup2', A.messages[0])
4601 self.assertNotIn('dup1', A.messages[0])
4602 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004603
4604
4605class TestSchedulerOneJobProject(ZuulTestCase):
4606 tenant_config_file = 'config/one-job-project/main.yaml'
4607
4608 def test_one_job_project(self):
4609 "Test that queueing works with one job"
4610 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4611 'master', 'A')
4612 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4613 'master', 'B')
4614 A.addApproval('code-review', 2)
4615 B.addApproval('code-review', 2)
4616 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4617 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4618 self.waitUntilSettled()
4619
4620 self.assertEqual(A.data['status'], 'MERGED')
4621 self.assertEqual(A.reported, 2)
4622 self.assertEqual(B.data['status'], 'MERGED')
4623 self.assertEqual(B.reported, 2)
Paul Belanger86085b32016-11-03 12:48:57 -04004624
4625
4626class TestSchedulerTemplatedProject(ZuulTestCase):
4627 tenant_config_file = 'config/templated-project/main.yaml'
4628
4629 def test_job_from_templates_launched(self):
4630 "Test whether a job generated via a template can be launched"
4631
4632 A = self.fake_gerrit.addFakeChange(
4633 'org/templated-project', 'master', 'A')
4634 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4635 self.waitUntilSettled()
4636
4637 self.assertEqual(self.getJobFromHistory('project-test1').result,
4638 'SUCCESS')
4639 self.assertEqual(self.getJobFromHistory('project-test2').result,
4640 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004641
4642 def test_layered_templates(self):
4643 "Test whether a job generated via a template can be launched"
4644
4645 A = self.fake_gerrit.addFakeChange(
4646 'org/layered-project', 'master', 'A')
4647 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4648 self.waitUntilSettled()
4649
4650 self.assertEqual(self.getJobFromHistory('project-test1').result,
4651 'SUCCESS')
4652 self.assertEqual(self.getJobFromHistory('project-test2').result,
4653 'SUCCESS')
4654 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4655 ).result, 'SUCCESS')
4656 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4657 ).result, 'SUCCESS')
4658 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4659 ).result, 'SUCCESS')
4660 self.assertEqual(self.getJobFromHistory('project-test6').result,
4661 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004662
4663
4664class TestSchedulerSuccessURL(ZuulTestCase):
4665 tenant_config_file = 'config/success-url/main.yaml'
4666
4667 def test_success_url(self):
4668 "Ensure bad build params are ignored"
4669 self.sched.reconfigure(self.config)
4670 self.init_repo('org/docs')
4671
4672 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4673 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4674 self.waitUntilSettled()
4675
4676 # Both builds ran: docs-draft-test + docs-draft-test2
4677 self.assertEqual(len(self.history), 2)
4678
4679 # Grab build id
4680 uuid = self.history[0].uuid[:7]
4681
4682 # Two msgs: 'Starting...' + results
4683 self.assertEqual(len(self.smtp_messages), 2)
4684 body = self.smtp_messages[1]['body'].splitlines()
4685 self.assertEqual('Build succeeded.', body[0])
4686
4687 self.assertIn(
4688 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4689 'docs-draft-test/%s/publish-docs/' % uuid,
4690 body[2])
4691
4692 # NOTE: This default URL is currently hard-coded in launcher/server.py
4693 self.assertIn(
4694 '- docs-draft-test2 https://server/job',
4695 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004696
4697
4698class TestSchedulerMergeModes(ZuulTestCase):
4699 tenant_config_file = 'config/merge-modes/main.yaml'
4700
4701 def _test_project_merge_mode(self, mode):
4702 self.launch_server.keep_jobdir = False
4703 project = 'org/project-%s' % mode
4704 self.launch_server.hold_jobs_in_build = True
4705 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4706 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4707 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4708 A.addApproval('code-review', 2)
4709 B.addApproval('code-review', 2)
4710 C.addApproval('code-review', 2)
4711 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4712 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4713 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4714 self.waitUntilSettled()
4715
4716 build = self.builds[-1]
4717 ref = self.getParameter(build, 'ZUUL_REF')
4718
4719 path = os.path.join(build.jobdir.git_root, project)
4720 repo = git.Repo(path)
4721 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4722 repo_messages.reverse()
4723
4724 self.launch_server.hold_jobs_in_build = False
4725 self.launch_server.release()
4726 self.waitUntilSettled()
4727
4728 return repo_messages
4729
4730 def _test_merge(self, mode):
4731 us_path = os.path.join(
4732 self.upstream_root, 'org/project-%s' % mode)
4733 expected_messages = [
4734 'initial commit',
4735 'add content from fixture',
4736 # the intermediate commits order is nondeterministic
4737 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4738 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4739 ]
4740 result = self._test_project_merge_mode(mode)
4741 self.assertEqual(result[:2], expected_messages[:2])
4742 self.assertEqual(result[-2:], expected_messages[-2:])
4743
4744 def test_project_merge_mode_merge(self):
4745 self._test_merge('merge')
4746
4747 def test_project_merge_mode_merge_resolve(self):
4748 self._test_merge('merge-resolve')
4749
4750 def test_project_merge_mode_cherrypick(self):
4751 expected_messages = [
4752 'initial commit',
4753 'add content from fixture',
4754 'A-1',
4755 'B-1',
4756 'C-1']
4757 result = self._test_project_merge_mode('cherry-pick')
4758 self.assertEqual(result, expected_messages)