blob: 57bc88480dc95b1dce9a9489d3697f37e959d47c [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. Blairec056492016-07-22 09:45:56 -07001007 @skip("Disabled for early v3 development")
James E. Blairdaabed22012-08-15 15:38:57 -07001008 def test_post(self):
1009 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001010
Zhongyue Luo5d556072012-09-21 02:00:47 +09001011 e = {
1012 "type": "ref-updated",
1013 "submitter": {
1014 "name": "User Name",
1015 },
1016 "refUpdate": {
1017 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1018 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1019 "refName": "master",
1020 "project": "org/project",
1021 }
1022 }
James E. Blairdaabed22012-08-15 15:38:57 -07001023 self.fake_gerrit.addEvent(e)
1024 self.waitUntilSettled()
1025
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001026 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001027 self.assertEqual(len(self.history), 1)
1028 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001029
James E. Blairec056492016-07-22 09:45:56 -07001030 @skip("Disabled for early v3 development")
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001031 def test_post_ignore_deletes(self):
1032 "Test that deleting refs does not trigger post jobs"
1033
1034 e = {
1035 "type": "ref-updated",
1036 "submitter": {
1037 "name": "User Name",
1038 },
1039 "refUpdate": {
1040 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1041 "newRev": "0000000000000000000000000000000000000000",
1042 "refName": "master",
1043 "project": "org/project",
1044 }
1045 }
1046 self.fake_gerrit.addEvent(e)
1047 self.waitUntilSettled()
1048
1049 job_names = [x.name for x in self.history]
1050 self.assertEqual(len(self.history), 0)
1051 self.assertNotIn('project-post', job_names)
1052
James E. Blairec056492016-07-22 09:45:56 -07001053 @skip("Disabled for early v3 development")
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001054 def test_post_ignore_deletes_negative(self):
1055 "Test that deleting refs does trigger post jobs"
1056
James E. Blairf84026c2015-12-08 16:11:46 -08001057 self.updateConfigLayout(
1058 'tests/fixtures/layout-dont-ignore-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001059 self.sched.reconfigure(self.config)
1060
1061 e = {
1062 "type": "ref-updated",
1063 "submitter": {
1064 "name": "User Name",
1065 },
1066 "refUpdate": {
1067 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1068 "newRev": "0000000000000000000000000000000000000000",
1069 "refName": "master",
1070 "project": "org/project",
1071 }
1072 }
1073 self.fake_gerrit.addEvent(e)
1074 self.waitUntilSettled()
1075
1076 job_names = [x.name for x in self.history]
1077 self.assertEqual(len(self.history), 1)
1078 self.assertIn('project-post', job_names)
1079
James E. Blairec056492016-07-22 09:45:56 -07001080 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001081 def test_build_configuration_branch(self):
1082 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001083
1084 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001085 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1086 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1087 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001088 A.addApproval('code-review', 2)
1089 B.addApproval('code-review', 2)
1090 C.addApproval('code-review', 2)
1091 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1092 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1093 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairc6294a52012-08-17 10:19:48 -07001094 self.waitUntilSettled()
1095
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. Blair1f4c2bb2013-04-26 08:40:46 -07001098 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001099 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001100 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001101 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001102 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001103 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1104 self.gearman_server.hold_jobs_in_queue = False
1105 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001106 self.waitUntilSettled()
1107
Monty Taylorbc758832013-06-17 17:22:42 -04001108 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001109 repo = git.Repo(path)
1110 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1111 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001112 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001113 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001114
James E. Blairec056492016-07-22 09:45:56 -07001115 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001116 def test_build_configuration_branch_interaction(self):
1117 "Test that switching between branches works"
1118 self.test_build_configuration()
1119 self.test_build_configuration_branch()
1120 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001121 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001122 repo = git.Repo(path)
1123 repo.heads.master.commit = repo.commit('init')
1124 self.test_build_configuration()
1125
James E. Blairec056492016-07-22 09:45:56 -07001126 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001127 def test_build_configuration_multi_branch(self):
1128 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001129
1130 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001131 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1132 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1133 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001134 A.addApproval('code-review', 2)
1135 B.addApproval('code-review', 2)
1136 C.addApproval('code-review', 2)
1137 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1138 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1139 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairc6294a52012-08-17 10:19:48 -07001140 self.waitUntilSettled()
James E. Blairbb1fe502014-03-04 10:15:06 -08001141 queue = self.gearman_server.getQueue()
1142 job_A = None
1143 for job in queue:
1144 if 'project-merge' in job.name:
1145 job_A = job
1146 ref_A = self.getParameter(job_A, 'ZUUL_REF')
1147 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
1148 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
1149 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
James E. Blairc6294a52012-08-17 10:19:48 -07001150
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001151 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001152 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001153 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001154 job_B = None
1155 for job in queue:
1156 if 'project-merge' in job.name:
1157 job_B = job
1158 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001159 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001160 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blairbb1fe502014-03-04 10:15:06 -08001161 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
1162
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001163 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001164 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001165 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001166 for job in queue:
1167 if 'project-merge' in job.name:
1168 job_C = job
1169 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001170 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001171 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blairbb1fe502014-03-04 10:15:06 -08001172 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001173 self.gearman_server.hold_jobs_in_queue = False
1174 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001175 self.waitUntilSettled()
1176
Monty Taylorbc758832013-06-17 17:22:42 -04001177 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001178 repo = git.Repo(path)
1179
1180 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001181 for c in repo.iter_commits(ref_C)]
James E. Blairbb1fe502014-03-04 10:15:06 -08001182 repo_shas = [c.hexsha for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001183 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001184 correct_messages = ['initial commit', 'A-1', 'C-1']
James E. Blairbb1fe502014-03-04 10:15:06 -08001185 # Ensure the right commits are in the history for this ref
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001186 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08001187 # Ensure ZUUL_REF -> ZUUL_COMMIT
1188 self.assertEqual(repo_shas[0], commit_C)
James E. Blairc6294a52012-08-17 10:19:48 -07001189
1190 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001191 for c in repo.iter_commits(ref_B)]
James E. Blairbb1fe502014-03-04 10:15:06 -08001192 repo_shas = [c.hexsha for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001193 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001194 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001195 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08001196 self.assertEqual(repo_shas[0], commit_B)
1197
1198 repo_messages = [c.message.strip()
1199 for c in repo.iter_commits(ref_A)]
1200 repo_shas = [c.hexsha for c in repo.iter_commits(ref_A)]
1201 repo_messages.reverse()
1202 correct_messages = ['initial commit', 'A-1']
1203 self.assertEqual(repo_messages, correct_messages)
1204 self.assertEqual(repo_shas[0], commit_A)
1205
1206 self.assertNotEqual(ref_A, ref_B, ref_C)
1207 self.assertNotEqual(commit_A, commit_B, commit_C)
James E. Blair7f71c802012-08-22 13:04:32 -07001208
James E. Blaircaec0c52012-08-22 14:52:22 -07001209 def test_dependent_changes_dequeue(self):
1210 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001211
James E. Blaircaec0c52012-08-22 14:52:22 -07001212 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1213 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1214 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001215 A.addApproval('code-review', 2)
1216 B.addApproval('code-review', 2)
1217 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001218
1219 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1220 M1.setMerged()
1221
1222 # C -> B -> A -> M1
1223
1224 C.setDependsOn(B, 1)
1225 B.setDependsOn(A, 1)
1226 A.setDependsOn(M1, 1)
1227
James E. Blair08d19992016-08-10 15:25:31 -07001228 self.launch_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001229
James E. Blair8b5408c2016-08-08 15:37:46 -07001230 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1231 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1232 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001233
1234 self.waitUntilSettled()
1235
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001236 self.assertEqual(A.data['status'], 'NEW')
1237 self.assertEqual(A.reported, 2)
1238 self.assertEqual(B.data['status'], 'NEW')
1239 self.assertEqual(B.reported, 2)
1240 self.assertEqual(C.data['status'], 'NEW')
1241 self.assertEqual(C.reported, 2)
1242 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001243
James E. Blair972e3c72013-08-29 12:04:55 -07001244 def test_failing_dependent_changes(self):
1245 "Test that failing dependent patches are taken out of stream"
James E. Blair08d19992016-08-10 15:25:31 -07001246 self.launch_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001247 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1248 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1249 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1250 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1251 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001252 A.addApproval('code-review', 2)
1253 B.addApproval('code-review', 2)
1254 C.addApproval('code-review', 2)
1255 D.addApproval('code-review', 2)
1256 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001257
1258 # E, D -> C -> B, A
1259
1260 D.setDependsOn(C, 1)
1261 C.setDependsOn(B, 1)
1262
James E. Blair08d19992016-08-10 15:25:31 -07001263 self.launch_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001264
James E. Blair8b5408c2016-08-08 15:37:46 -07001265 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1266 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1267 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1268 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1269 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001270
1271 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()
James E. Blair08d19992016-08-10 15:25:31 -07001278 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001279 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001280 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001281 self.waitUntilSettled()
1282
James E. Blair08d19992016-08-10 15:25:31 -07001283 self.launch_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001284 for build in self.builds:
1285 if build.parameters['ZUUL_CHANGE'] != '1':
1286 build.release()
1287 self.waitUntilSettled()
1288
James E. Blair08d19992016-08-10 15:25:31 -07001289 self.launch_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001290 self.waitUntilSettled()
1291
1292 self.assertEqual(A.data['status'], 'MERGED')
1293 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001294 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001295 self.assertEqual(B.data['status'], 'NEW')
1296 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001297 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001298 self.assertEqual(C.data['status'], 'NEW')
1299 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001300 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001301 self.assertEqual(D.data['status'], 'NEW')
1302 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001303 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001304 self.assertEqual(E.data['status'], 'MERGED')
1305 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001306 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001307 self.assertEqual(len(self.history), 18)
1308
James E. Blairec590122012-08-22 15:19:31 -07001309 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001310 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001311 # If it's dequeued more than once, we should see extra
1312 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001313
James E. Blair08d19992016-08-10 15:25:31 -07001314 self.launch_server.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001315 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1316 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1317 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001318 A.addApproval('code-review', 2)
1319 B.addApproval('code-review', 2)
1320 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001321
Paul Belanger58bf6912016-11-11 19:36:01 -05001322 self.launch_server.failJob('project-test1', A)
1323 self.launch_server.failJob('project-test2', A)
James E. Blair08d19992016-08-10 15:25:31 -07001324 self.launch_server.failJob('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001325
James E. Blair8b5408c2016-08-08 15:37:46 -07001326 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1327 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1328 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001329
1330 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001331
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001332 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001333 self.assertEqual(self.builds[0].name, 'project-merge')
1334 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001335
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()
James E. Blair08d19992016-08-10 15:25:31 -07001338 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001339 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001340 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001341 self.waitUntilSettled()
1342
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001343 self.assertEqual(len(self.builds), 9)
Paul Belanger58bf6912016-11-11 19:36:01 -05001344 self.assertEqual(self.builds[0].name, 'project-test1')
1345 self.assertEqual(self.builds[1].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001346 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001347 self.assertEqual(self.builds[3].name, 'project-test1')
1348 self.assertEqual(self.builds[4].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001349 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001350 self.assertEqual(self.builds[6].name, 'project-test1')
1351 self.assertEqual(self.builds[7].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001352 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001353
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001354 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001355 self.waitUntilSettled()
1356
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001357 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1358 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001359
James E. Blair08d19992016-08-10 15:25:31 -07001360 self.launch_server.hold_jobs_in_build = False
1361 self.launch_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001362 self.waitUntilSettled()
1363
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001364 self.assertEqual(len(self.builds), 0)
1365 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001366
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001367 self.assertEqual(A.data['status'], 'NEW')
1368 self.assertEqual(B.data['status'], 'MERGED')
1369 self.assertEqual(C.data['status'], 'MERGED')
1370 self.assertEqual(A.reported, 2)
1371 self.assertEqual(B.reported, 2)
1372 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001373
James E. Blairec056492016-07-22 09:45:56 -07001374 @skip("Disabled for early v3 development")
James E. Blair4ec821f2012-08-23 15:28:28 -07001375 def test_nonvoting_job(self):
1376 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001377
James E. Blair4ec821f2012-08-23 15:28:28 -07001378 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1379 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001380 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07001381 self.launch_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001382 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001383
1384 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001385
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001386 self.assertEqual(A.data['status'], 'MERGED')
1387 self.assertEqual(A.reported, 2)
1388 self.assertEqual(
1389 self.getJobFromHistory('nonvoting-project-merge').result,
1390 'SUCCESS')
1391 self.assertEqual(
1392 self.getJobFromHistory('nonvoting-project-test1').result,
1393 'SUCCESS')
1394 self.assertEqual(
1395 self.getJobFromHistory('nonvoting-project-test2').result,
1396 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001397
James E. Blair5821bd92015-09-16 08:48:15 -07001398 for build in self.builds:
1399 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1400
James E. Blaire0487072012-08-29 17:38:31 -07001401 def test_check_queue_success(self):
1402 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001403
James E. Blaire0487072012-08-29 17:38:31 -07001404 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1405 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1406
1407 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001408
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001409 self.assertEqual(A.data['status'], 'NEW')
1410 self.assertEqual(A.reported, 1)
1411 self.assertEqual(self.getJobFromHistory('project-merge').result,
1412 'SUCCESS')
1413 self.assertEqual(self.getJobFromHistory('project-test1').result,
1414 'SUCCESS')
1415 self.assertEqual(self.getJobFromHistory('project-test2').result,
1416 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001417
1418 def test_check_queue_failure(self):
1419 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001420
James E. Blaire0487072012-08-29 17:38:31 -07001421 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair08d19992016-08-10 15:25:31 -07001422 self.launch_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001423 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1424
1425 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001426
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001427 self.assertEqual(A.data['status'], 'NEW')
1428 self.assertEqual(A.reported, 1)
1429 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001430 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001431 self.assertEqual(self.getJobFromHistory('project-test1').result,
1432 'SUCCESS')
1433 self.assertEqual(self.getJobFromHistory('project-test2').result,
1434 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001435
James E. Blairec056492016-07-22 09:45:56 -07001436 @skip("Disabled for early v3 development")
James E. Blair127bc182012-08-28 15:55:15 -07001437 def test_dependent_behind_dequeue(self):
1438 "test that dependent changes behind dequeued changes work"
1439 # This complicated test is a reproduction of a real life bug
1440 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001441
James E. Blair08d19992016-08-10 15:25:31 -07001442 self.launch_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001443 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1444 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1445 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1446 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1447 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1448 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1449 D.setDependsOn(C, 1)
1450 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001451 A.addApproval('code-review', 2)
1452 B.addApproval('code-review', 2)
1453 C.addApproval('code-review', 2)
1454 D.addApproval('code-review', 2)
1455 E.addApproval('code-review', 2)
1456 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001457
1458 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001459
1460 # Change object re-use in the gerrit trigger is hidden if
1461 # changes are added in quick succession; waiting makes it more
1462 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001463 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001464 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001465 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001466 self.waitUntilSettled()
1467
James E. Blair08d19992016-08-10 15:25:31 -07001468 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001469 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001470 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001471 self.waitUntilSettled()
1472
James E. Blair8b5408c2016-08-08 15:37:46 -07001473 self.fake_gerrit.addEvent(C.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(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001476 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001477 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001478 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001479 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001480 self.waitUntilSettled()
1481
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()
James E. Blair08d19992016-08-10 15:25:31 -07001486 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001487 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001488 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001489 self.waitUntilSettled()
1490
1491 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001492
1493 # Grab pointers to the jobs we want to release before
1494 # releasing any, because list indexes may change as
1495 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001496 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001497 a.release()
1498 b.release()
1499 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001500 self.waitUntilSettled()
1501
James E. Blair08d19992016-08-10 15:25:31 -07001502 self.launch_server.hold_jobs_in_build = False
1503 self.launch_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001504 self.waitUntilSettled()
1505
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001506 self.assertEqual(A.data['status'], 'NEW')
1507 self.assertEqual(B.data['status'], 'MERGED')
1508 self.assertEqual(C.data['status'], 'MERGED')
1509 self.assertEqual(D.data['status'], 'MERGED')
1510 self.assertEqual(E.data['status'], 'MERGED')
1511 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001512
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001513 self.assertEqual(A.reported, 2)
1514 self.assertEqual(B.reported, 2)
1515 self.assertEqual(C.reported, 2)
1516 self.assertEqual(D.reported, 2)
1517 self.assertEqual(E.reported, 2)
1518 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001519
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001520 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1521 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001522
1523 def test_merger_repack(self):
1524 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001525
James E. Blair05fed602012-09-07 12:45:24 -07001526 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001527 A.addApproval('code-review', 2)
1528 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001529 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001530 self.assertEqual(self.getJobFromHistory('project-merge').result,
1531 'SUCCESS')
1532 self.assertEqual(self.getJobFromHistory('project-test1').result,
1533 'SUCCESS')
1534 self.assertEqual(self.getJobFromHistory('project-test2').result,
1535 'SUCCESS')
1536 self.assertEqual(A.data['status'], 'MERGED')
1537 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001538 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001539 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001540
Monty Taylorbc758832013-06-17 17:22:42 -04001541 path = os.path.join(self.git_root, "org/project")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001542 print(repack_repo(path))
James E. Blair05fed602012-09-07 12:45:24 -07001543
1544 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001545 A.addApproval('code-review', 2)
1546 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001547 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001548 self.assertEqual(self.getJobFromHistory('project-merge').result,
1549 'SUCCESS')
1550 self.assertEqual(self.getJobFromHistory('project-test1').result,
1551 'SUCCESS')
1552 self.assertEqual(self.getJobFromHistory('project-test2').result,
1553 'SUCCESS')
1554 self.assertEqual(A.data['status'], 'MERGED')
1555 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001556
James E. Blair4886f282012-11-15 09:27:33 -08001557 def test_merger_repack_large_change(self):
1558 "Test that the merger works with large changes after a repack"
1559 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001560 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001561 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Hesketh352264b2015-08-11 23:42:08 +10001562 url = self.fake_gerrit.getGitUrl(
Paul Belanger503bded2016-11-11 19:43:38 -05001563 tenant.layout.project_configs.get('org/project1'))
James E. Blair4076e2b2014-01-28 12:42:20 -08001564 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08001565 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1566 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001567 path = os.path.join(self.upstream_root, "org/project1")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001568 print(repack_repo(path))
Monty Taylorbc758832013-06-17 17:22:42 -04001569 path = os.path.join(self.git_root, "org/project1")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001570 print(repack_repo(path))
James E. Blair4886f282012-11-15 09:27:33 -08001571
James E. Blair8b5408c2016-08-08 15:37:46 -07001572 A.addApproval('code-review', 2)
1573 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001574 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001575 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001576 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001577 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001578 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001579 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001580 'SUCCESS')
1581 self.assertEqual(A.data['status'], 'MERGED')
1582 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001583
James E. Blairec056492016-07-22 09:45:56 -07001584 @skip("Disabled for early v3 development")
James E. Blair7ee88a22012-09-12 18:59:31 +02001585 def test_nonexistent_job(self):
1586 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001587 # Set to the state immediately after a restart
1588 self.resetGearmanServer()
1589 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02001590
1591 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001592 A.addApproval('code-review', 2)
1593 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair7ee88a22012-09-12 18:59:31 +02001594 # There may be a thread about to report a lost change
1595 while A.reported < 2:
1596 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001597 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001598 self.assertFalse(job_names)
1599 self.assertEqual(A.data['status'], 'NEW')
1600 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001601 self.assertEmptyQueues()
1602
1603 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001604 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02001605 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001606 A.addApproval('code-review', 2)
1607 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair7ee88a22012-09-12 18:59:31 +02001608 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001609 self.assertEqual(self.getJobFromHistory('project-merge').result,
1610 'SUCCESS')
1611 self.assertEqual(self.getJobFromHistory('project-test1').result,
1612 'SUCCESS')
1613 self.assertEqual(self.getJobFromHistory('project-test2').result,
1614 'SUCCESS')
1615 self.assertEqual(A.data['status'], 'MERGED')
1616 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08001617
James E. Blair2fa50962013-01-30 21:50:41 -08001618 def test_new_patchset_dequeues_old(self):
1619 "Test that a new patchset causes the old to be dequeued"
1620 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair08d19992016-08-10 15:25:31 -07001621 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001622 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1623 M.setMerged()
1624
1625 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1626 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1627 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1628 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001629 A.addApproval('code-review', 2)
1630 B.addApproval('code-review', 2)
1631 C.addApproval('code-review', 2)
1632 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001633
1634 C.setDependsOn(B, 1)
1635 B.setDependsOn(A, 1)
1636 A.setDependsOn(M, 1)
1637
James E. Blair8b5408c2016-08-08 15:37:46 -07001638 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1639 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1640 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1641 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001642 self.waitUntilSettled()
1643
1644 B.addPatchset()
1645 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1646 self.waitUntilSettled()
1647
James E. Blair08d19992016-08-10 15:25:31 -07001648 self.launch_server.hold_jobs_in_build = False
1649 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001650 self.waitUntilSettled()
1651
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001652 self.assertEqual(A.data['status'], 'MERGED')
1653 self.assertEqual(A.reported, 2)
1654 self.assertEqual(B.data['status'], 'NEW')
1655 self.assertEqual(B.reported, 2)
1656 self.assertEqual(C.data['status'], 'NEW')
1657 self.assertEqual(C.reported, 2)
1658 self.assertEqual(D.data['status'], 'MERGED')
1659 self.assertEqual(D.reported, 2)
1660 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001661
James E. Blairba437362015-02-07 11:41:52 -08001662 def test_new_patchset_check(self):
1663 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001664
James E. Blair08d19992016-08-10 15:25:31 -07001665 self.launch_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001666
1667 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001668 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001669 tenant = self.sched.abide.tenants.get('tenant-one')
1670 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001671
1672 # Add two git-dependent changes
1673 B.setDependsOn(A, 1)
1674 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1675 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001676 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1677 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001678
1679 # A live item, and a non-live/live pair
1680 items = check_pipeline.getAllItems()
1681 self.assertEqual(len(items), 3)
1682
1683 self.assertEqual(items[0].change.number, '1')
1684 self.assertEqual(items[0].change.patchset, '1')
1685 self.assertFalse(items[0].live)
1686
1687 self.assertEqual(items[1].change.number, '2')
1688 self.assertEqual(items[1].change.patchset, '1')
1689 self.assertTrue(items[1].live)
1690
1691 self.assertEqual(items[2].change.number, '1')
1692 self.assertEqual(items[2].change.patchset, '1')
1693 self.assertTrue(items[2].live)
1694
1695 # Add a new patchset to A
1696 A.addPatchset()
1697 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1698 self.waitUntilSettled()
1699
1700 # The live copy of A,1 should be gone, but the non-live and B
1701 # should continue, and we should have a new A,2
1702 items = check_pipeline.getAllItems()
1703 self.assertEqual(len(items), 3)
1704
1705 self.assertEqual(items[0].change.number, '1')
1706 self.assertEqual(items[0].change.patchset, '1')
1707 self.assertFalse(items[0].live)
1708
1709 self.assertEqual(items[1].change.number, '2')
1710 self.assertEqual(items[1].change.patchset, '1')
1711 self.assertTrue(items[1].live)
1712
1713 self.assertEqual(items[2].change.number, '1')
1714 self.assertEqual(items[2].change.patchset, '2')
1715 self.assertTrue(items[2].live)
1716
1717 # Add a new patchset to B
1718 B.addPatchset()
1719 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1720 self.waitUntilSettled()
1721
1722 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1723 # but we should have a new B,2 (still based on A,1)
1724 items = check_pipeline.getAllItems()
1725 self.assertEqual(len(items), 3)
1726
1727 self.assertEqual(items[0].change.number, '1')
1728 self.assertEqual(items[0].change.patchset, '2')
1729 self.assertTrue(items[0].live)
1730
1731 self.assertEqual(items[1].change.number, '1')
1732 self.assertEqual(items[1].change.patchset, '1')
1733 self.assertFalse(items[1].live)
1734
1735 self.assertEqual(items[2].change.number, '2')
1736 self.assertEqual(items[2].change.patchset, '2')
1737 self.assertTrue(items[2].live)
1738
1739 self.builds[0].release()
1740 self.waitUntilSettled()
1741 self.builds[0].release()
1742 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001743 self.launch_server.hold_jobs_in_build = False
1744 self.launch_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001745 self.waitUntilSettled()
1746
1747 self.assertEqual(A.reported, 1)
1748 self.assertEqual(B.reported, 1)
1749 self.assertEqual(self.history[0].result, 'ABORTED')
1750 self.assertEqual(self.history[0].changes, '1,1')
1751 self.assertEqual(self.history[1].result, 'ABORTED')
1752 self.assertEqual(self.history[1].changes, '1,1 2,1')
1753 self.assertEqual(self.history[2].result, 'SUCCESS')
1754 self.assertEqual(self.history[2].changes, '1,2')
1755 self.assertEqual(self.history[3].result, 'SUCCESS')
1756 self.assertEqual(self.history[3].changes, '1,1 2,2')
1757
1758 def test_abandoned_gate(self):
1759 "Test that an abandoned change is dequeued from gate"
1760
James E. Blair08d19992016-08-10 15:25:31 -07001761 self.launch_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001762
1763 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001764 A.addApproval('code-review', 2)
1765 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001766 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001767 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1768 self.assertEqual(self.builds[0].name, 'project-merge')
1769
1770 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1771 self.waitUntilSettled()
1772
James E. Blair08d19992016-08-10 15:25:31 -07001773 self.launch_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001774 self.waitUntilSettled()
1775
Arie5b4048c2016-10-05 16:12:06 +03001776 self.assertBuilds([])
1777 self.assertHistory([
1778 dict(name='project-merge', result='ABORTED', changes='1,1')],
1779 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001780 self.assertEqual(A.reported, 1,
1781 "Abandoned gate change should report only start")
1782
1783 def test_abandoned_check(self):
1784 "Test that an abandoned change is dequeued from check"
1785
James E. Blair08d19992016-08-10 15:25:31 -07001786 self.launch_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001787
1788 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1789 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001790 tenant = self.sched.abide.tenants.get('tenant-one')
1791 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001792
1793 # Add two git-dependent changes
1794 B.setDependsOn(A, 1)
1795 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1796 self.waitUntilSettled()
1797 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1798 self.waitUntilSettled()
1799 # A live item, and a non-live/live pair
1800 items = check_pipeline.getAllItems()
1801 self.assertEqual(len(items), 3)
1802
1803 self.assertEqual(items[0].change.number, '1')
1804 self.assertFalse(items[0].live)
1805
1806 self.assertEqual(items[1].change.number, '2')
1807 self.assertTrue(items[1].live)
1808
1809 self.assertEqual(items[2].change.number, '1')
1810 self.assertTrue(items[2].live)
1811
1812 # Abandon A
1813 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1814 self.waitUntilSettled()
1815
1816 # The live copy of A should be gone, but the non-live and B
1817 # should continue
1818 items = check_pipeline.getAllItems()
1819 self.assertEqual(len(items), 2)
1820
1821 self.assertEqual(items[0].change.number, '1')
1822 self.assertFalse(items[0].live)
1823
1824 self.assertEqual(items[1].change.number, '2')
1825 self.assertTrue(items[1].live)
1826
James E. Blair08d19992016-08-10 15:25:31 -07001827 self.launch_server.hold_jobs_in_build = False
1828 self.launch_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001829 self.waitUntilSettled()
1830
1831 self.assertEqual(len(self.history), 4)
1832 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001833 'Build should have been aborted')
1834 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001835 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001836
James E. Blairec056492016-07-22 09:45:56 -07001837 @skip("Disabled for early v3 development")
Steve Varnau7b78b312015-04-03 14:49:46 -07001838 def test_abandoned_not_timer(self):
1839 "Test that an abandoned change does not cancel timer jobs"
1840
James E. Blair08d19992016-08-10 15:25:31 -07001841 self.launch_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001842
1843 # Start timer trigger - also org/project
James E. Blairf84026c2015-12-08 16:11:46 -08001844 self.updateConfigLayout(
1845 'tests/fixtures/layout-idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001846 self.sched.reconfigure(self.config)
1847 self.registerJobs()
1848 # The pipeline triggers every second, so we should have seen
1849 # several by now.
1850 time.sleep(5)
1851 self.waitUntilSettled()
1852 # Stop queuing timer triggered jobs so that the assertions
1853 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08001854 self.updateConfigLayout(
1855 'tests/fixtures/layout-no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001856 self.sched.reconfigure(self.config)
1857 self.registerJobs()
1858 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1859
1860 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1861 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1862 self.waitUntilSettled()
1863 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1864
1865 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1866 self.waitUntilSettled()
1867
1868 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1869
James E. Blair08d19992016-08-10 15:25:31 -07001870 self.launch_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001871 self.waitUntilSettled()
1872
Arx Cruzb1b010d2013-10-28 19:49:59 -02001873 def test_zuul_url_return(self):
1874 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001875 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
James E. Blair08d19992016-08-10 15:25:31 -07001876 self.launch_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001877
1878 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001879 A.addApproval('code-review', 2)
1880 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001881 self.waitUntilSettled()
1882
1883 self.assertEqual(len(self.builds), 1)
1884 for build in self.builds:
1885 self.assertTrue('ZUUL_URL' in build.parameters)
1886
James E. Blair08d19992016-08-10 15:25:31 -07001887 self.launch_server.hold_jobs_in_build = False
1888 self.launch_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001889 self.waitUntilSettled()
1890
James E. Blair2fa50962013-01-30 21:50:41 -08001891 def test_new_patchset_dequeues_old_on_head(self):
1892 "Test that a new patchset causes the old to be dequeued (at head)"
1893 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair08d19992016-08-10 15:25:31 -07001894 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001895 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1896 M.setMerged()
1897 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1898 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1899 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1900 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001901 A.addApproval('code-review', 2)
1902 B.addApproval('code-review', 2)
1903 C.addApproval('code-review', 2)
1904 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001905
1906 C.setDependsOn(B, 1)
1907 B.setDependsOn(A, 1)
1908 A.setDependsOn(M, 1)
1909
James E. Blair8b5408c2016-08-08 15:37:46 -07001910 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1911 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1912 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1913 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001914 self.waitUntilSettled()
1915
1916 A.addPatchset()
1917 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1918 self.waitUntilSettled()
1919
James E. Blair08d19992016-08-10 15:25:31 -07001920 self.launch_server.hold_jobs_in_build = False
1921 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001922 self.waitUntilSettled()
1923
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001924 self.assertEqual(A.data['status'], 'NEW')
1925 self.assertEqual(A.reported, 2)
1926 self.assertEqual(B.data['status'], 'NEW')
1927 self.assertEqual(B.reported, 2)
1928 self.assertEqual(C.data['status'], 'NEW')
1929 self.assertEqual(C.reported, 2)
1930 self.assertEqual(D.data['status'], 'MERGED')
1931 self.assertEqual(D.reported, 2)
1932 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001933
1934 def test_new_patchset_dequeues_old_without_dependents(self):
1935 "Test that a new patchset causes only the old to be dequeued"
James E. Blair08d19992016-08-10 15:25:31 -07001936 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001937 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1938 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1939 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001940 A.addApproval('code-review', 2)
1941 B.addApproval('code-review', 2)
1942 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001943
James E. Blair8b5408c2016-08-08 15:37:46 -07001944 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1945 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1946 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001947 self.waitUntilSettled()
1948
1949 B.addPatchset()
1950 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1951 self.waitUntilSettled()
1952
James E. Blair08d19992016-08-10 15:25:31 -07001953 self.launch_server.hold_jobs_in_build = False
1954 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001955 self.waitUntilSettled()
1956
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001957 self.assertEqual(A.data['status'], 'MERGED')
1958 self.assertEqual(A.reported, 2)
1959 self.assertEqual(B.data['status'], 'NEW')
1960 self.assertEqual(B.reported, 2)
1961 self.assertEqual(C.data['status'], 'MERGED')
1962 self.assertEqual(C.reported, 2)
1963 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001964
1965 def test_new_patchset_dequeues_old_independent_queue(self):
1966 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair08d19992016-08-10 15:25:31 -07001967 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001968 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1969 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1970 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1971 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1972 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1973 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1974 self.waitUntilSettled()
1975
1976 B.addPatchset()
1977 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1978 self.waitUntilSettled()
1979
James E. Blair08d19992016-08-10 15:25:31 -07001980 self.launch_server.hold_jobs_in_build = False
1981 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001982 self.waitUntilSettled()
1983
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001984 self.assertEqual(A.data['status'], 'NEW')
1985 self.assertEqual(A.reported, 1)
1986 self.assertEqual(B.data['status'], 'NEW')
1987 self.assertEqual(B.reported, 1)
1988 self.assertEqual(C.data['status'], 'NEW')
1989 self.assertEqual(C.reported, 1)
1990 self.assertEqual(len(self.history), 10)
1991 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001992
James E. Blair18c64442014-03-18 10:14:45 -07001993 def test_noop_job(self):
1994 "Test that the internal noop job works"
1995 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001996 A.addApproval('code-review', 2)
1997 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001998 self.waitUntilSettled()
1999
2000 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2001 self.assertTrue(self.sched._areAllBuildsComplete())
2002 self.assertEqual(len(self.history), 0)
2003 self.assertEqual(A.data['status'], 'MERGED')
2004 self.assertEqual(A.reported, 2)
2005
James E. Blairec056492016-07-22 09:45:56 -07002006 @skip("Disabled for early v3 development")
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002007 def test_no_job_project(self):
2008 "Test that reports with no jobs don't get sent"
2009 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
2010 'master', 'A')
2011 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2012 self.waitUntilSettled()
2013
2014 # Change wasn't reported to
2015 self.assertEqual(A.reported, False)
2016
2017 # Check queue is empty afterwards
2018 check_pipeline = self.sched.layout.pipelines['check']
2019 items = check_pipeline.getAllItems()
2020 self.assertEqual(len(items), 0)
2021
2022 self.assertEqual(len(self.history), 0)
2023
James E. Blair7d0dedc2013-02-21 17:26:09 -08002024 def test_zuul_refs(self):
2025 "Test that zuul refs exist and have the right changes"
James E. Blair08d19992016-08-10 15:25:31 -07002026 self.launch_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002027 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2028 M1.setMerged()
2029 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2030 M2.setMerged()
2031
2032 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2033 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2034 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2035 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07002036 A.addApproval('code-review', 2)
2037 B.addApproval('code-review', 2)
2038 C.addApproval('code-review', 2)
2039 D.addApproval('code-review', 2)
2040 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2041 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
2042 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
2043 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002044
2045 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()
James E. Blair08d19992016-08-10 15:25:31 -07002050 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002051 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002052 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002053 self.waitUntilSettled()
2054
James E. Blair7d0dedc2013-02-21 17:26:09 -08002055 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08002056 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002057 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002058 if x.parameters['ZUUL_CHANGE'] == '3':
2059 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002060 a_build = x
2061 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002062 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002063 b_build = x
2064 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002065 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002066 c_build = x
2067 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002068 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002069 d_build = x
2070 if a_build and b_build and c_build and d_build:
2071 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002072
2073 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002074 self.assertIsNotNone(a_zref)
2075 self.assertIsNotNone(b_zref)
2076 self.assertIsNotNone(c_zref)
2077 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002078
2079 # And they should all be different
2080 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002081 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002082
Clint Byrum3343e3e2016-11-15 16:05:03 -08002083 # should have a, not b, and should not be in project2
2084 self.assertTrue(a_build.hasChanges(A))
2085 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002086
Clint Byrum3343e3e2016-11-15 16:05:03 -08002087 # should have a and b, and should not be in project2
2088 self.assertTrue(b_build.hasChanges(A, B))
2089 self.assertFalse(b_build.hasChanges(M2))
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 in 2
2092 self.assertTrue(c_build.hasChanges(A, B, C))
2093 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002094
Clint Byrum3343e3e2016-11-15 16:05:03 -08002095 # should have a and b in 1, c and d in 2
2096 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002097
James E. Blair08d19992016-08-10 15:25:31 -07002098 self.launch_server.hold_jobs_in_build = False
2099 self.launch_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002100 self.waitUntilSettled()
2101
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002102 self.assertEqual(A.data['status'], 'MERGED')
2103 self.assertEqual(A.reported, 2)
2104 self.assertEqual(B.data['status'], 'MERGED')
2105 self.assertEqual(B.reported, 2)
2106 self.assertEqual(C.data['status'], 'MERGED')
2107 self.assertEqual(C.reported, 2)
2108 self.assertEqual(D.data['status'], 'MERGED')
2109 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002110
James E. Blairec056492016-07-22 09:45:56 -07002111 @skip("Disabled for early v3 development")
James E. Blair4a28a882013-08-23 15:17:33 -07002112 def test_rerun_on_error(self):
2113 "Test that if a worker fails to run a job, it is run again"
James E. Blair08d19992016-08-10 15:25:31 -07002114 self.launch_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002115 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002116 A.addApproval('code-review', 2)
2117 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002118 self.waitUntilSettled()
2119
2120 self.builds[0].run_error = True
James E. Blair08d19992016-08-10 15:25:31 -07002121 self.launch_server.hold_jobs_in_build = False
2122 self.launch_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002123 self.waitUntilSettled()
2124 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2125 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2126
James E. Blair412e5582013-04-22 15:50:12 -07002127 def test_statsd(self):
2128 "Test each of the statsd methods used in the scheduler"
2129 import extras
2130 statsd = extras.try_import('statsd.statsd')
2131 statsd.incr('test-incr')
2132 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002133 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002134 self.assertReportedStat('test-incr', '1|c')
2135 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002136 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002137
James E. Blairec056492016-07-22 09:45:56 -07002138 @skip("Disabled for early v3 development")
James E. Blairdad52252014-02-07 16:59:17 -08002139 def test_stuck_job_cleanup(self):
2140 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07002141 # This job won't be registered at startup because it is not in
2142 # the standard layout, but we need it to already be registerd
2143 # for when we reconfigure, as that is when Zuul will attempt
2144 # to run the new job.
2145 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002146 self.gearman_server.hold_jobs_in_queue = True
2147 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002148 A.addApproval('code-review', 2)
2149 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002150 self.waitUntilSettled()
2151 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2152
James E. Blairf84026c2015-12-08 16:11:46 -08002153 self.updateConfigLayout(
2154 'tests/fixtures/layout-no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002155 self.sched.reconfigure(self.config)
2156 self.waitUntilSettled()
2157
James E. Blair18c64442014-03-18 10:14:45 -07002158 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002159 self.waitUntilSettled()
2160 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2161 self.assertTrue(self.sched._areAllBuildsComplete())
2162
2163 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002164 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002165 self.assertEqual(self.history[0].result, 'SUCCESS')
2166
James E. Blair879dafb2015-07-17 14:04:49 -07002167 def test_file_head(self):
2168 # This is a regression test for an observed bug. A change
2169 # with a file named "HEAD" in the root directory of the repo
2170 # was processed by a merger. It then was unable to reset the
2171 # repo because of:
2172 # GitCommandError: 'git reset --hard HEAD' returned
2173 # with exit code 128
2174 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2175 # and filename
2176 # Use '--' to separate filenames from revisions'
2177
2178 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002179 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002180 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2181
2182 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2183 self.waitUntilSettled()
2184
2185 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2186 self.waitUntilSettled()
2187
2188 self.assertIn('Build succeeded', A.messages[0])
2189 self.assertIn('Build succeeded', B.messages[0])
2190
James E. Blairec056492016-07-22 09:45:56 -07002191 @skip("Disabled for early v3 development")
James E. Blair70c71582013-03-06 08:50:50 -08002192 def test_file_jobs(self):
2193 "Test that file jobs run only when appropriate"
2194 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2195 A.addPatchset(['pip-requires'])
2196 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002197 A.addApproval('code-review', 2)
2198 B.addApproval('code-review', 2)
2199 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2200 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002201 self.waitUntilSettled()
2202
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002203 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002204 if x.name == 'project-testfile']
2205
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002206 self.assertEqual(len(testfile_jobs), 1)
2207 self.assertEqual(testfile_jobs[0].changes, '1,2')
2208 self.assertEqual(A.data['status'], 'MERGED')
2209 self.assertEqual(A.reported, 2)
2210 self.assertEqual(B.data['status'], 'MERGED')
2211 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002212
Clint Byrum3ee39f32016-11-17 23:45:07 -08002213 def _test_irrelevant_files_jobs(self, should_skip):
2214 "Test that jobs with irrelevant-files filter run only when appropriate"
2215 self.updateConfigLayout('layout-irrelevant-files')
Maru Newby3fe5f852015-01-13 04:22:14 +00002216 self.sched.reconfigure(self.config)
Clint Byrum3ee39f32016-11-17 23:45:07 -08002217
2218 if should_skip:
2219 files = {'ignoreme': 'ignored\n'}
2220 else:
2221 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002222
2223 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002224 'master',
2225 'test irrelevant-files',
2226 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002227 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2228 self.waitUntilSettled()
2229
2230 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002231 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002232
2233 if should_skip:
2234 self.assertEqual([], tested_change_ids)
2235 else:
2236 self.assertIn(change.data['number'], tested_change_ids)
2237
Clint Byrum3ee39f32016-11-17 23:45:07 -08002238 def test_irrelevant_files_match_skips_job(self):
2239 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002240
Clint Byrum3ee39f32016-11-17 23:45:07 -08002241 def test_irrelevant_files_no_match_runs_job(self):
2242 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002243
James E. Blairec056492016-07-22 09:45:56 -07002244 @skip("Disabled for early v3 development")
James E. Blair3c5e5b52013-04-26 11:17:03 -07002245 def test_test_config(self):
2246 "Test that we can test the config"
James E. Blairf84026c2015-12-08 16:11:46 -08002247 self.sched.testConfig(self.config.get('zuul', 'tenant_config'),
Joshua Hesketh352264b2015-08-11 23:42:08 +10002248 self.connections)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002249
James E. Blairec056492016-07-22 09:45:56 -07002250 @skip("Disabled for early v3 development")
James E. Blairc8a1e052014-02-25 09:29:26 -08002251 def test_queue_names(self):
2252 "Test shared change queue names"
2253 project1 = self.sched.layout.projects['org/project1']
2254 project2 = self.sched.layout.projects['org/project2']
2255 q1 = self.sched.layout.pipelines['gate'].getQueue(project1)
2256 q2 = self.sched.layout.pipelines['gate'].getQueue(project2)
2257 self.assertEqual(q1.name, 'integration')
2258 self.assertEqual(q2.name, 'integration')
2259
James E. Blairf84026c2015-12-08 16:11:46 -08002260 self.updateConfigLayout(
2261 'tests/fixtures/layout-bad-queue.yaml')
James E. Blairc8a1e052014-02-25 09:29:26 -08002262 with testtools.ExpectedException(
2263 Exception, "More than one name assigned to change queue"):
2264 self.sched.reconfigure(self.config)
2265
James E. Blair64ed6f22013-07-10 14:07:23 -07002266 def test_queue_precedence(self):
2267 "Test that queue precedence works"
2268
2269 self.gearman_server.hold_jobs_in_queue = True
James E. Blair08d19992016-08-10 15:25:31 -07002270 self.launch_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002271 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2272 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002273 A.addApproval('code-review', 2)
2274 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002275
2276 self.waitUntilSettled()
2277 self.gearman_server.hold_jobs_in_queue = False
2278 self.gearman_server.release()
2279 self.waitUntilSettled()
2280
James E. Blair8de58bd2013-07-18 16:23:33 -07002281 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002282 self.orderedRelease()
James E. Blair08d19992016-08-10 15:25:31 -07002283 self.launch_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002284 self.waitUntilSettled()
2285
James E. Blair64ed6f22013-07-10 14:07:23 -07002286 self.log.debug(self.history)
2287 self.assertEqual(self.history[0].pipeline, 'gate')
2288 self.assertEqual(self.history[1].pipeline, 'check')
2289 self.assertEqual(self.history[2].pipeline, 'gate')
2290 self.assertEqual(self.history[3].pipeline, 'gate')
2291 self.assertEqual(self.history[4].pipeline, 'check')
2292 self.assertEqual(self.history[5].pipeline, 'check')
2293
James E. Blairec056492016-07-22 09:45:56 -07002294 @skip("Disabled for early v3 development")
Clark Boylana5edbe42014-06-03 16:39:10 -07002295 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002296 "Test that we can retrieve JSON status info"
James E. Blair08d19992016-08-10 15:25:31 -07002297 self.launch_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002298 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002299 A.addApproval('code-review', 2)
2300 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002301 self.waitUntilSettled()
2302
James E. Blair08d19992016-08-10 15:25:31 -07002303 self.launch_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002304 self.waitUntilSettled()
2305
James E. Blair1843a552013-07-03 14:19:52 -07002306 port = self.webapp.server.socket.getsockname()[1]
2307
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002308 req = urllib.request.Request("http://localhost:%s/status.json" % port)
2309 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002310 headers = f.info()
2311 self.assertIn('Content-Length', headers)
2312 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002313 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2314 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002315 self.assertIn('Access-Control-Allow-Origin', headers)
2316 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002317 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002318 self.assertIn('Expires', headers)
James E. Blair1843a552013-07-03 14:19:52 -07002319 data = f.read()
2320
James E. Blair08d19992016-08-10 15:25:31 -07002321 self.launch_server.hold_jobs_in_build = False
2322 self.launch_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002323 self.waitUntilSettled()
2324
2325 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002326 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002327 for p in data['pipelines']:
2328 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002329 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002330 self.assertEqual(q['window'], 20)
2331 else:
2332 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002333 for head in q['heads']:
2334 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002335 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002336 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002337 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002338 status_jobs.append(job)
2339 self.assertEqual('project-merge', status_jobs[0]['name'])
2340 self.assertEqual('https://server/job/project-merge/0/',
2341 status_jobs[0]['url'])
2342 self.assertEqual('http://logs.example.com/1/1/gate/project-merge/0',
2343 status_jobs[0]['report_url'])
2344
2345 self.assertEqual('project-test1', status_jobs[1]['name'])
2346 self.assertEqual('https://server/job/project-test1/1/',
2347 status_jobs[1]['url'])
2348 self.assertEqual('http://logs.example.com/1/1/gate/project-test1/1',
2349 status_jobs[1]['report_url'])
2350
2351 self.assertEqual('project-test2', status_jobs[2]['name'])
2352 self.assertEqual('https://server/job/project-test2/2/',
2353 status_jobs[2]['url'])
2354 self.assertEqual('http://logs.example.com/1/1/gate/project-test2/2',
2355 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002356
James E. Blairec056492016-07-22 09:45:56 -07002357 @skip("Disabled for early v3 development")
James E. Blairc3d428e2013-12-03 15:06:48 -08002358 def test_merging_queues(self):
2359 "Test that transitively-connected change queues are merged"
James E. Blairf84026c2015-12-08 16:11:46 -08002360 self.updateConfigLayout(
2361 'tests/fixtures/layout-merge-queues.yaml')
James E. Blairc3d428e2013-12-03 15:06:48 -08002362 self.sched.reconfigure(self.config)
2363 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
2364
James E. Blairec056492016-07-22 09:45:56 -07002365 @skip("Disabled for early v3 development")
James E. Blairaf17a972016-02-03 15:07:18 -08002366 def test_mutex(self):
2367 "Test job mutexes"
2368 self.config.set('zuul', 'layout_config',
2369 'tests/fixtures/layout-mutex.yaml')
2370 self.sched.reconfigure(self.config)
2371
James E. Blair08d19992016-08-10 15:25:31 -07002372 self.launch_server.hold_jobs_in_build = True
James E. Blairaf17a972016-02-03 15:07:18 -08002373 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2374 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2375 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2376
2377 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2378 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2379 self.waitUntilSettled()
2380 self.assertEqual(len(self.builds), 3)
2381 self.assertEqual(self.builds[0].name, 'project-test1')
2382 self.assertEqual(self.builds[1].name, 'mutex-one')
2383 self.assertEqual(self.builds[2].name, 'project-test1')
2384
James E. Blair08d19992016-08-10 15:25:31 -07002385 self.launch_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002386 self.waitUntilSettled()
2387
2388 self.assertEqual(len(self.builds), 3)
2389 self.assertEqual(self.builds[0].name, 'project-test1')
2390 self.assertEqual(self.builds[1].name, 'project-test1')
2391 self.assertEqual(self.builds[2].name, 'mutex-two')
2392 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2393
James E. Blair08d19992016-08-10 15:25:31 -07002394 self.launch_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002395 self.waitUntilSettled()
2396
2397 self.assertEqual(len(self.builds), 3)
2398 self.assertEqual(self.builds[0].name, 'project-test1')
2399 self.assertEqual(self.builds[1].name, 'project-test1')
2400 self.assertEqual(self.builds[2].name, 'mutex-one')
2401 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2402
James E. Blair08d19992016-08-10 15:25:31 -07002403 self.launch_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002404 self.waitUntilSettled()
2405
2406 self.assertEqual(len(self.builds), 3)
2407 self.assertEqual(self.builds[0].name, 'project-test1')
2408 self.assertEqual(self.builds[1].name, 'project-test1')
2409 self.assertEqual(self.builds[2].name, 'mutex-two')
2410 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2411
James E. Blair08d19992016-08-10 15:25:31 -07002412 self.launch_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002413 self.waitUntilSettled()
2414
2415 self.assertEqual(len(self.builds), 2)
2416 self.assertEqual(self.builds[0].name, 'project-test1')
2417 self.assertEqual(self.builds[1].name, 'project-test1')
2418 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2419
James E. Blair08d19992016-08-10 15:25:31 -07002420 self.launch_server.hold_jobs_in_build = False
2421 self.launch_server.release()
James E. Blairaf17a972016-02-03 15:07:18 -08002422
2423 self.waitUntilSettled()
2424 self.assertEqual(len(self.builds), 0)
2425
2426 self.assertEqual(A.reported, 1)
2427 self.assertEqual(B.reported, 1)
2428 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2429
James E. Blairec056492016-07-22 09:45:56 -07002430 @skip("Disabled for early v3 development")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002431 def test_node_label(self):
2432 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002433 self.worker.registerFunction('build:node-project-test1:debian')
2434
2435 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002436 A.addApproval('code-review', 2)
2437 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002438 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002439
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002440 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2441 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2442 'debian')
2443 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002444
2445 def test_live_reconfiguration(self):
2446 "Test that live reconfiguration works"
James E. Blair08d19992016-08-10 15:25:31 -07002447 self.launch_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002448 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002449 A.addApproval('code-review', 2)
2450 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002451 self.waitUntilSettled()
2452
2453 self.sched.reconfigure(self.config)
2454
James E. Blair08d19992016-08-10 15:25:31 -07002455 self.launch_server.hold_jobs_in_build = False
2456 self.launch_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002457 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002458 self.assertEqual(self.getJobFromHistory('project-merge').result,
2459 'SUCCESS')
2460 self.assertEqual(self.getJobFromHistory('project-test1').result,
2461 'SUCCESS')
2462 self.assertEqual(self.getJobFromHistory('project-test2').result,
2463 'SUCCESS')
2464 self.assertEqual(A.data['status'], 'MERGED')
2465 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002466
James E. Blairec056492016-07-22 09:45:56 -07002467 @skip("Disabled for early v3 development")
James E. Blair6bc782d2015-07-17 16:20:21 -07002468 def test_live_reconfiguration_merge_conflict(self):
2469 # A real-world bug: a change in a gate queue has a merge
2470 # conflict and a job is added to its project while it's
2471 # sitting in the queue. The job gets added to the change and
2472 # enqueued and the change gets stuck.
2473 self.worker.registerFunction('build:project-test3')
James E. Blair08d19992016-08-10 15:25:31 -07002474 self.launch_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002475
2476 # This change is fine. It's here to stop the queue long
2477 # enough for the next change to be subject to the
2478 # reconfiguration, as well as to provide a conflict for the
2479 # next change. This change will succeed and merge.
2480 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2481 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002482 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002483
2484 # This change will be in merge conflict. During the
2485 # reconfiguration, we will add a job. We want to make sure
2486 # that doesn't cause it to get stuck.
2487 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2488 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002489 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002490
James E. Blair8b5408c2016-08-08 15:37:46 -07002491 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2492 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002493
2494 self.waitUntilSettled()
2495
2496 # No jobs have run yet
2497 self.assertEqual(A.data['status'], 'NEW')
2498 self.assertEqual(A.reported, 1)
2499 self.assertEqual(B.data['status'], 'NEW')
2500 self.assertEqual(B.reported, 1)
2501 self.assertEqual(len(self.history), 0)
2502
2503 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002504 self.updateConfigLayout(
2505 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002506 self.sched.reconfigure(self.config)
2507 self.waitUntilSettled()
2508
James E. Blair08d19992016-08-10 15:25:31 -07002509 self.launch_server.hold_jobs_in_build = False
2510 self.launch_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002511 self.waitUntilSettled()
2512
2513 self.assertEqual(A.data['status'], 'MERGED')
2514 self.assertEqual(A.reported, 2)
2515 self.assertEqual(B.data['status'], 'NEW')
2516 self.assertEqual(B.reported, 2)
2517 self.assertEqual(self.getJobFromHistory('project-merge').result,
2518 'SUCCESS')
2519 self.assertEqual(self.getJobFromHistory('project-test1').result,
2520 'SUCCESS')
2521 self.assertEqual(self.getJobFromHistory('project-test2').result,
2522 'SUCCESS')
2523 self.assertEqual(self.getJobFromHistory('project-test3').result,
2524 'SUCCESS')
2525 self.assertEqual(len(self.history), 4)
2526
James E. Blairec056492016-07-22 09:45:56 -07002527 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002528 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002529 # An extrapolation of test_live_reconfiguration_merge_conflict
2530 # that tests a job added to a job tree with a failed root does
2531 # not run.
2532 self.worker.registerFunction('build:project-test3')
James E. Blair08d19992016-08-10 15:25:31 -07002533 self.launch_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002534
2535 # This change is fine. It's here to stop the queue long
2536 # enough for the next change to be subject to the
2537 # reconfiguration. This change will succeed and merge.
2538 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2539 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002540 A.addApproval('code-review', 2)
2541 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002542 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002543 self.launch_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002544 self.waitUntilSettled()
2545
2546 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair08d19992016-08-10 15:25:31 -07002547 self.launch_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002548 B.addApproval('code-review', 2)
2549 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002550 self.waitUntilSettled()
2551
James E. Blair08d19992016-08-10 15:25:31 -07002552 self.launch_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002553 self.waitUntilSettled()
2554
2555 # Both -merge jobs have run, but no others.
2556 self.assertEqual(A.data['status'], 'NEW')
2557 self.assertEqual(A.reported, 1)
2558 self.assertEqual(B.data['status'], 'NEW')
2559 self.assertEqual(B.reported, 1)
2560 self.assertEqual(self.history[0].result, 'SUCCESS')
2561 self.assertEqual(self.history[0].name, 'project-merge')
2562 self.assertEqual(self.history[1].result, 'FAILURE')
2563 self.assertEqual(self.history[1].name, 'project-merge')
2564 self.assertEqual(len(self.history), 2)
2565
2566 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002567 self.updateConfigLayout(
2568 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002569 self.sched.reconfigure(self.config)
2570 self.waitUntilSettled()
2571
James E. Blair08d19992016-08-10 15:25:31 -07002572 self.launch_server.hold_jobs_in_build = False
2573 self.launch_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002574 self.waitUntilSettled()
2575
2576 self.assertEqual(A.data['status'], 'MERGED')
2577 self.assertEqual(A.reported, 2)
2578 self.assertEqual(B.data['status'], 'NEW')
2579 self.assertEqual(B.reported, 2)
2580 self.assertEqual(self.history[0].result, 'SUCCESS')
2581 self.assertEqual(self.history[0].name, 'project-merge')
2582 self.assertEqual(self.history[1].result, 'FAILURE')
2583 self.assertEqual(self.history[1].name, 'project-merge')
2584 self.assertEqual(self.history[2].result, 'SUCCESS')
2585 self.assertEqual(self.history[3].result, 'SUCCESS')
2586 self.assertEqual(self.history[4].result, 'SUCCESS')
2587 self.assertEqual(len(self.history), 5)
2588
James E. Blairec056492016-07-22 09:45:56 -07002589 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002590 def test_live_reconfiguration_failed_job(self):
2591 # Test that a change with a removed failing job does not
2592 # disrupt reconfiguration. If a change has a failed job and
2593 # that job is removed during a reconfiguration, we observed a
2594 # bug where the code to re-set build statuses would run on
2595 # that build and raise an exception because the job no longer
2596 # existed.
James E. Blair08d19992016-08-10 15:25:31 -07002597 self.launch_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002598
2599 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2600
2601 # This change will fail and later be removed by the reconfiguration.
James E. Blair08d19992016-08-10 15:25:31 -07002602 self.launch_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002603
2604 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2605 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002606 self.launch_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002607 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002608 self.launch_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002609 self.waitUntilSettled()
2610
2611 self.assertEqual(A.data['status'], 'NEW')
2612 self.assertEqual(A.reported, 0)
2613
2614 self.assertEqual(self.getJobFromHistory('project-merge').result,
2615 'SUCCESS')
2616 self.assertEqual(self.getJobFromHistory('project-test1').result,
2617 'FAILURE')
2618 self.assertEqual(len(self.history), 2)
2619
2620 # Remove the test1 job.
James E. Blairf84026c2015-12-08 16:11:46 -08002621 self.updateConfigLayout(
2622 'tests/fixtures/layout-live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002623 self.sched.reconfigure(self.config)
2624 self.waitUntilSettled()
2625
James E. Blair08d19992016-08-10 15:25:31 -07002626 self.launch_server.hold_jobs_in_build = False
2627 self.launch_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002628 self.waitUntilSettled()
2629
2630 self.assertEqual(self.getJobFromHistory('project-test2').result,
2631 'SUCCESS')
2632 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2633 'SUCCESS')
2634 self.assertEqual(len(self.history), 4)
2635
2636 self.assertEqual(A.data['status'], 'NEW')
2637 self.assertEqual(A.reported, 1)
2638 self.assertIn('Build succeeded', A.messages[0])
2639 # Ensure the removed job was not included in the report.
2640 self.assertNotIn('project-test1', A.messages[0])
2641
James E. Blairec056492016-07-22 09:45:56 -07002642 @skip("Disabled for early v3 development")
James E. Blairfe707d12015-08-05 15:18:15 -07002643 def test_live_reconfiguration_shared_queue(self):
2644 # Test that a change with a failing job which was removed from
2645 # this project but otherwise still exists in the system does
2646 # not disrupt reconfiguration.
2647
James E. Blair08d19992016-08-10 15:25:31 -07002648 self.launch_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002649
2650 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2651
James E. Blair08d19992016-08-10 15:25:31 -07002652 self.launch_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002653
2654 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2655 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002656 self.launch_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002657 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002658 self.launch_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002659 self.waitUntilSettled()
2660
2661 self.assertEqual(A.data['status'], 'NEW')
2662 self.assertEqual(A.reported, 0)
2663
2664 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2665 'SUCCESS')
2666 self.assertEqual(self.getJobFromHistory(
2667 'project1-project2-integration').result, 'FAILURE')
2668 self.assertEqual(len(self.history), 2)
2669
2670 # Remove the integration job.
James E. Blairf84026c2015-12-08 16:11:46 -08002671 self.updateConfigLayout(
2672 'tests/fixtures/layout-live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002673 self.sched.reconfigure(self.config)
2674 self.waitUntilSettled()
2675
James E. Blair08d19992016-08-10 15:25:31 -07002676 self.launch_server.hold_jobs_in_build = False
2677 self.launch_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002678 self.waitUntilSettled()
2679
2680 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2681 'SUCCESS')
2682 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2683 'SUCCESS')
2684 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2685 'SUCCESS')
2686 self.assertEqual(self.getJobFromHistory(
2687 'project1-project2-integration').result, 'FAILURE')
2688 self.assertEqual(len(self.history), 4)
2689
2690 self.assertEqual(A.data['status'], 'NEW')
2691 self.assertEqual(A.reported, 1)
2692 self.assertIn('Build succeeded', A.messages[0])
2693 # Ensure the removed job was not included in the report.
2694 self.assertNotIn('project1-project2-integration', A.messages[0])
2695
James E. Blairec056492016-07-22 09:45:56 -07002696 @skip("Disabled for early v3 development")
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002697 def test_double_live_reconfiguration_shared_queue(self):
2698 # This was a real-world regression. A change is added to
2699 # gate; a reconfigure happens, a second change which depends
2700 # on the first is added, and a second reconfiguration happens.
2701 # Ensure that both changes merge.
2702
2703 # A failure may indicate incorrect caching or cleaning up of
2704 # references during a reconfiguration.
James E. Blair08d19992016-08-10 15:25:31 -07002705 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002706
2707 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2708 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2709 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002710 A.addApproval('code-review', 2)
2711 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002712
2713 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002714 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002715 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002716 self.launch_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002717 self.waitUntilSettled()
2718
2719 # Reconfigure (with only one change in the pipeline).
2720 self.sched.reconfigure(self.config)
2721 self.waitUntilSettled()
2722
2723 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002724 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002725 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002726 self.launch_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002727 self.waitUntilSettled()
2728
2729 # Reconfigure (with both in the pipeline).
2730 self.sched.reconfigure(self.config)
2731 self.waitUntilSettled()
2732
James E. Blair08d19992016-08-10 15:25:31 -07002733 self.launch_server.hold_jobs_in_build = False
2734 self.launch_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002735 self.waitUntilSettled()
2736
2737 self.assertEqual(len(self.history), 8)
2738
2739 self.assertEqual(A.data['status'], 'MERGED')
2740 self.assertEqual(A.reported, 2)
2741 self.assertEqual(B.data['status'], 'MERGED')
2742 self.assertEqual(B.reported, 2)
2743
James E. Blairec056492016-07-22 09:45:56 -07002744 @skip("Disabled for early v3 development")
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002745 def test_live_reconfiguration_del_project(self):
2746 # Test project deletion from layout
2747 # while changes are enqueued
2748
James E. Blair08d19992016-08-10 15:25:31 -07002749 self.launch_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002750 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2751 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2752 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2753
2754 # A Depends-On: B
2755 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2756 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002757 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002758
2759 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2760 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2761 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002762 self.launch_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002763 self.waitUntilSettled()
2764 self.assertEqual(len(self.builds), 5)
2765
2766 # This layout defines only org/project, not org/project1
James E. Blairf84026c2015-12-08 16:11:46 -08002767 self.updateConfigLayout(
2768 'tests/fixtures/layout-live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002769 self.sched.reconfigure(self.config)
2770 self.waitUntilSettled()
2771
2772 # Builds for C aborted, builds for A succeed,
2773 # and have change B applied ahead
2774 job_c = self.getJobFromHistory('project1-test1')
2775 self.assertEqual(job_c.changes, '3,1')
2776 self.assertEqual(job_c.result, 'ABORTED')
2777
James E. Blair08d19992016-08-10 15:25:31 -07002778 self.launch_server.hold_jobs_in_build = False
2779 self.launch_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002780 self.waitUntilSettled()
2781
2782 self.assertEqual(self.getJobFromHistory('project-test1').changes,
2783 '2,1 1,1')
2784
2785 self.assertEqual(A.data['status'], 'NEW')
2786 self.assertEqual(B.data['status'], 'NEW')
2787 self.assertEqual(C.data['status'], 'NEW')
2788 self.assertEqual(A.reported, 1)
2789 self.assertEqual(B.reported, 0)
2790 self.assertEqual(C.reported, 0)
2791
2792 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
2793 self.assertIn('Build succeeded', A.messages[0])
2794
James E. Blairec056492016-07-22 09:45:56 -07002795 @skip("Disabled for early v3 development")
James E. Blaire712d9f2013-07-31 11:40:11 -07002796 def test_live_reconfiguration_functions(self):
2797 "Test live reconfiguration with a custom function"
2798 self.worker.registerFunction('build:node-project-test1:debian')
2799 self.worker.registerFunction('build:node-project-test1:wheezy')
2800 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002801 A.addApproval('code-review', 2)
2802 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002803 self.waitUntilSettled()
2804
2805 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2806 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2807 'debian')
2808 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2809
James E. Blairf84026c2015-12-08 16:11:46 -08002810 self.updateConfigLayout(
2811 'tests/fixtures/layout-live-reconfiguration-functions.yaml')
James E. Blaire712d9f2013-07-31 11:40:11 -07002812 self.sched.reconfigure(self.config)
2813 self.worker.build_history = []
2814
2815 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002816 B.addApproval('code-review', 2)
2817 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002818 self.waitUntilSettled()
2819
2820 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2821 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2822 'wheezy')
2823 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2824
James E. Blairec056492016-07-22 09:45:56 -07002825 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002826 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002827 self.updateConfigLayout(
2828 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002829 self.sched.reconfigure(self.config)
2830
2831 self.init_repo("org/new-project")
2832 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2833
James E. Blair8b5408c2016-08-08 15:37:46 -07002834 A.addApproval('code-review', 2)
2835 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002836 self.waitUntilSettled()
2837 self.assertEqual(self.getJobFromHistory('project-merge').result,
2838 'SUCCESS')
2839 self.assertEqual(self.getJobFromHistory('project-test1').result,
2840 'SUCCESS')
2841 self.assertEqual(self.getJobFromHistory('project-test2').result,
2842 'SUCCESS')
2843 self.assertEqual(A.data['status'], 'MERGED')
2844 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002845
Clark Boylan6dbbc482013-10-18 10:57:31 -07002846 def test_repo_deleted(self):
Paul Belanger66e95962016-11-11 12:11:06 -05002847 self.updateConfigLayout('layout-repo-deleted')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002848 self.sched.reconfigure(self.config)
2849
2850 self.init_repo("org/delete-project")
2851 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2852
James E. Blair8b5408c2016-08-08 15:37:46 -07002853 A.addApproval('code-review', 2)
2854 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002855 self.waitUntilSettled()
2856 self.assertEqual(self.getJobFromHistory('project-merge').result,
2857 'SUCCESS')
2858 self.assertEqual(self.getJobFromHistory('project-test1').result,
2859 'SUCCESS')
2860 self.assertEqual(self.getJobFromHistory('project-test2').result,
2861 'SUCCESS')
2862 self.assertEqual(A.data['status'], 'MERGED')
2863 self.assertEqual(A.reported, 2)
2864
2865 # Delete org/new-project zuul repo. Should be recloned.
2866 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
2867
2868 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2869
James E. Blair8b5408c2016-08-08 15:37:46 -07002870 B.addApproval('code-review', 2)
2871 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002872 self.waitUntilSettled()
2873 self.assertEqual(self.getJobFromHistory('project-merge').result,
2874 'SUCCESS')
2875 self.assertEqual(self.getJobFromHistory('project-test1').result,
2876 'SUCCESS')
2877 self.assertEqual(self.getJobFromHistory('project-test2').result,
2878 'SUCCESS')
2879 self.assertEqual(B.data['status'], 'MERGED')
2880 self.assertEqual(B.reported, 2)
2881
James E. Blairec056492016-07-22 09:45:56 -07002882 @skip("Disabled for early v3 development")
James E. Blair456f2fb2016-02-09 09:29:33 -08002883 def test_tags(self):
2884 "Test job tags"
2885 self.config.set('zuul', 'layout_config',
2886 'tests/fixtures/layout-tags.yaml')
2887 self.sched.reconfigure(self.config)
2888
2889 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2890 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2891 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2892 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2893 self.waitUntilSettled()
2894
2895 results = {'project1-merge': 'extratag merge project1',
2896 'project2-merge': 'merge'}
2897
2898 for build in self.history:
2899 self.assertEqual(results.get(build.name, ''),
2900 build.parameters.get('BUILD_TAGS'))
2901
James E. Blairec056492016-07-22 09:45:56 -07002902 @skip("Disabled for early v3 development")
James E. Blair63bb0ef2013-07-29 17:14:51 -07002903 def test_timer(self):
2904 "Test that a periodic job is triggered"
James E. Blair08d19992016-08-10 15:25:31 -07002905 self.launch_server.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08002906 self.updateConfigLayout(
2907 'tests/fixtures/layout-timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002908 self.sched.reconfigure(self.config)
2909 self.registerJobs()
2910
Clark Boylan3ee090a2014-04-03 20:55:09 -07002911 # The pipeline triggers every second, so we should have seen
2912 # several by now.
2913 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002914 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002915
2916 self.assertEqual(len(self.builds), 2)
2917
James E. Blair63bb0ef2013-07-29 17:14:51 -07002918 port = self.webapp.server.socket.getsockname()[1]
2919
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002920 req = urllib.request.Request("http://localhost:%s/status.json" % port)
2921 f = urllib.request.urlopen(req)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002922 data = f.read()
2923
James E. Blair08d19992016-08-10 15:25:31 -07002924 self.launch_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002925 # Stop queuing timer triggered jobs so that the assertions
2926 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002927 self.updateConfigLayout(
2928 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002929 self.sched.reconfigure(self.config)
2930 self.registerJobs()
James E. Blair08d19992016-08-10 15:25:31 -07002931 self.launch_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002932 self.waitUntilSettled()
2933
2934 self.assertEqual(self.getJobFromHistory(
2935 'project-bitrot-stable-old').result, 'SUCCESS')
2936 self.assertEqual(self.getJobFromHistory(
2937 'project-bitrot-stable-older').result, 'SUCCESS')
2938
2939 data = json.loads(data)
2940 status_jobs = set()
2941 for p in data['pipelines']:
2942 for q in p['change_queues']:
2943 for head in q['heads']:
2944 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07002945 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002946 for job in change['jobs']:
2947 status_jobs.add(job['name'])
2948 self.assertIn('project-bitrot-stable-old', status_jobs)
2949 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002950
James E. Blairec056492016-07-22 09:45:56 -07002951 @skip("Disabled for early v3 development")
James E. Blair4f6033c2014-03-27 15:49:09 -07002952 def test_idle(self):
2953 "Test that frequent periodic jobs work"
James E. Blair08d19992016-08-10 15:25:31 -07002954 self.launch_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002955
Clark Boylan3ee090a2014-04-03 20:55:09 -07002956 for x in range(1, 3):
2957 # Test that timer triggers periodic jobs even across
2958 # layout config reloads.
2959 # Start timer trigger
James E. Blairf84026c2015-12-08 16:11:46 -08002960 self.updateConfigLayout(
2961 'tests/fixtures/layout-idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002962 self.sched.reconfigure(self.config)
2963 self.registerJobs()
James E. Blair995fc0f2016-02-04 16:48:31 -08002964 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002965
Clark Boylan3ee090a2014-04-03 20:55:09 -07002966 # The pipeline triggers every second, so we should have seen
2967 # several by now.
2968 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002969
2970 # Stop queuing timer triggered jobs so that the assertions
2971 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002972 self.updateConfigLayout(
2973 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002974 self.sched.reconfigure(self.config)
2975 self.registerJobs()
James E. Blair995fc0f2016-02-04 16:48:31 -08002976 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002977
2978 self.assertEqual(len(self.builds), 2)
James E. Blair08d19992016-08-10 15:25:31 -07002979 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002980 self.waitUntilSettled()
2981 self.assertEqual(len(self.builds), 0)
2982 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002983
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002984 def test_check_smtp_pool(self):
Paul Belanger01be8ca2016-11-11 12:21:06 -05002985 self.updateConfigLayout('layout-smtp')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002986 self.sched.reconfigure(self.config)
2987
2988 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2989 self.waitUntilSettled()
2990
2991 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2992 self.waitUntilSettled()
2993
James E. Blairff80a2f2013-12-27 13:24:06 -08002994 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002995
2996 # A.messages only holds what FakeGerrit places in it. Thus we
2997 # work on the knowledge of what the first message should be as
2998 # it is only configured to go to SMTP.
2999
3000 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003001 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003002 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003003 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003004 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003005 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003006
3007 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003008 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003009 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003010 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003011 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003012 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003013
James E. Blairec056492016-07-22 09:45:56 -07003014 @skip("Disabled for early v3 development")
James E. Blaire5910202013-12-27 09:50:31 -08003015 def test_timer_smtp(self):
3016 "Test that a periodic job is triggered"
James E. Blair08d19992016-08-10 15:25:31 -07003017 self.launch_server.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08003018 self.updateConfigLayout(
3019 'tests/fixtures/layout-timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08003020 self.sched.reconfigure(self.config)
3021 self.registerJobs()
3022
Clark Boylan3ee090a2014-04-03 20:55:09 -07003023 # The pipeline triggers every second, so we should have seen
3024 # several by now.
3025 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003026 self.waitUntilSettled()
3027
Clark Boylan3ee090a2014-04-03 20:55:09 -07003028 self.assertEqual(len(self.builds), 2)
James E. Blair08d19992016-08-10 15:25:31 -07003029 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003030 self.waitUntilSettled()
3031 self.assertEqual(len(self.history), 2)
3032
James E. Blaire5910202013-12-27 09:50:31 -08003033 self.assertEqual(self.getJobFromHistory(
3034 'project-bitrot-stable-old').result, 'SUCCESS')
3035 self.assertEqual(self.getJobFromHistory(
3036 'project-bitrot-stable-older').result, 'SUCCESS')
3037
James E. Blairff80a2f2013-12-27 13:24:06 -08003038 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003039
3040 # A.messages only holds what FakeGerrit places in it. Thus we
3041 # work on the knowledge of what the first message should be as
3042 # it is only configured to go to SMTP.
3043
3044 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003045 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003046 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003047 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003048 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003049 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003050
Clark Boylan3ee090a2014-04-03 20:55:09 -07003051 # Stop queuing timer triggered jobs and let any that may have
3052 # queued through so that end of test assertions pass.
James E. Blairf84026c2015-12-08 16:11:46 -08003053 self.updateConfigLayout(
3054 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003055 self.sched.reconfigure(self.config)
3056 self.registerJobs()
James E. Blairf8058972014-08-15 16:09:16 -07003057 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003058 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003059 self.waitUntilSettled()
3060
James E. Blair91e34592015-07-31 16:45:59 -07003061 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003062 "Test that the RPC client can enqueue a change"
3063 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003064 A.addApproval('code-review', 2)
3065 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003066
3067 client = zuul.rpcclient.RPCClient('127.0.0.1',
3068 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003069 r = client.enqueue(tenant='tenant-one',
3070 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003071 project='org/project',
3072 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003073 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003074 self.waitUntilSettled()
3075 self.assertEqual(self.getJobFromHistory('project-merge').result,
3076 'SUCCESS')
3077 self.assertEqual(self.getJobFromHistory('project-test1').result,
3078 'SUCCESS')
3079 self.assertEqual(self.getJobFromHistory('project-test2').result,
3080 'SUCCESS')
3081 self.assertEqual(A.data['status'], 'MERGED')
3082 self.assertEqual(A.reported, 2)
3083 self.assertEqual(r, True)
3084
James E. Blairec056492016-07-22 09:45:56 -07003085 @skip("Disabled for early v3 development")
James E. Blair91e34592015-07-31 16:45:59 -07003086 def test_client_enqueue_ref(self):
3087 "Test that the RPC client can enqueue a ref"
3088
3089 client = zuul.rpcclient.RPCClient('127.0.0.1',
3090 self.gearman_server.port)
3091 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003092 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003093 pipeline='post',
3094 project='org/project',
3095 trigger='gerrit',
3096 ref='master',
3097 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
3098 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
3099 self.waitUntilSettled()
3100 job_names = [x.name for x in self.history]
3101 self.assertEqual(len(self.history), 1)
3102 self.assertIn('project-post', job_names)
3103 self.assertEqual(r, True)
3104
James E. Blairad28e912013-11-27 10:43:22 -08003105 def test_client_enqueue_negative(self):
3106 "Test that the RPC client returns errors"
3107 client = zuul.rpcclient.RPCClient('127.0.0.1',
3108 self.gearman_server.port)
3109 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003110 "Invalid tenant"):
3111 r = client.enqueue(tenant='tenant-foo',
3112 pipeline='gate',
3113 project='org/project',
3114 trigger='gerrit',
3115 change='1,1')
3116 client.shutdown()
3117 self.assertEqual(r, False)
3118
3119 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003120 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003121 r = client.enqueue(tenant='tenant-one',
3122 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003123 project='project-does-not-exist',
3124 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003125 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003126 client.shutdown()
3127 self.assertEqual(r, False)
3128
3129 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3130 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003131 r = client.enqueue(tenant='tenant-one',
3132 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003133 project='org/project',
3134 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003135 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003136 client.shutdown()
3137 self.assertEqual(r, False)
3138
3139 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3140 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003141 r = client.enqueue(tenant='tenant-one',
3142 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003143 project='org/project',
3144 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003145 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003146 client.shutdown()
3147 self.assertEqual(r, False)
3148
3149 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3150 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003151 r = client.enqueue(tenant='tenant-one',
3152 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003153 project='org/project',
3154 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003155 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003156 client.shutdown()
3157 self.assertEqual(r, False)
3158
3159 self.waitUntilSettled()
3160 self.assertEqual(len(self.history), 0)
3161 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003162
3163 def test_client_promote(self):
3164 "Test that the RPC client can promote a change"
James E. Blair08d19992016-08-10 15:25:31 -07003165 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003166 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3167 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3168 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003169 A.addApproval('code-review', 2)
3170 B.addApproval('code-review', 2)
3171 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003172
James E. Blair8b5408c2016-08-08 15:37:46 -07003173 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3174 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3175 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003176
3177 self.waitUntilSettled()
3178
Paul Belangerbaca3132016-11-04 12:49:54 -04003179 tenant = self.sched.abide.tenants.get('tenant-one')
3180 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003181 enqueue_times = {}
3182 for item in items:
3183 enqueue_times[str(item.change)] = item.enqueue_time
3184
James E. Blair36658cf2013-12-06 17:53:48 -08003185 client = zuul.rpcclient.RPCClient('127.0.0.1',
3186 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003187 r = client.promote(tenant='tenant-one',
3188 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003189 change_ids=['2,1', '3,1'])
3190
Sean Daguef39b9ca2014-01-10 21:34:35 -05003191 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003192 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003193 for item in items:
3194 self.assertEqual(
3195 enqueue_times[str(item.change)], item.enqueue_time)
3196
James E. Blair78acec92014-02-06 07:11:32 -08003197 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003198 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003199 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003200 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003201 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003202 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003203 self.waitUntilSettled()
3204
3205 self.assertEqual(len(self.builds), 6)
3206 self.assertEqual(self.builds[0].name, 'project-test1')
3207 self.assertEqual(self.builds[1].name, 'project-test2')
3208 self.assertEqual(self.builds[2].name, 'project-test1')
3209 self.assertEqual(self.builds[3].name, 'project-test2')
3210 self.assertEqual(self.builds[4].name, 'project-test1')
3211 self.assertEqual(self.builds[5].name, 'project-test2')
3212
Paul Belangerbaca3132016-11-04 12:49:54 -04003213 self.assertTrue(self.builds[0].hasChanges(B))
3214 self.assertFalse(self.builds[0].hasChanges(A))
3215 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003216
Paul Belangerbaca3132016-11-04 12:49:54 -04003217 self.assertTrue(self.builds[2].hasChanges(B))
3218 self.assertTrue(self.builds[2].hasChanges(C))
3219 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003220
Paul Belangerbaca3132016-11-04 12:49:54 -04003221 self.assertTrue(self.builds[4].hasChanges(B))
3222 self.assertTrue(self.builds[4].hasChanges(C))
3223 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003224
James E. Blair08d19992016-08-10 15:25:31 -07003225 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003226 self.waitUntilSettled()
3227
3228 self.assertEqual(A.data['status'], 'MERGED')
3229 self.assertEqual(A.reported, 2)
3230 self.assertEqual(B.data['status'], 'MERGED')
3231 self.assertEqual(B.reported, 2)
3232 self.assertEqual(C.data['status'], 'MERGED')
3233 self.assertEqual(C.reported, 2)
3234
3235 client.shutdown()
3236 self.assertEqual(r, True)
3237
3238 def test_client_promote_dependent(self):
3239 "Test that the RPC client can promote a dependent change"
3240 # C (depends on B) -> B -> A ; then promote C to get:
3241 # A -> C (depends on B) -> B
James E. Blair08d19992016-08-10 15:25:31 -07003242 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003243 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3244 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3245 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3246
3247 C.setDependsOn(B, 1)
3248
James E. Blair8b5408c2016-08-08 15:37:46 -07003249 A.addApproval('code-review', 2)
3250 B.addApproval('code-review', 2)
3251 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003252
James E. Blair8b5408c2016-08-08 15:37:46 -07003253 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3254 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3255 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003256
3257 self.waitUntilSettled()
3258
3259 client = zuul.rpcclient.RPCClient('127.0.0.1',
3260 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003261 r = client.promote(tenant='tenant-one',
3262 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003263 change_ids=['3,1'])
3264
James E. Blair78acec92014-02-06 07:11:32 -08003265 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003266 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003267 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003268 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003269 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003270 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003271 self.waitUntilSettled()
3272
3273 self.assertEqual(len(self.builds), 6)
3274 self.assertEqual(self.builds[0].name, 'project-test1')
3275 self.assertEqual(self.builds[1].name, 'project-test2')
3276 self.assertEqual(self.builds[2].name, 'project-test1')
3277 self.assertEqual(self.builds[3].name, 'project-test2')
3278 self.assertEqual(self.builds[4].name, 'project-test1')
3279 self.assertEqual(self.builds[5].name, 'project-test2')
3280
Paul Belangerbaca3132016-11-04 12:49:54 -04003281 self.assertTrue(self.builds[0].hasChanges(B))
3282 self.assertFalse(self.builds[0].hasChanges(A))
3283 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003284
Paul Belangerbaca3132016-11-04 12:49:54 -04003285 self.assertTrue(self.builds[2].hasChanges(B))
3286 self.assertTrue(self.builds[2].hasChanges(C))
3287 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003288
Paul Belangerbaca3132016-11-04 12:49:54 -04003289 self.assertTrue(self.builds[4].hasChanges(B))
3290 self.assertTrue(self.builds[4].hasChanges(C))
3291 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003292
James E. Blair08d19992016-08-10 15:25:31 -07003293 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003294 self.waitUntilSettled()
3295
3296 self.assertEqual(A.data['status'], 'MERGED')
3297 self.assertEqual(A.reported, 2)
3298 self.assertEqual(B.data['status'], 'MERGED')
3299 self.assertEqual(B.reported, 2)
3300 self.assertEqual(C.data['status'], 'MERGED')
3301 self.assertEqual(C.reported, 2)
3302
3303 client.shutdown()
3304 self.assertEqual(r, True)
3305
3306 def test_client_promote_negative(self):
3307 "Test that the RPC client returns errors for promotion"
James E. Blair08d19992016-08-10 15:25:31 -07003308 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003309 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003310 A.addApproval('code-review', 2)
3311 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003312 self.waitUntilSettled()
3313
3314 client = zuul.rpcclient.RPCClient('127.0.0.1',
3315 self.gearman_server.port)
3316
3317 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003318 r = client.promote(tenant='tenant-one',
3319 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003320 change_ids=['2,1', '3,1'])
3321 client.shutdown()
3322 self.assertEqual(r, False)
3323
3324 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003325 r = client.promote(tenant='tenant-one',
3326 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003327 change_ids=['4,1'])
3328 client.shutdown()
3329 self.assertEqual(r, False)
3330
James E. Blair08d19992016-08-10 15:25:31 -07003331 self.launch_server.hold_jobs_in_build = False
3332 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003333 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003334
James E. Blairec056492016-07-22 09:45:56 -07003335 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003336 def test_queue_rate_limiting(self):
3337 "Test that DependentPipelines are rate limited with dep across window"
James E. Blairf84026c2015-12-08 16:11:46 -08003338 self.updateConfigLayout(
3339 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003340 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07003341 self.launch_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003342 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3343 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3344 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3345
3346 C.setDependsOn(B, 1)
James E. Blair08d19992016-08-10 15:25:31 -07003347 self.launch_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003348
James E. Blair8b5408c2016-08-08 15:37:46 -07003349 A.addApproval('code-review', 2)
3350 B.addApproval('code-review', 2)
3351 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003352
James E. Blair8b5408c2016-08-08 15:37:46 -07003353 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3354 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3355 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003356 self.waitUntilSettled()
3357
3358 # Only A and B will have their merge jobs queued because
3359 # window is 2.
3360 self.assertEqual(len(self.builds), 2)
3361 self.assertEqual(self.builds[0].name, 'project-merge')
3362 self.assertEqual(self.builds[1].name, 'project-merge')
3363
James E. Blair08d19992016-08-10 15:25:31 -07003364 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003365 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003366 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003367 self.waitUntilSettled()
3368
3369 # Only A and B will have their test jobs queued because
3370 # window is 2.
3371 self.assertEqual(len(self.builds), 4)
3372 self.assertEqual(self.builds[0].name, 'project-test1')
3373 self.assertEqual(self.builds[1].name, 'project-test2')
3374 self.assertEqual(self.builds[2].name, 'project-test1')
3375 self.assertEqual(self.builds[3].name, 'project-test2')
3376
James E. Blair08d19992016-08-10 15:25:31 -07003377 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003378 self.waitUntilSettled()
3379
3380 queue = self.sched.layout.pipelines['gate'].queues[0]
3381 # A failed so window is reduced by 1 to 1.
3382 self.assertEqual(queue.window, 1)
3383 self.assertEqual(queue.window_floor, 1)
3384 self.assertEqual(A.data['status'], 'NEW')
3385
3386 # Gate is reset and only B's merge job is queued because
3387 # window shrunk to 1.
3388 self.assertEqual(len(self.builds), 1)
3389 self.assertEqual(self.builds[0].name, 'project-merge')
3390
James E. Blair08d19992016-08-10 15:25:31 -07003391 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003392 self.waitUntilSettled()
3393
3394 # Only B's test jobs are queued because window is still 1.
3395 self.assertEqual(len(self.builds), 2)
3396 self.assertEqual(self.builds[0].name, 'project-test1')
3397 self.assertEqual(self.builds[1].name, 'project-test2')
3398
James E. Blair08d19992016-08-10 15:25:31 -07003399 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003400 self.waitUntilSettled()
3401
3402 # B was successfully merged so window is increased to 2.
3403 self.assertEqual(queue.window, 2)
3404 self.assertEqual(queue.window_floor, 1)
3405 self.assertEqual(B.data['status'], 'MERGED')
3406
3407 # Only C is left and its merge job is queued.
3408 self.assertEqual(len(self.builds), 1)
3409 self.assertEqual(self.builds[0].name, 'project-merge')
3410
James E. Blair08d19992016-08-10 15:25:31 -07003411 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003412 self.waitUntilSettled()
3413
3414 # After successful merge job the test jobs for C are queued.
3415 self.assertEqual(len(self.builds), 2)
3416 self.assertEqual(self.builds[0].name, 'project-test1')
3417 self.assertEqual(self.builds[1].name, 'project-test2')
3418
James E. Blair08d19992016-08-10 15:25:31 -07003419 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003420 self.waitUntilSettled()
3421
3422 # C successfully merged so window is bumped to 3.
3423 self.assertEqual(queue.window, 3)
3424 self.assertEqual(queue.window_floor, 1)
3425 self.assertEqual(C.data['status'], 'MERGED')
3426
James E. Blairec056492016-07-22 09:45:56 -07003427 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003428 def test_queue_rate_limiting_dependent(self):
3429 "Test that DependentPipelines are rate limited with dep in window"
James E. Blairf84026c2015-12-08 16:11:46 -08003430 self.updateConfigLayout(
3431 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003432 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07003433 self.launch_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003434 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3435 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3436 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3437
3438 B.setDependsOn(A, 1)
3439
James E. Blair08d19992016-08-10 15:25:31 -07003440 self.launch_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003441
James E. Blair8b5408c2016-08-08 15:37:46 -07003442 A.addApproval('code-review', 2)
3443 B.addApproval('code-review', 2)
3444 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003445
James E. Blair8b5408c2016-08-08 15:37:46 -07003446 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3447 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3448 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003449 self.waitUntilSettled()
3450
3451 # Only A and B will have their merge jobs queued because
3452 # window is 2.
3453 self.assertEqual(len(self.builds), 2)
3454 self.assertEqual(self.builds[0].name, 'project-merge')
3455 self.assertEqual(self.builds[1].name, 'project-merge')
3456
James E. Blair08d19992016-08-10 15:25:31 -07003457 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003458 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003459 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003460 self.waitUntilSettled()
3461
3462 # Only A and B will have their test jobs queued because
3463 # window is 2.
3464 self.assertEqual(len(self.builds), 4)
3465 self.assertEqual(self.builds[0].name, 'project-test1')
3466 self.assertEqual(self.builds[1].name, 'project-test2')
3467 self.assertEqual(self.builds[2].name, 'project-test1')
3468 self.assertEqual(self.builds[3].name, 'project-test2')
3469
James E. Blair08d19992016-08-10 15:25:31 -07003470 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003471 self.waitUntilSettled()
3472
3473 queue = self.sched.layout.pipelines['gate'].queues[0]
3474 # A failed so window is reduced by 1 to 1.
3475 self.assertEqual(queue.window, 1)
3476 self.assertEqual(queue.window_floor, 1)
3477 self.assertEqual(A.data['status'], 'NEW')
3478 self.assertEqual(B.data['status'], 'NEW')
3479
3480 # Gate is reset and only C's merge job is queued because
3481 # window shrunk to 1 and A and B were dequeued.
3482 self.assertEqual(len(self.builds), 1)
3483 self.assertEqual(self.builds[0].name, 'project-merge')
3484
James E. Blair08d19992016-08-10 15:25:31 -07003485 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003486 self.waitUntilSettled()
3487
3488 # Only C's test jobs are queued because window is still 1.
3489 self.assertEqual(len(self.builds), 2)
3490 self.assertEqual(self.builds[0].name, 'project-test1')
3491 self.assertEqual(self.builds[1].name, 'project-test2')
3492
James E. Blair08d19992016-08-10 15:25:31 -07003493 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003494 self.waitUntilSettled()
3495
3496 # C was successfully merged so window is increased to 2.
3497 self.assertEqual(queue.window, 2)
3498 self.assertEqual(queue.window_floor, 1)
3499 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003500
James E. Blairec056492016-07-22 09:45:56 -07003501 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003502 def test_worker_update_metadata(self):
3503 "Test if a worker can send back metadata about itself"
James E. Blair08d19992016-08-10 15:25:31 -07003504 self.launch_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003505
3506 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003507 A.addApproval('code-review', 2)
3508 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003509 self.waitUntilSettled()
3510
3511 self.assertEqual(len(self.launcher.builds), 1)
3512
3513 self.log.debug('Current builds:')
3514 self.log.debug(self.launcher.builds)
3515
3516 start = time.time()
3517 while True:
3518 if time.time() - start > 10:
3519 raise Exception("Timeout waiting for gearman server to report "
3520 + "back to the client")
3521 build = self.launcher.builds.values()[0]
3522 if build.worker.name == "My Worker":
3523 break
3524 else:
3525 time.sleep(0)
3526
3527 self.log.debug(build)
3528 self.assertEqual("My Worker", build.worker.name)
3529 self.assertEqual("localhost", build.worker.hostname)
3530 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3531 self.assertEqual("zuul.example.org", build.worker.fqdn)
3532 self.assertEqual("FakeBuilder", build.worker.program)
3533 self.assertEqual("v1.1", build.worker.version)
3534 self.assertEqual({'something': 'else'}, build.worker.extra)
3535
James E. Blair08d19992016-08-10 15:25:31 -07003536 self.launch_server.hold_jobs_in_build = False
3537 self.launch_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003538 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003539
James E. Blairec056492016-07-22 09:45:56 -07003540 @skip("Disabled for early v3 development")
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003541 def test_footer_message(self):
3542 "Test a pipeline's footer message is correctly added to the report."
James E. Blairf84026c2015-12-08 16:11:46 -08003543 self.updateConfigLayout(
3544 'tests/fixtures/layout-footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003545 self.sched.reconfigure(self.config)
3546 self.registerJobs()
3547
3548 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003549 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07003550 self.launch_server.failJob('test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003551 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003552 self.waitUntilSettled()
3553
3554 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003555 B.addApproval('code-review', 2)
3556 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003557 self.waitUntilSettled()
3558
3559 self.assertEqual(2, len(self.smtp_messages))
3560
3561 failure_body = """\
3562Build failed. For information on how to proceed, see \
3563http://wiki.example.org/Test_Failures
3564
3565- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
3566- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
3567
3568For CI problems and help debugging, contact ci@example.org"""
3569
3570 success_body = """\
3571Build succeeded.
3572
3573- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
3574- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
3575
3576For CI problems and help debugging, contact ci@example.org"""
3577
3578 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
3579 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11003580
James E. Blairec056492016-07-22 09:45:56 -07003581 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003582 def test_merge_failure_reporters(self):
3583 """Check that the config is set up correctly"""
3584
James E. Blairf84026c2015-12-08 16:11:46 -08003585 self.updateConfigLayout(
3586 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003587 self.sched.reconfigure(self.config)
3588 self.registerJobs()
3589
3590 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003591 "Merge Failed.\n\nThis change or one of its cross-repo "
3592 "dependencies was unable to be automatically merged with the "
3593 "current state of its repository. Please rebase the change and "
3594 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003595 self.sched.layout.pipelines['check'].merge_failure_message)
3596 self.assertEqual(
3597 "The merge failed! For more information...",
3598 self.sched.layout.pipelines['gate'].merge_failure_message)
3599
3600 self.assertEqual(
3601 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3602 self.assertEqual(
3603 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3604
3605 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003606 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3607 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003608
3609 self.assertTrue(
3610 (
3611 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003612 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003613 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003614 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003615 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003616 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003617 ) or (
3618 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003619 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003620 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003621 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003622 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003623 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003624 )
3625 )
3626
James E. Blairec056492016-07-22 09:45:56 -07003627 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003628 def test_merge_failure_reports(self):
3629 """Check that when a change fails to merge the correct message is sent
3630 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003631 self.updateConfigLayout(
3632 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003633 self.sched.reconfigure(self.config)
3634 self.registerJobs()
3635
3636 # Check a test failure isn't reported to SMTP
3637 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003638 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07003639 self.launch_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003640 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003641 self.waitUntilSettled()
3642
3643 self.assertEqual(3, len(self.history)) # 3 jobs
3644 self.assertEqual(0, len(self.smtp_messages))
3645
3646 # Check a merge failure is reported to SMTP
3647 # B should be merged, but C will conflict with B
3648 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3649 B.addPatchset(['conflict'])
3650 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3651 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003652 B.addApproval('code-review', 2)
3653 C.addApproval('code-review', 2)
3654 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3655 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003656 self.waitUntilSettled()
3657
3658 self.assertEqual(6, len(self.history)) # A and B jobs
3659 self.assertEqual(1, len(self.smtp_messages))
3660 self.assertEqual('The merge failed! For more information...',
3661 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003662
James E. Blairec056492016-07-22 09:45:56 -07003663 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003664 def test_default_merge_failure_reports(self):
3665 """Check that the default merge failure reports are correct."""
3666
3667 # A should report success, B should report merge failure.
3668 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3669 A.addPatchset(['conflict'])
3670 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3671 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003672 A.addApproval('code-review', 2)
3673 B.addApproval('code-review', 2)
3674 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3675 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003676 self.waitUntilSettled()
3677
3678 self.assertEqual(3, len(self.history)) # A jobs
3679 self.assertEqual(A.reported, 2)
3680 self.assertEqual(B.reported, 2)
3681 self.assertEqual(A.data['status'], 'MERGED')
3682 self.assertEqual(B.data['status'], 'NEW')
3683 self.assertIn('Build succeeded', A.messages[1])
3684 self.assertIn('Merge Failed', B.messages[1])
3685 self.assertIn('automatically merged', B.messages[1])
3686 self.assertNotIn('logs.example.com', B.messages[1])
3687 self.assertNotIn('SKIPPED', B.messages[1])
3688
James E. Blairec056492016-07-22 09:45:56 -07003689 @skip("Disabled for early v3 development")
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003690 def test_swift_instructions(self):
3691 "Test that the correct swift instructions are sent to the workers"
James E. Blairf84026c2015-12-08 16:11:46 -08003692 self.updateConfigLayout(
3693 'tests/fixtures/layout-swift.yaml')
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003694 self.sched.reconfigure(self.config)
3695 self.registerJobs()
3696
James E. Blair08d19992016-08-10 15:25:31 -07003697 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003698 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3699
James E. Blair8b5408c2016-08-08 15:37:46 -07003700 A.addApproval('code-review', 2)
3701 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003702 self.waitUntilSettled()
3703
3704 self.assertEqual(
3705 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
3706 "gate/test-merge/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003707 self.builds[0].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003708 self.assertEqual(5,
3709 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
3710 split('\n')))
3711 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
3712
3713 self.assertEqual(
3714 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
3715 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003716 self.builds[1].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003717 self.assertEqual(5,
3718 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
3719 split('\n')))
3720 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
3721
3722 self.assertEqual(
3723 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
3724 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003725 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003726 self.assertEqual(5,
3727 len(self.builds[1].
3728 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
3729 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
3730
James E. Blair08d19992016-08-10 15:25:31 -07003731 self.launch_server.hold_jobs_in_build = False
3732 self.launch_server.release()
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003733 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003734
James E. Blairec056492016-07-22 09:45:56 -07003735 @skip("Disabled for early v3 development")
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003736 def test_client_get_running_jobs(self):
3737 "Test that the RPC client can get a list of running jobs"
James E. Blair08d19992016-08-10 15:25:31 -07003738 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003739 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003740 A.addApproval('code-review', 2)
3741 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003742 self.waitUntilSettled()
3743
3744 client = zuul.rpcclient.RPCClient('127.0.0.1',
3745 self.gearman_server.port)
3746
3747 # Wait for gearman server to send the initial workData back to zuul
3748 start = time.time()
3749 while True:
3750 if time.time() - start > 10:
3751 raise Exception("Timeout waiting for gearman server to report "
3752 + "back to the client")
3753 build = self.launcher.builds.values()[0]
3754 if build.worker.name == "My Worker":
3755 break
3756 else:
3757 time.sleep(0)
3758
3759 running_items = client.get_running_jobs()
3760
3761 self.assertEqual(1, len(running_items))
3762 running_item = running_items[0]
3763 self.assertEqual([], running_item['failing_reasons'])
3764 self.assertEqual([], running_item['items_behind'])
3765 self.assertEqual('https://hostname/1', running_item['url'])
3766 self.assertEqual(None, running_item['item_ahead'])
3767 self.assertEqual('org/project', running_item['project'])
3768 self.assertEqual(None, running_item['remaining_time'])
3769 self.assertEqual(True, running_item['active'])
3770 self.assertEqual('1,1', running_item['id'])
3771
3772 self.assertEqual(3, len(running_item['jobs']))
3773 for job in running_item['jobs']:
3774 if job['name'] == 'project-merge':
3775 self.assertEqual('project-merge', job['name'])
3776 self.assertEqual('gate', job['pipeline'])
3777 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003778 self.assertEqual('https://server/job/project-merge/0/',
3779 job['url'])
3780 self.assertEqual(7, len(job['worker']))
3781 self.assertEqual(False, job['canceled'])
3782 self.assertEqual(True, job['voting'])
3783 self.assertEqual(None, job['result'])
3784 self.assertEqual('gate', job['pipeline'])
3785 break
3786
James E. Blair08d19992016-08-10 15:25:31 -07003787 self.launch_server.hold_jobs_in_build = False
3788 self.launch_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003789 self.waitUntilSettled()
3790
3791 running_items = client.get_running_jobs()
3792 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003793
3794 def test_nonvoting_pipeline(self):
3795 "Test that a nonvoting pipeline (experimental) can still report"
3796
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003797 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3798 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003799 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3800 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003801
3802 self.assertEqual(self.getJobFromHistory('project-merge').result,
3803 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003804 self.assertEqual(
3805 self.getJobFromHistory('experimental-project-test').result,
3806 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003807 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003808
3809 def test_crd_gate(self):
3810 "Test cross-repo dependencies"
3811 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3812 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003813 A.addApproval('code-review', 2)
3814 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003815
3816 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3817 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3818 AM2.setMerged()
3819 AM1.setMerged()
3820
3821 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3822 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3823 BM2.setMerged()
3824 BM1.setMerged()
3825
3826 # A -> AM1 -> AM2
3827 # B -> BM1 -> BM2
3828 # A Depends-On: B
3829 # M2 is here to make sure it is never queried. If it is, it
3830 # means zuul is walking down the entire history of merged
3831 # changes.
3832
3833 B.setDependsOn(BM1, 1)
3834 BM1.setDependsOn(BM2, 1)
3835
3836 A.setDependsOn(AM1, 1)
3837 AM1.setDependsOn(AM2, 1)
3838
3839 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3840 A.subject, B.data['id'])
3841
James E. Blair8b5408c2016-08-08 15:37:46 -07003842 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003843 self.waitUntilSettled()
3844
3845 self.assertEqual(A.data['status'], 'NEW')
3846 self.assertEqual(B.data['status'], 'NEW')
3847
Paul Belanger5dccbe72016-11-14 11:17:37 -05003848 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003849 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003850
James E. Blair08d19992016-08-10 15:25:31 -07003851 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003852 B.addApproval('approved', 1)
3853 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003854 self.waitUntilSettled()
3855
James E. Blair08d19992016-08-10 15:25:31 -07003856 self.launch_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003857 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003858 self.launch_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003859 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003860 self.launch_server.hold_jobs_in_build = False
3861 self.launch_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003862 self.waitUntilSettled()
3863
3864 self.assertEqual(AM2.queried, 0)
3865 self.assertEqual(BM2.queried, 0)
3866 self.assertEqual(A.data['status'], 'MERGED')
3867 self.assertEqual(B.data['status'], 'MERGED')
3868 self.assertEqual(A.reported, 2)
3869 self.assertEqual(B.reported, 2)
3870
Paul Belanger5dccbe72016-11-14 11:17:37 -05003871 changes = self.getJobFromHistory(
3872 'project-merge', 'org/project1').changes
3873 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003874
3875 def test_crd_branch(self):
3876 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003877
3878 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003879 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3880 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3881 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3882 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003883 A.addApproval('code-review', 2)
3884 B.addApproval('code-review', 2)
3885 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003886
3887 # A Depends-On: B+C
3888 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3889 A.subject, B.data['id'])
3890
James E. Blair08d19992016-08-10 15:25:31 -07003891 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003892 B.addApproval('approved', 1)
3893 C.addApproval('approved', 1)
3894 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003895 self.waitUntilSettled()
3896
James E. Blair08d19992016-08-10 15:25:31 -07003897 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003898 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003899 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003900 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003901 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003902 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003903 self.launch_server.hold_jobs_in_build = False
3904 self.launch_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003905 self.waitUntilSettled()
3906
3907 self.assertEqual(A.data['status'], 'MERGED')
3908 self.assertEqual(B.data['status'], 'MERGED')
3909 self.assertEqual(C.data['status'], 'MERGED')
3910 self.assertEqual(A.reported, 2)
3911 self.assertEqual(B.reported, 2)
3912 self.assertEqual(C.reported, 2)
3913
Paul Belanger6379db12016-11-14 13:57:54 -05003914 changes = self.getJobFromHistory(
3915 'project-merge', 'org/project1').changes
3916 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003917
3918 def test_crd_multiline(self):
3919 "Test multiple depends-on lines in commit"
3920 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3921 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3922 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003923 A.addApproval('code-review', 2)
3924 B.addApproval('code-review', 2)
3925 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003926
3927 # A Depends-On: B+C
3928 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3929 A.subject, B.data['id'], C.data['id'])
3930
James E. Blair08d19992016-08-10 15:25:31 -07003931 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003932 B.addApproval('approved', 1)
3933 C.addApproval('approved', 1)
3934 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003935 self.waitUntilSettled()
3936
James E. Blair08d19992016-08-10 15:25:31 -07003937 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003938 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003939 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003940 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003941 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003942 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003943 self.launch_server.hold_jobs_in_build = False
3944 self.launch_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003945 self.waitUntilSettled()
3946
3947 self.assertEqual(A.data['status'], 'MERGED')
3948 self.assertEqual(B.data['status'], 'MERGED')
3949 self.assertEqual(C.data['status'], 'MERGED')
3950 self.assertEqual(A.reported, 2)
3951 self.assertEqual(B.reported, 2)
3952 self.assertEqual(C.reported, 2)
3953
Paul Belanger5dccbe72016-11-14 11:17:37 -05003954 changes = self.getJobFromHistory(
3955 'project-merge', 'org/project1').changes
3956 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003957
3958 def test_crd_unshared_gate(self):
3959 "Test cross-repo dependencies in unshared gate queues"
3960 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3961 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003962 A.addApproval('code-review', 2)
3963 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003964
3965 # A Depends-On: B
3966 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3967 A.subject, B.data['id'])
3968
3969 # A and B do not share a queue, make sure that A is unable to
3970 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003971 B.addApproval('approved', 1)
3972 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003973 self.waitUntilSettled()
3974
3975 self.assertEqual(A.data['status'], 'NEW')
3976 self.assertEqual(B.data['status'], 'NEW')
3977 self.assertEqual(A.reported, 0)
3978 self.assertEqual(B.reported, 0)
3979 self.assertEqual(len(self.history), 0)
3980
3981 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003982 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003983 self.waitUntilSettled()
3984
3985 self.assertEqual(B.data['status'], 'MERGED')
3986 self.assertEqual(B.reported, 2)
3987
3988 # Now that B is merged, A should be able to be enqueued and
3989 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003990 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003991 self.waitUntilSettled()
3992
3993 self.assertEqual(A.data['status'], 'MERGED')
3994 self.assertEqual(A.reported, 2)
3995
James E. Blair96698e22015-04-02 07:48:21 -07003996 def test_crd_gate_reverse(self):
3997 "Test reverse cross-repo dependencies"
3998 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3999 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004000 A.addApproval('code-review', 2)
4001 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07004002
4003 # A Depends-On: B
4004
4005 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4006 A.subject, B.data['id'])
4007
James E. Blair8b5408c2016-08-08 15:37:46 -07004008 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004009 self.waitUntilSettled()
4010
4011 self.assertEqual(A.data['status'], 'NEW')
4012 self.assertEqual(B.data['status'], 'NEW')
4013
James E. Blair08d19992016-08-10 15:25:31 -07004014 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07004015 A.addApproval('approved', 1)
4016 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004017 self.waitUntilSettled()
4018
James E. Blair08d19992016-08-10 15:25:31 -07004019 self.launch_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004020 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07004021 self.launch_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004022 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07004023 self.launch_server.hold_jobs_in_build = False
4024 self.launch_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07004025 self.waitUntilSettled()
4026
4027 self.assertEqual(A.data['status'], 'MERGED')
4028 self.assertEqual(B.data['status'], 'MERGED')
4029 self.assertEqual(A.reported, 2)
4030 self.assertEqual(B.reported, 2)
4031
Paul Belanger5dccbe72016-11-14 11:17:37 -05004032 changes = self.getJobFromHistory(
4033 'project-merge', 'org/project1').changes
4034 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07004035
James E. Blair5ee24252014-12-30 10:12:29 -08004036 def test_crd_cycle(self):
4037 "Test cross-repo dependency cycles"
4038 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4039 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004040 A.addApproval('code-review', 2)
4041 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004042
4043 # A -> B -> A (via commit-depends)
4044
4045 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4046 A.subject, B.data['id'])
4047 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4048 B.subject, A.data['id'])
4049
James E. Blair8b5408c2016-08-08 15:37:46 -07004050 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004051 self.waitUntilSettled()
4052
4053 self.assertEqual(A.reported, 0)
4054 self.assertEqual(B.reported, 0)
4055 self.assertEqual(A.data['status'], 'NEW')
4056 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004057
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004058 def test_crd_gate_unknown(self):
4059 "Test unknown projects in dependent pipeline"
4060 self.init_repo("org/unknown")
4061 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4062 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004063 A.addApproval('code-review', 2)
4064 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004065
4066 # A Depends-On: B
4067 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4068 A.subject, B.data['id'])
4069
James E. Blair8b5408c2016-08-08 15:37:46 -07004070 B.addApproval('approved', 1)
4071 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004072 self.waitUntilSettled()
4073
4074 # Unknown projects cannot share a queue with any other
4075 # since they don't have common jobs with any other (they have no jobs).
4076 # Changes which depend on unknown project changes
4077 # should not be processed in dependent pipeline
4078 self.assertEqual(A.data['status'], 'NEW')
4079 self.assertEqual(B.data['status'], 'NEW')
4080 self.assertEqual(A.reported, 0)
4081 self.assertEqual(B.reported, 0)
4082 self.assertEqual(len(self.history), 0)
4083
4084 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07004085 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004086 B.setMerged()
4087 self.waitUntilSettled()
4088 self.assertEqual(len(self.history), 0)
4089
4090 # Now that B is merged, A should be able to be enqueued and
4091 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07004092 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004093 self.waitUntilSettled()
4094
4095 self.assertEqual(A.data['status'], 'MERGED')
4096 self.assertEqual(A.reported, 2)
4097 self.assertEqual(B.data['status'], 'MERGED')
4098 self.assertEqual(B.reported, 0)
4099
James E. Blairbfb8e042014-12-30 17:01:44 -08004100 def test_crd_check(self):
4101 "Test cross-repo dependencies in independent pipelines"
4102
Paul Belangerb30342b2016-11-14 12:30:43 -05004103 self.launch_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004104 self.gearman_server.hold_jobs_in_queue = True
4105 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4106 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4107
4108 # A Depends-On: B
4109 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4110 A.subject, B.data['id'])
4111
4112 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4113 self.waitUntilSettled()
4114
4115 queue = self.gearman_server.getQueue()
4116 ref = self.getParameter(queue[-1], 'ZUUL_REF')
4117 self.gearman_server.hold_jobs_in_queue = False
4118 self.gearman_server.release()
4119 self.waitUntilSettled()
4120
Paul Belangerb30342b2016-11-14 12:30:43 -05004121 self.launch_server.release('.*-merge')
4122 self.waitUntilSettled()
4123
4124 path = os.path.join(self.builds[0].jobdir.git_root, "org/project1")
James E. Blairbfb8e042014-12-30 17:01:44 -08004125 repo = git.Repo(path)
4126 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4127 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004128 correct_messages = [
4129 'initial commit', 'add content from fixture', 'A-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004130 self.assertEqual(repo_messages, correct_messages)
4131
Paul Belangerb30342b2016-11-14 12:30:43 -05004132 path = os.path.join(self.builds[0].jobdir.git_root, "org/project2")
James E. Blairbfb8e042014-12-30 17:01:44 -08004133 repo = git.Repo(path)
4134 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4135 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004136 correct_messages = [
4137 'initial commit', 'add content from fixture', 'B-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004138 self.assertEqual(repo_messages, correct_messages)
4139
Paul Belangerb30342b2016-11-14 12:30:43 -05004140 self.launch_server.hold_jobs_in_build = False
4141 self.launch_server.release()
4142 self.waitUntilSettled()
4143
James E. Blairbfb8e042014-12-30 17:01:44 -08004144 self.assertEqual(A.data['status'], 'NEW')
4145 self.assertEqual(B.data['status'], 'NEW')
4146 self.assertEqual(A.reported, 1)
4147 self.assertEqual(B.reported, 0)
4148
4149 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004150 tenant = self.sched.abide.tenants.get('tenant-one')
4151 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004152
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004153 def test_crd_check_git_depends(self):
4154 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004155 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004156 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4157 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4158
4159 # Add two git-dependent changes and make sure they both report
4160 # success.
4161 B.setDependsOn(A, 1)
4162 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4163 self.waitUntilSettled()
4164 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4165 self.waitUntilSettled()
4166
James E. Blairb8c16472015-05-05 14:55:26 -07004167 self.orderedRelease()
4168 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004169 self.waitUntilSettled()
4170
4171 self.assertEqual(A.data['status'], 'NEW')
4172 self.assertEqual(B.data['status'], 'NEW')
4173 self.assertEqual(A.reported, 1)
4174 self.assertEqual(B.reported, 1)
4175
4176 self.assertEqual(self.history[0].changes, '1,1')
4177 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004178 tenant = self.sched.abide.tenants.get('tenant-one')
4179 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004180
4181 self.assertIn('Build succeeded', A.messages[0])
4182 self.assertIn('Build succeeded', B.messages[0])
4183
4184 def test_crd_check_duplicate(self):
4185 "Test duplicate check in independent pipelines"
James E. Blair08d19992016-08-10 15:25:31 -07004186 self.launch_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004187 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4188 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004189 tenant = self.sched.abide.tenants.get('tenant-one')
4190 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004191
4192 # Add two git-dependent changes...
4193 B.setDependsOn(A, 1)
4194 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4195 self.waitUntilSettled()
4196 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4197
4198 # ...make sure the live one is not duplicated...
4199 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4200 self.waitUntilSettled()
4201 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4202
4203 # ...but the non-live one is able to be.
4204 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4205 self.waitUntilSettled()
4206 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4207
Clark Boylandd849822015-03-02 12:38:14 -08004208 # Release jobs in order to avoid races with change A jobs
4209 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004210 self.orderedRelease()
James E. Blair08d19992016-08-10 15:25:31 -07004211 self.launch_server.hold_jobs_in_build = False
4212 self.launch_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004213 self.waitUntilSettled()
4214
4215 self.assertEqual(A.data['status'], 'NEW')
4216 self.assertEqual(B.data['status'], 'NEW')
4217 self.assertEqual(A.reported, 1)
4218 self.assertEqual(B.reported, 1)
4219
4220 self.assertEqual(self.history[0].changes, '1,1 2,1')
4221 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004222 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004223
4224 self.assertIn('Build succeeded', A.messages[0])
4225 self.assertIn('Build succeeded', B.messages[0])
4226
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004227 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004228 "Test cross-repo dependencies re-enqueued in independent pipelines"
4229
4230 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004231 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4232 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004233
4234 # A Depends-On: B
4235 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4236 A.subject, B.data['id'])
4237
4238 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4239 self.waitUntilSettled()
4240
4241 self.sched.reconfigure(self.config)
4242
4243 # Make sure the items still share a change queue, and the
4244 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004245 tenant = self.sched.abide.tenants.get('tenant-one')
4246 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4247 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004248 first_item = queue.queue[0]
4249 for item in queue.queue:
4250 self.assertEqual(item.queue, first_item.queue)
4251 self.assertFalse(first_item.live)
4252 self.assertTrue(queue.queue[1].live)
4253
4254 self.gearman_server.hold_jobs_in_queue = False
4255 self.gearman_server.release()
4256 self.waitUntilSettled()
4257
4258 self.assertEqual(A.data['status'], 'NEW')
4259 self.assertEqual(B.data['status'], 'NEW')
4260 self.assertEqual(A.reported, 1)
4261 self.assertEqual(B.reported, 0)
4262
4263 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004264 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004265
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004266 def test_crd_check_reconfiguration(self):
4267 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4268
4269 def test_crd_undefined_project(self):
4270 """Test that undefined projects in dependencies are handled for
4271 independent pipelines"""
4272 # It's a hack for fake gerrit,
4273 # as it implies repo creation upon the creation of any change
4274 self.init_repo("org/unknown")
4275 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4276
James E. Blairec056492016-07-22 09:45:56 -07004277 @skip("Disabled for early v3 development")
James E. Blair17dd6772015-02-09 14:45:18 -08004278 def test_crd_check_ignore_dependencies(self):
4279 "Test cross-repo dependencies can be ignored"
James E. Blairf84026c2015-12-08 16:11:46 -08004280 self.updateConfigLayout(
4281 'tests/fixtures/layout-ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004282 self.sched.reconfigure(self.config)
4283 self.registerJobs()
4284
4285 self.gearman_server.hold_jobs_in_queue = True
4286 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4287 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4288 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4289
4290 # A Depends-On: B
4291 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4292 A.subject, B.data['id'])
4293 # C git-depends on B
4294 C.setDependsOn(B, 1)
4295 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4296 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4297 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4298 self.waitUntilSettled()
4299
4300 # Make sure none of the items share a change queue, and all
4301 # are live.
4302 check_pipeline = self.sched.layout.pipelines['check']
4303 self.assertEqual(len(check_pipeline.queues), 3)
4304 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4305 for item in check_pipeline.getAllItems():
4306 self.assertTrue(item.live)
4307
4308 self.gearman_server.hold_jobs_in_queue = False
4309 self.gearman_server.release()
4310 self.waitUntilSettled()
4311
4312 self.assertEqual(A.data['status'], 'NEW')
4313 self.assertEqual(B.data['status'], 'NEW')
4314 self.assertEqual(C.data['status'], 'NEW')
4315 self.assertEqual(A.reported, 1)
4316 self.assertEqual(B.reported, 1)
4317 self.assertEqual(C.reported, 1)
4318
4319 # Each job should have tested exactly one change
4320 for job in self.history:
4321 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004322
James E. Blairec056492016-07-22 09:45:56 -07004323 @skip("Disabled for early v3 development")
James E. Blair96698e22015-04-02 07:48:21 -07004324 def test_crd_check_transitive(self):
4325 "Test transitive cross-repo dependencies"
4326 # Specifically, if A -> B -> C, and C gets a new patchset and
4327 # A gets a new patchset, ensure the test of A,2 includes B,1
4328 # and C,2 (not C,1 which would indicate stale data in the
4329 # cache for B).
4330 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4331 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4332 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4333
4334 # A Depends-On: B
4335 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4336 A.subject, B.data['id'])
4337
4338 # B Depends-On: C
4339 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4340 B.subject, C.data['id'])
4341
4342 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4343 self.waitUntilSettled()
4344 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4345
4346 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4347 self.waitUntilSettled()
4348 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4349
4350 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4351 self.waitUntilSettled()
4352 self.assertEqual(self.history[-1].changes, '3,1')
4353
4354 C.addPatchset()
4355 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4356 self.waitUntilSettled()
4357 self.assertEqual(self.history[-1].changes, '3,2')
4358
4359 A.addPatchset()
4360 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4361 self.waitUntilSettled()
4362 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004363
James E. Blaira8b90b32016-08-24 15:18:50 -07004364 def test_crd_check_unknown(self):
4365 "Test unknown projects in independent pipeline"
4366 self.init_repo("org/unknown")
4367 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4368 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4369 # A Depends-On: B
4370 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4371 A.subject, B.data['id'])
4372
4373 # Make sure zuul has seen an event on B.
4374 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4375 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4376 self.waitUntilSettled()
4377
4378 self.assertEqual(A.data['status'], 'NEW')
4379 self.assertEqual(A.reported, 1)
4380 self.assertEqual(B.data['status'], 'NEW')
4381 self.assertEqual(B.reported, 0)
4382
James E. Blair92464a22016-04-05 10:21:26 -07004383 def test_crd_cycle_join(self):
4384 "Test an updated change creates a cycle"
4385 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4386
4387 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4388 self.waitUntilSettled()
4389
4390 # Create B->A
4391 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4392 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4393 B.subject, A.data['id'])
4394 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4395 self.waitUntilSettled()
4396
4397 # Update A to add A->B (a cycle).
4398 A.addPatchset()
4399 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4400 A.subject, B.data['id'])
4401 # Normally we would submit the patchset-created event for
4402 # processing here, however, we have no way of noting whether
4403 # the dependency cycle detection correctly raised an
4404 # exception, so instead, we reach into the source driver and
4405 # call the method that would ultimately be called by the event
4406 # processing.
4407
Paul Belanger0e155e22016-11-14 14:12:23 -05004408 tenant = self.sched.abide.tenants.get('tenant-one')
4409 source = tenant.layout.pipelines['gate'].source
4410
4411 # TODO(pabelanger): As we add more source / trigger APIs we should make
4412 # it easier for users to create events for testing.
4413 event = zuul.model.TriggerEvent()
4414 event.trigger_name = 'gerrit'
4415 event.change_number = '1'
4416 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004417 with testtools.ExpectedException(
4418 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004419 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004420 self.log.debug("Got expected dependency cycle exception")
4421
4422 # Now if we update B to remove the depends-on, everything
4423 # should be okay. B; A->B
4424
4425 B.addPatchset()
4426 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004427
4428 source.getChange(event, True)
4429 event.change_number = '2'
4430 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004431
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004432 def test_disable_at(self):
4433 "Test a pipeline will only report to the disabled trigger when failing"
4434
Paul Belanger7dc76e82016-11-11 16:51:08 -05004435 self.updateConfigLayout('layout-disabled-at')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004436 self.sched.reconfigure(self.config)
4437
Paul Belanger7dc76e82016-11-11 16:51:08 -05004438 tenant = self.sched.abide.tenants.get('openstack')
4439 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004440 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004441 0, tenant.layout.pipelines['check']._consecutive_failures)
4442 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004443
4444 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4445 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4446 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4447 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4448 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4449 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4450 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4451 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4452 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4453 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4454 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4455
James E. Blair08d19992016-08-10 15:25:31 -07004456 self.launch_server.failJob('project-test1', A)
4457 self.launch_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004458 # Let C pass, resetting the counter
James E. Blair08d19992016-08-10 15:25:31 -07004459 self.launch_server.failJob('project-test1', D)
4460 self.launch_server.failJob('project-test1', E)
4461 self.launch_server.failJob('project-test1', F)
4462 self.launch_server.failJob('project-test1', G)
4463 self.launch_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004464 # I also passes but should only report to the disabled reporters
James E. Blair08d19992016-08-10 15:25:31 -07004465 self.launch_server.failJob('project-test1', J)
4466 self.launch_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004467
4468 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4469 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4470 self.waitUntilSettled()
4471
4472 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004473 2, tenant.layout.pipelines['check']._consecutive_failures)
4474 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004475
4476 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4477 self.waitUntilSettled()
4478
4479 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004480 0, tenant.layout.pipelines['check']._consecutive_failures)
4481 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004482
4483 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4484 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4485 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4486 self.waitUntilSettled()
4487
4488 # We should be disabled now
4489 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004490 3, tenant.layout.pipelines['check']._consecutive_failures)
4491 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004492
4493 # We need to wait between each of these patches to make sure the
4494 # smtp messages come back in an expected order
4495 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4496 self.waitUntilSettled()
4497 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4498 self.waitUntilSettled()
4499 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4500 self.waitUntilSettled()
4501
4502 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4503 # leaving a message on each change
4504 self.assertEqual(1, len(A.messages))
4505 self.assertIn('Build failed.', A.messages[0])
4506 self.assertEqual(1, len(B.messages))
4507 self.assertIn('Build failed.', B.messages[0])
4508 self.assertEqual(1, len(C.messages))
4509 self.assertIn('Build succeeded.', C.messages[0])
4510 self.assertEqual(1, len(D.messages))
4511 self.assertIn('Build failed.', D.messages[0])
4512 self.assertEqual(1, len(E.messages))
4513 self.assertIn('Build failed.', E.messages[0])
4514 self.assertEqual(1, len(F.messages))
4515 self.assertIn('Build failed.', F.messages[0])
4516
4517 # The last 3 (GHI) would have only reported via smtp.
4518 self.assertEqual(3, len(self.smtp_messages))
4519 self.assertEqual(0, len(G.messages))
4520 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004521 self.assertIn(
4522 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004523 self.assertEqual(0, len(H.messages))
4524 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004525 self.assertIn(
4526 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004527 self.assertEqual(0, len(I.messages))
4528 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004529 self.assertIn(
4530 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004531
4532 # Now reload the configuration (simulate a HUP) to check the pipeline
4533 # comes out of disabled
4534 self.sched.reconfigure(self.config)
4535
Paul Belanger7dc76e82016-11-11 16:51:08 -05004536 tenant = self.sched.abide.tenants.get('openstack')
4537
4538 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004539 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004540 0, tenant.layout.pipelines['check']._consecutive_failures)
4541 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004542
4543 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4544 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4545 self.waitUntilSettled()
4546
4547 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004548 2, tenant.layout.pipelines['check']._consecutive_failures)
4549 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004550
4551 # J and K went back to gerrit
4552 self.assertEqual(1, len(J.messages))
4553 self.assertIn('Build failed.', J.messages[0])
4554 self.assertEqual(1, len(K.messages))
4555 self.assertIn('Build failed.', K.messages[0])
4556 # No more messages reported via smtp
4557 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004558
James E. Blairec056492016-07-22 09:45:56 -07004559 @skip("Disabled for early v3 development")
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004560 def test_success_pattern(self):
4561 "Ensure bad build params are ignored"
4562
4563 # Use SMTP reporter to grab the result message easier
4564 self.init_repo("org/docs")
4565 self.config.set('zuul', 'layout_config',
4566 'tests/fixtures/layout-success-pattern.yaml')
4567 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07004568 self.launch_server.hold_jobs_in_build = True
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004569 self.registerJobs()
4570
4571 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4572 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4573 self.waitUntilSettled()
4574
4575 # Grab build id
4576 self.assertEqual(len(self.builds), 1)
4577 uuid = self.builds[0].unique[:7]
4578
James E. Blair08d19992016-08-10 15:25:31 -07004579 self.launch_server.hold_jobs_in_build = False
4580 self.launch_server.release()
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004581 self.waitUntilSettled()
4582
4583 self.assertEqual(len(self.smtp_messages), 1)
4584 body = self.smtp_messages[0]['body'].splitlines()
4585 self.assertEqual('Build succeeded.', body[0])
4586
4587 self.assertIn(
4588 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4589 'docs-draft-test/%s/publish-docs/' % uuid,
4590 body[2])
4591 self.assertIn(
4592 '- docs-draft-test2 https://server/job/docs-draft-test2/1/',
4593 body[3])
Arieb6f068c2016-10-09 13:11:06 +03004594
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004595 @skip("Disabled for early v3 development")
Paul Belanger71d98172016-11-08 10:56:31 -05004596 def test_rerun_on_abort(self):
4597 "Test that if a worker fails to run a job, it is run again"
4598
4599 self.config.set('zuul', 'layout_config',
4600 'tests/fixtures/layout-abort-attempts.yaml')
4601 self.sched.reconfigure(self.config)
4602 self.worker.hold_jobs_in_build = True
4603 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4604 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4605 self.waitUntilSettled()
4606
4607 self.worker.release('.*-merge')
4608 self.waitUntilSettled()
4609
4610 self.assertEqual(len(self.builds), 2)
4611 self.builds[0].requeue = True
4612 self.worker.release('.*-test*')
4613 self.waitUntilSettled()
4614
4615 for x in range(3):
4616 self.assertEqual(len(self.builds), 1)
4617 self.builds[0].requeue = True
4618 self.worker.release('.*-test1')
4619 self.waitUntilSettled()
4620
4621 self.worker.hold_jobs_in_build = False
4622 self.worker.release()
4623 self.waitUntilSettled()
4624 self.assertEqual(len(self.history), 6)
4625 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4626 self.assertEqual(A.reported, 1)
4627 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004628
Arieb6f068c2016-10-09 13:11:06 +03004629
4630class TestDuplicatePipeline(ZuulTestCase):
4631 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4632
4633 def test_duplicate_pipelines(self):
4634 "Test that a change matching multiple pipelines works"
4635
4636 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4637 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4638 self.waitUntilSettled()
4639
4640 self.assertHistory([
4641 dict(name='project-test1', result='SUCCESS', changes='1,1',
4642 pipeline='dup1'),
4643 dict(name='project-test1', result='SUCCESS', changes='1,1',
4644 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004645 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004646
4647 self.assertEqual(len(A.messages), 2)
4648
Paul Belangera46a3742016-11-09 14:23:03 -05004649 if 'dup1' in A.messages[0]:
4650 self.assertIn('dup1', A.messages[0])
4651 self.assertNotIn('dup2', A.messages[0])
4652 self.assertIn('project-test1', A.messages[0])
4653 self.assertIn('dup2', A.messages[1])
4654 self.assertNotIn('dup1', A.messages[1])
4655 self.assertIn('project-test1', A.messages[1])
4656 else:
4657 self.assertIn('dup1', A.messages[1])
4658 self.assertNotIn('dup2', A.messages[1])
4659 self.assertIn('project-test1', A.messages[1])
4660 self.assertIn('dup2', A.messages[0])
4661 self.assertNotIn('dup1', A.messages[0])
4662 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004663
4664
4665class TestSchedulerOneJobProject(ZuulTestCase):
4666 tenant_config_file = 'config/one-job-project/main.yaml'
4667
4668 def test_one_job_project(self):
4669 "Test that queueing works with one job"
4670 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4671 'master', 'A')
4672 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4673 'master', 'B')
4674 A.addApproval('code-review', 2)
4675 B.addApproval('code-review', 2)
4676 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4677 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4678 self.waitUntilSettled()
4679
4680 self.assertEqual(A.data['status'], 'MERGED')
4681 self.assertEqual(A.reported, 2)
4682 self.assertEqual(B.data['status'], 'MERGED')
4683 self.assertEqual(B.reported, 2)
Paul Belanger86085b32016-11-03 12:48:57 -04004684
4685
4686class TestSchedulerTemplatedProject(ZuulTestCase):
4687 tenant_config_file = 'config/templated-project/main.yaml'
4688
4689 def test_job_from_templates_launched(self):
4690 "Test whether a job generated via a template can be launched"
4691
4692 A = self.fake_gerrit.addFakeChange(
4693 'org/templated-project', 'master', 'A')
4694 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4695 self.waitUntilSettled()
4696
4697 self.assertEqual(self.getJobFromHistory('project-test1').result,
4698 'SUCCESS')
4699 self.assertEqual(self.getJobFromHistory('project-test2').result,
4700 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004701
4702 def test_layered_templates(self):
4703 "Test whether a job generated via a template can be launched"
4704
4705 A = self.fake_gerrit.addFakeChange(
4706 'org/layered-project', 'master', 'A')
4707 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4708 self.waitUntilSettled()
4709
4710 self.assertEqual(self.getJobFromHistory('project-test1').result,
4711 'SUCCESS')
4712 self.assertEqual(self.getJobFromHistory('project-test2').result,
4713 'SUCCESS')
4714 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4715 ).result, 'SUCCESS')
4716 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4717 ).result, 'SUCCESS')
4718 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4719 ).result, 'SUCCESS')
4720 self.assertEqual(self.getJobFromHistory('project-test6').result,
4721 'SUCCESS')