blob: d01a88d67af1ef5c4f088d62217a5b89d1262815 [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. Blair4886cc12012-07-18 15:39:41 -0700927 def test_build_configuration(self):
928 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700929
930 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -0700931 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
932 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
933 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700934 A.addApproval('code-review', 2)
935 B.addApproval('code-review', 2)
936 C.addApproval('code-review', 2)
937 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
938 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
939 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair4886cc12012-07-18 15:39:41 -0700940 self.waitUntilSettled()
941
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700942 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -0700943 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700944 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -0700945 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700946 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -0700947 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700948 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700949 ref = self.getParameter(queue[-1], 'ZUUL_REF')
950 self.gearman_server.hold_jobs_in_queue = False
951 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700952 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -0700953
Monty Taylorbc758832013-06-17 17:22:42 -0400954 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -0700955 repo = git.Repo(path)
956 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
957 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -0700958 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400959 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -0700960
James E. Blairec056492016-07-22 09:45:56 -0700961 @skip("Disabled for early v3 development")
James E. Blair973721f2012-08-15 10:19:43 -0700962 def test_build_configuration_conflict(self):
963 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700964
965 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -0700966 A = self.fake_gerrit.addFakeChange('org/conflict-project',
967 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -0700968 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700969 B = self.fake_gerrit.addFakeChange('org/conflict-project',
970 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -0700971 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700972 C = self.fake_gerrit.addFakeChange('org/conflict-project',
973 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700974 A.addApproval('code-review', 2)
975 B.addApproval('code-review', 2)
976 C.addApproval('code-review', 2)
977 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
978 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
979 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700980 self.waitUntilSettled()
981
James E. Blair6736beb2013-07-11 15:18:15 -0700982 self.assertEqual(A.reported, 1)
983 self.assertEqual(B.reported, 1)
984 self.assertEqual(C.reported, 1)
985
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700986 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700987 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700988 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700989 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700990 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700991 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700992
993 self.assertEqual(len(self.history), 2) # A and C merge jobs
994
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700995 self.gearman_server.hold_jobs_in_queue = False
996 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700997 self.waitUntilSettled()
998
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400999 self.assertEqual(A.data['status'], 'MERGED')
1000 self.assertEqual(B.data['status'], 'NEW')
1001 self.assertEqual(C.data['status'], 'MERGED')
1002 self.assertEqual(A.reported, 2)
1003 self.assertEqual(B.reported, 2)
1004 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001005 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -07001006
James E. Blairdaabed22012-08-15 15:38:57 -07001007 def test_post(self):
1008 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001009
Zhongyue Luo5d556072012-09-21 02:00:47 +09001010 e = {
1011 "type": "ref-updated",
1012 "submitter": {
1013 "name": "User Name",
1014 },
1015 "refUpdate": {
1016 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1017 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1018 "refName": "master",
1019 "project": "org/project",
1020 }
1021 }
James E. Blairdaabed22012-08-15 15:38:57 -07001022 self.fake_gerrit.addEvent(e)
1023 self.waitUntilSettled()
1024
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001025 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001026 self.assertEqual(len(self.history), 1)
1027 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001028
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001029 def test_post_ignore_deletes(self):
1030 "Test that deleting refs does not trigger post jobs"
1031
1032 e = {
1033 "type": "ref-updated",
1034 "submitter": {
1035 "name": "User Name",
1036 },
1037 "refUpdate": {
1038 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1039 "newRev": "0000000000000000000000000000000000000000",
1040 "refName": "master",
1041 "project": "org/project",
1042 }
1043 }
1044 self.fake_gerrit.addEvent(e)
1045 self.waitUntilSettled()
1046
1047 job_names = [x.name for x in self.history]
1048 self.assertEqual(len(self.history), 0)
1049 self.assertNotIn('project-post', job_names)
1050
1051 def test_post_ignore_deletes_negative(self):
1052 "Test that deleting refs does trigger post jobs"
1053
Adam Gandelmanc5e4f1d2016-11-29 14:27:17 -08001054 self.updateConfigLayout('layout-dont-ignore-ref-deletes')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001055 self.sched.reconfigure(self.config)
1056
1057 e = {
1058 "type": "ref-updated",
1059 "submitter": {
1060 "name": "User Name",
1061 },
1062 "refUpdate": {
1063 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1064 "newRev": "0000000000000000000000000000000000000000",
1065 "refName": "master",
1066 "project": "org/project",
1067 }
1068 }
1069 self.fake_gerrit.addEvent(e)
1070 self.waitUntilSettled()
1071
1072 job_names = [x.name for x in self.history]
1073 self.assertEqual(len(self.history), 1)
1074 self.assertIn('project-post', job_names)
1075
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(self):
1078 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001079
1080 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001081 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1082 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1083 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001084 A.addApproval('code-review', 2)
1085 B.addApproval('code-review', 2)
1086 C.addApproval('code-review', 2)
1087 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1088 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1089 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairc6294a52012-08-17 10:19:48 -07001090 self.waitUntilSettled()
1091
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001092 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001093 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001094 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001095 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001096 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001097 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001098 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001099 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1100 self.gearman_server.hold_jobs_in_queue = False
1101 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001102 self.waitUntilSettled()
1103
Monty Taylorbc758832013-06-17 17:22:42 -04001104 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001105 repo = git.Repo(path)
1106 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1107 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001108 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001109 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001110
James E. Blairec056492016-07-22 09:45:56 -07001111 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001112 def test_build_configuration_branch_interaction(self):
1113 "Test that switching between branches works"
1114 self.test_build_configuration()
1115 self.test_build_configuration_branch()
1116 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001117 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001118 repo = git.Repo(path)
1119 repo.heads.master.commit = repo.commit('init')
1120 self.test_build_configuration()
1121
James E. Blairec056492016-07-22 09:45:56 -07001122 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001123 def test_build_configuration_multi_branch(self):
1124 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001125
1126 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001127 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1128 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1129 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001130 A.addApproval('code-review', 2)
1131 B.addApproval('code-review', 2)
1132 C.addApproval('code-review', 2)
1133 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1134 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1135 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairc6294a52012-08-17 10:19:48 -07001136 self.waitUntilSettled()
James E. Blairbb1fe502014-03-04 10:15:06 -08001137 queue = self.gearman_server.getQueue()
1138 job_A = None
1139 for job in queue:
1140 if 'project-merge' in job.name:
1141 job_A = job
1142 ref_A = self.getParameter(job_A, 'ZUUL_REF')
1143 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
1144 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
1145 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
James E. Blairc6294a52012-08-17 10:19:48 -07001146
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001147 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001148 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001149 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001150 job_B = None
1151 for job in queue:
1152 if 'project-merge' in job.name:
1153 job_B = job
1154 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001155 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001156 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blairbb1fe502014-03-04 10:15:06 -08001157 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
1158
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001159 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001160 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001161 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001162 for job in queue:
1163 if 'project-merge' in job.name:
1164 job_C = job
1165 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001166 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001167 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blairbb1fe502014-03-04 10:15:06 -08001168 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001169 self.gearman_server.hold_jobs_in_queue = False
1170 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001171 self.waitUntilSettled()
1172
Monty Taylorbc758832013-06-17 17:22:42 -04001173 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001174 repo = git.Repo(path)
1175
1176 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001177 for c in repo.iter_commits(ref_C)]
James E. Blairbb1fe502014-03-04 10:15:06 -08001178 repo_shas = [c.hexsha for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001179 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001180 correct_messages = ['initial commit', 'A-1', 'C-1']
James E. Blairbb1fe502014-03-04 10:15:06 -08001181 # Ensure the right commits are in the history for this ref
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001182 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08001183 # Ensure ZUUL_REF -> ZUUL_COMMIT
1184 self.assertEqual(repo_shas[0], commit_C)
James E. Blairc6294a52012-08-17 10:19:48 -07001185
1186 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001187 for c in repo.iter_commits(ref_B)]
James E. Blairbb1fe502014-03-04 10:15:06 -08001188 repo_shas = [c.hexsha for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001189 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001190 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001191 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08001192 self.assertEqual(repo_shas[0], commit_B)
1193
1194 repo_messages = [c.message.strip()
1195 for c in repo.iter_commits(ref_A)]
1196 repo_shas = [c.hexsha for c in repo.iter_commits(ref_A)]
1197 repo_messages.reverse()
1198 correct_messages = ['initial commit', 'A-1']
1199 self.assertEqual(repo_messages, correct_messages)
1200 self.assertEqual(repo_shas[0], commit_A)
1201
1202 self.assertNotEqual(ref_A, ref_B, ref_C)
1203 self.assertNotEqual(commit_A, commit_B, commit_C)
James E. Blair7f71c802012-08-22 13:04:32 -07001204
James E. Blaircaec0c52012-08-22 14:52:22 -07001205 def test_dependent_changes_dequeue(self):
1206 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001207
James E. Blaircaec0c52012-08-22 14:52:22 -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')
James E. Blair8b5408c2016-08-08 15:37:46 -07001211 A.addApproval('code-review', 2)
1212 B.addApproval('code-review', 2)
1213 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001214
1215 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1216 M1.setMerged()
1217
1218 # C -> B -> A -> M1
1219
1220 C.setDependsOn(B, 1)
1221 B.setDependsOn(A, 1)
1222 A.setDependsOn(M1, 1)
1223
James E. Blair08d19992016-08-10 15:25:31 -07001224 self.launch_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001225
James E. Blair8b5408c2016-08-08 15:37:46 -07001226 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1227 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1228 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001229
1230 self.waitUntilSettled()
1231
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001232 self.assertEqual(A.data['status'], 'NEW')
1233 self.assertEqual(A.reported, 2)
1234 self.assertEqual(B.data['status'], 'NEW')
1235 self.assertEqual(B.reported, 2)
1236 self.assertEqual(C.data['status'], 'NEW')
1237 self.assertEqual(C.reported, 2)
1238 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001239
James E. Blair972e3c72013-08-29 12:04:55 -07001240 def test_failing_dependent_changes(self):
1241 "Test that failing dependent patches are taken out of stream"
James E. Blair08d19992016-08-10 15:25:31 -07001242 self.launch_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001243 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1244 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1245 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1246 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1247 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001248 A.addApproval('code-review', 2)
1249 B.addApproval('code-review', 2)
1250 C.addApproval('code-review', 2)
1251 D.addApproval('code-review', 2)
1252 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001253
1254 # E, D -> C -> B, A
1255
1256 D.setDependsOn(C, 1)
1257 C.setDependsOn(B, 1)
1258
James E. Blair08d19992016-08-10 15:25:31 -07001259 self.launch_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001260
James E. Blair8b5408c2016-08-08 15:37:46 -07001261 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1262 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1263 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1264 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1265 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001266
1267 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001268 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001269 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001270 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001271 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001272 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001273 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001274 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001275 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001276 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001277 self.waitUntilSettled()
1278
James E. Blair08d19992016-08-10 15:25:31 -07001279 self.launch_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001280 for build in self.builds:
1281 if build.parameters['ZUUL_CHANGE'] != '1':
1282 build.release()
1283 self.waitUntilSettled()
1284
James E. Blair08d19992016-08-10 15:25:31 -07001285 self.launch_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001286 self.waitUntilSettled()
1287
1288 self.assertEqual(A.data['status'], 'MERGED')
1289 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001290 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001291 self.assertEqual(B.data['status'], 'NEW')
1292 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001293 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001294 self.assertEqual(C.data['status'], 'NEW')
1295 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001296 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001297 self.assertEqual(D.data['status'], 'NEW')
1298 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001299 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001300 self.assertEqual(E.data['status'], 'MERGED')
1301 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001302 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001303 self.assertEqual(len(self.history), 18)
1304
James E. Blairec590122012-08-22 15:19:31 -07001305 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001306 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001307 # If it's dequeued more than once, we should see extra
1308 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001309
James E. Blair08d19992016-08-10 15:25:31 -07001310 self.launch_server.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001311 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1312 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1313 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001314 A.addApproval('code-review', 2)
1315 B.addApproval('code-review', 2)
1316 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001317
Paul Belanger58bf6912016-11-11 19:36:01 -05001318 self.launch_server.failJob('project-test1', A)
1319 self.launch_server.failJob('project-test2', A)
James E. Blair08d19992016-08-10 15:25:31 -07001320 self.launch_server.failJob('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001321
James E. Blair8b5408c2016-08-08 15:37:46 -07001322 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1323 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1324 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001325
1326 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001327
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001328 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001329 self.assertEqual(self.builds[0].name, 'project-merge')
1330 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001331
James E. Blair08d19992016-08-10 15:25:31 -07001332 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001333 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001334 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001335 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001336 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001337 self.waitUntilSettled()
1338
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001339 self.assertEqual(len(self.builds), 9)
Paul Belanger58bf6912016-11-11 19:36:01 -05001340 self.assertEqual(self.builds[0].name, 'project-test1')
1341 self.assertEqual(self.builds[1].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001342 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001343 self.assertEqual(self.builds[3].name, 'project-test1')
1344 self.assertEqual(self.builds[4].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001345 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001346 self.assertEqual(self.builds[6].name, 'project-test1')
1347 self.assertEqual(self.builds[7].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001348 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001349
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001350 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001351 self.waitUntilSettled()
1352
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001353 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1354 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001355
James E. Blair08d19992016-08-10 15:25:31 -07001356 self.launch_server.hold_jobs_in_build = False
1357 self.launch_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001358 self.waitUntilSettled()
1359
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001360 self.assertEqual(len(self.builds), 0)
1361 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001362
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001363 self.assertEqual(A.data['status'], 'NEW')
1364 self.assertEqual(B.data['status'], 'MERGED')
1365 self.assertEqual(C.data['status'], 'MERGED')
1366 self.assertEqual(A.reported, 2)
1367 self.assertEqual(B.reported, 2)
1368 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001369
James E. Blairec056492016-07-22 09:45:56 -07001370 @skip("Disabled for early v3 development")
James E. Blair4ec821f2012-08-23 15:28:28 -07001371 def test_nonvoting_job(self):
1372 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001373
James E. Blair4ec821f2012-08-23 15:28:28 -07001374 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1375 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001376 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07001377 self.launch_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001378 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001379
1380 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001381
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001382 self.assertEqual(A.data['status'], 'MERGED')
1383 self.assertEqual(A.reported, 2)
1384 self.assertEqual(
1385 self.getJobFromHistory('nonvoting-project-merge').result,
1386 'SUCCESS')
1387 self.assertEqual(
1388 self.getJobFromHistory('nonvoting-project-test1').result,
1389 'SUCCESS')
1390 self.assertEqual(
1391 self.getJobFromHistory('nonvoting-project-test2').result,
1392 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001393
James E. Blair5821bd92015-09-16 08:48:15 -07001394 for build in self.builds:
1395 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1396
James E. Blaire0487072012-08-29 17:38:31 -07001397 def test_check_queue_success(self):
1398 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001399
James E. Blaire0487072012-08-29 17:38:31 -07001400 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1401 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1402
1403 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001404
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001405 self.assertEqual(A.data['status'], 'NEW')
1406 self.assertEqual(A.reported, 1)
1407 self.assertEqual(self.getJobFromHistory('project-merge').result,
1408 'SUCCESS')
1409 self.assertEqual(self.getJobFromHistory('project-test1').result,
1410 'SUCCESS')
1411 self.assertEqual(self.getJobFromHistory('project-test2').result,
1412 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001413
1414 def test_check_queue_failure(self):
1415 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001416
James E. Blaire0487072012-08-29 17:38:31 -07001417 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair08d19992016-08-10 15:25:31 -07001418 self.launch_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001419 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1420
1421 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001422
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001423 self.assertEqual(A.data['status'], 'NEW')
1424 self.assertEqual(A.reported, 1)
1425 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001426 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001427 self.assertEqual(self.getJobFromHistory('project-test1').result,
1428 'SUCCESS')
1429 self.assertEqual(self.getJobFromHistory('project-test2').result,
1430 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001431
James E. Blairec056492016-07-22 09:45:56 -07001432 @skip("Disabled for early v3 development")
James E. Blair127bc182012-08-28 15:55:15 -07001433 def test_dependent_behind_dequeue(self):
1434 "test that dependent changes behind dequeued changes work"
1435 # This complicated test is a reproduction of a real life bug
1436 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001437
James E. Blair08d19992016-08-10 15:25:31 -07001438 self.launch_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001439 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1440 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1441 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1442 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1443 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1444 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1445 D.setDependsOn(C, 1)
1446 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001447 A.addApproval('code-review', 2)
1448 B.addApproval('code-review', 2)
1449 C.addApproval('code-review', 2)
1450 D.addApproval('code-review', 2)
1451 E.addApproval('code-review', 2)
1452 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001453
1454 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001455
1456 # Change object re-use in the gerrit trigger is hidden if
1457 # changes are added in quick succession; waiting makes it more
1458 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001459 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001460 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001461 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001462 self.waitUntilSettled()
1463
James E. Blair08d19992016-08-10 15:25:31 -07001464 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001465 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001466 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001467 self.waitUntilSettled()
1468
James E. Blair8b5408c2016-08-08 15:37:46 -07001469 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001470 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001471 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001472 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001473 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001474 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001475 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001476 self.waitUntilSettled()
1477
James E. Blair08d19992016-08-10 15:25:31 -07001478 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001479 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001480 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001481 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001482 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001483 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001484 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001485 self.waitUntilSettled()
1486
1487 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001488
1489 # Grab pointers to the jobs we want to release before
1490 # releasing any, because list indexes may change as
1491 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001492 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001493 a.release()
1494 b.release()
1495 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001496 self.waitUntilSettled()
1497
James E. Blair08d19992016-08-10 15:25:31 -07001498 self.launch_server.hold_jobs_in_build = False
1499 self.launch_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001500 self.waitUntilSettled()
1501
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001502 self.assertEqual(A.data['status'], 'NEW')
1503 self.assertEqual(B.data['status'], 'MERGED')
1504 self.assertEqual(C.data['status'], 'MERGED')
1505 self.assertEqual(D.data['status'], 'MERGED')
1506 self.assertEqual(E.data['status'], 'MERGED')
1507 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001508
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001509 self.assertEqual(A.reported, 2)
1510 self.assertEqual(B.reported, 2)
1511 self.assertEqual(C.reported, 2)
1512 self.assertEqual(D.reported, 2)
1513 self.assertEqual(E.reported, 2)
1514 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001515
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001516 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1517 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001518
1519 def test_merger_repack(self):
1520 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001521
James E. Blair05fed602012-09-07 12:45:24 -07001522 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001523 A.addApproval('code-review', 2)
1524 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001525 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001526 self.assertEqual(self.getJobFromHistory('project-merge').result,
1527 'SUCCESS')
1528 self.assertEqual(self.getJobFromHistory('project-test1').result,
1529 'SUCCESS')
1530 self.assertEqual(self.getJobFromHistory('project-test2').result,
1531 'SUCCESS')
1532 self.assertEqual(A.data['status'], 'MERGED')
1533 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001534 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001535 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001536
Monty Taylorbc758832013-06-17 17:22:42 -04001537 path = os.path.join(self.git_root, "org/project")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001538 print(repack_repo(path))
James E. Blair05fed602012-09-07 12:45:24 -07001539
1540 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001541 A.addApproval('code-review', 2)
1542 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001543 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001544 self.assertEqual(self.getJobFromHistory('project-merge').result,
1545 'SUCCESS')
1546 self.assertEqual(self.getJobFromHistory('project-test1').result,
1547 'SUCCESS')
1548 self.assertEqual(self.getJobFromHistory('project-test2').result,
1549 'SUCCESS')
1550 self.assertEqual(A.data['status'], 'MERGED')
1551 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001552
James E. Blair4886f282012-11-15 09:27:33 -08001553 def test_merger_repack_large_change(self):
1554 "Test that the merger works with large changes after a repack"
1555 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001556 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001557 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Hesketh352264b2015-08-11 23:42:08 +10001558 url = self.fake_gerrit.getGitUrl(
Paul Belanger503bded2016-11-11 19:43:38 -05001559 tenant.layout.project_configs.get('org/project1'))
James E. Blair4076e2b2014-01-28 12:42:20 -08001560 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08001561 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1562 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001563 path = os.path.join(self.upstream_root, "org/project1")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001564 print(repack_repo(path))
Monty Taylorbc758832013-06-17 17:22:42 -04001565 path = os.path.join(self.git_root, "org/project1")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001566 print(repack_repo(path))
James E. Blair4886f282012-11-15 09:27:33 -08001567
James E. Blair8b5408c2016-08-08 15:37:46 -07001568 A.addApproval('code-review', 2)
1569 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001570 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001571 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001572 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001573 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001574 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001575 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001576 'SUCCESS')
1577 self.assertEqual(A.data['status'], 'MERGED')
1578 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001579
James E. Blairec056492016-07-22 09:45:56 -07001580 @skip("Disabled for early v3 development")
James E. Blair7ee88a22012-09-12 18:59:31 +02001581 def test_nonexistent_job(self):
1582 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001583 # Set to the state immediately after a restart
1584 self.resetGearmanServer()
1585 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02001586
1587 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001588 A.addApproval('code-review', 2)
1589 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair7ee88a22012-09-12 18:59:31 +02001590 # There may be a thread about to report a lost change
1591 while A.reported < 2:
1592 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001593 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001594 self.assertFalse(job_names)
1595 self.assertEqual(A.data['status'], 'NEW')
1596 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001597 self.assertEmptyQueues()
1598
1599 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001600 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02001601 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001602 A.addApproval('code-review', 2)
1603 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair7ee88a22012-09-12 18:59:31 +02001604 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001605 self.assertEqual(self.getJobFromHistory('project-merge').result,
1606 'SUCCESS')
1607 self.assertEqual(self.getJobFromHistory('project-test1').result,
1608 'SUCCESS')
1609 self.assertEqual(self.getJobFromHistory('project-test2').result,
1610 'SUCCESS')
1611 self.assertEqual(A.data['status'], 'MERGED')
1612 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08001613
James E. Blair2fa50962013-01-30 21:50:41 -08001614 def test_new_patchset_dequeues_old(self):
1615 "Test that a new patchset causes the old to be dequeued"
1616 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair08d19992016-08-10 15:25:31 -07001617 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001618 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1619 M.setMerged()
1620
1621 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1622 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1623 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1624 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001625 A.addApproval('code-review', 2)
1626 B.addApproval('code-review', 2)
1627 C.addApproval('code-review', 2)
1628 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001629
1630 C.setDependsOn(B, 1)
1631 B.setDependsOn(A, 1)
1632 A.setDependsOn(M, 1)
1633
James E. Blair8b5408c2016-08-08 15:37:46 -07001634 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1635 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1636 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1637 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001638 self.waitUntilSettled()
1639
1640 B.addPatchset()
1641 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1642 self.waitUntilSettled()
1643
James E. Blair08d19992016-08-10 15:25:31 -07001644 self.launch_server.hold_jobs_in_build = False
1645 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001646 self.waitUntilSettled()
1647
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001648 self.assertEqual(A.data['status'], 'MERGED')
1649 self.assertEqual(A.reported, 2)
1650 self.assertEqual(B.data['status'], 'NEW')
1651 self.assertEqual(B.reported, 2)
1652 self.assertEqual(C.data['status'], 'NEW')
1653 self.assertEqual(C.reported, 2)
1654 self.assertEqual(D.data['status'], 'MERGED')
1655 self.assertEqual(D.reported, 2)
1656 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001657
James E. Blairba437362015-02-07 11:41:52 -08001658 def test_new_patchset_check(self):
1659 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001660
James E. Blair08d19992016-08-10 15:25:31 -07001661 self.launch_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001662
1663 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001664 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001665 tenant = self.sched.abide.tenants.get('tenant-one')
1666 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001667
1668 # Add two git-dependent changes
1669 B.setDependsOn(A, 1)
1670 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1671 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001672 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1673 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001674
1675 # A live item, and a non-live/live pair
1676 items = check_pipeline.getAllItems()
1677 self.assertEqual(len(items), 3)
1678
1679 self.assertEqual(items[0].change.number, '1')
1680 self.assertEqual(items[0].change.patchset, '1')
1681 self.assertFalse(items[0].live)
1682
1683 self.assertEqual(items[1].change.number, '2')
1684 self.assertEqual(items[1].change.patchset, '1')
1685 self.assertTrue(items[1].live)
1686
1687 self.assertEqual(items[2].change.number, '1')
1688 self.assertEqual(items[2].change.patchset, '1')
1689 self.assertTrue(items[2].live)
1690
1691 # Add a new patchset to A
1692 A.addPatchset()
1693 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1694 self.waitUntilSettled()
1695
1696 # The live copy of A,1 should be gone, but the non-live and B
1697 # should continue, and we should have a new A,2
1698 items = check_pipeline.getAllItems()
1699 self.assertEqual(len(items), 3)
1700
1701 self.assertEqual(items[0].change.number, '1')
1702 self.assertEqual(items[0].change.patchset, '1')
1703 self.assertFalse(items[0].live)
1704
1705 self.assertEqual(items[1].change.number, '2')
1706 self.assertEqual(items[1].change.patchset, '1')
1707 self.assertTrue(items[1].live)
1708
1709 self.assertEqual(items[2].change.number, '1')
1710 self.assertEqual(items[2].change.patchset, '2')
1711 self.assertTrue(items[2].live)
1712
1713 # Add a new patchset to B
1714 B.addPatchset()
1715 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1716 self.waitUntilSettled()
1717
1718 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1719 # but we should have a new B,2 (still based on A,1)
1720 items = check_pipeline.getAllItems()
1721 self.assertEqual(len(items), 3)
1722
1723 self.assertEqual(items[0].change.number, '1')
1724 self.assertEqual(items[0].change.patchset, '2')
1725 self.assertTrue(items[0].live)
1726
1727 self.assertEqual(items[1].change.number, '1')
1728 self.assertEqual(items[1].change.patchset, '1')
1729 self.assertFalse(items[1].live)
1730
1731 self.assertEqual(items[2].change.number, '2')
1732 self.assertEqual(items[2].change.patchset, '2')
1733 self.assertTrue(items[2].live)
1734
1735 self.builds[0].release()
1736 self.waitUntilSettled()
1737 self.builds[0].release()
1738 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001739 self.launch_server.hold_jobs_in_build = False
1740 self.launch_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001741 self.waitUntilSettled()
1742
1743 self.assertEqual(A.reported, 1)
1744 self.assertEqual(B.reported, 1)
1745 self.assertEqual(self.history[0].result, 'ABORTED')
1746 self.assertEqual(self.history[0].changes, '1,1')
1747 self.assertEqual(self.history[1].result, 'ABORTED')
1748 self.assertEqual(self.history[1].changes, '1,1 2,1')
1749 self.assertEqual(self.history[2].result, 'SUCCESS')
1750 self.assertEqual(self.history[2].changes, '1,2')
1751 self.assertEqual(self.history[3].result, 'SUCCESS')
1752 self.assertEqual(self.history[3].changes, '1,1 2,2')
1753
1754 def test_abandoned_gate(self):
1755 "Test that an abandoned change is dequeued from gate"
1756
James E. Blair08d19992016-08-10 15:25:31 -07001757 self.launch_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001758
1759 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001760 A.addApproval('code-review', 2)
1761 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001762 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001763 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1764 self.assertEqual(self.builds[0].name, 'project-merge')
1765
1766 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1767 self.waitUntilSettled()
1768
James E. Blair08d19992016-08-10 15:25:31 -07001769 self.launch_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001770 self.waitUntilSettled()
1771
Arie5b4048c2016-10-05 16:12:06 +03001772 self.assertBuilds([])
1773 self.assertHistory([
1774 dict(name='project-merge', result='ABORTED', changes='1,1')],
1775 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001776 self.assertEqual(A.reported, 1,
1777 "Abandoned gate change should report only start")
1778
1779 def test_abandoned_check(self):
1780 "Test that an abandoned change is dequeued from check"
1781
James E. Blair08d19992016-08-10 15:25:31 -07001782 self.launch_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001783
1784 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1785 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001786 tenant = self.sched.abide.tenants.get('tenant-one')
1787 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001788
1789 # Add two git-dependent changes
1790 B.setDependsOn(A, 1)
1791 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1792 self.waitUntilSettled()
1793 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1794 self.waitUntilSettled()
1795 # A live item, and a non-live/live pair
1796 items = check_pipeline.getAllItems()
1797 self.assertEqual(len(items), 3)
1798
1799 self.assertEqual(items[0].change.number, '1')
1800 self.assertFalse(items[0].live)
1801
1802 self.assertEqual(items[1].change.number, '2')
1803 self.assertTrue(items[1].live)
1804
1805 self.assertEqual(items[2].change.number, '1')
1806 self.assertTrue(items[2].live)
1807
1808 # Abandon A
1809 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1810 self.waitUntilSettled()
1811
1812 # The live copy of A should be gone, but the non-live and B
1813 # should continue
1814 items = check_pipeline.getAllItems()
1815 self.assertEqual(len(items), 2)
1816
1817 self.assertEqual(items[0].change.number, '1')
1818 self.assertFalse(items[0].live)
1819
1820 self.assertEqual(items[1].change.number, '2')
1821 self.assertTrue(items[1].live)
1822
James E. Blair08d19992016-08-10 15:25:31 -07001823 self.launch_server.hold_jobs_in_build = False
1824 self.launch_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001825 self.waitUntilSettled()
1826
1827 self.assertEqual(len(self.history), 4)
1828 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001829 'Build should have been aborted')
1830 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001831 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001832
James E. Blairec056492016-07-22 09:45:56 -07001833 @skip("Disabled for early v3 development")
Steve Varnau7b78b312015-04-03 14:49:46 -07001834 def test_abandoned_not_timer(self):
1835 "Test that an abandoned change does not cancel timer jobs"
1836
James E. Blair08d19992016-08-10 15:25:31 -07001837 self.launch_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001838
1839 # Start timer trigger - also org/project
James E. Blairf84026c2015-12-08 16:11:46 -08001840 self.updateConfigLayout(
1841 'tests/fixtures/layout-idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001842 self.sched.reconfigure(self.config)
1843 self.registerJobs()
1844 # The pipeline triggers every second, so we should have seen
1845 # several by now.
1846 time.sleep(5)
1847 self.waitUntilSettled()
1848 # Stop queuing timer triggered jobs so that the assertions
1849 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08001850 self.updateConfigLayout(
1851 'tests/fixtures/layout-no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001852 self.sched.reconfigure(self.config)
1853 self.registerJobs()
1854 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1855
1856 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1857 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1858 self.waitUntilSettled()
1859 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1860
1861 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1862 self.waitUntilSettled()
1863
1864 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1865
James E. Blair08d19992016-08-10 15:25:31 -07001866 self.launch_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001867 self.waitUntilSettled()
1868
Arx Cruzb1b010d2013-10-28 19:49:59 -02001869 def test_zuul_url_return(self):
1870 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001871 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
James E. Blair08d19992016-08-10 15:25:31 -07001872 self.launch_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001873
1874 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001875 A.addApproval('code-review', 2)
1876 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001877 self.waitUntilSettled()
1878
1879 self.assertEqual(len(self.builds), 1)
1880 for build in self.builds:
1881 self.assertTrue('ZUUL_URL' in build.parameters)
1882
James E. Blair08d19992016-08-10 15:25:31 -07001883 self.launch_server.hold_jobs_in_build = False
1884 self.launch_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001885 self.waitUntilSettled()
1886
James E. Blair2fa50962013-01-30 21:50:41 -08001887 def test_new_patchset_dequeues_old_on_head(self):
1888 "Test that a new patchset causes the old to be dequeued (at head)"
1889 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair08d19992016-08-10 15:25:31 -07001890 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001891 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1892 M.setMerged()
1893 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1894 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1895 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1896 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001897 A.addApproval('code-review', 2)
1898 B.addApproval('code-review', 2)
1899 C.addApproval('code-review', 2)
1900 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001901
1902 C.setDependsOn(B, 1)
1903 B.setDependsOn(A, 1)
1904 A.setDependsOn(M, 1)
1905
James E. Blair8b5408c2016-08-08 15:37:46 -07001906 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1907 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1908 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1909 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001910 self.waitUntilSettled()
1911
1912 A.addPatchset()
1913 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1914 self.waitUntilSettled()
1915
James E. Blair08d19992016-08-10 15:25:31 -07001916 self.launch_server.hold_jobs_in_build = False
1917 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001918 self.waitUntilSettled()
1919
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001920 self.assertEqual(A.data['status'], 'NEW')
1921 self.assertEqual(A.reported, 2)
1922 self.assertEqual(B.data['status'], 'NEW')
1923 self.assertEqual(B.reported, 2)
1924 self.assertEqual(C.data['status'], 'NEW')
1925 self.assertEqual(C.reported, 2)
1926 self.assertEqual(D.data['status'], 'MERGED')
1927 self.assertEqual(D.reported, 2)
1928 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001929
1930 def test_new_patchset_dequeues_old_without_dependents(self):
1931 "Test that a new patchset causes only the old to be dequeued"
James E. Blair08d19992016-08-10 15:25:31 -07001932 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001933 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1934 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1935 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001936 A.addApproval('code-review', 2)
1937 B.addApproval('code-review', 2)
1938 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001939
James E. Blair8b5408c2016-08-08 15:37:46 -07001940 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1941 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1942 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001943 self.waitUntilSettled()
1944
1945 B.addPatchset()
1946 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1947 self.waitUntilSettled()
1948
James E. Blair08d19992016-08-10 15:25:31 -07001949 self.launch_server.hold_jobs_in_build = False
1950 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001951 self.waitUntilSettled()
1952
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001953 self.assertEqual(A.data['status'], 'MERGED')
1954 self.assertEqual(A.reported, 2)
1955 self.assertEqual(B.data['status'], 'NEW')
1956 self.assertEqual(B.reported, 2)
1957 self.assertEqual(C.data['status'], 'MERGED')
1958 self.assertEqual(C.reported, 2)
1959 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001960
1961 def test_new_patchset_dequeues_old_independent_queue(self):
1962 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair08d19992016-08-10 15:25:31 -07001963 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001964 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1965 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1966 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1967 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1968 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1969 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1970 self.waitUntilSettled()
1971
1972 B.addPatchset()
1973 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1974 self.waitUntilSettled()
1975
James E. Blair08d19992016-08-10 15:25:31 -07001976 self.launch_server.hold_jobs_in_build = False
1977 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001978 self.waitUntilSettled()
1979
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001980 self.assertEqual(A.data['status'], 'NEW')
1981 self.assertEqual(A.reported, 1)
1982 self.assertEqual(B.data['status'], 'NEW')
1983 self.assertEqual(B.reported, 1)
1984 self.assertEqual(C.data['status'], 'NEW')
1985 self.assertEqual(C.reported, 1)
1986 self.assertEqual(len(self.history), 10)
1987 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001988
James E. Blair18c64442014-03-18 10:14:45 -07001989 def test_noop_job(self):
1990 "Test that the internal noop job works"
1991 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001992 A.addApproval('code-review', 2)
1993 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001994 self.waitUntilSettled()
1995
1996 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1997 self.assertTrue(self.sched._areAllBuildsComplete())
1998 self.assertEqual(len(self.history), 0)
1999 self.assertEqual(A.data['status'], 'MERGED')
2000 self.assertEqual(A.reported, 2)
2001
James E. Blairec056492016-07-22 09:45:56 -07002002 @skip("Disabled for early v3 development")
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002003 def test_no_job_project(self):
2004 "Test that reports with no jobs don't get sent"
2005 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
2006 'master', 'A')
2007 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2008 self.waitUntilSettled()
2009
2010 # Change wasn't reported to
2011 self.assertEqual(A.reported, False)
2012
2013 # Check queue is empty afterwards
2014 check_pipeline = self.sched.layout.pipelines['check']
2015 items = check_pipeline.getAllItems()
2016 self.assertEqual(len(items), 0)
2017
2018 self.assertEqual(len(self.history), 0)
2019
James E. Blair7d0dedc2013-02-21 17:26:09 -08002020 def test_zuul_refs(self):
2021 "Test that zuul refs exist and have the right changes"
James E. Blair08d19992016-08-10 15:25:31 -07002022 self.launch_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002023 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2024 M1.setMerged()
2025 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2026 M2.setMerged()
2027
2028 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2029 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2030 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2031 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07002032 A.addApproval('code-review', 2)
2033 B.addApproval('code-review', 2)
2034 C.addApproval('code-review', 2)
2035 D.addApproval('code-review', 2)
2036 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2037 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
2038 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
2039 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002040
2041 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002042 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002043 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002044 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002045 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002046 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002047 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002048 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002049 self.waitUntilSettled()
2050
James E. Blair7d0dedc2013-02-21 17:26:09 -08002051 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08002052 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002053 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002054 if x.parameters['ZUUL_CHANGE'] == '3':
2055 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002056 a_build = x
2057 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002058 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002059 b_build = x
2060 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002061 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002062 c_build = x
2063 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002064 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002065 d_build = x
2066 if a_build and b_build and c_build and d_build:
2067 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002068
2069 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002070 self.assertIsNotNone(a_zref)
2071 self.assertIsNotNone(b_zref)
2072 self.assertIsNotNone(c_zref)
2073 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002074
2075 # And they should all be different
2076 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002077 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002078
Clint Byrum3343e3e2016-11-15 16:05:03 -08002079 # should have a, not b, and should not be in project2
2080 self.assertTrue(a_build.hasChanges(A))
2081 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002082
Clint Byrum3343e3e2016-11-15 16:05:03 -08002083 # should have a and b, and should not be in project2
2084 self.assertTrue(b_build.hasChanges(A, B))
2085 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002086
Clint Byrum3343e3e2016-11-15 16:05:03 -08002087 # should have a and b in 1, c in 2
2088 self.assertTrue(c_build.hasChanges(A, B, C))
2089 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002090
Clint Byrum3343e3e2016-11-15 16:05:03 -08002091 # should have a and b in 1, c and d in 2
2092 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002093
James E. Blair08d19992016-08-10 15:25:31 -07002094 self.launch_server.hold_jobs_in_build = False
2095 self.launch_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002096 self.waitUntilSettled()
2097
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002098 self.assertEqual(A.data['status'], 'MERGED')
2099 self.assertEqual(A.reported, 2)
2100 self.assertEqual(B.data['status'], 'MERGED')
2101 self.assertEqual(B.reported, 2)
2102 self.assertEqual(C.data['status'], 'MERGED')
2103 self.assertEqual(C.reported, 2)
2104 self.assertEqual(D.data['status'], 'MERGED')
2105 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002106
James E. Blair4a28a882013-08-23 15:17:33 -07002107 def test_rerun_on_error(self):
2108 "Test that if a worker fails to run a job, it is run again"
James E. Blair08d19992016-08-10 15:25:31 -07002109 self.launch_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002110 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002111 A.addApproval('code-review', 2)
2112 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002113 self.waitUntilSettled()
2114
2115 self.builds[0].run_error = True
James E. Blair08d19992016-08-10 15:25:31 -07002116 self.launch_server.hold_jobs_in_build = False
2117 self.launch_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002118 self.waitUntilSettled()
2119 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2120 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2121
James E. Blair412e5582013-04-22 15:50:12 -07002122 def test_statsd(self):
2123 "Test each of the statsd methods used in the scheduler"
2124 import extras
2125 statsd = extras.try_import('statsd.statsd')
2126 statsd.incr('test-incr')
2127 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002128 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002129 self.assertReportedStat('test-incr', '1|c')
2130 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002131 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002132
James E. Blairec056492016-07-22 09:45:56 -07002133 @skip("Disabled for early v3 development")
James E. Blairdad52252014-02-07 16:59:17 -08002134 def test_stuck_job_cleanup(self):
2135 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07002136 # This job won't be registered at startup because it is not in
2137 # the standard layout, but we need it to already be registerd
2138 # for when we reconfigure, as that is when Zuul will attempt
2139 # to run the new job.
2140 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002141 self.gearman_server.hold_jobs_in_queue = True
2142 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002143 A.addApproval('code-review', 2)
2144 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002145 self.waitUntilSettled()
2146 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2147
James E. Blairf84026c2015-12-08 16:11:46 -08002148 self.updateConfigLayout(
2149 'tests/fixtures/layout-no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002150 self.sched.reconfigure(self.config)
2151 self.waitUntilSettled()
2152
James E. Blair18c64442014-03-18 10:14:45 -07002153 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002154 self.waitUntilSettled()
2155 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2156 self.assertTrue(self.sched._areAllBuildsComplete())
2157
2158 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002159 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002160 self.assertEqual(self.history[0].result, 'SUCCESS')
2161
James E. Blair879dafb2015-07-17 14:04:49 -07002162 def test_file_head(self):
2163 # This is a regression test for an observed bug. A change
2164 # with a file named "HEAD" in the root directory of the repo
2165 # was processed by a merger. It then was unable to reset the
2166 # repo because of:
2167 # GitCommandError: 'git reset --hard HEAD' returned
2168 # with exit code 128
2169 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2170 # and filename
2171 # Use '--' to separate filenames from revisions'
2172
2173 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002174 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002175 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2176
2177 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2178 self.waitUntilSettled()
2179
2180 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2181 self.waitUntilSettled()
2182
2183 self.assertIn('Build succeeded', A.messages[0])
2184 self.assertIn('Build succeeded', B.messages[0])
2185
James E. Blairec056492016-07-22 09:45:56 -07002186 @skip("Disabled for early v3 development")
James E. Blair70c71582013-03-06 08:50:50 -08002187 def test_file_jobs(self):
2188 "Test that file jobs run only when appropriate"
2189 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2190 A.addPatchset(['pip-requires'])
2191 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002192 A.addApproval('code-review', 2)
2193 B.addApproval('code-review', 2)
2194 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2195 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002196 self.waitUntilSettled()
2197
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002198 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002199 if x.name == 'project-testfile']
2200
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002201 self.assertEqual(len(testfile_jobs), 1)
2202 self.assertEqual(testfile_jobs[0].changes, '1,2')
2203 self.assertEqual(A.data['status'], 'MERGED')
2204 self.assertEqual(A.reported, 2)
2205 self.assertEqual(B.data['status'], 'MERGED')
2206 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002207
Clint Byrum3ee39f32016-11-17 23:45:07 -08002208 def _test_irrelevant_files_jobs(self, should_skip):
2209 "Test that jobs with irrelevant-files filter run only when appropriate"
2210 self.updateConfigLayout('layout-irrelevant-files')
Maru Newby3fe5f852015-01-13 04:22:14 +00002211 self.sched.reconfigure(self.config)
Clint Byrum3ee39f32016-11-17 23:45:07 -08002212
2213 if should_skip:
2214 files = {'ignoreme': 'ignored\n'}
2215 else:
2216 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002217
2218 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002219 'master',
2220 'test irrelevant-files',
2221 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002222 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2223 self.waitUntilSettled()
2224
2225 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002226 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002227
2228 if should_skip:
2229 self.assertEqual([], tested_change_ids)
2230 else:
2231 self.assertIn(change.data['number'], tested_change_ids)
2232
Clint Byrum3ee39f32016-11-17 23:45:07 -08002233 def test_irrelevant_files_match_skips_job(self):
2234 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002235
Clint Byrum3ee39f32016-11-17 23:45:07 -08002236 def test_irrelevant_files_no_match_runs_job(self):
2237 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002238
James E. Blairec056492016-07-22 09:45:56 -07002239 @skip("Disabled for early v3 development")
James E. Blair3c5e5b52013-04-26 11:17:03 -07002240 def test_test_config(self):
2241 "Test that we can test the config"
James E. Blairf84026c2015-12-08 16:11:46 -08002242 self.sched.testConfig(self.config.get('zuul', 'tenant_config'),
Joshua Hesketh352264b2015-08-11 23:42:08 +10002243 self.connections)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002244
James E. Blairec056492016-07-22 09:45:56 -07002245 @skip("Disabled for early v3 development")
James E. Blairc8a1e052014-02-25 09:29:26 -08002246 def test_queue_names(self):
2247 "Test shared change queue names"
2248 project1 = self.sched.layout.projects['org/project1']
2249 project2 = self.sched.layout.projects['org/project2']
2250 q1 = self.sched.layout.pipelines['gate'].getQueue(project1)
2251 q2 = self.sched.layout.pipelines['gate'].getQueue(project2)
2252 self.assertEqual(q1.name, 'integration')
2253 self.assertEqual(q2.name, 'integration')
2254
James E. Blairf84026c2015-12-08 16:11:46 -08002255 self.updateConfigLayout(
2256 'tests/fixtures/layout-bad-queue.yaml')
James E. Blairc8a1e052014-02-25 09:29:26 -08002257 with testtools.ExpectedException(
2258 Exception, "More than one name assigned to change queue"):
2259 self.sched.reconfigure(self.config)
2260
James E. Blair64ed6f22013-07-10 14:07:23 -07002261 def test_queue_precedence(self):
2262 "Test that queue precedence works"
2263
2264 self.gearman_server.hold_jobs_in_queue = True
James E. Blair08d19992016-08-10 15:25:31 -07002265 self.launch_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002266 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2267 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002268 A.addApproval('code-review', 2)
2269 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002270
2271 self.waitUntilSettled()
2272 self.gearman_server.hold_jobs_in_queue = False
2273 self.gearman_server.release()
2274 self.waitUntilSettled()
2275
James E. Blair8de58bd2013-07-18 16:23:33 -07002276 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002277 self.orderedRelease()
James E. Blair08d19992016-08-10 15:25:31 -07002278 self.launch_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002279 self.waitUntilSettled()
2280
James E. Blair64ed6f22013-07-10 14:07:23 -07002281 self.log.debug(self.history)
2282 self.assertEqual(self.history[0].pipeline, 'gate')
2283 self.assertEqual(self.history[1].pipeline, 'check')
2284 self.assertEqual(self.history[2].pipeline, 'gate')
2285 self.assertEqual(self.history[3].pipeline, 'gate')
2286 self.assertEqual(self.history[4].pipeline, 'check')
2287 self.assertEqual(self.history[5].pipeline, 'check')
2288
James E. Blairec056492016-07-22 09:45:56 -07002289 @skip("Disabled for early v3 development")
Clark Boylana5edbe42014-06-03 16:39:10 -07002290 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002291 "Test that we can retrieve JSON status info"
James E. Blair08d19992016-08-10 15:25:31 -07002292 self.launch_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002293 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002294 A.addApproval('code-review', 2)
2295 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002296 self.waitUntilSettled()
2297
James E. Blair08d19992016-08-10 15:25:31 -07002298 self.launch_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002299 self.waitUntilSettled()
2300
James E. Blair1843a552013-07-03 14:19:52 -07002301 port = self.webapp.server.socket.getsockname()[1]
2302
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002303 req = urllib.request.Request("http://localhost:%s/status.json" % port)
2304 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002305 headers = f.info()
2306 self.assertIn('Content-Length', headers)
2307 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002308 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2309 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002310 self.assertIn('Access-Control-Allow-Origin', headers)
2311 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002312 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002313 self.assertIn('Expires', headers)
James E. Blair1843a552013-07-03 14:19:52 -07002314 data = f.read()
2315
James E. Blair08d19992016-08-10 15:25:31 -07002316 self.launch_server.hold_jobs_in_build = False
2317 self.launch_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002318 self.waitUntilSettled()
2319
2320 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002321 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002322 for p in data['pipelines']:
2323 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002324 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002325 self.assertEqual(q['window'], 20)
2326 else:
2327 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002328 for head in q['heads']:
2329 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002330 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002331 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002332 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002333 status_jobs.append(job)
2334 self.assertEqual('project-merge', status_jobs[0]['name'])
2335 self.assertEqual('https://server/job/project-merge/0/',
2336 status_jobs[0]['url'])
2337 self.assertEqual('http://logs.example.com/1/1/gate/project-merge/0',
2338 status_jobs[0]['report_url'])
2339
2340 self.assertEqual('project-test1', status_jobs[1]['name'])
2341 self.assertEqual('https://server/job/project-test1/1/',
2342 status_jobs[1]['url'])
2343 self.assertEqual('http://logs.example.com/1/1/gate/project-test1/1',
2344 status_jobs[1]['report_url'])
2345
2346 self.assertEqual('project-test2', status_jobs[2]['name'])
2347 self.assertEqual('https://server/job/project-test2/2/',
2348 status_jobs[2]['url'])
2349 self.assertEqual('http://logs.example.com/1/1/gate/project-test2/2',
2350 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002351
James E. Blairec056492016-07-22 09:45:56 -07002352 @skip("Disabled for early v3 development")
James E. Blairc3d428e2013-12-03 15:06:48 -08002353 def test_merging_queues(self):
2354 "Test that transitively-connected change queues are merged"
James E. Blairf84026c2015-12-08 16:11:46 -08002355 self.updateConfigLayout(
2356 'tests/fixtures/layout-merge-queues.yaml')
James E. Blairc3d428e2013-12-03 15:06:48 -08002357 self.sched.reconfigure(self.config)
2358 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
2359
James E. Blairec056492016-07-22 09:45:56 -07002360 @skip("Disabled for early v3 development")
James E. Blairaf17a972016-02-03 15:07:18 -08002361 def test_mutex(self):
2362 "Test job mutexes"
2363 self.config.set('zuul', 'layout_config',
2364 'tests/fixtures/layout-mutex.yaml')
2365 self.sched.reconfigure(self.config)
2366
James E. Blair08d19992016-08-10 15:25:31 -07002367 self.launch_server.hold_jobs_in_build = True
James E. Blairaf17a972016-02-03 15:07:18 -08002368 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2369 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2370 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2371
2372 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2373 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2374 self.waitUntilSettled()
2375 self.assertEqual(len(self.builds), 3)
2376 self.assertEqual(self.builds[0].name, 'project-test1')
2377 self.assertEqual(self.builds[1].name, 'mutex-one')
2378 self.assertEqual(self.builds[2].name, 'project-test1')
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), 3)
2393 self.assertEqual(self.builds[0].name, 'project-test1')
2394 self.assertEqual(self.builds[1].name, 'project-test1')
2395 self.assertEqual(self.builds[2].name, 'mutex-one')
2396 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2397
James E. Blair08d19992016-08-10 15:25:31 -07002398 self.launch_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002399 self.waitUntilSettled()
2400
2401 self.assertEqual(len(self.builds), 3)
2402 self.assertEqual(self.builds[0].name, 'project-test1')
2403 self.assertEqual(self.builds[1].name, 'project-test1')
2404 self.assertEqual(self.builds[2].name, 'mutex-two')
2405 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2406
James E. Blair08d19992016-08-10 15:25:31 -07002407 self.launch_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002408 self.waitUntilSettled()
2409
2410 self.assertEqual(len(self.builds), 2)
2411 self.assertEqual(self.builds[0].name, 'project-test1')
2412 self.assertEqual(self.builds[1].name, 'project-test1')
2413 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2414
James E. Blair08d19992016-08-10 15:25:31 -07002415 self.launch_server.hold_jobs_in_build = False
2416 self.launch_server.release()
James E. Blairaf17a972016-02-03 15:07:18 -08002417
2418 self.waitUntilSettled()
2419 self.assertEqual(len(self.builds), 0)
2420
2421 self.assertEqual(A.reported, 1)
2422 self.assertEqual(B.reported, 1)
2423 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2424
James E. Blairec056492016-07-22 09:45:56 -07002425 @skip("Disabled for early v3 development")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002426 def test_node_label(self):
2427 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002428 self.worker.registerFunction('build:node-project-test1:debian')
2429
2430 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002431 A.addApproval('code-review', 2)
2432 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002433 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002434
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002435 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2436 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2437 'debian')
2438 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002439
2440 def test_live_reconfiguration(self):
2441 "Test that live reconfiguration works"
James E. Blair08d19992016-08-10 15:25:31 -07002442 self.launch_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002443 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002444 A.addApproval('code-review', 2)
2445 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002446 self.waitUntilSettled()
2447
2448 self.sched.reconfigure(self.config)
2449
James E. Blair08d19992016-08-10 15:25:31 -07002450 self.launch_server.hold_jobs_in_build = False
2451 self.launch_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002452 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002453 self.assertEqual(self.getJobFromHistory('project-merge').result,
2454 'SUCCESS')
2455 self.assertEqual(self.getJobFromHistory('project-test1').result,
2456 'SUCCESS')
2457 self.assertEqual(self.getJobFromHistory('project-test2').result,
2458 'SUCCESS')
2459 self.assertEqual(A.data['status'], 'MERGED')
2460 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002461
James E. Blairec056492016-07-22 09:45:56 -07002462 @skip("Disabled for early v3 development")
James E. Blair6bc782d2015-07-17 16:20:21 -07002463 def test_live_reconfiguration_merge_conflict(self):
2464 # A real-world bug: a change in a gate queue has a merge
2465 # conflict and a job is added to its project while it's
2466 # sitting in the queue. The job gets added to the change and
2467 # enqueued and the change gets stuck.
2468 self.worker.registerFunction('build:project-test3')
James E. Blair08d19992016-08-10 15:25:31 -07002469 self.launch_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002470
2471 # This change is fine. It's here to stop the queue long
2472 # enough for the next change to be subject to the
2473 # reconfiguration, as well as to provide a conflict for the
2474 # next change. This change will succeed and merge.
2475 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2476 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002477 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002478
2479 # This change will be in merge conflict. During the
2480 # reconfiguration, we will add a job. We want to make sure
2481 # that doesn't cause it to get stuck.
2482 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2483 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002484 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002485
James E. Blair8b5408c2016-08-08 15:37:46 -07002486 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2487 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002488
2489 self.waitUntilSettled()
2490
2491 # No jobs have run yet
2492 self.assertEqual(A.data['status'], 'NEW')
2493 self.assertEqual(A.reported, 1)
2494 self.assertEqual(B.data['status'], 'NEW')
2495 self.assertEqual(B.reported, 1)
2496 self.assertEqual(len(self.history), 0)
2497
2498 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002499 self.updateConfigLayout(
2500 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002501 self.sched.reconfigure(self.config)
2502 self.waitUntilSettled()
2503
James E. Blair08d19992016-08-10 15:25:31 -07002504 self.launch_server.hold_jobs_in_build = False
2505 self.launch_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002506 self.waitUntilSettled()
2507
2508 self.assertEqual(A.data['status'], 'MERGED')
2509 self.assertEqual(A.reported, 2)
2510 self.assertEqual(B.data['status'], 'NEW')
2511 self.assertEqual(B.reported, 2)
2512 self.assertEqual(self.getJobFromHistory('project-merge').result,
2513 'SUCCESS')
2514 self.assertEqual(self.getJobFromHistory('project-test1').result,
2515 'SUCCESS')
2516 self.assertEqual(self.getJobFromHistory('project-test2').result,
2517 'SUCCESS')
2518 self.assertEqual(self.getJobFromHistory('project-test3').result,
2519 'SUCCESS')
2520 self.assertEqual(len(self.history), 4)
2521
James E. Blairec056492016-07-22 09:45:56 -07002522 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002523 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002524 # An extrapolation of test_live_reconfiguration_merge_conflict
2525 # that tests a job added to a job tree with a failed root does
2526 # not run.
2527 self.worker.registerFunction('build:project-test3')
James E. Blair08d19992016-08-10 15:25:31 -07002528 self.launch_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002529
2530 # This change is fine. It's here to stop the queue long
2531 # enough for the next change to be subject to the
2532 # reconfiguration. This change will succeed and merge.
2533 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2534 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002535 A.addApproval('code-review', 2)
2536 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002537 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002538 self.launch_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002539 self.waitUntilSettled()
2540
2541 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair08d19992016-08-10 15:25:31 -07002542 self.launch_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002543 B.addApproval('code-review', 2)
2544 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002545 self.waitUntilSettled()
2546
James E. Blair08d19992016-08-10 15:25:31 -07002547 self.launch_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002548 self.waitUntilSettled()
2549
2550 # Both -merge jobs have run, but no others.
2551 self.assertEqual(A.data['status'], 'NEW')
2552 self.assertEqual(A.reported, 1)
2553 self.assertEqual(B.data['status'], 'NEW')
2554 self.assertEqual(B.reported, 1)
2555 self.assertEqual(self.history[0].result, 'SUCCESS')
2556 self.assertEqual(self.history[0].name, 'project-merge')
2557 self.assertEqual(self.history[1].result, 'FAILURE')
2558 self.assertEqual(self.history[1].name, 'project-merge')
2559 self.assertEqual(len(self.history), 2)
2560
2561 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002562 self.updateConfigLayout(
2563 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002564 self.sched.reconfigure(self.config)
2565 self.waitUntilSettled()
2566
James E. Blair08d19992016-08-10 15:25:31 -07002567 self.launch_server.hold_jobs_in_build = False
2568 self.launch_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002569 self.waitUntilSettled()
2570
2571 self.assertEqual(A.data['status'], 'MERGED')
2572 self.assertEqual(A.reported, 2)
2573 self.assertEqual(B.data['status'], 'NEW')
2574 self.assertEqual(B.reported, 2)
2575 self.assertEqual(self.history[0].result, 'SUCCESS')
2576 self.assertEqual(self.history[0].name, 'project-merge')
2577 self.assertEqual(self.history[1].result, 'FAILURE')
2578 self.assertEqual(self.history[1].name, 'project-merge')
2579 self.assertEqual(self.history[2].result, 'SUCCESS')
2580 self.assertEqual(self.history[3].result, 'SUCCESS')
2581 self.assertEqual(self.history[4].result, 'SUCCESS')
2582 self.assertEqual(len(self.history), 5)
2583
James E. Blairec056492016-07-22 09:45:56 -07002584 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002585 def test_live_reconfiguration_failed_job(self):
2586 # Test that a change with a removed failing job does not
2587 # disrupt reconfiguration. If a change has a failed job and
2588 # that job is removed during a reconfiguration, we observed a
2589 # bug where the code to re-set build statuses would run on
2590 # that build and raise an exception because the job no longer
2591 # existed.
James E. Blair08d19992016-08-10 15:25:31 -07002592 self.launch_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002593
2594 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2595
2596 # This change will fail and later be removed by the reconfiguration.
James E. Blair08d19992016-08-10 15:25:31 -07002597 self.launch_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002598
2599 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2600 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002601 self.launch_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002602 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002603 self.launch_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002604 self.waitUntilSettled()
2605
2606 self.assertEqual(A.data['status'], 'NEW')
2607 self.assertEqual(A.reported, 0)
2608
2609 self.assertEqual(self.getJobFromHistory('project-merge').result,
2610 'SUCCESS')
2611 self.assertEqual(self.getJobFromHistory('project-test1').result,
2612 'FAILURE')
2613 self.assertEqual(len(self.history), 2)
2614
2615 # Remove the test1 job.
James E. Blairf84026c2015-12-08 16:11:46 -08002616 self.updateConfigLayout(
2617 'tests/fixtures/layout-live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002618 self.sched.reconfigure(self.config)
2619 self.waitUntilSettled()
2620
James E. Blair08d19992016-08-10 15:25:31 -07002621 self.launch_server.hold_jobs_in_build = False
2622 self.launch_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002623 self.waitUntilSettled()
2624
2625 self.assertEqual(self.getJobFromHistory('project-test2').result,
2626 'SUCCESS')
2627 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2628 'SUCCESS')
2629 self.assertEqual(len(self.history), 4)
2630
2631 self.assertEqual(A.data['status'], 'NEW')
2632 self.assertEqual(A.reported, 1)
2633 self.assertIn('Build succeeded', A.messages[0])
2634 # Ensure the removed job was not included in the report.
2635 self.assertNotIn('project-test1', A.messages[0])
2636
James E. Blairec056492016-07-22 09:45:56 -07002637 @skip("Disabled for early v3 development")
James E. Blairfe707d12015-08-05 15:18:15 -07002638 def test_live_reconfiguration_shared_queue(self):
2639 # Test that a change with a failing job which was removed from
2640 # this project but otherwise still exists in the system does
2641 # not disrupt reconfiguration.
2642
James E. Blair08d19992016-08-10 15:25:31 -07002643 self.launch_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002644
2645 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2646
James E. Blair08d19992016-08-10 15:25:31 -07002647 self.launch_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002648
2649 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2650 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002651 self.launch_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002652 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002653 self.launch_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002654 self.waitUntilSettled()
2655
2656 self.assertEqual(A.data['status'], 'NEW')
2657 self.assertEqual(A.reported, 0)
2658
2659 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2660 'SUCCESS')
2661 self.assertEqual(self.getJobFromHistory(
2662 'project1-project2-integration').result, 'FAILURE')
2663 self.assertEqual(len(self.history), 2)
2664
2665 # Remove the integration job.
James E. Blairf84026c2015-12-08 16:11:46 -08002666 self.updateConfigLayout(
2667 'tests/fixtures/layout-live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002668 self.sched.reconfigure(self.config)
2669 self.waitUntilSettled()
2670
James E. Blair08d19992016-08-10 15:25:31 -07002671 self.launch_server.hold_jobs_in_build = False
2672 self.launch_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002673 self.waitUntilSettled()
2674
2675 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2676 'SUCCESS')
2677 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2678 'SUCCESS')
2679 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2680 'SUCCESS')
2681 self.assertEqual(self.getJobFromHistory(
2682 'project1-project2-integration').result, 'FAILURE')
2683 self.assertEqual(len(self.history), 4)
2684
2685 self.assertEqual(A.data['status'], 'NEW')
2686 self.assertEqual(A.reported, 1)
2687 self.assertIn('Build succeeded', A.messages[0])
2688 # Ensure the removed job was not included in the report.
2689 self.assertNotIn('project1-project2-integration', A.messages[0])
2690
James E. Blairec056492016-07-22 09:45:56 -07002691 @skip("Disabled for early v3 development")
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002692 def test_double_live_reconfiguration_shared_queue(self):
2693 # This was a real-world regression. A change is added to
2694 # gate; a reconfigure happens, a second change which depends
2695 # on the first is added, and a second reconfiguration happens.
2696 # Ensure that both changes merge.
2697
2698 # A failure may indicate incorrect caching or cleaning up of
2699 # references during a reconfiguration.
James E. Blair08d19992016-08-10 15:25:31 -07002700 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002701
2702 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2703 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2704 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002705 A.addApproval('code-review', 2)
2706 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002707
2708 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002709 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002710 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002711 self.launch_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002712 self.waitUntilSettled()
2713
2714 # Reconfigure (with only one change in the pipeline).
2715 self.sched.reconfigure(self.config)
2716 self.waitUntilSettled()
2717
2718 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002719 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002720 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002721 self.launch_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002722 self.waitUntilSettled()
2723
2724 # Reconfigure (with both in the pipeline).
2725 self.sched.reconfigure(self.config)
2726 self.waitUntilSettled()
2727
James E. Blair08d19992016-08-10 15:25:31 -07002728 self.launch_server.hold_jobs_in_build = False
2729 self.launch_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002730 self.waitUntilSettled()
2731
2732 self.assertEqual(len(self.history), 8)
2733
2734 self.assertEqual(A.data['status'], 'MERGED')
2735 self.assertEqual(A.reported, 2)
2736 self.assertEqual(B.data['status'], 'MERGED')
2737 self.assertEqual(B.reported, 2)
2738
James E. Blairec056492016-07-22 09:45:56 -07002739 @skip("Disabled for early v3 development")
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002740 def test_live_reconfiguration_del_project(self):
2741 # Test project deletion from layout
2742 # while changes are enqueued
2743
James E. Blair08d19992016-08-10 15:25:31 -07002744 self.launch_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002745 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2746 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2747 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2748
2749 # A Depends-On: B
2750 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2751 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002752 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002753
2754 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2755 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2756 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002757 self.launch_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002758 self.waitUntilSettled()
2759 self.assertEqual(len(self.builds), 5)
2760
2761 # This layout defines only org/project, not org/project1
James E. Blairf84026c2015-12-08 16:11:46 -08002762 self.updateConfigLayout(
2763 'tests/fixtures/layout-live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002764 self.sched.reconfigure(self.config)
2765 self.waitUntilSettled()
2766
2767 # Builds for C aborted, builds for A succeed,
2768 # and have change B applied ahead
2769 job_c = self.getJobFromHistory('project1-test1')
2770 self.assertEqual(job_c.changes, '3,1')
2771 self.assertEqual(job_c.result, 'ABORTED')
2772
James E. Blair08d19992016-08-10 15:25:31 -07002773 self.launch_server.hold_jobs_in_build = False
2774 self.launch_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002775 self.waitUntilSettled()
2776
2777 self.assertEqual(self.getJobFromHistory('project-test1').changes,
2778 '2,1 1,1')
2779
2780 self.assertEqual(A.data['status'], 'NEW')
2781 self.assertEqual(B.data['status'], 'NEW')
2782 self.assertEqual(C.data['status'], 'NEW')
2783 self.assertEqual(A.reported, 1)
2784 self.assertEqual(B.reported, 0)
2785 self.assertEqual(C.reported, 0)
2786
2787 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
2788 self.assertIn('Build succeeded', A.messages[0])
2789
James E. Blairec056492016-07-22 09:45:56 -07002790 @skip("Disabled for early v3 development")
James E. Blaire712d9f2013-07-31 11:40:11 -07002791 def test_live_reconfiguration_functions(self):
2792 "Test live reconfiguration with a custom function"
2793 self.worker.registerFunction('build:node-project-test1:debian')
2794 self.worker.registerFunction('build:node-project-test1:wheezy')
2795 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002796 A.addApproval('code-review', 2)
2797 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002798 self.waitUntilSettled()
2799
2800 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2801 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2802 'debian')
2803 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2804
James E. Blairf84026c2015-12-08 16:11:46 -08002805 self.updateConfigLayout(
2806 'tests/fixtures/layout-live-reconfiguration-functions.yaml')
James E. Blaire712d9f2013-07-31 11:40:11 -07002807 self.sched.reconfigure(self.config)
2808 self.worker.build_history = []
2809
2810 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002811 B.addApproval('code-review', 2)
2812 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002813 self.waitUntilSettled()
2814
2815 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2816 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2817 'wheezy')
2818 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2819
James E. Blairec056492016-07-22 09:45:56 -07002820 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002821 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002822 self.updateConfigLayout(
2823 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002824 self.sched.reconfigure(self.config)
2825
2826 self.init_repo("org/new-project")
2827 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2828
James E. Blair8b5408c2016-08-08 15:37:46 -07002829 A.addApproval('code-review', 2)
2830 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002831 self.waitUntilSettled()
2832 self.assertEqual(self.getJobFromHistory('project-merge').result,
2833 'SUCCESS')
2834 self.assertEqual(self.getJobFromHistory('project-test1').result,
2835 'SUCCESS')
2836 self.assertEqual(self.getJobFromHistory('project-test2').result,
2837 'SUCCESS')
2838 self.assertEqual(A.data['status'], 'MERGED')
2839 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002840
Clark Boylan6dbbc482013-10-18 10:57:31 -07002841 def test_repo_deleted(self):
Paul Belanger66e95962016-11-11 12:11:06 -05002842 self.updateConfigLayout('layout-repo-deleted')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002843 self.sched.reconfigure(self.config)
2844
2845 self.init_repo("org/delete-project")
2846 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2847
James E. Blair8b5408c2016-08-08 15:37:46 -07002848 A.addApproval('code-review', 2)
2849 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002850 self.waitUntilSettled()
2851 self.assertEqual(self.getJobFromHistory('project-merge').result,
2852 'SUCCESS')
2853 self.assertEqual(self.getJobFromHistory('project-test1').result,
2854 'SUCCESS')
2855 self.assertEqual(self.getJobFromHistory('project-test2').result,
2856 'SUCCESS')
2857 self.assertEqual(A.data['status'], 'MERGED')
2858 self.assertEqual(A.reported, 2)
2859
2860 # Delete org/new-project zuul repo. Should be recloned.
2861 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
2862
2863 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2864
James E. Blair8b5408c2016-08-08 15:37:46 -07002865 B.addApproval('code-review', 2)
2866 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002867 self.waitUntilSettled()
2868 self.assertEqual(self.getJobFromHistory('project-merge').result,
2869 'SUCCESS')
2870 self.assertEqual(self.getJobFromHistory('project-test1').result,
2871 'SUCCESS')
2872 self.assertEqual(self.getJobFromHistory('project-test2').result,
2873 'SUCCESS')
2874 self.assertEqual(B.data['status'], 'MERGED')
2875 self.assertEqual(B.reported, 2)
2876
James E. Blairec056492016-07-22 09:45:56 -07002877 @skip("Disabled for early v3 development")
James E. Blair456f2fb2016-02-09 09:29:33 -08002878 def test_tags(self):
2879 "Test job tags"
2880 self.config.set('zuul', 'layout_config',
2881 'tests/fixtures/layout-tags.yaml')
2882 self.sched.reconfigure(self.config)
2883
2884 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2885 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2886 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2887 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2888 self.waitUntilSettled()
2889
2890 results = {'project1-merge': 'extratag merge project1',
2891 'project2-merge': 'merge'}
2892
2893 for build in self.history:
2894 self.assertEqual(results.get(build.name, ''),
2895 build.parameters.get('BUILD_TAGS'))
2896
James E. Blairec056492016-07-22 09:45:56 -07002897 @skip("Disabled for early v3 development")
James E. Blair63bb0ef2013-07-29 17:14:51 -07002898 def test_timer(self):
2899 "Test that a periodic job is triggered"
James E. Blair08d19992016-08-10 15:25:31 -07002900 self.launch_server.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08002901 self.updateConfigLayout(
2902 'tests/fixtures/layout-timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002903 self.sched.reconfigure(self.config)
2904 self.registerJobs()
2905
Clark Boylan3ee090a2014-04-03 20:55:09 -07002906 # The pipeline triggers every second, so we should have seen
2907 # several by now.
2908 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002909 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002910
2911 self.assertEqual(len(self.builds), 2)
2912
James E. Blair63bb0ef2013-07-29 17:14:51 -07002913 port = self.webapp.server.socket.getsockname()[1]
2914
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002915 req = urllib.request.Request("http://localhost:%s/status.json" % port)
2916 f = urllib.request.urlopen(req)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002917 data = f.read()
2918
James E. Blair08d19992016-08-10 15:25:31 -07002919 self.launch_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002920 # Stop queuing timer triggered jobs so that the assertions
2921 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002922 self.updateConfigLayout(
2923 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002924 self.sched.reconfigure(self.config)
2925 self.registerJobs()
James E. Blair08d19992016-08-10 15:25:31 -07002926 self.launch_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002927 self.waitUntilSettled()
2928
2929 self.assertEqual(self.getJobFromHistory(
2930 'project-bitrot-stable-old').result, 'SUCCESS')
2931 self.assertEqual(self.getJobFromHistory(
2932 'project-bitrot-stable-older').result, 'SUCCESS')
2933
2934 data = json.loads(data)
2935 status_jobs = set()
2936 for p in data['pipelines']:
2937 for q in p['change_queues']:
2938 for head in q['heads']:
2939 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07002940 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002941 for job in change['jobs']:
2942 status_jobs.add(job['name'])
2943 self.assertIn('project-bitrot-stable-old', status_jobs)
2944 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002945
James E. Blairec056492016-07-22 09:45:56 -07002946 @skip("Disabled for early v3 development")
James E. Blair4f6033c2014-03-27 15:49:09 -07002947 def test_idle(self):
2948 "Test that frequent periodic jobs work"
James E. Blair08d19992016-08-10 15:25:31 -07002949 self.launch_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002950
Clark Boylan3ee090a2014-04-03 20:55:09 -07002951 for x in range(1, 3):
2952 # Test that timer triggers periodic jobs even across
2953 # layout config reloads.
2954 # Start timer trigger
James E. Blairf84026c2015-12-08 16:11:46 -08002955 self.updateConfigLayout(
2956 'tests/fixtures/layout-idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002957 self.sched.reconfigure(self.config)
2958 self.registerJobs()
James E. Blair995fc0f2016-02-04 16:48:31 -08002959 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002960
Clark Boylan3ee090a2014-04-03 20:55:09 -07002961 # The pipeline triggers every second, so we should have seen
2962 # several by now.
2963 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002964
2965 # Stop queuing timer triggered jobs so that the assertions
2966 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002967 self.updateConfigLayout(
2968 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002969 self.sched.reconfigure(self.config)
2970 self.registerJobs()
James E. Blair995fc0f2016-02-04 16:48:31 -08002971 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002972
2973 self.assertEqual(len(self.builds), 2)
James E. Blair08d19992016-08-10 15:25:31 -07002974 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002975 self.waitUntilSettled()
2976 self.assertEqual(len(self.builds), 0)
2977 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002978
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002979 def test_check_smtp_pool(self):
Paul Belanger01be8ca2016-11-11 12:21:06 -05002980 self.updateConfigLayout('layout-smtp')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002981 self.sched.reconfigure(self.config)
2982
2983 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2984 self.waitUntilSettled()
2985
2986 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2987 self.waitUntilSettled()
2988
James E. Blairff80a2f2013-12-27 13:24:06 -08002989 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002990
2991 # A.messages only holds what FakeGerrit places in it. Thus we
2992 # work on the knowledge of what the first message should be as
2993 # it is only configured to go to SMTP.
2994
2995 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002996 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002997 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002998 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002999 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003000 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003001
3002 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003003 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003004 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003005 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003006 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003007 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003008
James E. Blairec056492016-07-22 09:45:56 -07003009 @skip("Disabled for early v3 development")
James E. Blaire5910202013-12-27 09:50:31 -08003010 def test_timer_smtp(self):
3011 "Test that a periodic job is triggered"
James E. Blair08d19992016-08-10 15:25:31 -07003012 self.launch_server.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08003013 self.updateConfigLayout(
3014 'tests/fixtures/layout-timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08003015 self.sched.reconfigure(self.config)
3016 self.registerJobs()
3017
Clark Boylan3ee090a2014-04-03 20:55:09 -07003018 # The pipeline triggers every second, so we should have seen
3019 # several by now.
3020 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003021 self.waitUntilSettled()
3022
Clark Boylan3ee090a2014-04-03 20:55:09 -07003023 self.assertEqual(len(self.builds), 2)
James E. Blair08d19992016-08-10 15:25:31 -07003024 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003025 self.waitUntilSettled()
3026 self.assertEqual(len(self.history), 2)
3027
James E. Blaire5910202013-12-27 09:50:31 -08003028 self.assertEqual(self.getJobFromHistory(
3029 'project-bitrot-stable-old').result, 'SUCCESS')
3030 self.assertEqual(self.getJobFromHistory(
3031 'project-bitrot-stable-older').result, 'SUCCESS')
3032
James E. Blairff80a2f2013-12-27 13:24:06 -08003033 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003034
3035 # A.messages only holds what FakeGerrit places in it. Thus we
3036 # work on the knowledge of what the first message should be as
3037 # it is only configured to go to SMTP.
3038
3039 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003040 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003041 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003042 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003043 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003044 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003045
Clark Boylan3ee090a2014-04-03 20:55:09 -07003046 # Stop queuing timer triggered jobs and let any that may have
3047 # queued through so that end of test assertions pass.
James E. Blairf84026c2015-12-08 16:11:46 -08003048 self.updateConfigLayout(
3049 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003050 self.sched.reconfigure(self.config)
3051 self.registerJobs()
James E. Blairf8058972014-08-15 16:09:16 -07003052 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003053 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003054 self.waitUntilSettled()
3055
James E. Blair91e34592015-07-31 16:45:59 -07003056 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003057 "Test that the RPC client can enqueue a change"
3058 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003059 A.addApproval('code-review', 2)
3060 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003061
3062 client = zuul.rpcclient.RPCClient('127.0.0.1',
3063 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003064 r = client.enqueue(tenant='tenant-one',
3065 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003066 project='org/project',
3067 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003068 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003069 self.waitUntilSettled()
3070 self.assertEqual(self.getJobFromHistory('project-merge').result,
3071 'SUCCESS')
3072 self.assertEqual(self.getJobFromHistory('project-test1').result,
3073 'SUCCESS')
3074 self.assertEqual(self.getJobFromHistory('project-test2').result,
3075 'SUCCESS')
3076 self.assertEqual(A.data['status'], 'MERGED')
3077 self.assertEqual(A.reported, 2)
3078 self.assertEqual(r, True)
3079
James E. Blairec056492016-07-22 09:45:56 -07003080 @skip("Disabled for early v3 development")
James E. Blair91e34592015-07-31 16:45:59 -07003081 def test_client_enqueue_ref(self):
3082 "Test that the RPC client can enqueue a ref"
3083
3084 client = zuul.rpcclient.RPCClient('127.0.0.1',
3085 self.gearman_server.port)
3086 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003087 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003088 pipeline='post',
3089 project='org/project',
3090 trigger='gerrit',
3091 ref='master',
3092 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
3093 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
3094 self.waitUntilSettled()
3095 job_names = [x.name for x in self.history]
3096 self.assertEqual(len(self.history), 1)
3097 self.assertIn('project-post', job_names)
3098 self.assertEqual(r, True)
3099
James E. Blairad28e912013-11-27 10:43:22 -08003100 def test_client_enqueue_negative(self):
3101 "Test that the RPC client returns errors"
3102 client = zuul.rpcclient.RPCClient('127.0.0.1',
3103 self.gearman_server.port)
3104 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003105 "Invalid tenant"):
3106 r = client.enqueue(tenant='tenant-foo',
3107 pipeline='gate',
3108 project='org/project',
3109 trigger='gerrit',
3110 change='1,1')
3111 client.shutdown()
3112 self.assertEqual(r, False)
3113
3114 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003115 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003116 r = client.enqueue(tenant='tenant-one',
3117 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003118 project='project-does-not-exist',
3119 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003120 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003121 client.shutdown()
3122 self.assertEqual(r, False)
3123
3124 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3125 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003126 r = client.enqueue(tenant='tenant-one',
3127 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003128 project='org/project',
3129 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003130 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003131 client.shutdown()
3132 self.assertEqual(r, False)
3133
3134 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3135 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003136 r = client.enqueue(tenant='tenant-one',
3137 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003138 project='org/project',
3139 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003140 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003141 client.shutdown()
3142 self.assertEqual(r, False)
3143
3144 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3145 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003146 r = client.enqueue(tenant='tenant-one',
3147 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003148 project='org/project',
3149 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003150 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003151 client.shutdown()
3152 self.assertEqual(r, False)
3153
3154 self.waitUntilSettled()
3155 self.assertEqual(len(self.history), 0)
3156 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003157
3158 def test_client_promote(self):
3159 "Test that the RPC client can promote a change"
James E. Blair08d19992016-08-10 15:25:31 -07003160 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003161 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3162 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3163 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003164 A.addApproval('code-review', 2)
3165 B.addApproval('code-review', 2)
3166 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003167
James E. Blair8b5408c2016-08-08 15:37:46 -07003168 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3169 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3170 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003171
3172 self.waitUntilSettled()
3173
Paul Belangerbaca3132016-11-04 12:49:54 -04003174 tenant = self.sched.abide.tenants.get('tenant-one')
3175 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003176 enqueue_times = {}
3177 for item in items:
3178 enqueue_times[str(item.change)] = item.enqueue_time
3179
James E. Blair36658cf2013-12-06 17:53:48 -08003180 client = zuul.rpcclient.RPCClient('127.0.0.1',
3181 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003182 r = client.promote(tenant='tenant-one',
3183 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003184 change_ids=['2,1', '3,1'])
3185
Sean Daguef39b9ca2014-01-10 21:34:35 -05003186 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003187 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003188 for item in items:
3189 self.assertEqual(
3190 enqueue_times[str(item.change)], item.enqueue_time)
3191
James E. Blair78acec92014-02-06 07:11:32 -08003192 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003193 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003194 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003195 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003196 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003197 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003198 self.waitUntilSettled()
3199
3200 self.assertEqual(len(self.builds), 6)
3201 self.assertEqual(self.builds[0].name, 'project-test1')
3202 self.assertEqual(self.builds[1].name, 'project-test2')
3203 self.assertEqual(self.builds[2].name, 'project-test1')
3204 self.assertEqual(self.builds[3].name, 'project-test2')
3205 self.assertEqual(self.builds[4].name, 'project-test1')
3206 self.assertEqual(self.builds[5].name, 'project-test2')
3207
Paul Belangerbaca3132016-11-04 12:49:54 -04003208 self.assertTrue(self.builds[0].hasChanges(B))
3209 self.assertFalse(self.builds[0].hasChanges(A))
3210 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003211
Paul Belangerbaca3132016-11-04 12:49:54 -04003212 self.assertTrue(self.builds[2].hasChanges(B))
3213 self.assertTrue(self.builds[2].hasChanges(C))
3214 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003215
Paul Belangerbaca3132016-11-04 12:49:54 -04003216 self.assertTrue(self.builds[4].hasChanges(B))
3217 self.assertTrue(self.builds[4].hasChanges(C))
3218 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003219
James E. Blair08d19992016-08-10 15:25:31 -07003220 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003221 self.waitUntilSettled()
3222
3223 self.assertEqual(A.data['status'], 'MERGED')
3224 self.assertEqual(A.reported, 2)
3225 self.assertEqual(B.data['status'], 'MERGED')
3226 self.assertEqual(B.reported, 2)
3227 self.assertEqual(C.data['status'], 'MERGED')
3228 self.assertEqual(C.reported, 2)
3229
3230 client.shutdown()
3231 self.assertEqual(r, True)
3232
3233 def test_client_promote_dependent(self):
3234 "Test that the RPC client can promote a dependent change"
3235 # C (depends on B) -> B -> A ; then promote C to get:
3236 # A -> C (depends on B) -> B
James E. Blair08d19992016-08-10 15:25:31 -07003237 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003238 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3239 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3240 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3241
3242 C.setDependsOn(B, 1)
3243
James E. Blair8b5408c2016-08-08 15:37:46 -07003244 A.addApproval('code-review', 2)
3245 B.addApproval('code-review', 2)
3246 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003247
James E. Blair8b5408c2016-08-08 15:37:46 -07003248 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3249 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3250 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003251
3252 self.waitUntilSettled()
3253
3254 client = zuul.rpcclient.RPCClient('127.0.0.1',
3255 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003256 r = client.promote(tenant='tenant-one',
3257 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003258 change_ids=['3,1'])
3259
James E. Blair78acec92014-02-06 07:11:32 -08003260 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003261 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003262 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003263 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003264 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003265 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003266 self.waitUntilSettled()
3267
3268 self.assertEqual(len(self.builds), 6)
3269 self.assertEqual(self.builds[0].name, 'project-test1')
3270 self.assertEqual(self.builds[1].name, 'project-test2')
3271 self.assertEqual(self.builds[2].name, 'project-test1')
3272 self.assertEqual(self.builds[3].name, 'project-test2')
3273 self.assertEqual(self.builds[4].name, 'project-test1')
3274 self.assertEqual(self.builds[5].name, 'project-test2')
3275
Paul Belangerbaca3132016-11-04 12:49:54 -04003276 self.assertTrue(self.builds[0].hasChanges(B))
3277 self.assertFalse(self.builds[0].hasChanges(A))
3278 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003279
Paul Belangerbaca3132016-11-04 12:49:54 -04003280 self.assertTrue(self.builds[2].hasChanges(B))
3281 self.assertTrue(self.builds[2].hasChanges(C))
3282 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003283
Paul Belangerbaca3132016-11-04 12:49:54 -04003284 self.assertTrue(self.builds[4].hasChanges(B))
3285 self.assertTrue(self.builds[4].hasChanges(C))
3286 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003287
James E. Blair08d19992016-08-10 15:25:31 -07003288 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003289 self.waitUntilSettled()
3290
3291 self.assertEqual(A.data['status'], 'MERGED')
3292 self.assertEqual(A.reported, 2)
3293 self.assertEqual(B.data['status'], 'MERGED')
3294 self.assertEqual(B.reported, 2)
3295 self.assertEqual(C.data['status'], 'MERGED')
3296 self.assertEqual(C.reported, 2)
3297
3298 client.shutdown()
3299 self.assertEqual(r, True)
3300
3301 def test_client_promote_negative(self):
3302 "Test that the RPC client returns errors for promotion"
James E. Blair08d19992016-08-10 15:25:31 -07003303 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003304 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003305 A.addApproval('code-review', 2)
3306 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003307 self.waitUntilSettled()
3308
3309 client = zuul.rpcclient.RPCClient('127.0.0.1',
3310 self.gearman_server.port)
3311
3312 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003313 r = client.promote(tenant='tenant-one',
3314 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003315 change_ids=['2,1', '3,1'])
3316 client.shutdown()
3317 self.assertEqual(r, False)
3318
3319 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003320 r = client.promote(tenant='tenant-one',
3321 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003322 change_ids=['4,1'])
3323 client.shutdown()
3324 self.assertEqual(r, False)
3325
James E. Blair08d19992016-08-10 15:25:31 -07003326 self.launch_server.hold_jobs_in_build = False
3327 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003328 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003329
James E. Blairec056492016-07-22 09:45:56 -07003330 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003331 def test_queue_rate_limiting(self):
3332 "Test that DependentPipelines are rate limited with dep across window"
James E. Blairf84026c2015-12-08 16:11:46 -08003333 self.updateConfigLayout(
3334 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003335 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07003336 self.launch_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003337 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3338 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3339 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3340
3341 C.setDependsOn(B, 1)
James E. Blair08d19992016-08-10 15:25:31 -07003342 self.launch_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003343
James E. Blair8b5408c2016-08-08 15:37:46 -07003344 A.addApproval('code-review', 2)
3345 B.addApproval('code-review', 2)
3346 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003347
James E. Blair8b5408c2016-08-08 15:37:46 -07003348 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3349 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3350 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003351 self.waitUntilSettled()
3352
3353 # Only A and B will have their merge jobs queued because
3354 # window is 2.
3355 self.assertEqual(len(self.builds), 2)
3356 self.assertEqual(self.builds[0].name, 'project-merge')
3357 self.assertEqual(self.builds[1].name, 'project-merge')
3358
James E. Blair08d19992016-08-10 15:25:31 -07003359 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003360 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003361 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003362 self.waitUntilSettled()
3363
3364 # Only A and B will have their test jobs queued because
3365 # window is 2.
3366 self.assertEqual(len(self.builds), 4)
3367 self.assertEqual(self.builds[0].name, 'project-test1')
3368 self.assertEqual(self.builds[1].name, 'project-test2')
3369 self.assertEqual(self.builds[2].name, 'project-test1')
3370 self.assertEqual(self.builds[3].name, 'project-test2')
3371
James E. Blair08d19992016-08-10 15:25:31 -07003372 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003373 self.waitUntilSettled()
3374
3375 queue = self.sched.layout.pipelines['gate'].queues[0]
3376 # A failed so window is reduced by 1 to 1.
3377 self.assertEqual(queue.window, 1)
3378 self.assertEqual(queue.window_floor, 1)
3379 self.assertEqual(A.data['status'], 'NEW')
3380
3381 # Gate is reset and only B's merge job is queued because
3382 # window shrunk to 1.
3383 self.assertEqual(len(self.builds), 1)
3384 self.assertEqual(self.builds[0].name, 'project-merge')
3385
James E. Blair08d19992016-08-10 15:25:31 -07003386 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003387 self.waitUntilSettled()
3388
3389 # Only B's test jobs are queued because window is still 1.
3390 self.assertEqual(len(self.builds), 2)
3391 self.assertEqual(self.builds[0].name, 'project-test1')
3392 self.assertEqual(self.builds[1].name, 'project-test2')
3393
James E. Blair08d19992016-08-10 15:25:31 -07003394 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003395 self.waitUntilSettled()
3396
3397 # B was successfully merged so window is increased to 2.
3398 self.assertEqual(queue.window, 2)
3399 self.assertEqual(queue.window_floor, 1)
3400 self.assertEqual(B.data['status'], 'MERGED')
3401
3402 # Only C is left and its merge job is queued.
3403 self.assertEqual(len(self.builds), 1)
3404 self.assertEqual(self.builds[0].name, 'project-merge')
3405
James E. Blair08d19992016-08-10 15:25:31 -07003406 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003407 self.waitUntilSettled()
3408
3409 # After successful merge job the test jobs for C are queued.
3410 self.assertEqual(len(self.builds), 2)
3411 self.assertEqual(self.builds[0].name, 'project-test1')
3412 self.assertEqual(self.builds[1].name, 'project-test2')
3413
James E. Blair08d19992016-08-10 15:25:31 -07003414 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003415 self.waitUntilSettled()
3416
3417 # C successfully merged so window is bumped to 3.
3418 self.assertEqual(queue.window, 3)
3419 self.assertEqual(queue.window_floor, 1)
3420 self.assertEqual(C.data['status'], 'MERGED')
3421
James E. Blairec056492016-07-22 09:45:56 -07003422 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003423 def test_queue_rate_limiting_dependent(self):
3424 "Test that DependentPipelines are rate limited with dep in window"
James E. Blairf84026c2015-12-08 16:11:46 -08003425 self.updateConfigLayout(
3426 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003427 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07003428 self.launch_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003429 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3430 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3431 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3432
3433 B.setDependsOn(A, 1)
3434
James E. Blair08d19992016-08-10 15:25:31 -07003435 self.launch_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003436
James E. Blair8b5408c2016-08-08 15:37:46 -07003437 A.addApproval('code-review', 2)
3438 B.addApproval('code-review', 2)
3439 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003440
James E. Blair8b5408c2016-08-08 15:37:46 -07003441 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3442 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3443 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003444 self.waitUntilSettled()
3445
3446 # Only A and B will have their merge jobs queued because
3447 # window is 2.
3448 self.assertEqual(len(self.builds), 2)
3449 self.assertEqual(self.builds[0].name, 'project-merge')
3450 self.assertEqual(self.builds[1].name, 'project-merge')
3451
James E. Blair08d19992016-08-10 15:25:31 -07003452 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003453 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003454 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003455 self.waitUntilSettled()
3456
3457 # Only A and B will have their test jobs queued because
3458 # window is 2.
3459 self.assertEqual(len(self.builds), 4)
3460 self.assertEqual(self.builds[0].name, 'project-test1')
3461 self.assertEqual(self.builds[1].name, 'project-test2')
3462 self.assertEqual(self.builds[2].name, 'project-test1')
3463 self.assertEqual(self.builds[3].name, 'project-test2')
3464
James E. Blair08d19992016-08-10 15:25:31 -07003465 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003466 self.waitUntilSettled()
3467
3468 queue = self.sched.layout.pipelines['gate'].queues[0]
3469 # A failed so window is reduced by 1 to 1.
3470 self.assertEqual(queue.window, 1)
3471 self.assertEqual(queue.window_floor, 1)
3472 self.assertEqual(A.data['status'], 'NEW')
3473 self.assertEqual(B.data['status'], 'NEW')
3474
3475 # Gate is reset and only C's merge job is queued because
3476 # window shrunk to 1 and A and B were dequeued.
3477 self.assertEqual(len(self.builds), 1)
3478 self.assertEqual(self.builds[0].name, 'project-merge')
3479
James E. Blair08d19992016-08-10 15:25:31 -07003480 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003481 self.waitUntilSettled()
3482
3483 # Only C's test jobs are queued because window is still 1.
3484 self.assertEqual(len(self.builds), 2)
3485 self.assertEqual(self.builds[0].name, 'project-test1')
3486 self.assertEqual(self.builds[1].name, 'project-test2')
3487
James E. Blair08d19992016-08-10 15:25:31 -07003488 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003489 self.waitUntilSettled()
3490
3491 # C was successfully merged so window is increased to 2.
3492 self.assertEqual(queue.window, 2)
3493 self.assertEqual(queue.window_floor, 1)
3494 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003495
James E. Blairec056492016-07-22 09:45:56 -07003496 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003497 def test_worker_update_metadata(self):
3498 "Test if a worker can send back metadata about itself"
James E. Blair08d19992016-08-10 15:25:31 -07003499 self.launch_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003500
3501 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003502 A.addApproval('code-review', 2)
3503 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003504 self.waitUntilSettled()
3505
3506 self.assertEqual(len(self.launcher.builds), 1)
3507
3508 self.log.debug('Current builds:')
3509 self.log.debug(self.launcher.builds)
3510
3511 start = time.time()
3512 while True:
3513 if time.time() - start > 10:
3514 raise Exception("Timeout waiting for gearman server to report "
3515 + "back to the client")
3516 build = self.launcher.builds.values()[0]
3517 if build.worker.name == "My Worker":
3518 break
3519 else:
3520 time.sleep(0)
3521
3522 self.log.debug(build)
3523 self.assertEqual("My Worker", build.worker.name)
3524 self.assertEqual("localhost", build.worker.hostname)
3525 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3526 self.assertEqual("zuul.example.org", build.worker.fqdn)
3527 self.assertEqual("FakeBuilder", build.worker.program)
3528 self.assertEqual("v1.1", build.worker.version)
3529 self.assertEqual({'something': 'else'}, build.worker.extra)
3530
James E. Blair08d19992016-08-10 15:25:31 -07003531 self.launch_server.hold_jobs_in_build = False
3532 self.launch_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003533 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003534
James E. Blairec056492016-07-22 09:45:56 -07003535 @skip("Disabled for early v3 development")
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003536 def test_footer_message(self):
3537 "Test a pipeline's footer message is correctly added to the report."
James E. Blairf84026c2015-12-08 16:11:46 -08003538 self.updateConfigLayout(
3539 'tests/fixtures/layout-footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003540 self.sched.reconfigure(self.config)
3541 self.registerJobs()
3542
3543 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003544 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07003545 self.launch_server.failJob('test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003546 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003547 self.waitUntilSettled()
3548
3549 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003550 B.addApproval('code-review', 2)
3551 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003552 self.waitUntilSettled()
3553
3554 self.assertEqual(2, len(self.smtp_messages))
3555
3556 failure_body = """\
3557Build failed. For information on how to proceed, see \
3558http://wiki.example.org/Test_Failures
3559
3560- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
3561- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
3562
3563For CI problems and help debugging, contact ci@example.org"""
3564
3565 success_body = """\
3566Build succeeded.
3567
3568- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
3569- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
3570
3571For CI problems and help debugging, contact ci@example.org"""
3572
3573 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
3574 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11003575
James E. Blairec056492016-07-22 09:45:56 -07003576 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003577 def test_merge_failure_reporters(self):
3578 """Check that the config is set up correctly"""
3579
James E. Blairf84026c2015-12-08 16:11:46 -08003580 self.updateConfigLayout(
3581 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003582 self.sched.reconfigure(self.config)
3583 self.registerJobs()
3584
3585 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003586 "Merge Failed.\n\nThis change or one of its cross-repo "
3587 "dependencies was unable to be automatically merged with the "
3588 "current state of its repository. Please rebase the change and "
3589 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003590 self.sched.layout.pipelines['check'].merge_failure_message)
3591 self.assertEqual(
3592 "The merge failed! For more information...",
3593 self.sched.layout.pipelines['gate'].merge_failure_message)
3594
3595 self.assertEqual(
3596 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3597 self.assertEqual(
3598 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3599
3600 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003601 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3602 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003603
3604 self.assertTrue(
3605 (
3606 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003607 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003608 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003609 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003610 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003611 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003612 ) or (
3613 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003614 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003615 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003616 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003617 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003618 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003619 )
3620 )
3621
James E. Blairec056492016-07-22 09:45:56 -07003622 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003623 def test_merge_failure_reports(self):
3624 """Check that when a change fails to merge the correct message is sent
3625 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003626 self.updateConfigLayout(
3627 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003628 self.sched.reconfigure(self.config)
3629 self.registerJobs()
3630
3631 # Check a test failure isn't reported to SMTP
3632 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003633 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07003634 self.launch_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003635 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003636 self.waitUntilSettled()
3637
3638 self.assertEqual(3, len(self.history)) # 3 jobs
3639 self.assertEqual(0, len(self.smtp_messages))
3640
3641 # Check a merge failure is reported to SMTP
3642 # B should be merged, but C will conflict with B
3643 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3644 B.addPatchset(['conflict'])
3645 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3646 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003647 B.addApproval('code-review', 2)
3648 C.addApproval('code-review', 2)
3649 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3650 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003651 self.waitUntilSettled()
3652
3653 self.assertEqual(6, len(self.history)) # A and B jobs
3654 self.assertEqual(1, len(self.smtp_messages))
3655 self.assertEqual('The merge failed! For more information...',
3656 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003657
James E. Blairec056492016-07-22 09:45:56 -07003658 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003659 def test_default_merge_failure_reports(self):
3660 """Check that the default merge failure reports are correct."""
3661
3662 # A should report success, B should report merge failure.
3663 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3664 A.addPatchset(['conflict'])
3665 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3666 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003667 A.addApproval('code-review', 2)
3668 B.addApproval('code-review', 2)
3669 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3670 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003671 self.waitUntilSettled()
3672
3673 self.assertEqual(3, len(self.history)) # A jobs
3674 self.assertEqual(A.reported, 2)
3675 self.assertEqual(B.reported, 2)
3676 self.assertEqual(A.data['status'], 'MERGED')
3677 self.assertEqual(B.data['status'], 'NEW')
3678 self.assertIn('Build succeeded', A.messages[1])
3679 self.assertIn('Merge Failed', B.messages[1])
3680 self.assertIn('automatically merged', B.messages[1])
3681 self.assertNotIn('logs.example.com', B.messages[1])
3682 self.assertNotIn('SKIPPED', B.messages[1])
3683
James E. Blairec056492016-07-22 09:45:56 -07003684 @skip("Disabled for early v3 development")
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003685 def test_swift_instructions(self):
3686 "Test that the correct swift instructions are sent to the workers"
James E. Blairf84026c2015-12-08 16:11:46 -08003687 self.updateConfigLayout(
3688 'tests/fixtures/layout-swift.yaml')
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003689 self.sched.reconfigure(self.config)
3690 self.registerJobs()
3691
James E. Blair08d19992016-08-10 15:25:31 -07003692 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003693 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3694
James E. Blair8b5408c2016-08-08 15:37:46 -07003695 A.addApproval('code-review', 2)
3696 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003697 self.waitUntilSettled()
3698
3699 self.assertEqual(
3700 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
3701 "gate/test-merge/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003702 self.builds[0].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003703 self.assertEqual(5,
3704 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
3705 split('\n')))
3706 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
3707
3708 self.assertEqual(
3709 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
3710 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003711 self.builds[1].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003712 self.assertEqual(5,
3713 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
3714 split('\n')))
3715 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
3716
3717 self.assertEqual(
3718 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
3719 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003720 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003721 self.assertEqual(5,
3722 len(self.builds[1].
3723 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
3724 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
3725
James E. Blair08d19992016-08-10 15:25:31 -07003726 self.launch_server.hold_jobs_in_build = False
3727 self.launch_server.release()
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003728 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003729
James E. Blairec056492016-07-22 09:45:56 -07003730 @skip("Disabled for early v3 development")
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003731 def test_client_get_running_jobs(self):
3732 "Test that the RPC client can get a list of running jobs"
James E. Blair08d19992016-08-10 15:25:31 -07003733 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003734 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003735 A.addApproval('code-review', 2)
3736 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003737 self.waitUntilSettled()
3738
3739 client = zuul.rpcclient.RPCClient('127.0.0.1',
3740 self.gearman_server.port)
3741
3742 # Wait for gearman server to send the initial workData back to zuul
3743 start = time.time()
3744 while True:
3745 if time.time() - start > 10:
3746 raise Exception("Timeout waiting for gearman server to report "
3747 + "back to the client")
3748 build = self.launcher.builds.values()[0]
3749 if build.worker.name == "My Worker":
3750 break
3751 else:
3752 time.sleep(0)
3753
3754 running_items = client.get_running_jobs()
3755
3756 self.assertEqual(1, len(running_items))
3757 running_item = running_items[0]
3758 self.assertEqual([], running_item['failing_reasons'])
3759 self.assertEqual([], running_item['items_behind'])
3760 self.assertEqual('https://hostname/1', running_item['url'])
3761 self.assertEqual(None, running_item['item_ahead'])
3762 self.assertEqual('org/project', running_item['project'])
3763 self.assertEqual(None, running_item['remaining_time'])
3764 self.assertEqual(True, running_item['active'])
3765 self.assertEqual('1,1', running_item['id'])
3766
3767 self.assertEqual(3, len(running_item['jobs']))
3768 for job in running_item['jobs']:
3769 if job['name'] == 'project-merge':
3770 self.assertEqual('project-merge', job['name'])
3771 self.assertEqual('gate', job['pipeline'])
3772 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003773 self.assertEqual('https://server/job/project-merge/0/',
3774 job['url'])
3775 self.assertEqual(7, len(job['worker']))
3776 self.assertEqual(False, job['canceled'])
3777 self.assertEqual(True, job['voting'])
3778 self.assertEqual(None, job['result'])
3779 self.assertEqual('gate', job['pipeline'])
3780 break
3781
James E. Blair08d19992016-08-10 15:25:31 -07003782 self.launch_server.hold_jobs_in_build = False
3783 self.launch_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003784 self.waitUntilSettled()
3785
3786 running_items = client.get_running_jobs()
3787 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003788
3789 def test_nonvoting_pipeline(self):
3790 "Test that a nonvoting pipeline (experimental) can still report"
3791
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003792 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3793 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003794 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3795 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003796
3797 self.assertEqual(self.getJobFromHistory('project-merge').result,
3798 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003799 self.assertEqual(
3800 self.getJobFromHistory('experimental-project-test').result,
3801 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003802 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003803
3804 def test_crd_gate(self):
3805 "Test cross-repo dependencies"
3806 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3807 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003808 A.addApproval('code-review', 2)
3809 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003810
3811 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3812 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3813 AM2.setMerged()
3814 AM1.setMerged()
3815
3816 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3817 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3818 BM2.setMerged()
3819 BM1.setMerged()
3820
3821 # A -> AM1 -> AM2
3822 # B -> BM1 -> BM2
3823 # A Depends-On: B
3824 # M2 is here to make sure it is never queried. If it is, it
3825 # means zuul is walking down the entire history of merged
3826 # changes.
3827
3828 B.setDependsOn(BM1, 1)
3829 BM1.setDependsOn(BM2, 1)
3830
3831 A.setDependsOn(AM1, 1)
3832 AM1.setDependsOn(AM2, 1)
3833
3834 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3835 A.subject, B.data['id'])
3836
James E. Blair8b5408c2016-08-08 15:37:46 -07003837 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003838 self.waitUntilSettled()
3839
3840 self.assertEqual(A.data['status'], 'NEW')
3841 self.assertEqual(B.data['status'], 'NEW')
3842
Paul Belanger5dccbe72016-11-14 11:17:37 -05003843 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003844 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003845
James E. Blair08d19992016-08-10 15:25:31 -07003846 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003847 B.addApproval('approved', 1)
3848 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003849 self.waitUntilSettled()
3850
James E. Blair08d19992016-08-10 15:25:31 -07003851 self.launch_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003852 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003853 self.launch_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003854 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003855 self.launch_server.hold_jobs_in_build = False
3856 self.launch_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003857 self.waitUntilSettled()
3858
3859 self.assertEqual(AM2.queried, 0)
3860 self.assertEqual(BM2.queried, 0)
3861 self.assertEqual(A.data['status'], 'MERGED')
3862 self.assertEqual(B.data['status'], 'MERGED')
3863 self.assertEqual(A.reported, 2)
3864 self.assertEqual(B.reported, 2)
3865
Paul Belanger5dccbe72016-11-14 11:17:37 -05003866 changes = self.getJobFromHistory(
3867 'project-merge', 'org/project1').changes
3868 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003869
3870 def test_crd_branch(self):
3871 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003872
3873 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003874 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3875 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3876 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3877 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003878 A.addApproval('code-review', 2)
3879 B.addApproval('code-review', 2)
3880 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003881
3882 # A Depends-On: B+C
3883 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3884 A.subject, B.data['id'])
3885
James E. Blair08d19992016-08-10 15:25:31 -07003886 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003887 B.addApproval('approved', 1)
3888 C.addApproval('approved', 1)
3889 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003890 self.waitUntilSettled()
3891
James E. Blair08d19992016-08-10 15:25:31 -07003892 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003893 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003894 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003895 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003896 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003897 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003898 self.launch_server.hold_jobs_in_build = False
3899 self.launch_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003900 self.waitUntilSettled()
3901
3902 self.assertEqual(A.data['status'], 'MERGED')
3903 self.assertEqual(B.data['status'], 'MERGED')
3904 self.assertEqual(C.data['status'], 'MERGED')
3905 self.assertEqual(A.reported, 2)
3906 self.assertEqual(B.reported, 2)
3907 self.assertEqual(C.reported, 2)
3908
Paul Belanger6379db12016-11-14 13:57:54 -05003909 changes = self.getJobFromHistory(
3910 'project-merge', 'org/project1').changes
3911 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003912
3913 def test_crd_multiline(self):
3914 "Test multiple depends-on lines in commit"
3915 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3916 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3917 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003918 A.addApproval('code-review', 2)
3919 B.addApproval('code-review', 2)
3920 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003921
3922 # A Depends-On: B+C
3923 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3924 A.subject, B.data['id'], C.data['id'])
3925
James E. Blair08d19992016-08-10 15:25:31 -07003926 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003927 B.addApproval('approved', 1)
3928 C.addApproval('approved', 1)
3929 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003930 self.waitUntilSettled()
3931
James E. Blair08d19992016-08-10 15:25:31 -07003932 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003933 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003934 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003935 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003936 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003937 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003938 self.launch_server.hold_jobs_in_build = False
3939 self.launch_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003940 self.waitUntilSettled()
3941
3942 self.assertEqual(A.data['status'], 'MERGED')
3943 self.assertEqual(B.data['status'], 'MERGED')
3944 self.assertEqual(C.data['status'], 'MERGED')
3945 self.assertEqual(A.reported, 2)
3946 self.assertEqual(B.reported, 2)
3947 self.assertEqual(C.reported, 2)
3948
Paul Belanger5dccbe72016-11-14 11:17:37 -05003949 changes = self.getJobFromHistory(
3950 'project-merge', 'org/project1').changes
3951 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003952
3953 def test_crd_unshared_gate(self):
3954 "Test cross-repo dependencies in unshared gate queues"
3955 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3956 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003957 A.addApproval('code-review', 2)
3958 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003959
3960 # A Depends-On: B
3961 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3962 A.subject, B.data['id'])
3963
3964 # A and B do not share a queue, make sure that A is unable to
3965 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003966 B.addApproval('approved', 1)
3967 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003968 self.waitUntilSettled()
3969
3970 self.assertEqual(A.data['status'], 'NEW')
3971 self.assertEqual(B.data['status'], 'NEW')
3972 self.assertEqual(A.reported, 0)
3973 self.assertEqual(B.reported, 0)
3974 self.assertEqual(len(self.history), 0)
3975
3976 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003977 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003978 self.waitUntilSettled()
3979
3980 self.assertEqual(B.data['status'], 'MERGED')
3981 self.assertEqual(B.reported, 2)
3982
3983 # Now that B is merged, A should be able to be enqueued and
3984 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003985 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003986 self.waitUntilSettled()
3987
3988 self.assertEqual(A.data['status'], 'MERGED')
3989 self.assertEqual(A.reported, 2)
3990
James E. Blair96698e22015-04-02 07:48:21 -07003991 def test_crd_gate_reverse(self):
3992 "Test reverse cross-repo dependencies"
3993 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3994 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003995 A.addApproval('code-review', 2)
3996 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003997
3998 # A Depends-On: B
3999
4000 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4001 A.subject, B.data['id'])
4002
James E. Blair8b5408c2016-08-08 15:37:46 -07004003 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004004 self.waitUntilSettled()
4005
4006 self.assertEqual(A.data['status'], 'NEW')
4007 self.assertEqual(B.data['status'], 'NEW')
4008
James E. Blair08d19992016-08-10 15:25:31 -07004009 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07004010 A.addApproval('approved', 1)
4011 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004012 self.waitUntilSettled()
4013
James E. Blair08d19992016-08-10 15:25:31 -07004014 self.launch_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004015 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07004016 self.launch_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004017 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07004018 self.launch_server.hold_jobs_in_build = False
4019 self.launch_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07004020 self.waitUntilSettled()
4021
4022 self.assertEqual(A.data['status'], 'MERGED')
4023 self.assertEqual(B.data['status'], 'MERGED')
4024 self.assertEqual(A.reported, 2)
4025 self.assertEqual(B.reported, 2)
4026
Paul Belanger5dccbe72016-11-14 11:17:37 -05004027 changes = self.getJobFromHistory(
4028 'project-merge', 'org/project1').changes
4029 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07004030
James E. Blair5ee24252014-12-30 10:12:29 -08004031 def test_crd_cycle(self):
4032 "Test cross-repo dependency cycles"
4033 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4034 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004035 A.addApproval('code-review', 2)
4036 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004037
4038 # A -> B -> A (via commit-depends)
4039
4040 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4041 A.subject, B.data['id'])
4042 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4043 B.subject, A.data['id'])
4044
James E. Blair8b5408c2016-08-08 15:37:46 -07004045 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004046 self.waitUntilSettled()
4047
4048 self.assertEqual(A.reported, 0)
4049 self.assertEqual(B.reported, 0)
4050 self.assertEqual(A.data['status'], 'NEW')
4051 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004052
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004053 def test_crd_gate_unknown(self):
4054 "Test unknown projects in dependent pipeline"
4055 self.init_repo("org/unknown")
4056 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4057 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004058 A.addApproval('code-review', 2)
4059 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004060
4061 # A Depends-On: B
4062 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4063 A.subject, B.data['id'])
4064
James E. Blair8b5408c2016-08-08 15:37:46 -07004065 B.addApproval('approved', 1)
4066 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004067 self.waitUntilSettled()
4068
4069 # Unknown projects cannot share a queue with any other
4070 # since they don't have common jobs with any other (they have no jobs).
4071 # Changes which depend on unknown project changes
4072 # should not be processed in dependent pipeline
4073 self.assertEqual(A.data['status'], 'NEW')
4074 self.assertEqual(B.data['status'], 'NEW')
4075 self.assertEqual(A.reported, 0)
4076 self.assertEqual(B.reported, 0)
4077 self.assertEqual(len(self.history), 0)
4078
4079 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07004080 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004081 B.setMerged()
4082 self.waitUntilSettled()
4083 self.assertEqual(len(self.history), 0)
4084
4085 # Now that B is merged, A should be able to be enqueued and
4086 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07004087 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004088 self.waitUntilSettled()
4089
4090 self.assertEqual(A.data['status'], 'MERGED')
4091 self.assertEqual(A.reported, 2)
4092 self.assertEqual(B.data['status'], 'MERGED')
4093 self.assertEqual(B.reported, 0)
4094
James E. Blairbfb8e042014-12-30 17:01:44 -08004095 def test_crd_check(self):
4096 "Test cross-repo dependencies in independent pipelines"
4097
Paul Belangerb30342b2016-11-14 12:30:43 -05004098 self.launch_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004099 self.gearman_server.hold_jobs_in_queue = True
4100 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4101 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4102
4103 # A Depends-On: B
4104 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4105 A.subject, B.data['id'])
4106
4107 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4108 self.waitUntilSettled()
4109
4110 queue = self.gearman_server.getQueue()
4111 ref = self.getParameter(queue[-1], 'ZUUL_REF')
4112 self.gearman_server.hold_jobs_in_queue = False
4113 self.gearman_server.release()
4114 self.waitUntilSettled()
4115
Paul Belangerb30342b2016-11-14 12:30:43 -05004116 self.launch_server.release('.*-merge')
4117 self.waitUntilSettled()
4118
4119 path = os.path.join(self.builds[0].jobdir.git_root, "org/project1")
James E. Blairbfb8e042014-12-30 17:01:44 -08004120 repo = git.Repo(path)
4121 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4122 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004123 correct_messages = [
4124 'initial commit', 'add content from fixture', 'A-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004125 self.assertEqual(repo_messages, correct_messages)
4126
Paul Belangerb30342b2016-11-14 12:30:43 -05004127 path = os.path.join(self.builds[0].jobdir.git_root, "org/project2")
James E. Blairbfb8e042014-12-30 17:01:44 -08004128 repo = git.Repo(path)
4129 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4130 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004131 correct_messages = [
4132 'initial commit', 'add content from fixture', 'B-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004133 self.assertEqual(repo_messages, correct_messages)
4134
Paul Belangerb30342b2016-11-14 12:30:43 -05004135 self.launch_server.hold_jobs_in_build = False
4136 self.launch_server.release()
4137 self.waitUntilSettled()
4138
James E. Blairbfb8e042014-12-30 17:01:44 -08004139 self.assertEqual(A.data['status'], 'NEW')
4140 self.assertEqual(B.data['status'], 'NEW')
4141 self.assertEqual(A.reported, 1)
4142 self.assertEqual(B.reported, 0)
4143
4144 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004145 tenant = self.sched.abide.tenants.get('tenant-one')
4146 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004147
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004148 def test_crd_check_git_depends(self):
4149 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004150 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004151 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4152 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4153
4154 # Add two git-dependent changes and make sure they both report
4155 # success.
4156 B.setDependsOn(A, 1)
4157 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4158 self.waitUntilSettled()
4159 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4160 self.waitUntilSettled()
4161
James E. Blairb8c16472015-05-05 14:55:26 -07004162 self.orderedRelease()
4163 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004164 self.waitUntilSettled()
4165
4166 self.assertEqual(A.data['status'], 'NEW')
4167 self.assertEqual(B.data['status'], 'NEW')
4168 self.assertEqual(A.reported, 1)
4169 self.assertEqual(B.reported, 1)
4170
4171 self.assertEqual(self.history[0].changes, '1,1')
4172 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004173 tenant = self.sched.abide.tenants.get('tenant-one')
4174 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004175
4176 self.assertIn('Build succeeded', A.messages[0])
4177 self.assertIn('Build succeeded', B.messages[0])
4178
4179 def test_crd_check_duplicate(self):
4180 "Test duplicate check in independent pipelines"
James E. Blair08d19992016-08-10 15:25:31 -07004181 self.launch_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004182 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4183 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004184 tenant = self.sched.abide.tenants.get('tenant-one')
4185 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004186
4187 # Add two git-dependent changes...
4188 B.setDependsOn(A, 1)
4189 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4190 self.waitUntilSettled()
4191 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4192
4193 # ...make sure the live one is not duplicated...
4194 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4195 self.waitUntilSettled()
4196 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4197
4198 # ...but the non-live one is able to be.
4199 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4200 self.waitUntilSettled()
4201 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4202
Clark Boylandd849822015-03-02 12:38:14 -08004203 # Release jobs in order to avoid races with change A jobs
4204 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004205 self.orderedRelease()
James E. Blair08d19992016-08-10 15:25:31 -07004206 self.launch_server.hold_jobs_in_build = False
4207 self.launch_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004208 self.waitUntilSettled()
4209
4210 self.assertEqual(A.data['status'], 'NEW')
4211 self.assertEqual(B.data['status'], 'NEW')
4212 self.assertEqual(A.reported, 1)
4213 self.assertEqual(B.reported, 1)
4214
4215 self.assertEqual(self.history[0].changes, '1,1 2,1')
4216 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004217 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004218
4219 self.assertIn('Build succeeded', A.messages[0])
4220 self.assertIn('Build succeeded', B.messages[0])
4221
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004222 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004223 "Test cross-repo dependencies re-enqueued in independent pipelines"
4224
4225 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004226 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4227 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004228
4229 # A Depends-On: B
4230 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4231 A.subject, B.data['id'])
4232
4233 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4234 self.waitUntilSettled()
4235
4236 self.sched.reconfigure(self.config)
4237
4238 # Make sure the items still share a change queue, and the
4239 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004240 tenant = self.sched.abide.tenants.get('tenant-one')
4241 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4242 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004243 first_item = queue.queue[0]
4244 for item in queue.queue:
4245 self.assertEqual(item.queue, first_item.queue)
4246 self.assertFalse(first_item.live)
4247 self.assertTrue(queue.queue[1].live)
4248
4249 self.gearman_server.hold_jobs_in_queue = False
4250 self.gearman_server.release()
4251 self.waitUntilSettled()
4252
4253 self.assertEqual(A.data['status'], 'NEW')
4254 self.assertEqual(B.data['status'], 'NEW')
4255 self.assertEqual(A.reported, 1)
4256 self.assertEqual(B.reported, 0)
4257
4258 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004259 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004260
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004261 def test_crd_check_reconfiguration(self):
4262 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4263
4264 def test_crd_undefined_project(self):
4265 """Test that undefined projects in dependencies are handled for
4266 independent pipelines"""
4267 # It's a hack for fake gerrit,
4268 # as it implies repo creation upon the creation of any change
4269 self.init_repo("org/unknown")
4270 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4271
James E. Blairec056492016-07-22 09:45:56 -07004272 @skip("Disabled for early v3 development")
James E. Blair17dd6772015-02-09 14:45:18 -08004273 def test_crd_check_ignore_dependencies(self):
4274 "Test cross-repo dependencies can be ignored"
James E. Blairf84026c2015-12-08 16:11:46 -08004275 self.updateConfigLayout(
4276 'tests/fixtures/layout-ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004277 self.sched.reconfigure(self.config)
4278 self.registerJobs()
4279
4280 self.gearman_server.hold_jobs_in_queue = True
4281 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4282 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4283 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4284
4285 # A Depends-On: B
4286 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4287 A.subject, B.data['id'])
4288 # C git-depends on B
4289 C.setDependsOn(B, 1)
4290 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4291 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4292 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4293 self.waitUntilSettled()
4294
4295 # Make sure none of the items share a change queue, and all
4296 # are live.
4297 check_pipeline = self.sched.layout.pipelines['check']
4298 self.assertEqual(len(check_pipeline.queues), 3)
4299 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4300 for item in check_pipeline.getAllItems():
4301 self.assertTrue(item.live)
4302
4303 self.gearman_server.hold_jobs_in_queue = False
4304 self.gearman_server.release()
4305 self.waitUntilSettled()
4306
4307 self.assertEqual(A.data['status'], 'NEW')
4308 self.assertEqual(B.data['status'], 'NEW')
4309 self.assertEqual(C.data['status'], 'NEW')
4310 self.assertEqual(A.reported, 1)
4311 self.assertEqual(B.reported, 1)
4312 self.assertEqual(C.reported, 1)
4313
4314 # Each job should have tested exactly one change
4315 for job in self.history:
4316 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004317
James E. Blairec056492016-07-22 09:45:56 -07004318 @skip("Disabled for early v3 development")
James E. Blair96698e22015-04-02 07:48:21 -07004319 def test_crd_check_transitive(self):
4320 "Test transitive cross-repo dependencies"
4321 # Specifically, if A -> B -> C, and C gets a new patchset and
4322 # A gets a new patchset, ensure the test of A,2 includes B,1
4323 # and C,2 (not C,1 which would indicate stale data in the
4324 # cache for B).
4325 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4326 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4327 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4328
4329 # A Depends-On: B
4330 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4331 A.subject, B.data['id'])
4332
4333 # B Depends-On: C
4334 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4335 B.subject, C.data['id'])
4336
4337 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4338 self.waitUntilSettled()
4339 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4340
4341 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4342 self.waitUntilSettled()
4343 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4344
4345 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4346 self.waitUntilSettled()
4347 self.assertEqual(self.history[-1].changes, '3,1')
4348
4349 C.addPatchset()
4350 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4351 self.waitUntilSettled()
4352 self.assertEqual(self.history[-1].changes, '3,2')
4353
4354 A.addPatchset()
4355 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4356 self.waitUntilSettled()
4357 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004358
James E. Blaira8b90b32016-08-24 15:18:50 -07004359 def test_crd_check_unknown(self):
4360 "Test unknown projects in independent pipeline"
4361 self.init_repo("org/unknown")
4362 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4363 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4364 # A Depends-On: B
4365 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4366 A.subject, B.data['id'])
4367
4368 # Make sure zuul has seen an event on B.
4369 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4370 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4371 self.waitUntilSettled()
4372
4373 self.assertEqual(A.data['status'], 'NEW')
4374 self.assertEqual(A.reported, 1)
4375 self.assertEqual(B.data['status'], 'NEW')
4376 self.assertEqual(B.reported, 0)
4377
James E. Blair92464a22016-04-05 10:21:26 -07004378 def test_crd_cycle_join(self):
4379 "Test an updated change creates a cycle"
4380 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4381
4382 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4383 self.waitUntilSettled()
4384
4385 # Create B->A
4386 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4387 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4388 B.subject, A.data['id'])
4389 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4390 self.waitUntilSettled()
4391
4392 # Update A to add A->B (a cycle).
4393 A.addPatchset()
4394 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4395 A.subject, B.data['id'])
4396 # Normally we would submit the patchset-created event for
4397 # processing here, however, we have no way of noting whether
4398 # the dependency cycle detection correctly raised an
4399 # exception, so instead, we reach into the source driver and
4400 # call the method that would ultimately be called by the event
4401 # processing.
4402
Paul Belanger0e155e22016-11-14 14:12:23 -05004403 tenant = self.sched.abide.tenants.get('tenant-one')
4404 source = tenant.layout.pipelines['gate'].source
4405
4406 # TODO(pabelanger): As we add more source / trigger APIs we should make
4407 # it easier for users to create events for testing.
4408 event = zuul.model.TriggerEvent()
4409 event.trigger_name = 'gerrit'
4410 event.change_number = '1'
4411 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004412 with testtools.ExpectedException(
4413 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004414 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004415 self.log.debug("Got expected dependency cycle exception")
4416
4417 # Now if we update B to remove the depends-on, everything
4418 # should be okay. B; A->B
4419
4420 B.addPatchset()
4421 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004422
4423 source.getChange(event, True)
4424 event.change_number = '2'
4425 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004426
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004427 def test_disable_at(self):
4428 "Test a pipeline will only report to the disabled trigger when failing"
4429
Paul Belanger7dc76e82016-11-11 16:51:08 -05004430 self.updateConfigLayout('layout-disabled-at')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004431 self.sched.reconfigure(self.config)
4432
Paul Belanger7dc76e82016-11-11 16:51:08 -05004433 tenant = self.sched.abide.tenants.get('openstack')
4434 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004435 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004436 0, tenant.layout.pipelines['check']._consecutive_failures)
4437 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004438
4439 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4440 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4441 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4442 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4443 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4444 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4445 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4446 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4447 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4448 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4449 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4450
James E. Blair08d19992016-08-10 15:25:31 -07004451 self.launch_server.failJob('project-test1', A)
4452 self.launch_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004453 # Let C pass, resetting the counter
James E. Blair08d19992016-08-10 15:25:31 -07004454 self.launch_server.failJob('project-test1', D)
4455 self.launch_server.failJob('project-test1', E)
4456 self.launch_server.failJob('project-test1', F)
4457 self.launch_server.failJob('project-test1', G)
4458 self.launch_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004459 # I also passes but should only report to the disabled reporters
James E. Blair08d19992016-08-10 15:25:31 -07004460 self.launch_server.failJob('project-test1', J)
4461 self.launch_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004462
4463 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4464 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4465 self.waitUntilSettled()
4466
4467 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004468 2, tenant.layout.pipelines['check']._consecutive_failures)
4469 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004470
4471 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4472 self.waitUntilSettled()
4473
4474 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004475 0, tenant.layout.pipelines['check']._consecutive_failures)
4476 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004477
4478 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4479 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4480 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4481 self.waitUntilSettled()
4482
4483 # We should be disabled now
4484 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004485 3, tenant.layout.pipelines['check']._consecutive_failures)
4486 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004487
4488 # We need to wait between each of these patches to make sure the
4489 # smtp messages come back in an expected order
4490 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4491 self.waitUntilSettled()
4492 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4493 self.waitUntilSettled()
4494 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4495 self.waitUntilSettled()
4496
4497 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4498 # leaving a message on each change
4499 self.assertEqual(1, len(A.messages))
4500 self.assertIn('Build failed.', A.messages[0])
4501 self.assertEqual(1, len(B.messages))
4502 self.assertIn('Build failed.', B.messages[0])
4503 self.assertEqual(1, len(C.messages))
4504 self.assertIn('Build succeeded.', C.messages[0])
4505 self.assertEqual(1, len(D.messages))
4506 self.assertIn('Build failed.', D.messages[0])
4507 self.assertEqual(1, len(E.messages))
4508 self.assertIn('Build failed.', E.messages[0])
4509 self.assertEqual(1, len(F.messages))
4510 self.assertIn('Build failed.', F.messages[0])
4511
4512 # The last 3 (GHI) would have only reported via smtp.
4513 self.assertEqual(3, len(self.smtp_messages))
4514 self.assertEqual(0, len(G.messages))
4515 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004516 self.assertIn(
4517 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004518 self.assertEqual(0, len(H.messages))
4519 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004520 self.assertIn(
4521 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004522 self.assertEqual(0, len(I.messages))
4523 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004524 self.assertIn(
4525 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004526
4527 # Now reload the configuration (simulate a HUP) to check the pipeline
4528 # comes out of disabled
4529 self.sched.reconfigure(self.config)
4530
Paul Belanger7dc76e82016-11-11 16:51:08 -05004531 tenant = self.sched.abide.tenants.get('openstack')
4532
4533 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004534 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004535 0, tenant.layout.pipelines['check']._consecutive_failures)
4536 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004537
4538 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4539 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4540 self.waitUntilSettled()
4541
4542 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004543 2, tenant.layout.pipelines['check']._consecutive_failures)
4544 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004545
4546 # J and K went back to gerrit
4547 self.assertEqual(1, len(J.messages))
4548 self.assertIn('Build failed.', J.messages[0])
4549 self.assertEqual(1, len(K.messages))
4550 self.assertIn('Build failed.', K.messages[0])
4551 # No more messages reported via smtp
4552 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004553
Paul Belanger71d98172016-11-08 10:56:31 -05004554 def test_rerun_on_abort(self):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004555 "Test that if a launch server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004556
4557 self.config.set('zuul', 'layout_config',
4558 'tests/fixtures/layout-abort-attempts.yaml')
4559 self.sched.reconfigure(self.config)
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004560 self.launch_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004561 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4562 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4563 self.waitUntilSettled()
4564
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004565 self.launch_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004566 self.waitUntilSettled()
4567
4568 self.assertEqual(len(self.builds), 2)
4569 self.builds[0].requeue = True
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004570 self.launch_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004571 self.waitUntilSettled()
4572
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004573 for x in range(2):
4574 self.assertEqual(len(self.builds), 1,
4575 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004576 self.builds[0].requeue = True
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004577 self.launch_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004578 self.waitUntilSettled()
4579
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004580 self.launch_server.hold_jobs_in_build = False
4581 self.launch_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004582 self.waitUntilSettled()
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004583 self.assertEqual(len(self.history), 5)
Paul Belanger71d98172016-11-08 10:56:31 -05004584 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4585 self.assertEqual(A.reported, 1)
4586 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004587
Arieb6f068c2016-10-09 13:11:06 +03004588
4589class TestDuplicatePipeline(ZuulTestCase):
4590 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4591
4592 def test_duplicate_pipelines(self):
4593 "Test that a change matching multiple pipelines works"
4594
4595 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4596 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4597 self.waitUntilSettled()
4598
4599 self.assertHistory([
4600 dict(name='project-test1', result='SUCCESS', changes='1,1',
4601 pipeline='dup1'),
4602 dict(name='project-test1', result='SUCCESS', changes='1,1',
4603 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004604 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004605
4606 self.assertEqual(len(A.messages), 2)
4607
Paul Belangera46a3742016-11-09 14:23:03 -05004608 if 'dup1' in A.messages[0]:
4609 self.assertIn('dup1', A.messages[0])
4610 self.assertNotIn('dup2', A.messages[0])
4611 self.assertIn('project-test1', A.messages[0])
4612 self.assertIn('dup2', A.messages[1])
4613 self.assertNotIn('dup1', A.messages[1])
4614 self.assertIn('project-test1', A.messages[1])
4615 else:
4616 self.assertIn('dup1', A.messages[1])
4617 self.assertNotIn('dup2', A.messages[1])
4618 self.assertIn('project-test1', A.messages[1])
4619 self.assertIn('dup2', A.messages[0])
4620 self.assertNotIn('dup1', A.messages[0])
4621 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004622
4623
4624class TestSchedulerOneJobProject(ZuulTestCase):
4625 tenant_config_file = 'config/one-job-project/main.yaml'
4626
4627 def test_one_job_project(self):
4628 "Test that queueing works with one job"
4629 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4630 'master', 'A')
4631 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4632 'master', 'B')
4633 A.addApproval('code-review', 2)
4634 B.addApproval('code-review', 2)
4635 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4636 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4637 self.waitUntilSettled()
4638
4639 self.assertEqual(A.data['status'], 'MERGED')
4640 self.assertEqual(A.reported, 2)
4641 self.assertEqual(B.data['status'], 'MERGED')
4642 self.assertEqual(B.reported, 2)
Paul Belanger86085b32016-11-03 12:48:57 -04004643
4644
4645class TestSchedulerTemplatedProject(ZuulTestCase):
4646 tenant_config_file = 'config/templated-project/main.yaml'
4647
4648 def test_job_from_templates_launched(self):
4649 "Test whether a job generated via a template can be launched"
4650
4651 A = self.fake_gerrit.addFakeChange(
4652 'org/templated-project', 'master', 'A')
4653 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4654 self.waitUntilSettled()
4655
4656 self.assertEqual(self.getJobFromHistory('project-test1').result,
4657 'SUCCESS')
4658 self.assertEqual(self.getJobFromHistory('project-test2').result,
4659 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004660
4661 def test_layered_templates(self):
4662 "Test whether a job generated via a template can be launched"
4663
4664 A = self.fake_gerrit.addFakeChange(
4665 'org/layered-project', 'master', 'A')
4666 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4667 self.waitUntilSettled()
4668
4669 self.assertEqual(self.getJobFromHistory('project-test1').result,
4670 'SUCCESS')
4671 self.assertEqual(self.getJobFromHistory('project-test2').result,
4672 'SUCCESS')
4673 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4674 ).result, 'SUCCESS')
4675 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4676 ).result, 'SUCCESS')
4677 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4678 ).result, 'SUCCESS')
4679 self.assertEqual(self.getJobFromHistory('project-test6').result,
4680 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004681
4682
4683class TestSchedulerSuccessURL(ZuulTestCase):
4684 tenant_config_file = 'config/success-url/main.yaml'
4685
4686 def test_success_url(self):
4687 "Ensure bad build params are ignored"
4688 self.sched.reconfigure(self.config)
4689 self.init_repo('org/docs')
4690
4691 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4692 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4693 self.waitUntilSettled()
4694
4695 # Both builds ran: docs-draft-test + docs-draft-test2
4696 self.assertEqual(len(self.history), 2)
4697
4698 # Grab build id
4699 uuid = self.history[0].uuid[:7]
4700
4701 # Two msgs: 'Starting...' + results
4702 self.assertEqual(len(self.smtp_messages), 2)
4703 body = self.smtp_messages[1]['body'].splitlines()
4704 self.assertEqual('Build succeeded.', body[0])
4705
4706 self.assertIn(
4707 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4708 'docs-draft-test/%s/publish-docs/' % uuid,
4709 body[2])
4710
4711 # NOTE: This default URL is currently hard-coded in launcher/server.py
4712 self.assertIn(
4713 '- docs-draft-test2 https://server/job',
4714 body[3])