blob: 328089b6aeedc76a6ed2bda1196046c74ee797cd [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
Maru Newby3fe5f852015-01-13 04:22:14 +000023import yaml
James E. Blairec056492016-07-22 09:45:56 -070024from unittest import skip
Monty Taylorbc758832013-06-17 17:22:42 -040025
James E. Blair4886cc12012-07-18 15:39:41 -070026import git
Morgan Fainberg293f7f82016-05-30 14:01:22 -070027from six.moves import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040028import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070029
Maru Newby3fe5f852015-01-13 04:22:14 +000030import zuul.change_matcher
James E. Blairb0fcae42012-07-17 11:12:10 -070031import zuul.scheduler
James E. Blairad28e912013-11-27 10:43:22 -080032import zuul.rpcclient
Joshua Hesketh1879cf72013-08-19 14:13:15 +100033import zuul.reporter.gerrit
Joshua Hesketh5fea8672013-08-19 17:32:01 +100034import zuul.reporter.smtp
Paul Belanger9bba4902016-11-02 16:07:33 -040035import zuul.model
James E. Blairb0fcae42012-07-17 11:12:10 -070036
Maru Newby3fe5f852015-01-13 04:22:14 +000037from tests.base import (
Maru Newby3fe5f852015-01-13 04:22:14 +000038 ZuulTestCase,
39 repack_repo,
40)
James E. Blairb0fcae42012-07-17 11:12:10 -070041
James E. Blair1f4c2bb2013-04-26 08:40:46 -070042logging.basicConfig(level=logging.DEBUG,
43 format='%(asctime)s %(name)-32s '
44 '%(levelname)-8s %(message)s')
James E. Blairb0fcae42012-07-17 11:12:10 -070045
46
Clark Boylanb640e052014-04-03 16:41:46 -070047class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070048 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010049
James E. Blairb0fcae42012-07-17 11:12:10 -070050 def test_jobs_launched(self):
51 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070052
James E. Blairb0fcae42012-07-17 11:12:10 -070053 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070054 A.addApproval('code-review', 2)
55 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070056 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040057 self.assertEqual(self.getJobFromHistory('project-merge').result,
58 'SUCCESS')
59 self.assertEqual(self.getJobFromHistory('project-test1').result,
60 'SUCCESS')
61 self.assertEqual(self.getJobFromHistory('project-test2').result,
62 'SUCCESS')
63 self.assertEqual(A.data['status'], 'MERGED')
64 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070065 self.assertEqual(self.getJobFromHistory('project-test1').node,
66 'image1')
James E. Blairb0fcae42012-07-17 11:12:10 -070067
James E. Blair552b54f2016-07-22 13:55:32 -070068 # TODOv3(jeblair): we may want to report stats by tenant (also?).
James E. Blair66eeebf2013-07-27 17:44:32 -070069 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
70 self.assertReportedStat('zuul.pipeline.gate.current_changes',
71 value='1|g')
72 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
73 kind='ms')
74 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
75 value='1|c')
76 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
77 self.assertReportedStat('zuul.pipeline.gate.total_changes',
78 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070079 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070080 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -070081 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070082 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070083
James E. Blair5821bd92015-09-16 08:48:15 -070084 for build in self.builds:
85 self.assertEqual(build.parameters['ZUUL_VOTING'], '1')
86
James E. Blair3cb10702013-08-24 08:56:03 -070087 def test_initial_pipeline_gauges(self):
88 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -070089 self.assertReportedStat('zuul.pipeline.gate.current_changes',
90 value='0|g')
91 self.assertReportedStat('zuul.pipeline.check.current_changes',
92 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -070093
James E. Blair34776ee2016-08-25 13:53:54 -070094 def test_job_branch(self):
95 "Test the correct variant of a job runs on a branch"
96 self.create_branch('org/project', 'stable')
97 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
98 A.addApproval('code-review', 2)
99 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
100 self.waitUntilSettled()
101 self.assertEqual(self.getJobFromHistory('project-test1').result,
102 'SUCCESS')
103 self.assertEqual(self.getJobFromHistory('project-test2').result,
104 'SUCCESS')
105 self.assertEqual(A.data['status'], 'MERGED')
106 self.assertEqual(A.reported, 2,
107 "A should report start and success")
108 self.assertIn('gate', A.messages[1],
109 "A should transit gate")
110 self.assertEqual(self.getJobFromHistory('project-test1').node,
111 'image2')
112
James E. Blairb0fcae42012-07-17 11:12:10 -0700113 def test_parallel_changes(self):
114 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700115
James E. Blair791b5392016-08-03 11:25:56 -0700116 self.launch_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700117 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
118 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
119 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700120 A.addApproval('code-review', 2)
121 B.addApproval('code-review', 2)
122 C.addApproval('code-review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700123
James E. Blair8b5408c2016-08-08 15:37:46 -0700124 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
125 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
126 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700127
128 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400129 self.assertEqual(len(self.builds), 1)
130 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700131 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700132
James E. Blair791b5392016-08-03 11:25:56 -0700133 self.launch_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700134 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400135 self.assertEqual(len(self.builds), 3)
136 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700137 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400138 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700139 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400140 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700141 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700142
James E. Blair791b5392016-08-03 11:25:56 -0700143 self.launch_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700144 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400145 self.assertEqual(len(self.builds), 5)
146 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700147 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400148 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700149 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700150
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400151 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700152 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400153 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700154 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700155
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400156 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700157 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700158
James E. Blair791b5392016-08-03 11:25:56 -0700159 self.launch_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700160 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400161 self.assertEqual(len(self.builds), 6)
162 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700163 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400164 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700165 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700166
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400167 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700168 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400169 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700170 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700171
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400172 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700173 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400174 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700175 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700176
James E. Blair791b5392016-08-03 11:25:56 -0700177 self.launch_server.hold_jobs_in_build = False
178 self.launch_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700179 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400180 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700181
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400182 self.assertEqual(len(self.history), 9)
183 self.assertEqual(A.data['status'], 'MERGED')
184 self.assertEqual(B.data['status'], 'MERGED')
185 self.assertEqual(C.data['status'], 'MERGED')
186 self.assertEqual(A.reported, 2)
187 self.assertEqual(B.reported, 2)
188 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700189
190 def test_failed_changes(self):
191 "Test that a change behind a failed change is retested"
James E. Blair08d19992016-08-10 15:25:31 -0700192 self.launch_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700193
James E. Blairb02a3bb2012-07-30 17:49:55 -0700194 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
195 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -0700196 A.addApproval('code-review', 2)
197 B.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700198
James E. Blair08d19992016-08-10 15:25:31 -0700199 self.launch_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700200
James E. Blair8b5408c2016-08-08 15:37:46 -0700201 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
202 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700203 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700204 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400205
James E. Blair08d19992016-08-10 15:25:31 -0700206 self.launch_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400207 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700208 # A/project-merge is complete
209 self.assertBuilds([
210 dict(name='project-test1', changes='1,1'),
211 dict(name='project-test2', changes='1,1'),
212 dict(name='project-merge', changes='1,1 2,1'),
213 ])
James E. Blaire2819012013-06-28 17:17:26 -0400214
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700215 self.launch_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400216 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700217 # A/project-merge is complete
218 # B/project-merge is complete
219 self.assertBuilds([
220 dict(name='project-test1', changes='1,1'),
221 dict(name='project-test2', changes='1,1'),
222 dict(name='project-test1', changes='1,1 2,1'),
223 dict(name='project-test2', changes='1,1 2,1'),
224 ])
225
226 # Release project-test1 for A which will fail. This will
227 # abort both running B jobs and relaunch project-merge for B.
228 self.builds[0].release()
229 self.waitUntilSettled()
230
231 self.orderedRelease()
232 self.assertHistory([
233 dict(name='project-merge', result='SUCCESS', changes='1,1'),
234 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
235 dict(name='project-test1', result='FAILURE', changes='1,1'),
236 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
237 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
238 dict(name='project-test2', result='SUCCESS', changes='1,1'),
239 dict(name='project-merge', result='SUCCESS', changes='2,1'),
240 dict(name='project-test1', result='SUCCESS', changes='2,1'),
241 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700242 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700243
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400244 self.assertEqual(A.data['status'], 'NEW')
245 self.assertEqual(B.data['status'], 'MERGED')
246 self.assertEqual(A.reported, 2)
247 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700248
249 def test_independent_queues(self):
250 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700251
James E. Blair08d19992016-08-10 15:25:31 -0700252 self.launch_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900253 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700254 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
255 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700256 A.addApproval('code-review', 2)
257 B.addApproval('code-review', 2)
258 C.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700259
James E. Blair8b5408c2016-08-08 15:37:46 -0700260 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
261 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
262 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700263
James E. Blairb02a3bb2012-07-30 17:49:55 -0700264 self.waitUntilSettled()
265
266 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700267 self.assertBuilds([
268 dict(name='project-merge', changes='1,1'),
269 dict(name='project-merge', changes='2,1'),
270 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700271
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700272 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700273 self.builds[0].release()
274 self.waitUntilSettled()
275 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700276 self.waitUntilSettled()
277 # Release the merge job for project2 which is behind project1
James E. Blair08d19992016-08-10 15:25:31 -0700278 self.launch_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700279 self.waitUntilSettled()
280
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700281 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700282 self.assertBuilds([
283 dict(name='project-test1', changes='1,1'),
284 dict(name='project-test2', changes='1,1'),
285 dict(name='project-test1', changes='2,1'),
286 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500287 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700288 dict(name='project-test1', changes='2,1 3,1'),
289 dict(name='project-test2', changes='2,1 3,1'),
290 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700291
James E. Blair5f0d49e2016-08-19 09:36:50 -0700292 self.orderedRelease()
293 self.assertHistory([
294 dict(name='project-merge', result='SUCCESS', changes='1,1'),
295 dict(name='project-merge', result='SUCCESS', changes='2,1'),
296 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
297 dict(name='project-test1', result='SUCCESS', changes='1,1'),
298 dict(name='project-test2', result='SUCCESS', changes='1,1'),
299 dict(name='project-test1', result='SUCCESS', changes='2,1'),
300 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500301 dict(
302 name='project1-project2-integration',
303 result='SUCCESS',
304 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700305 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
306 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
307 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700308
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400309 self.assertEqual(A.data['status'], 'MERGED')
310 self.assertEqual(B.data['status'], 'MERGED')
311 self.assertEqual(C.data['status'], 'MERGED')
312 self.assertEqual(A.reported, 2)
313 self.assertEqual(B.reported, 2)
314 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700315
316 def test_failed_change_at_head(self):
317 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700318
James E. Blair08d19992016-08-10 15:25:31 -0700319 self.launch_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700320 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
321 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
322 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700323 A.addApproval('code-review', 2)
324 B.addApproval('code-review', 2)
325 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700326
James E. Blair08d19992016-08-10 15:25:31 -0700327 self.launch_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700328
James E. Blair8b5408c2016-08-08 15:37:46 -0700329 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
330 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
331 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700332
333 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700334
James E. Blairb536ecc2016-08-31 10:11:42 -0700335 self.assertBuilds([
336 dict(name='project-merge', changes='1,1'),
337 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700338
James E. Blair08d19992016-08-10 15:25:31 -0700339 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700340 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700341 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700342 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700343 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700344 self.waitUntilSettled()
345
James E. Blairb536ecc2016-08-31 10:11:42 -0700346 self.assertBuilds([
347 dict(name='project-test1', changes='1,1'),
348 dict(name='project-test2', changes='1,1'),
349 dict(name='project-test1', changes='1,1 2,1'),
350 dict(name='project-test2', changes='1,1 2,1'),
351 dict(name='project-test1', changes='1,1 2,1 3,1'),
352 dict(name='project-test2', changes='1,1 2,1 3,1'),
353 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700354
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400355 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700356 self.waitUntilSettled()
357
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400358 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700359 self.assertBuilds([
360 dict(name='project-test2', changes='1,1'),
361 dict(name='project-merge', changes='2,1'),
362 ])
363 # Unordered history comparison because the aborts can finish
364 # in any order.
365 self.assertHistory([
366 dict(name='project-merge', result='SUCCESS',
367 changes='1,1'),
368 dict(name='project-merge', result='SUCCESS',
369 changes='1,1 2,1'),
370 dict(name='project-merge', result='SUCCESS',
371 changes='1,1 2,1 3,1'),
372 dict(name='project-test1', result='FAILURE',
373 changes='1,1'),
374 dict(name='project-test1', result='ABORTED',
375 changes='1,1 2,1'),
376 dict(name='project-test2', result='ABORTED',
377 changes='1,1 2,1'),
378 dict(name='project-test1', result='ABORTED',
379 changes='1,1 2,1 3,1'),
380 dict(name='project-test2', result='ABORTED',
381 changes='1,1 2,1 3,1'),
382 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700383
James E. Blairb536ecc2016-08-31 10:11:42 -0700384 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700385 self.waitUntilSettled()
James E. Blairb536ecc2016-08-31 10:11:42 -0700386 self.launch_server.release('.*-merge')
387 self.waitUntilSettled()
388 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700389
James E. Blairb536ecc2016-08-31 10:11:42 -0700390 self.assertBuilds([])
391 self.assertHistory([
392 dict(name='project-merge', result='SUCCESS',
393 changes='1,1'),
394 dict(name='project-merge', result='SUCCESS',
395 changes='1,1 2,1'),
396 dict(name='project-merge', result='SUCCESS',
397 changes='1,1 2,1 3,1'),
398 dict(name='project-test1', result='FAILURE',
399 changes='1,1'),
400 dict(name='project-test1', result='ABORTED',
401 changes='1,1 2,1'),
402 dict(name='project-test2', result='ABORTED',
403 changes='1,1 2,1'),
404 dict(name='project-test1', result='ABORTED',
405 changes='1,1 2,1 3,1'),
406 dict(name='project-test2', result='ABORTED',
407 changes='1,1 2,1 3,1'),
408 dict(name='project-merge', result='SUCCESS',
409 changes='2,1'),
410 dict(name='project-merge', result='SUCCESS',
411 changes='2,1 3,1'),
412 dict(name='project-test2', result='SUCCESS',
413 changes='1,1'),
414 dict(name='project-test1', result='SUCCESS',
415 changes='2,1'),
416 dict(name='project-test2', result='SUCCESS',
417 changes='2,1'),
418 dict(name='project-test1', result='SUCCESS',
419 changes='2,1 3,1'),
420 dict(name='project-test2', result='SUCCESS',
421 changes='2,1 3,1'),
422 ], ordered=False)
423
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400424 self.assertEqual(A.data['status'], 'NEW')
425 self.assertEqual(B.data['status'], 'MERGED')
426 self.assertEqual(C.data['status'], 'MERGED')
427 self.assertEqual(A.reported, 2)
428 self.assertEqual(B.reported, 2)
429 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700430
James E. Blair0aac4872013-08-23 14:02:38 -0700431 def test_failed_change_in_middle(self):
432 "Test a failed change in the middle of the queue"
433
James E. Blair08d19992016-08-10 15:25:31 -0700434 self.launch_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700435 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
436 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
437 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700438 A.addApproval('code-review', 2)
439 B.addApproval('code-review', 2)
440 C.addApproval('code-review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700441
James E. Blair08d19992016-08-10 15:25:31 -0700442 self.launch_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700443
James E. Blair8b5408c2016-08-08 15:37:46 -0700444 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
445 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
446 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700447
448 self.waitUntilSettled()
449
James E. Blair08d19992016-08-10 15:25:31 -0700450 self.launch_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700451 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700452 self.launch_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700453 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700454 self.launch_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700455 self.waitUntilSettled()
456
457 self.assertEqual(len(self.builds), 6)
458 self.assertEqual(self.builds[0].name, 'project-test1')
459 self.assertEqual(self.builds[1].name, 'project-test2')
460 self.assertEqual(self.builds[2].name, 'project-test1')
461 self.assertEqual(self.builds[3].name, 'project-test2')
462 self.assertEqual(self.builds[4].name, 'project-test1')
463 self.assertEqual(self.builds[5].name, 'project-test2')
464
465 self.release(self.builds[2])
466 self.waitUntilSettled()
467
James E. Blair972e3c72013-08-29 12:04:55 -0700468 # project-test1 and project-test2 for A
469 # project-test2 for B
470 # project-merge for C (without B)
471 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700472 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
473
James E. Blair08d19992016-08-10 15:25:31 -0700474 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700475 self.waitUntilSettled()
476
477 # project-test1 and project-test2 for A
478 # project-test2 for B
479 # project-test1 and project-test2 for C
480 self.assertEqual(len(self.builds), 5)
481
Paul Belanger2e2a0492016-10-30 18:33:37 -0400482 tenant = self.sched.abide.tenants.get('tenant-one')
483 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700484 builds = items[0].current_build_set.getBuilds()
485 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
486 self.assertEqual(self.countJobResults(builds, None), 2)
487 builds = items[1].current_build_set.getBuilds()
488 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
489 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
490 self.assertEqual(self.countJobResults(builds, None), 1)
491 builds = items[2].current_build_set.getBuilds()
492 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700493 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700494
James E. Blair08d19992016-08-10 15:25:31 -0700495 self.launch_server.hold_jobs_in_build = False
496 self.launch_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700497 self.waitUntilSettled()
498
499 self.assertEqual(len(self.builds), 0)
500 self.assertEqual(len(self.history), 12)
501 self.assertEqual(A.data['status'], 'MERGED')
502 self.assertEqual(B.data['status'], 'NEW')
503 self.assertEqual(C.data['status'], 'MERGED')
504 self.assertEqual(A.reported, 2)
505 self.assertEqual(B.reported, 2)
506 self.assertEqual(C.reported, 2)
507
James E. Blairec056492016-07-22 09:45:56 -0700508 @skip("Disabled for early v3 development")
James E. Blaird466dc42012-07-31 10:42:56 -0700509 def test_failed_change_at_head_with_queue(self):
510 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700511
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700512 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700513 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
514 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
515 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700516 A.addApproval('code-review', 2)
517 B.addApproval('code-review', 2)
518 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700519
James E. Blair08d19992016-08-10 15:25:31 -0700520 self.launch_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700521
James E. Blair8b5408c2016-08-08 15:37:46 -0700522 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
523 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
524 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700525
526 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700527 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400528 self.assertEqual(len(self.builds), 0)
529 self.assertEqual(len(queue), 1)
530 self.assertEqual(queue[0].name, 'build:project-merge')
531 self.assertTrue(self.job_has_changes(queue[0], A))
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)
543 self.assertEqual(queue[0].name, 'build:project-test1')
544 self.assertEqual(queue[1].name, 'build:project-test2')
545 self.assertEqual(queue[2].name, 'build:project-test1')
546 self.assertEqual(queue[3].name, 'build:project-test2')
547 self.assertEqual(queue[4].name, 'build:project-test1')
548 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700549
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700550 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700551 self.waitUntilSettled()
552
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400553 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700554 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400555 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
556 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700557
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700558 self.gearman_server.hold_jobs_in_queue = False
559 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700560 self.waitUntilSettled()
561
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400562 self.assertEqual(len(self.builds), 0)
563 self.assertEqual(len(self.history), 11)
564 self.assertEqual(A.data['status'], 'NEW')
565 self.assertEqual(B.data['status'], 'MERGED')
566 self.assertEqual(C.data['status'], 'MERGED')
567 self.assertEqual(A.reported, 2)
568 self.assertEqual(B.reported, 2)
569 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700570
James E. Blairec056492016-07-22 09:45:56 -0700571 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700572 def _test_time_database(self, iteration):
James E. Blair08d19992016-08-10 15:25:31 -0700573 self.launch_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700574 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -0700575 A.addApproval('code-review', 2)
576 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700577 self.waitUntilSettled()
578 time.sleep(2)
579
580 data = json.loads(self.sched.formatStatusJSON())
581 found_job = None
582 for pipeline in data['pipelines']:
583 if pipeline['name'] != 'gate':
584 continue
585 for queue in pipeline['change_queues']:
586 for head in queue['heads']:
587 for item in head:
588 for job in item['jobs']:
589 if job['name'] == 'project-merge':
590 found_job = job
591 break
592
593 self.assertIsNotNone(found_job)
594 if iteration == 1:
595 self.assertIsNotNone(found_job['estimated_time'])
596 self.assertIsNone(found_job['remaining_time'])
597 else:
598 self.assertIsNotNone(found_job['estimated_time'])
599 self.assertTrue(found_job['estimated_time'] >= 2)
600 self.assertIsNotNone(found_job['remaining_time'])
601
James E. Blair08d19992016-08-10 15:25:31 -0700602 self.launch_server.hold_jobs_in_build = False
603 self.launch_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700604 self.waitUntilSettled()
605
James E. Blairec056492016-07-22 09:45:56 -0700606 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700607 def test_time_database(self):
608 "Test the time database"
609
610 self._test_time_database(1)
611 self._test_time_database(2)
612
James E. Blairfef71632013-09-23 11:15:47 -0700613 def test_two_failed_changes_at_head(self):
614 "Test that changes are reparented correctly if 2 fail at head"
615
James E. Blair08d19992016-08-10 15:25:31 -0700616 self.launch_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700617 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
618 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
619 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700620 A.addApproval('code-review', 2)
621 B.addApproval('code-review', 2)
622 C.addApproval('code-review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700623
James E. Blair08d19992016-08-10 15:25:31 -0700624 self.launch_server.failJob('project-test1', A)
625 self.launch_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700626
James E. Blair8b5408c2016-08-08 15:37:46 -0700627 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
628 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
629 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700630 self.waitUntilSettled()
631
James E. Blair08d19992016-08-10 15:25:31 -0700632 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700633 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700634 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700635 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700636 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700637 self.waitUntilSettled()
638
639 self.assertEqual(len(self.builds), 6)
640 self.assertEqual(self.builds[0].name, 'project-test1')
641 self.assertEqual(self.builds[1].name, 'project-test2')
642 self.assertEqual(self.builds[2].name, 'project-test1')
643 self.assertEqual(self.builds[3].name, 'project-test2')
644 self.assertEqual(self.builds[4].name, 'project-test1')
645 self.assertEqual(self.builds[5].name, 'project-test2')
646
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400647 self.assertTrue(self.builds[0].hasChanges(A))
648 self.assertTrue(self.builds[2].hasChanges(A))
649 self.assertTrue(self.builds[2].hasChanges(B))
650 self.assertTrue(self.builds[4].hasChanges(A))
651 self.assertTrue(self.builds[4].hasChanges(B))
652 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700653
654 # Fail change B first
655 self.release(self.builds[2])
656 self.waitUntilSettled()
657
658 # restart of C after B failure
James E. Blair08d19992016-08-10 15:25:31 -0700659 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700660 self.waitUntilSettled()
661
662 self.assertEqual(len(self.builds), 5)
663 self.assertEqual(self.builds[0].name, 'project-test1')
664 self.assertEqual(self.builds[1].name, 'project-test2')
665 self.assertEqual(self.builds[2].name, 'project-test2')
666 self.assertEqual(self.builds[3].name, 'project-test1')
667 self.assertEqual(self.builds[4].name, 'project-test2')
668
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400669 self.assertTrue(self.builds[1].hasChanges(A))
670 self.assertTrue(self.builds[2].hasChanges(A))
671 self.assertTrue(self.builds[2].hasChanges(B))
672 self.assertTrue(self.builds[4].hasChanges(A))
673 self.assertFalse(self.builds[4].hasChanges(B))
674 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700675
676 # Finish running all passing jobs for change A
677 self.release(self.builds[1])
678 self.waitUntilSettled()
679 # Fail and report change A
680 self.release(self.builds[0])
681 self.waitUntilSettled()
682
683 # restart of B,C after A failure
James E. Blair08d19992016-08-10 15:25:31 -0700684 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700685 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700686 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700687 self.waitUntilSettled()
688
689 self.assertEqual(len(self.builds), 4)
690 self.assertEqual(self.builds[0].name, 'project-test1') # B
691 self.assertEqual(self.builds[1].name, 'project-test2') # B
692 self.assertEqual(self.builds[2].name, 'project-test1') # C
693 self.assertEqual(self.builds[3].name, 'project-test2') # C
694
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400695 self.assertFalse(self.builds[1].hasChanges(A))
696 self.assertTrue(self.builds[1].hasChanges(B))
697 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700698
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400699 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700700 # After A failed and B and C restarted, B should be back in
701 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400702 self.assertTrue(self.builds[2].hasChanges(B))
703 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700704
James E. Blair08d19992016-08-10 15:25:31 -0700705 self.launch_server.hold_jobs_in_build = False
706 self.launch_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700707 self.waitUntilSettled()
708
709 self.assertEqual(len(self.builds), 0)
710 self.assertEqual(len(self.history), 21)
711 self.assertEqual(A.data['status'], 'NEW')
712 self.assertEqual(B.data['status'], 'NEW')
713 self.assertEqual(C.data['status'], 'MERGED')
714 self.assertEqual(A.reported, 2)
715 self.assertEqual(B.reported, 2)
716 self.assertEqual(C.reported, 2)
717
James E. Blairec056492016-07-22 09:45:56 -0700718 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700719 def test_parse_skip_if(self):
720 job_yaml = """
721jobs:
722 - name: job_name
723 skip-if:
724 - project: ^project_name$
725 branch: ^stable/icehouse$
726 all-files-match-any:
727 - ^filename$
728 - project: ^project2_name$
729 all-files-match-any:
730 - ^filename2$
731 """.strip()
732 data = yaml.load(job_yaml)
733 config_job = data.get('jobs')[0]
734 cm = zuul.change_matcher
735 expected = cm.MatchAny([
736 cm.MatchAll([
737 cm.ProjectMatcher('^project_name$'),
738 cm.BranchMatcher('^stable/icehouse$'),
739 cm.MatchAllFiles([cm.FileMatcher('^filename$')]),
740 ]),
741 cm.MatchAll([
742 cm.ProjectMatcher('^project2_name$'),
743 cm.MatchAllFiles([cm.FileMatcher('^filename2$')]),
744 ]),
745 ])
746 matcher = self.sched._parseSkipIf(config_job)
747 self.assertEqual(expected, matcher)
748
James E. Blair8c803f82012-07-31 16:25:42 -0700749 def test_patch_order(self):
750 "Test that dependent patches are tested in the right order"
751 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
752 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
753 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700754 A.addApproval('code-review', 2)
755 B.addApproval('code-review', 2)
756 C.addApproval('code-review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700757
758 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
759 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
760 M2.setMerged()
761 M1.setMerged()
762
763 # C -> B -> A -> M1 -> M2
764 # M2 is here to make sure it is never queried. If it is, it
765 # means zuul is walking down the entire history of merged
766 # changes.
767
768 C.setDependsOn(B, 1)
769 B.setDependsOn(A, 1)
770 A.setDependsOn(M1, 1)
771 M1.setDependsOn(M2, 1)
772
James E. Blair8b5408c2016-08-08 15:37:46 -0700773 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700774
775 self.waitUntilSettled()
776
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400777 self.assertEqual(A.data['status'], 'NEW')
778 self.assertEqual(B.data['status'], 'NEW')
779 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700780
James E. Blair8b5408c2016-08-08 15:37:46 -0700781 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
782 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700783
784 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400785 self.assertEqual(M2.queried, 0)
786 self.assertEqual(A.data['status'], 'MERGED')
787 self.assertEqual(B.data['status'], 'MERGED')
788 self.assertEqual(C.data['status'], 'MERGED')
789 self.assertEqual(A.reported, 2)
790 self.assertEqual(B.reported, 2)
791 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700792
James E. Blair063672f2015-01-29 13:09:12 -0800793 def test_needed_changes_enqueue(self):
794 "Test that a needed change is enqueued ahead"
795 # A Given a git tree like this, if we enqueue
796 # / \ change C, we should walk up and down the tree
797 # B G and enqueue changes in the order ABCDEFG.
798 # /|\ This is also the order that you would get if
799 # *C E F you enqueued changes in the order ABCDEFG, so
800 # / the ordering is stable across re-enqueue events.
801 # D
802
803 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
804 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
805 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
806 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
807 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
808 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
809 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
810 B.setDependsOn(A, 1)
811 C.setDependsOn(B, 1)
812 D.setDependsOn(C, 1)
813 E.setDependsOn(B, 1)
814 F.setDependsOn(B, 1)
815 G.setDependsOn(A, 1)
816
James E. Blair8b5408c2016-08-08 15:37:46 -0700817 A.addApproval('code-review', 2)
818 B.addApproval('code-review', 2)
819 C.addApproval('code-review', 2)
820 D.addApproval('code-review', 2)
821 E.addApproval('code-review', 2)
822 F.addApproval('code-review', 2)
823 G.addApproval('code-review', 2)
824 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800825
826 self.waitUntilSettled()
827
828 self.assertEqual(A.data['status'], 'NEW')
829 self.assertEqual(B.data['status'], 'NEW')
830 self.assertEqual(C.data['status'], 'NEW')
831 self.assertEqual(D.data['status'], 'NEW')
832 self.assertEqual(E.data['status'], 'NEW')
833 self.assertEqual(F.data['status'], 'NEW')
834 self.assertEqual(G.data['status'], 'NEW')
835
836 # We're about to add approvals to changes without adding the
837 # triggering events to Zuul, so that we can be sure that it is
838 # enqueing the changes based on dependencies, not because of
839 # triggering events. Since it will have the changes cached
840 # already (without approvals), we need to clear the cache
841 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400842 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100843 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800844
James E. Blair08d19992016-08-10 15:25:31 -0700845 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -0700846 A.addApproval('approved', 1)
847 B.addApproval('approved', 1)
848 D.addApproval('approved', 1)
849 E.addApproval('approved', 1)
850 F.addApproval('approved', 1)
851 G.addApproval('approved', 1)
852 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800853
854 for x in range(8):
James E. Blair08d19992016-08-10 15:25:31 -0700855 self.launch_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800856 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700857 self.launch_server.hold_jobs_in_build = False
858 self.launch_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800859 self.waitUntilSettled()
860
861 self.assertEqual(A.data['status'], 'MERGED')
862 self.assertEqual(B.data['status'], 'MERGED')
863 self.assertEqual(C.data['status'], 'MERGED')
864 self.assertEqual(D.data['status'], 'MERGED')
865 self.assertEqual(E.data['status'], 'MERGED')
866 self.assertEqual(F.data['status'], 'MERGED')
867 self.assertEqual(G.data['status'], 'MERGED')
868 self.assertEqual(A.reported, 2)
869 self.assertEqual(B.reported, 2)
870 self.assertEqual(C.reported, 2)
871 self.assertEqual(D.reported, 2)
872 self.assertEqual(E.reported, 2)
873 self.assertEqual(F.reported, 2)
874 self.assertEqual(G.reported, 2)
875 self.assertEqual(self.history[6].changes,
876 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
877
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100878 def test_source_cache(self):
879 "Test that the source cache operates correctly"
James E. Blair08d19992016-08-10 15:25:31 -0700880 self.launch_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700881
882 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
883 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
884 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
James E. Blair8b5408c2016-08-08 15:37:46 -0700885 A.addApproval('code-review', 2)
886 B.addApproval('code-review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700887
888 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
889 M1.setMerged()
890
891 B.setDependsOn(A, 1)
892 A.setDependsOn(M1, 1)
893
James E. Blair8b5408c2016-08-08 15:37:46 -0700894 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700895 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
896
897 self.waitUntilSettled()
898
899 for build in self.builds:
900 if build.parameters['ZUUL_PIPELINE'] == 'check':
901 build.release()
902 self.waitUntilSettled()
903 for build in self.builds:
904 if build.parameters['ZUUL_PIPELINE'] == 'check':
905 build.release()
906 self.waitUntilSettled()
907
James E. Blair8b5408c2016-08-08 15:37:46 -0700908 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700909 self.waitUntilSettled()
910
Joshua Hesketh352264b2015-08-11 23:42:08 +1000911 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700912 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000913 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700914
James E. Blair08d19992016-08-10 15:25:31 -0700915 self.launch_server.hold_jobs_in_build = False
916 self.launch_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700917 self.waitUntilSettled()
918
919 self.assertEqual(A.data['status'], 'MERGED')
920 self.assertEqual(B.data['status'], 'MERGED')
921 self.assertEqual(A.queried, 2) # Initial and isMerged
922 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
923
James E. Blair8c803f82012-07-31 16:25:42 -0700924 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700925 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700926 # TODO: move to test_gerrit (this is a unit test!)
927 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400928 tenant = self.sched.abide.tenants.get('tenant-one')
929 source = tenant.layout.pipelines['gate'].source
930
931 # TODO(pabelanger): As we add more source / trigger APIs we should make
932 # it easier for users to create events for testing.
933 event = zuul.model.TriggerEvent()
934 event.trigger_name = 'gerrit'
935 event.change_number = '1'
936 event.patch_number = '2'
937
938 a = source.getChange(event)
939 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700940 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700941
James E. Blair8b5408c2016-08-08 15:37:46 -0700942 A.addApproval('code-review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400943 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700944 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700945
James E. Blair8b5408c2016-08-08 15:37:46 -0700946 A.addApproval('approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400947 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700948 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700949
James E. Blairec056492016-07-22 09:45:56 -0700950 @skip("Disabled for early v3 development")
James E. Blair4886cc12012-07-18 15:39:41 -0700951 def test_build_configuration(self):
952 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700953
954 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -0700955 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
956 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
957 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700958 A.addApproval('code-review', 2)
959 B.addApproval('code-review', 2)
960 C.addApproval('code-review', 2)
961 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
962 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
963 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair4886cc12012-07-18 15:39:41 -0700964 self.waitUntilSettled()
965
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700966 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -0700967 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700968 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -0700969 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700970 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -0700971 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700972 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700973 ref = self.getParameter(queue[-1], 'ZUUL_REF')
974 self.gearman_server.hold_jobs_in_queue = False
975 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700976 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -0700977
Monty Taylorbc758832013-06-17 17:22:42 -0400978 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -0700979 repo = git.Repo(path)
980 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
981 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -0700982 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400983 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -0700984
James E. Blairec056492016-07-22 09:45:56 -0700985 @skip("Disabled for early v3 development")
James E. Blair973721f2012-08-15 10:19:43 -0700986 def test_build_configuration_conflict(self):
987 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700988
989 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -0700990 A = self.fake_gerrit.addFakeChange('org/conflict-project',
991 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -0700992 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700993 B = self.fake_gerrit.addFakeChange('org/conflict-project',
994 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -0700995 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700996 C = self.fake_gerrit.addFakeChange('org/conflict-project',
997 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700998 A.addApproval('code-review', 2)
999 B.addApproval('code-review', 2)
1000 C.addApproval('code-review', 2)
1001 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1002 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1003 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -07001004 self.waitUntilSettled()
1005
James E. Blair6736beb2013-07-11 15:18:15 -07001006 self.assertEqual(A.reported, 1)
1007 self.assertEqual(B.reported, 1)
1008 self.assertEqual(C.reported, 1)
1009
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001010 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001011 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001012 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001013 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001014 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001015 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001016
1017 self.assertEqual(len(self.history), 2) # A and C merge jobs
1018
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001019 self.gearman_server.hold_jobs_in_queue = False
1020 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001021 self.waitUntilSettled()
1022
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001023 self.assertEqual(A.data['status'], 'MERGED')
1024 self.assertEqual(B.data['status'], 'NEW')
1025 self.assertEqual(C.data['status'], 'MERGED')
1026 self.assertEqual(A.reported, 2)
1027 self.assertEqual(B.reported, 2)
1028 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001029 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -07001030
James E. Blairec056492016-07-22 09:45:56 -07001031 @skip("Disabled for early v3 development")
James E. Blairdaabed22012-08-15 15:38:57 -07001032 def test_post(self):
1033 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001034
Zhongyue Luo5d556072012-09-21 02:00:47 +09001035 e = {
1036 "type": "ref-updated",
1037 "submitter": {
1038 "name": "User Name",
1039 },
1040 "refUpdate": {
1041 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1042 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1043 "refName": "master",
1044 "project": "org/project",
1045 }
1046 }
James E. Blairdaabed22012-08-15 15:38:57 -07001047 self.fake_gerrit.addEvent(e)
1048 self.waitUntilSettled()
1049
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001050 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001051 self.assertEqual(len(self.history), 1)
1052 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001053
James E. Blairec056492016-07-22 09:45:56 -07001054 @skip("Disabled for early v3 development")
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001055 def test_post_ignore_deletes(self):
1056 "Test that deleting refs does not trigger post jobs"
1057
1058 e = {
1059 "type": "ref-updated",
1060 "submitter": {
1061 "name": "User Name",
1062 },
1063 "refUpdate": {
1064 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1065 "newRev": "0000000000000000000000000000000000000000",
1066 "refName": "master",
1067 "project": "org/project",
1068 }
1069 }
1070 self.fake_gerrit.addEvent(e)
1071 self.waitUntilSettled()
1072
1073 job_names = [x.name for x in self.history]
1074 self.assertEqual(len(self.history), 0)
1075 self.assertNotIn('project-post', job_names)
1076
James E. Blairec056492016-07-22 09:45:56 -07001077 @skip("Disabled for early v3 development")
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001078 def test_post_ignore_deletes_negative(self):
1079 "Test that deleting refs does trigger post jobs"
1080
James E. Blairf84026c2015-12-08 16:11:46 -08001081 self.updateConfigLayout(
1082 'tests/fixtures/layout-dont-ignore-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001083 self.sched.reconfigure(self.config)
1084
1085 e = {
1086 "type": "ref-updated",
1087 "submitter": {
1088 "name": "User Name",
1089 },
1090 "refUpdate": {
1091 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1092 "newRev": "0000000000000000000000000000000000000000",
1093 "refName": "master",
1094 "project": "org/project",
1095 }
1096 }
1097 self.fake_gerrit.addEvent(e)
1098 self.waitUntilSettled()
1099
1100 job_names = [x.name for x in self.history]
1101 self.assertEqual(len(self.history), 1)
1102 self.assertIn('project-post', job_names)
1103
James E. Blairec056492016-07-22 09:45:56 -07001104 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001105 def test_build_configuration_branch(self):
1106 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001107
1108 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001109 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1110 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1111 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001112 A.addApproval('code-review', 2)
1113 B.addApproval('code-review', 2)
1114 C.addApproval('code-review', 2)
1115 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1116 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1117 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairc6294a52012-08-17 10:19:48 -07001118 self.waitUntilSettled()
1119
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001120 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001121 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001122 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001123 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001124 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001125 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001126 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001127 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1128 self.gearman_server.hold_jobs_in_queue = False
1129 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001130 self.waitUntilSettled()
1131
Monty Taylorbc758832013-06-17 17:22:42 -04001132 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001133 repo = git.Repo(path)
1134 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1135 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001136 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001137 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001138
James E. Blairec056492016-07-22 09:45:56 -07001139 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001140 def test_build_configuration_branch_interaction(self):
1141 "Test that switching between branches works"
1142 self.test_build_configuration()
1143 self.test_build_configuration_branch()
1144 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001145 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001146 repo = git.Repo(path)
1147 repo.heads.master.commit = repo.commit('init')
1148 self.test_build_configuration()
1149
James E. Blairec056492016-07-22 09:45:56 -07001150 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001151 def test_build_configuration_multi_branch(self):
1152 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001153
1154 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001155 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1156 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1157 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001158 A.addApproval('code-review', 2)
1159 B.addApproval('code-review', 2)
1160 C.addApproval('code-review', 2)
1161 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1162 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1163 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairc6294a52012-08-17 10:19:48 -07001164 self.waitUntilSettled()
James E. Blairbb1fe502014-03-04 10:15:06 -08001165 queue = self.gearman_server.getQueue()
1166 job_A = None
1167 for job in queue:
1168 if 'project-merge' in job.name:
1169 job_A = job
1170 ref_A = self.getParameter(job_A, 'ZUUL_REF')
1171 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
1172 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
1173 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
James E. Blairc6294a52012-08-17 10:19:48 -07001174
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001175 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001176 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001177 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001178 job_B = None
1179 for job in queue:
1180 if 'project-merge' in job.name:
1181 job_B = job
1182 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001183 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001184 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blairbb1fe502014-03-04 10:15:06 -08001185 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
1186
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001187 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001188 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001189 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001190 for job in queue:
1191 if 'project-merge' in job.name:
1192 job_C = job
1193 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001194 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001195 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blairbb1fe502014-03-04 10:15:06 -08001196 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001197 self.gearman_server.hold_jobs_in_queue = False
1198 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001199 self.waitUntilSettled()
1200
Monty Taylorbc758832013-06-17 17:22:42 -04001201 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001202 repo = git.Repo(path)
1203
1204 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001205 for c in repo.iter_commits(ref_C)]
James E. Blairbb1fe502014-03-04 10:15:06 -08001206 repo_shas = [c.hexsha for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001207 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001208 correct_messages = ['initial commit', 'A-1', 'C-1']
James E. Blairbb1fe502014-03-04 10:15:06 -08001209 # Ensure the right commits are in the history for this ref
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001210 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08001211 # Ensure ZUUL_REF -> ZUUL_COMMIT
1212 self.assertEqual(repo_shas[0], commit_C)
James E. Blairc6294a52012-08-17 10:19:48 -07001213
1214 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001215 for c in repo.iter_commits(ref_B)]
James E. Blairbb1fe502014-03-04 10:15:06 -08001216 repo_shas = [c.hexsha for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001217 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001218 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001219 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08001220 self.assertEqual(repo_shas[0], commit_B)
1221
1222 repo_messages = [c.message.strip()
1223 for c in repo.iter_commits(ref_A)]
1224 repo_shas = [c.hexsha for c in repo.iter_commits(ref_A)]
1225 repo_messages.reverse()
1226 correct_messages = ['initial commit', 'A-1']
1227 self.assertEqual(repo_messages, correct_messages)
1228 self.assertEqual(repo_shas[0], commit_A)
1229
1230 self.assertNotEqual(ref_A, ref_B, ref_C)
1231 self.assertNotEqual(commit_A, commit_B, commit_C)
James E. Blair7f71c802012-08-22 13:04:32 -07001232
James E. Blaircaec0c52012-08-22 14:52:22 -07001233 def test_dependent_changes_dequeue(self):
1234 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001235
James E. Blaircaec0c52012-08-22 14:52:22 -07001236 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1237 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1238 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001239 A.addApproval('code-review', 2)
1240 B.addApproval('code-review', 2)
1241 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001242
1243 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1244 M1.setMerged()
1245
1246 # C -> B -> A -> M1
1247
1248 C.setDependsOn(B, 1)
1249 B.setDependsOn(A, 1)
1250 A.setDependsOn(M1, 1)
1251
James E. Blair08d19992016-08-10 15:25:31 -07001252 self.launch_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001253
James E. Blair8b5408c2016-08-08 15:37:46 -07001254 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1255 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1256 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001257
1258 self.waitUntilSettled()
1259
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001260 self.assertEqual(A.data['status'], 'NEW')
1261 self.assertEqual(A.reported, 2)
1262 self.assertEqual(B.data['status'], 'NEW')
1263 self.assertEqual(B.reported, 2)
1264 self.assertEqual(C.data['status'], 'NEW')
1265 self.assertEqual(C.reported, 2)
1266 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001267
James E. Blair972e3c72013-08-29 12:04:55 -07001268 def test_failing_dependent_changes(self):
1269 "Test that failing dependent patches are taken out of stream"
James E. Blair08d19992016-08-10 15:25:31 -07001270 self.launch_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001271 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1272 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1273 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1274 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1275 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001276 A.addApproval('code-review', 2)
1277 B.addApproval('code-review', 2)
1278 C.addApproval('code-review', 2)
1279 D.addApproval('code-review', 2)
1280 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001281
1282 # E, D -> C -> B, A
1283
1284 D.setDependsOn(C, 1)
1285 C.setDependsOn(B, 1)
1286
James E. Blair08d19992016-08-10 15:25:31 -07001287 self.launch_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001288
James E. Blair8b5408c2016-08-08 15:37:46 -07001289 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1290 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1291 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1292 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1293 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001294
1295 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001296 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001297 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001298 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001299 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001300 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001301 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001302 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001303 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001304 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001305 self.waitUntilSettled()
1306
James E. Blair08d19992016-08-10 15:25:31 -07001307 self.launch_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001308 for build in self.builds:
1309 if build.parameters['ZUUL_CHANGE'] != '1':
1310 build.release()
1311 self.waitUntilSettled()
1312
James E. Blair08d19992016-08-10 15:25:31 -07001313 self.launch_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001314 self.waitUntilSettled()
1315
1316 self.assertEqual(A.data['status'], 'MERGED')
1317 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001318 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001319 self.assertEqual(B.data['status'], 'NEW')
1320 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001321 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001322 self.assertEqual(C.data['status'], 'NEW')
1323 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001324 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001325 self.assertEqual(D.data['status'], 'NEW')
1326 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001327 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001328 self.assertEqual(E.data['status'], 'MERGED')
1329 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001330 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001331 self.assertEqual(len(self.history), 18)
1332
James E. Blairec590122012-08-22 15:19:31 -07001333 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001334 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001335 # If it's dequeued more than once, we should see extra
1336 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001337
James E. Blair08d19992016-08-10 15:25:31 -07001338 self.launch_server.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001339 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1340 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1341 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001342 A.addApproval('code-review', 2)
1343 B.addApproval('code-review', 2)
1344 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001345
Paul Belanger58bf6912016-11-11 19:36:01 -05001346 self.launch_server.failJob('project-test1', A)
1347 self.launch_server.failJob('project-test2', A)
James E. Blair08d19992016-08-10 15:25:31 -07001348 self.launch_server.failJob('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001349
James E. Blair8b5408c2016-08-08 15:37:46 -07001350 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1351 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1352 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001353
1354 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001355
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001356 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001357 self.assertEqual(self.builds[0].name, 'project-merge')
1358 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001359
James E. Blair08d19992016-08-10 15:25:31 -07001360 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001361 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001362 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001363 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001364 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001365 self.waitUntilSettled()
1366
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001367 self.assertEqual(len(self.builds), 9)
Paul Belanger58bf6912016-11-11 19:36:01 -05001368 self.assertEqual(self.builds[0].name, 'project-test1')
1369 self.assertEqual(self.builds[1].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001370 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001371 self.assertEqual(self.builds[3].name, 'project-test1')
1372 self.assertEqual(self.builds[4].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001373 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001374 self.assertEqual(self.builds[6].name, 'project-test1')
1375 self.assertEqual(self.builds[7].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001376 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001377
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001378 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001379 self.waitUntilSettled()
1380
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001381 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1382 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001383
James E. Blair08d19992016-08-10 15:25:31 -07001384 self.launch_server.hold_jobs_in_build = False
1385 self.launch_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001386 self.waitUntilSettled()
1387
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001388 self.assertEqual(len(self.builds), 0)
1389 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001390
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001391 self.assertEqual(A.data['status'], 'NEW')
1392 self.assertEqual(B.data['status'], 'MERGED')
1393 self.assertEqual(C.data['status'], 'MERGED')
1394 self.assertEqual(A.reported, 2)
1395 self.assertEqual(B.reported, 2)
1396 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001397
James E. Blairec056492016-07-22 09:45:56 -07001398 @skip("Disabled for early v3 development")
James E. Blair4ec821f2012-08-23 15:28:28 -07001399 def test_nonvoting_job(self):
1400 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001401
James E. Blair4ec821f2012-08-23 15:28:28 -07001402 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1403 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001404 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07001405 self.launch_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001406 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001407
1408 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001409
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001410 self.assertEqual(A.data['status'], 'MERGED')
1411 self.assertEqual(A.reported, 2)
1412 self.assertEqual(
1413 self.getJobFromHistory('nonvoting-project-merge').result,
1414 'SUCCESS')
1415 self.assertEqual(
1416 self.getJobFromHistory('nonvoting-project-test1').result,
1417 'SUCCESS')
1418 self.assertEqual(
1419 self.getJobFromHistory('nonvoting-project-test2').result,
1420 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001421
James E. Blair5821bd92015-09-16 08:48:15 -07001422 for build in self.builds:
1423 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1424
James E. Blaire0487072012-08-29 17:38:31 -07001425 def test_check_queue_success(self):
1426 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001427
James E. Blaire0487072012-08-29 17:38:31 -07001428 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1429 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1430
1431 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001432
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001433 self.assertEqual(A.data['status'], 'NEW')
1434 self.assertEqual(A.reported, 1)
1435 self.assertEqual(self.getJobFromHistory('project-merge').result,
1436 'SUCCESS')
1437 self.assertEqual(self.getJobFromHistory('project-test1').result,
1438 'SUCCESS')
1439 self.assertEqual(self.getJobFromHistory('project-test2').result,
1440 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001441
1442 def test_check_queue_failure(self):
1443 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001444
James E. Blaire0487072012-08-29 17:38:31 -07001445 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair08d19992016-08-10 15:25:31 -07001446 self.launch_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001447 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1448
1449 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001450
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001451 self.assertEqual(A.data['status'], 'NEW')
1452 self.assertEqual(A.reported, 1)
1453 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001454 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001455 self.assertEqual(self.getJobFromHistory('project-test1').result,
1456 'SUCCESS')
1457 self.assertEqual(self.getJobFromHistory('project-test2').result,
1458 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001459
James E. Blairec056492016-07-22 09:45:56 -07001460 @skip("Disabled for early v3 development")
James E. Blair127bc182012-08-28 15:55:15 -07001461 def test_dependent_behind_dequeue(self):
1462 "test that dependent changes behind dequeued changes work"
1463 # This complicated test is a reproduction of a real life bug
1464 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001465
James E. Blair08d19992016-08-10 15:25:31 -07001466 self.launch_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001467 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1468 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1469 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1470 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1471 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1472 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1473 D.setDependsOn(C, 1)
1474 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001475 A.addApproval('code-review', 2)
1476 B.addApproval('code-review', 2)
1477 C.addApproval('code-review', 2)
1478 D.addApproval('code-review', 2)
1479 E.addApproval('code-review', 2)
1480 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001481
1482 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001483
1484 # Change object re-use in the gerrit trigger is hidden if
1485 # changes are added in quick succession; waiting makes it more
1486 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001487 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001488 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001489 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001490 self.waitUntilSettled()
1491
James E. Blair08d19992016-08-10 15:25:31 -07001492 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001493 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001494 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001495 self.waitUntilSettled()
1496
James E. Blair8b5408c2016-08-08 15:37:46 -07001497 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001498 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001499 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001500 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001501 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001502 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001503 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001504 self.waitUntilSettled()
1505
James E. Blair08d19992016-08-10 15:25:31 -07001506 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001507 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001508 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001509 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001510 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001511 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001512 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001513 self.waitUntilSettled()
1514
1515 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001516
1517 # Grab pointers to the jobs we want to release before
1518 # releasing any, because list indexes may change as
1519 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001520 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001521 a.release()
1522 b.release()
1523 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001524 self.waitUntilSettled()
1525
James E. Blair08d19992016-08-10 15:25:31 -07001526 self.launch_server.hold_jobs_in_build = False
1527 self.launch_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001528 self.waitUntilSettled()
1529
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001530 self.assertEqual(A.data['status'], 'NEW')
1531 self.assertEqual(B.data['status'], 'MERGED')
1532 self.assertEqual(C.data['status'], 'MERGED')
1533 self.assertEqual(D.data['status'], 'MERGED')
1534 self.assertEqual(E.data['status'], 'MERGED')
1535 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001536
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001537 self.assertEqual(A.reported, 2)
1538 self.assertEqual(B.reported, 2)
1539 self.assertEqual(C.reported, 2)
1540 self.assertEqual(D.reported, 2)
1541 self.assertEqual(E.reported, 2)
1542 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001543
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001544 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1545 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001546
1547 def test_merger_repack(self):
1548 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001549
James E. Blair05fed602012-09-07 12:45:24 -07001550 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001551 A.addApproval('code-review', 2)
1552 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001553 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001554 self.assertEqual(self.getJobFromHistory('project-merge').result,
1555 'SUCCESS')
1556 self.assertEqual(self.getJobFromHistory('project-test1').result,
1557 'SUCCESS')
1558 self.assertEqual(self.getJobFromHistory('project-test2').result,
1559 'SUCCESS')
1560 self.assertEqual(A.data['status'], 'MERGED')
1561 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001562 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001563 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001564
Monty Taylorbc758832013-06-17 17:22:42 -04001565 path = os.path.join(self.git_root, "org/project")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001566 print(repack_repo(path))
James E. Blair05fed602012-09-07 12:45:24 -07001567
1568 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001569 A.addApproval('code-review', 2)
1570 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001571 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001572 self.assertEqual(self.getJobFromHistory('project-merge').result,
1573 'SUCCESS')
1574 self.assertEqual(self.getJobFromHistory('project-test1').result,
1575 'SUCCESS')
1576 self.assertEqual(self.getJobFromHistory('project-test2').result,
1577 'SUCCESS')
1578 self.assertEqual(A.data['status'], 'MERGED')
1579 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001580
James E. Blair4886f282012-11-15 09:27:33 -08001581 def test_merger_repack_large_change(self):
1582 "Test that the merger works with large changes after a repack"
1583 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001584 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001585 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Hesketh352264b2015-08-11 23:42:08 +10001586 url = self.fake_gerrit.getGitUrl(
Paul Belanger503bded2016-11-11 19:43:38 -05001587 tenant.layout.project_configs.get('org/project1'))
James E. Blair4076e2b2014-01-28 12:42:20 -08001588 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08001589 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1590 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001591 path = os.path.join(self.upstream_root, "org/project1")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001592 print(repack_repo(path))
Monty Taylorbc758832013-06-17 17:22:42 -04001593 path = os.path.join(self.git_root, "org/project1")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001594 print(repack_repo(path))
James E. Blair4886f282012-11-15 09:27:33 -08001595
James E. Blair8b5408c2016-08-08 15:37:46 -07001596 A.addApproval('code-review', 2)
1597 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001598 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001599 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001600 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001601 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001602 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001603 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001604 'SUCCESS')
1605 self.assertEqual(A.data['status'], 'MERGED')
1606 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001607
James E. Blairec056492016-07-22 09:45:56 -07001608 @skip("Disabled for early v3 development")
James E. Blair7ee88a22012-09-12 18:59:31 +02001609 def test_nonexistent_job(self):
1610 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001611 # Set to the state immediately after a restart
1612 self.resetGearmanServer()
1613 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02001614
1615 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001616 A.addApproval('code-review', 2)
1617 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair7ee88a22012-09-12 18:59:31 +02001618 # There may be a thread about to report a lost change
1619 while A.reported < 2:
1620 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001621 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001622 self.assertFalse(job_names)
1623 self.assertEqual(A.data['status'], 'NEW')
1624 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001625 self.assertEmptyQueues()
1626
1627 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001628 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02001629 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001630 A.addApproval('code-review', 2)
1631 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair7ee88a22012-09-12 18:59:31 +02001632 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001633 self.assertEqual(self.getJobFromHistory('project-merge').result,
1634 'SUCCESS')
1635 self.assertEqual(self.getJobFromHistory('project-test1').result,
1636 'SUCCESS')
1637 self.assertEqual(self.getJobFromHistory('project-test2').result,
1638 'SUCCESS')
1639 self.assertEqual(A.data['status'], 'MERGED')
1640 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08001641
James E. Blair2fa50962013-01-30 21:50:41 -08001642 def test_new_patchset_dequeues_old(self):
1643 "Test that a new patchset causes the old to be dequeued"
1644 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair08d19992016-08-10 15:25:31 -07001645 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001646 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1647 M.setMerged()
1648
1649 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1650 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1651 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1652 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001653 A.addApproval('code-review', 2)
1654 B.addApproval('code-review', 2)
1655 C.addApproval('code-review', 2)
1656 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001657
1658 C.setDependsOn(B, 1)
1659 B.setDependsOn(A, 1)
1660 A.setDependsOn(M, 1)
1661
James E. Blair8b5408c2016-08-08 15:37:46 -07001662 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1663 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1664 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1665 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001666 self.waitUntilSettled()
1667
1668 B.addPatchset()
1669 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1670 self.waitUntilSettled()
1671
James E. Blair08d19992016-08-10 15:25:31 -07001672 self.launch_server.hold_jobs_in_build = False
1673 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001674 self.waitUntilSettled()
1675
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001676 self.assertEqual(A.data['status'], 'MERGED')
1677 self.assertEqual(A.reported, 2)
1678 self.assertEqual(B.data['status'], 'NEW')
1679 self.assertEqual(B.reported, 2)
1680 self.assertEqual(C.data['status'], 'NEW')
1681 self.assertEqual(C.reported, 2)
1682 self.assertEqual(D.data['status'], 'MERGED')
1683 self.assertEqual(D.reported, 2)
1684 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001685
James E. Blairba437362015-02-07 11:41:52 -08001686 def test_new_patchset_check(self):
1687 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001688
James E. Blair08d19992016-08-10 15:25:31 -07001689 self.launch_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001690
1691 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001692 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001693 tenant = self.sched.abide.tenants.get('tenant-one')
1694 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001695
1696 # Add two git-dependent changes
1697 B.setDependsOn(A, 1)
1698 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1699 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001700 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1701 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001702
1703 # A live item, and a non-live/live pair
1704 items = check_pipeline.getAllItems()
1705 self.assertEqual(len(items), 3)
1706
1707 self.assertEqual(items[0].change.number, '1')
1708 self.assertEqual(items[0].change.patchset, '1')
1709 self.assertFalse(items[0].live)
1710
1711 self.assertEqual(items[1].change.number, '2')
1712 self.assertEqual(items[1].change.patchset, '1')
1713 self.assertTrue(items[1].live)
1714
1715 self.assertEqual(items[2].change.number, '1')
1716 self.assertEqual(items[2].change.patchset, '1')
1717 self.assertTrue(items[2].live)
1718
1719 # Add a new patchset to A
1720 A.addPatchset()
1721 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1722 self.waitUntilSettled()
1723
1724 # The live copy of A,1 should be gone, but the non-live and B
1725 # should continue, and we should have a new A,2
1726 items = check_pipeline.getAllItems()
1727 self.assertEqual(len(items), 3)
1728
1729 self.assertEqual(items[0].change.number, '1')
1730 self.assertEqual(items[0].change.patchset, '1')
1731 self.assertFalse(items[0].live)
1732
1733 self.assertEqual(items[1].change.number, '2')
1734 self.assertEqual(items[1].change.patchset, '1')
1735 self.assertTrue(items[1].live)
1736
1737 self.assertEqual(items[2].change.number, '1')
1738 self.assertEqual(items[2].change.patchset, '2')
1739 self.assertTrue(items[2].live)
1740
1741 # Add a new patchset to B
1742 B.addPatchset()
1743 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1744 self.waitUntilSettled()
1745
1746 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1747 # but we should have a new B,2 (still based on A,1)
1748 items = check_pipeline.getAllItems()
1749 self.assertEqual(len(items), 3)
1750
1751 self.assertEqual(items[0].change.number, '1')
1752 self.assertEqual(items[0].change.patchset, '2')
1753 self.assertTrue(items[0].live)
1754
1755 self.assertEqual(items[1].change.number, '1')
1756 self.assertEqual(items[1].change.patchset, '1')
1757 self.assertFalse(items[1].live)
1758
1759 self.assertEqual(items[2].change.number, '2')
1760 self.assertEqual(items[2].change.patchset, '2')
1761 self.assertTrue(items[2].live)
1762
1763 self.builds[0].release()
1764 self.waitUntilSettled()
1765 self.builds[0].release()
1766 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001767 self.launch_server.hold_jobs_in_build = False
1768 self.launch_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001769 self.waitUntilSettled()
1770
1771 self.assertEqual(A.reported, 1)
1772 self.assertEqual(B.reported, 1)
1773 self.assertEqual(self.history[0].result, 'ABORTED')
1774 self.assertEqual(self.history[0].changes, '1,1')
1775 self.assertEqual(self.history[1].result, 'ABORTED')
1776 self.assertEqual(self.history[1].changes, '1,1 2,1')
1777 self.assertEqual(self.history[2].result, 'SUCCESS')
1778 self.assertEqual(self.history[2].changes, '1,2')
1779 self.assertEqual(self.history[3].result, 'SUCCESS')
1780 self.assertEqual(self.history[3].changes, '1,1 2,2')
1781
1782 def test_abandoned_gate(self):
1783 "Test that an abandoned change is dequeued from gate"
1784
James E. Blair08d19992016-08-10 15:25:31 -07001785 self.launch_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001786
1787 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001788 A.addApproval('code-review', 2)
1789 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001790 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001791 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1792 self.assertEqual(self.builds[0].name, 'project-merge')
1793
1794 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1795 self.waitUntilSettled()
1796
James E. Blair08d19992016-08-10 15:25:31 -07001797 self.launch_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001798 self.waitUntilSettled()
1799
Arie5b4048c2016-10-05 16:12:06 +03001800 self.assertBuilds([])
1801 self.assertHistory([
1802 dict(name='project-merge', result='ABORTED', changes='1,1')],
1803 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001804 self.assertEqual(A.reported, 1,
1805 "Abandoned gate change should report only start")
1806
1807 def test_abandoned_check(self):
1808 "Test that an abandoned change is dequeued from check"
1809
James E. Blair08d19992016-08-10 15:25:31 -07001810 self.launch_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001811
1812 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1813 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001814 tenant = self.sched.abide.tenants.get('tenant-one')
1815 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001816
1817 # Add two git-dependent changes
1818 B.setDependsOn(A, 1)
1819 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1820 self.waitUntilSettled()
1821 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1822 self.waitUntilSettled()
1823 # A live item, and a non-live/live pair
1824 items = check_pipeline.getAllItems()
1825 self.assertEqual(len(items), 3)
1826
1827 self.assertEqual(items[0].change.number, '1')
1828 self.assertFalse(items[0].live)
1829
1830 self.assertEqual(items[1].change.number, '2')
1831 self.assertTrue(items[1].live)
1832
1833 self.assertEqual(items[2].change.number, '1')
1834 self.assertTrue(items[2].live)
1835
1836 # Abandon A
1837 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1838 self.waitUntilSettled()
1839
1840 # The live copy of A should be gone, but the non-live and B
1841 # should continue
1842 items = check_pipeline.getAllItems()
1843 self.assertEqual(len(items), 2)
1844
1845 self.assertEqual(items[0].change.number, '1')
1846 self.assertFalse(items[0].live)
1847
1848 self.assertEqual(items[1].change.number, '2')
1849 self.assertTrue(items[1].live)
1850
James E. Blair08d19992016-08-10 15:25:31 -07001851 self.launch_server.hold_jobs_in_build = False
1852 self.launch_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001853 self.waitUntilSettled()
1854
1855 self.assertEqual(len(self.history), 4)
1856 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001857 'Build should have been aborted')
1858 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001859 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001860
James E. Blairec056492016-07-22 09:45:56 -07001861 @skip("Disabled for early v3 development")
Steve Varnau7b78b312015-04-03 14:49:46 -07001862 def test_abandoned_not_timer(self):
1863 "Test that an abandoned change does not cancel timer jobs"
1864
James E. Blair08d19992016-08-10 15:25:31 -07001865 self.launch_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001866
1867 # Start timer trigger - also org/project
James E. Blairf84026c2015-12-08 16:11:46 -08001868 self.updateConfigLayout(
1869 'tests/fixtures/layout-idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001870 self.sched.reconfigure(self.config)
1871 self.registerJobs()
1872 # The pipeline triggers every second, so we should have seen
1873 # several by now.
1874 time.sleep(5)
1875 self.waitUntilSettled()
1876 # Stop queuing timer triggered jobs so that the assertions
1877 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08001878 self.updateConfigLayout(
1879 'tests/fixtures/layout-no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001880 self.sched.reconfigure(self.config)
1881 self.registerJobs()
1882 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1883
1884 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1885 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1886 self.waitUntilSettled()
1887 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1888
1889 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1890 self.waitUntilSettled()
1891
1892 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1893
James E. Blair08d19992016-08-10 15:25:31 -07001894 self.launch_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001895 self.waitUntilSettled()
1896
Arx Cruzb1b010d2013-10-28 19:49:59 -02001897 def test_zuul_url_return(self):
1898 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001899 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
James E. Blair08d19992016-08-10 15:25:31 -07001900 self.launch_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001901
1902 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001903 A.addApproval('code-review', 2)
1904 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001905 self.waitUntilSettled()
1906
1907 self.assertEqual(len(self.builds), 1)
1908 for build in self.builds:
1909 self.assertTrue('ZUUL_URL' in build.parameters)
1910
James E. Blair08d19992016-08-10 15:25:31 -07001911 self.launch_server.hold_jobs_in_build = False
1912 self.launch_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001913 self.waitUntilSettled()
1914
James E. Blair2fa50962013-01-30 21:50:41 -08001915 def test_new_patchset_dequeues_old_on_head(self):
1916 "Test that a new patchset causes the old to be dequeued (at head)"
1917 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair08d19992016-08-10 15:25:31 -07001918 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001919 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1920 M.setMerged()
1921 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1922 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1923 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1924 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001925 A.addApproval('code-review', 2)
1926 B.addApproval('code-review', 2)
1927 C.addApproval('code-review', 2)
1928 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001929
1930 C.setDependsOn(B, 1)
1931 B.setDependsOn(A, 1)
1932 A.setDependsOn(M, 1)
1933
James E. Blair8b5408c2016-08-08 15:37:46 -07001934 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1935 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1936 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1937 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001938 self.waitUntilSettled()
1939
1940 A.addPatchset()
1941 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1942 self.waitUntilSettled()
1943
James E. Blair08d19992016-08-10 15:25:31 -07001944 self.launch_server.hold_jobs_in_build = False
1945 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001946 self.waitUntilSettled()
1947
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001948 self.assertEqual(A.data['status'], 'NEW')
1949 self.assertEqual(A.reported, 2)
1950 self.assertEqual(B.data['status'], 'NEW')
1951 self.assertEqual(B.reported, 2)
1952 self.assertEqual(C.data['status'], 'NEW')
1953 self.assertEqual(C.reported, 2)
1954 self.assertEqual(D.data['status'], 'MERGED')
1955 self.assertEqual(D.reported, 2)
1956 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001957
1958 def test_new_patchset_dequeues_old_without_dependents(self):
1959 "Test that a new patchset causes only the old to be dequeued"
James E. Blair08d19992016-08-10 15:25:31 -07001960 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001961 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1962 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1963 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001964 A.addApproval('code-review', 2)
1965 B.addApproval('code-review', 2)
1966 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001967
James E. Blair8b5408c2016-08-08 15:37:46 -07001968 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1969 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1970 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001971 self.waitUntilSettled()
1972
1973 B.addPatchset()
1974 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1975 self.waitUntilSettled()
1976
James E. Blair08d19992016-08-10 15:25:31 -07001977 self.launch_server.hold_jobs_in_build = False
1978 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001979 self.waitUntilSettled()
1980
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001981 self.assertEqual(A.data['status'], 'MERGED')
1982 self.assertEqual(A.reported, 2)
1983 self.assertEqual(B.data['status'], 'NEW')
1984 self.assertEqual(B.reported, 2)
1985 self.assertEqual(C.data['status'], 'MERGED')
1986 self.assertEqual(C.reported, 2)
1987 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001988
1989 def test_new_patchset_dequeues_old_independent_queue(self):
1990 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair08d19992016-08-10 15:25:31 -07001991 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001992 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1993 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1994 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1995 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1996 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1997 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1998 self.waitUntilSettled()
1999
2000 B.addPatchset()
2001 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2002 self.waitUntilSettled()
2003
James E. Blair08d19992016-08-10 15:25:31 -07002004 self.launch_server.hold_jobs_in_build = False
2005 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002006 self.waitUntilSettled()
2007
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002008 self.assertEqual(A.data['status'], 'NEW')
2009 self.assertEqual(A.reported, 1)
2010 self.assertEqual(B.data['status'], 'NEW')
2011 self.assertEqual(B.reported, 1)
2012 self.assertEqual(C.data['status'], 'NEW')
2013 self.assertEqual(C.reported, 1)
2014 self.assertEqual(len(self.history), 10)
2015 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002016
James E. Blair18c64442014-03-18 10:14:45 -07002017 def test_noop_job(self):
2018 "Test that the internal noop job works"
2019 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002020 A.addApproval('code-review', 2)
2021 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07002022 self.waitUntilSettled()
2023
2024 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2025 self.assertTrue(self.sched._areAllBuildsComplete())
2026 self.assertEqual(len(self.history), 0)
2027 self.assertEqual(A.data['status'], 'MERGED')
2028 self.assertEqual(A.reported, 2)
2029
James E. Blairec056492016-07-22 09:45:56 -07002030 @skip("Disabled for early v3 development")
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002031 def test_no_job_project(self):
2032 "Test that reports with no jobs don't get sent"
2033 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
2034 'master', 'A')
2035 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2036 self.waitUntilSettled()
2037
2038 # Change wasn't reported to
2039 self.assertEqual(A.reported, False)
2040
2041 # Check queue is empty afterwards
2042 check_pipeline = self.sched.layout.pipelines['check']
2043 items = check_pipeline.getAllItems()
2044 self.assertEqual(len(items), 0)
2045
2046 self.assertEqual(len(self.history), 0)
2047
James E. Blair7d0dedc2013-02-21 17:26:09 -08002048 def test_zuul_refs(self):
2049 "Test that zuul refs exist and have the right changes"
James E. Blair08d19992016-08-10 15:25:31 -07002050 self.launch_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002051 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2052 M1.setMerged()
2053 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2054 M2.setMerged()
2055
2056 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2057 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2058 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2059 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07002060 A.addApproval('code-review', 2)
2061 B.addApproval('code-review', 2)
2062 C.addApproval('code-review', 2)
2063 D.addApproval('code-review', 2)
2064 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2065 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
2066 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
2067 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002068
2069 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002070 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002071 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002072 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002073 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002074 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002075 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002076 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002077 self.waitUntilSettled()
2078
James E. Blair7d0dedc2013-02-21 17:26:09 -08002079 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08002080 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002081 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002082 if x.parameters['ZUUL_CHANGE'] == '3':
2083 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002084 a_build = x
2085 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002086 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002087 b_build = x
2088 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002089 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002090 c_build = x
2091 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002092 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002093 d_build = x
2094 if a_build and b_build and c_build and d_build:
2095 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002096
2097 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002098 self.assertIsNotNone(a_zref)
2099 self.assertIsNotNone(b_zref)
2100 self.assertIsNotNone(c_zref)
2101 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002102
2103 # And they should all be different
2104 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002105 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002106
Clint Byrum3343e3e2016-11-15 16:05:03 -08002107 # should have a, not b, and should not be in project2
2108 self.assertTrue(a_build.hasChanges(A))
2109 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002110
Clint Byrum3343e3e2016-11-15 16:05:03 -08002111 # should have a and b, and should not be in project2
2112 self.assertTrue(b_build.hasChanges(A, B))
2113 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002114
Clint Byrum3343e3e2016-11-15 16:05:03 -08002115 # should have a and b in 1, c in 2
2116 self.assertTrue(c_build.hasChanges(A, B, C))
2117 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002118
Clint Byrum3343e3e2016-11-15 16:05:03 -08002119 # should have a and b in 1, c and d in 2
2120 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002121
James E. Blair08d19992016-08-10 15:25:31 -07002122 self.launch_server.hold_jobs_in_build = False
2123 self.launch_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002124 self.waitUntilSettled()
2125
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002126 self.assertEqual(A.data['status'], 'MERGED')
2127 self.assertEqual(A.reported, 2)
2128 self.assertEqual(B.data['status'], 'MERGED')
2129 self.assertEqual(B.reported, 2)
2130 self.assertEqual(C.data['status'], 'MERGED')
2131 self.assertEqual(C.reported, 2)
2132 self.assertEqual(D.data['status'], 'MERGED')
2133 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002134
James E. Blairec056492016-07-22 09:45:56 -07002135 @skip("Disabled for early v3 development")
James E. Blair4a28a882013-08-23 15:17:33 -07002136 def test_rerun_on_error(self):
2137 "Test that if a worker fails to run a job, it is run again"
James E. Blair08d19992016-08-10 15:25:31 -07002138 self.launch_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002139 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002140 A.addApproval('code-review', 2)
2141 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002142 self.waitUntilSettled()
2143
2144 self.builds[0].run_error = True
James E. Blair08d19992016-08-10 15:25:31 -07002145 self.launch_server.hold_jobs_in_build = False
2146 self.launch_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002147 self.waitUntilSettled()
2148 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2149 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2150
James E. Blair412e5582013-04-22 15:50:12 -07002151 def test_statsd(self):
2152 "Test each of the statsd methods used in the scheduler"
2153 import extras
2154 statsd = extras.try_import('statsd.statsd')
2155 statsd.incr('test-incr')
2156 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002157 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002158 self.assertReportedStat('test-incr', '1|c')
2159 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002160 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002161
James E. Blairec056492016-07-22 09:45:56 -07002162 @skip("Disabled for early v3 development")
James E. Blairdad52252014-02-07 16:59:17 -08002163 def test_stuck_job_cleanup(self):
2164 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07002165 # This job won't be registered at startup because it is not in
2166 # the standard layout, but we need it to already be registerd
2167 # for when we reconfigure, as that is when Zuul will attempt
2168 # to run the new job.
2169 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002170 self.gearman_server.hold_jobs_in_queue = True
2171 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002172 A.addApproval('code-review', 2)
2173 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002174 self.waitUntilSettled()
2175 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2176
James E. Blairf84026c2015-12-08 16:11:46 -08002177 self.updateConfigLayout(
2178 'tests/fixtures/layout-no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002179 self.sched.reconfigure(self.config)
2180 self.waitUntilSettled()
2181
James E. Blair18c64442014-03-18 10:14:45 -07002182 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002183 self.waitUntilSettled()
2184 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2185 self.assertTrue(self.sched._areAllBuildsComplete())
2186
2187 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002188 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002189 self.assertEqual(self.history[0].result, 'SUCCESS')
2190
James E. Blair879dafb2015-07-17 14:04:49 -07002191 def test_file_head(self):
2192 # This is a regression test for an observed bug. A change
2193 # with a file named "HEAD" in the root directory of the repo
2194 # was processed by a merger. It then was unable to reset the
2195 # repo because of:
2196 # GitCommandError: 'git reset --hard HEAD' returned
2197 # with exit code 128
2198 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2199 # and filename
2200 # Use '--' to separate filenames from revisions'
2201
2202 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002203 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002204 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2205
2206 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2207 self.waitUntilSettled()
2208
2209 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2210 self.waitUntilSettled()
2211
2212 self.assertIn('Build succeeded', A.messages[0])
2213 self.assertIn('Build succeeded', B.messages[0])
2214
James E. Blairec056492016-07-22 09:45:56 -07002215 @skip("Disabled for early v3 development")
James E. Blair70c71582013-03-06 08:50:50 -08002216 def test_file_jobs(self):
2217 "Test that file jobs run only when appropriate"
2218 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2219 A.addPatchset(['pip-requires'])
2220 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002221 A.addApproval('code-review', 2)
2222 B.addApproval('code-review', 2)
2223 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2224 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002225 self.waitUntilSettled()
2226
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002227 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002228 if x.name == 'project-testfile']
2229
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002230 self.assertEqual(len(testfile_jobs), 1)
2231 self.assertEqual(testfile_jobs[0].changes, '1,2')
2232 self.assertEqual(A.data['status'], 'MERGED')
2233 self.assertEqual(A.reported, 2)
2234 self.assertEqual(B.data['status'], 'MERGED')
2235 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002236
James E. Blairec056492016-07-22 09:45:56 -07002237 @skip("Disabled for early v3 development")
Maru Newby3fe5f852015-01-13 04:22:14 +00002238 def _test_skip_if_jobs(self, branch, should_skip):
2239 "Test that jobs with a skip-if filter run only when appropriate"
James E. Blairf84026c2015-12-08 16:11:46 -08002240 self.updateConfigLayout(
2241 'tests/fixtures/layout-skip-if.yaml')
Maru Newby3fe5f852015-01-13 04:22:14 +00002242 self.sched.reconfigure(self.config)
2243 self.registerJobs()
2244
2245 change = self.fake_gerrit.addFakeChange('org/project',
2246 branch,
2247 'test skip-if')
2248 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2249 self.waitUntilSettled()
2250
2251 tested_change_ids = [x.changes[0] for x in self.history
2252 if x.name == 'project-test-skip-if']
2253
2254 if should_skip:
2255 self.assertEqual([], tested_change_ids)
2256 else:
2257 self.assertIn(change.data['number'], tested_change_ids)
2258
James E. Blairec056492016-07-22 09:45:56 -07002259 @skip("Disabled for early v3 development")
Maru Newby3fe5f852015-01-13 04:22:14 +00002260 def test_skip_if_match_skips_job(self):
2261 self._test_skip_if_jobs(branch='master', should_skip=True)
2262
James E. Blairec056492016-07-22 09:45:56 -07002263 @skip("Disabled for early v3 development")
Maru Newby3fe5f852015-01-13 04:22:14 +00002264 def test_skip_if_no_match_runs_job(self):
2265 self._test_skip_if_jobs(branch='mp', should_skip=False)
2266
James E. Blairec056492016-07-22 09:45:56 -07002267 @skip("Disabled for early v3 development")
James E. Blair3c5e5b52013-04-26 11:17:03 -07002268 def test_test_config(self):
2269 "Test that we can test the config"
James E. Blairf84026c2015-12-08 16:11:46 -08002270 self.sched.testConfig(self.config.get('zuul', 'tenant_config'),
Joshua Hesketh352264b2015-08-11 23:42:08 +10002271 self.connections)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002272
James E. Blairec056492016-07-22 09:45:56 -07002273 @skip("Disabled for early v3 development")
James E. Blairc8a1e052014-02-25 09:29:26 -08002274 def test_queue_names(self):
2275 "Test shared change queue names"
2276 project1 = self.sched.layout.projects['org/project1']
2277 project2 = self.sched.layout.projects['org/project2']
2278 q1 = self.sched.layout.pipelines['gate'].getQueue(project1)
2279 q2 = self.sched.layout.pipelines['gate'].getQueue(project2)
2280 self.assertEqual(q1.name, 'integration')
2281 self.assertEqual(q2.name, 'integration')
2282
James E. Blairf84026c2015-12-08 16:11:46 -08002283 self.updateConfigLayout(
2284 'tests/fixtures/layout-bad-queue.yaml')
James E. Blairc8a1e052014-02-25 09:29:26 -08002285 with testtools.ExpectedException(
2286 Exception, "More than one name assigned to change queue"):
2287 self.sched.reconfigure(self.config)
2288
James E. Blair64ed6f22013-07-10 14:07:23 -07002289 def test_queue_precedence(self):
2290 "Test that queue precedence works"
2291
2292 self.gearman_server.hold_jobs_in_queue = True
James E. Blair08d19992016-08-10 15:25:31 -07002293 self.launch_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002294 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2295 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002296 A.addApproval('code-review', 2)
2297 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002298
2299 self.waitUntilSettled()
2300 self.gearman_server.hold_jobs_in_queue = False
2301 self.gearman_server.release()
2302 self.waitUntilSettled()
2303
James E. Blair8de58bd2013-07-18 16:23:33 -07002304 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002305 self.orderedRelease()
James E. Blair08d19992016-08-10 15:25:31 -07002306 self.launch_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002307 self.waitUntilSettled()
2308
James E. Blair64ed6f22013-07-10 14:07:23 -07002309 self.log.debug(self.history)
2310 self.assertEqual(self.history[0].pipeline, 'gate')
2311 self.assertEqual(self.history[1].pipeline, 'check')
2312 self.assertEqual(self.history[2].pipeline, 'gate')
2313 self.assertEqual(self.history[3].pipeline, 'gate')
2314 self.assertEqual(self.history[4].pipeline, 'check')
2315 self.assertEqual(self.history[5].pipeline, 'check')
2316
James E. Blairec056492016-07-22 09:45:56 -07002317 @skip("Disabled for early v3 development")
Clark Boylana5edbe42014-06-03 16:39:10 -07002318 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002319 "Test that we can retrieve JSON status info"
James E. Blair08d19992016-08-10 15:25:31 -07002320 self.launch_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002321 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002322 A.addApproval('code-review', 2)
2323 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002324 self.waitUntilSettled()
2325
James E. Blair08d19992016-08-10 15:25:31 -07002326 self.launch_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002327 self.waitUntilSettled()
2328
James E. Blair1843a552013-07-03 14:19:52 -07002329 port = self.webapp.server.socket.getsockname()[1]
2330
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002331 req = urllib.request.Request("http://localhost:%s/status.json" % port)
2332 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002333 headers = f.info()
2334 self.assertIn('Content-Length', headers)
2335 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002336 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2337 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002338 self.assertIn('Access-Control-Allow-Origin', headers)
2339 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002340 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002341 self.assertIn('Expires', headers)
James E. Blair1843a552013-07-03 14:19:52 -07002342 data = f.read()
2343
James E. Blair08d19992016-08-10 15:25:31 -07002344 self.launch_server.hold_jobs_in_build = False
2345 self.launch_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002346 self.waitUntilSettled()
2347
2348 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002349 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002350 for p in data['pipelines']:
2351 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002352 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002353 self.assertEqual(q['window'], 20)
2354 else:
2355 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002356 for head in q['heads']:
2357 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002358 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002359 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002360 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002361 status_jobs.append(job)
2362 self.assertEqual('project-merge', status_jobs[0]['name'])
2363 self.assertEqual('https://server/job/project-merge/0/',
2364 status_jobs[0]['url'])
2365 self.assertEqual('http://logs.example.com/1/1/gate/project-merge/0',
2366 status_jobs[0]['report_url'])
2367
2368 self.assertEqual('project-test1', status_jobs[1]['name'])
2369 self.assertEqual('https://server/job/project-test1/1/',
2370 status_jobs[1]['url'])
2371 self.assertEqual('http://logs.example.com/1/1/gate/project-test1/1',
2372 status_jobs[1]['report_url'])
2373
2374 self.assertEqual('project-test2', status_jobs[2]['name'])
2375 self.assertEqual('https://server/job/project-test2/2/',
2376 status_jobs[2]['url'])
2377 self.assertEqual('http://logs.example.com/1/1/gate/project-test2/2',
2378 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002379
James E. Blairec056492016-07-22 09:45:56 -07002380 @skip("Disabled for early v3 development")
James E. Blairc3d428e2013-12-03 15:06:48 -08002381 def test_merging_queues(self):
2382 "Test that transitively-connected change queues are merged"
James E. Blairf84026c2015-12-08 16:11:46 -08002383 self.updateConfigLayout(
2384 'tests/fixtures/layout-merge-queues.yaml')
James E. Blairc3d428e2013-12-03 15:06:48 -08002385 self.sched.reconfigure(self.config)
2386 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
2387
James E. Blairec056492016-07-22 09:45:56 -07002388 @skip("Disabled for early v3 development")
James E. Blairaf17a972016-02-03 15:07:18 -08002389 def test_mutex(self):
2390 "Test job mutexes"
2391 self.config.set('zuul', 'layout_config',
2392 'tests/fixtures/layout-mutex.yaml')
2393 self.sched.reconfigure(self.config)
2394
James E. Blair08d19992016-08-10 15:25:31 -07002395 self.launch_server.hold_jobs_in_build = True
James E. Blairaf17a972016-02-03 15:07:18 -08002396 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2397 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2398 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2399
2400 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2401 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2402 self.waitUntilSettled()
2403 self.assertEqual(len(self.builds), 3)
2404 self.assertEqual(self.builds[0].name, 'project-test1')
2405 self.assertEqual(self.builds[1].name, 'mutex-one')
2406 self.assertEqual(self.builds[2].name, 'project-test1')
2407
James E. Blair08d19992016-08-10 15:25:31 -07002408 self.launch_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002409 self.waitUntilSettled()
2410
2411 self.assertEqual(len(self.builds), 3)
2412 self.assertEqual(self.builds[0].name, 'project-test1')
2413 self.assertEqual(self.builds[1].name, 'project-test1')
2414 self.assertEqual(self.builds[2].name, 'mutex-two')
2415 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2416
James E. Blair08d19992016-08-10 15:25:31 -07002417 self.launch_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002418 self.waitUntilSettled()
2419
2420 self.assertEqual(len(self.builds), 3)
2421 self.assertEqual(self.builds[0].name, 'project-test1')
2422 self.assertEqual(self.builds[1].name, 'project-test1')
2423 self.assertEqual(self.builds[2].name, 'mutex-one')
2424 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2425
James E. Blair08d19992016-08-10 15:25:31 -07002426 self.launch_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002427 self.waitUntilSettled()
2428
2429 self.assertEqual(len(self.builds), 3)
2430 self.assertEqual(self.builds[0].name, 'project-test1')
2431 self.assertEqual(self.builds[1].name, 'project-test1')
2432 self.assertEqual(self.builds[2].name, 'mutex-two')
2433 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2434
James E. Blair08d19992016-08-10 15:25:31 -07002435 self.launch_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002436 self.waitUntilSettled()
2437
2438 self.assertEqual(len(self.builds), 2)
2439 self.assertEqual(self.builds[0].name, 'project-test1')
2440 self.assertEqual(self.builds[1].name, 'project-test1')
2441 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2442
James E. Blair08d19992016-08-10 15:25:31 -07002443 self.launch_server.hold_jobs_in_build = False
2444 self.launch_server.release()
James E. Blairaf17a972016-02-03 15:07:18 -08002445
2446 self.waitUntilSettled()
2447 self.assertEqual(len(self.builds), 0)
2448
2449 self.assertEqual(A.reported, 1)
2450 self.assertEqual(B.reported, 1)
2451 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2452
James E. Blairec056492016-07-22 09:45:56 -07002453 @skip("Disabled for early v3 development")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002454 def test_node_label(self):
2455 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002456 self.worker.registerFunction('build:node-project-test1:debian')
2457
2458 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002459 A.addApproval('code-review', 2)
2460 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002461 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002462
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002463 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2464 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2465 'debian')
2466 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002467
2468 def test_live_reconfiguration(self):
2469 "Test that live reconfiguration works"
James E. Blair08d19992016-08-10 15:25:31 -07002470 self.launch_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002471 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002472 A.addApproval('code-review', 2)
2473 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002474 self.waitUntilSettled()
2475
2476 self.sched.reconfigure(self.config)
2477
James E. Blair08d19992016-08-10 15:25:31 -07002478 self.launch_server.hold_jobs_in_build = False
2479 self.launch_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002480 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002481 self.assertEqual(self.getJobFromHistory('project-merge').result,
2482 'SUCCESS')
2483 self.assertEqual(self.getJobFromHistory('project-test1').result,
2484 'SUCCESS')
2485 self.assertEqual(self.getJobFromHistory('project-test2').result,
2486 'SUCCESS')
2487 self.assertEqual(A.data['status'], 'MERGED')
2488 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002489
James E. Blairec056492016-07-22 09:45:56 -07002490 @skip("Disabled for early v3 development")
James E. Blair6bc782d2015-07-17 16:20:21 -07002491 def test_live_reconfiguration_merge_conflict(self):
2492 # A real-world bug: a change in a gate queue has a merge
2493 # conflict and a job is added to its project while it's
2494 # sitting in the queue. The job gets added to the change and
2495 # enqueued and the change gets stuck.
2496 self.worker.registerFunction('build:project-test3')
James E. Blair08d19992016-08-10 15:25:31 -07002497 self.launch_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002498
2499 # This change is fine. It's here to stop the queue long
2500 # enough for the next change to be subject to the
2501 # reconfiguration, as well as to provide a conflict for the
2502 # next change. This change will succeed and merge.
2503 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2504 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002505 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002506
2507 # This change will be in merge conflict. During the
2508 # reconfiguration, we will add a job. We want to make sure
2509 # that doesn't cause it to get stuck.
2510 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2511 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002512 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002513
James E. Blair8b5408c2016-08-08 15:37:46 -07002514 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2515 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002516
2517 self.waitUntilSettled()
2518
2519 # No jobs have run yet
2520 self.assertEqual(A.data['status'], 'NEW')
2521 self.assertEqual(A.reported, 1)
2522 self.assertEqual(B.data['status'], 'NEW')
2523 self.assertEqual(B.reported, 1)
2524 self.assertEqual(len(self.history), 0)
2525
2526 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002527 self.updateConfigLayout(
2528 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002529 self.sched.reconfigure(self.config)
2530 self.waitUntilSettled()
2531
James E. Blair08d19992016-08-10 15:25:31 -07002532 self.launch_server.hold_jobs_in_build = False
2533 self.launch_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002534 self.waitUntilSettled()
2535
2536 self.assertEqual(A.data['status'], 'MERGED')
2537 self.assertEqual(A.reported, 2)
2538 self.assertEqual(B.data['status'], 'NEW')
2539 self.assertEqual(B.reported, 2)
2540 self.assertEqual(self.getJobFromHistory('project-merge').result,
2541 'SUCCESS')
2542 self.assertEqual(self.getJobFromHistory('project-test1').result,
2543 'SUCCESS')
2544 self.assertEqual(self.getJobFromHistory('project-test2').result,
2545 'SUCCESS')
2546 self.assertEqual(self.getJobFromHistory('project-test3').result,
2547 'SUCCESS')
2548 self.assertEqual(len(self.history), 4)
2549
James E. Blairec056492016-07-22 09:45:56 -07002550 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002551 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002552 # An extrapolation of test_live_reconfiguration_merge_conflict
2553 # that tests a job added to a job tree with a failed root does
2554 # not run.
2555 self.worker.registerFunction('build:project-test3')
James E. Blair08d19992016-08-10 15:25:31 -07002556 self.launch_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002557
2558 # This change is fine. It's here to stop the queue long
2559 # enough for the next change to be subject to the
2560 # reconfiguration. This change will succeed and merge.
2561 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2562 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002563 A.addApproval('code-review', 2)
2564 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002565 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002566 self.launch_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002567 self.waitUntilSettled()
2568
2569 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair08d19992016-08-10 15:25:31 -07002570 self.launch_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002571 B.addApproval('code-review', 2)
2572 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002573 self.waitUntilSettled()
2574
James E. Blair08d19992016-08-10 15:25:31 -07002575 self.launch_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002576 self.waitUntilSettled()
2577
2578 # Both -merge jobs have run, but no others.
2579 self.assertEqual(A.data['status'], 'NEW')
2580 self.assertEqual(A.reported, 1)
2581 self.assertEqual(B.data['status'], 'NEW')
2582 self.assertEqual(B.reported, 1)
2583 self.assertEqual(self.history[0].result, 'SUCCESS')
2584 self.assertEqual(self.history[0].name, 'project-merge')
2585 self.assertEqual(self.history[1].result, 'FAILURE')
2586 self.assertEqual(self.history[1].name, 'project-merge')
2587 self.assertEqual(len(self.history), 2)
2588
2589 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002590 self.updateConfigLayout(
2591 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002592 self.sched.reconfigure(self.config)
2593 self.waitUntilSettled()
2594
James E. Blair08d19992016-08-10 15:25:31 -07002595 self.launch_server.hold_jobs_in_build = False
2596 self.launch_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002597 self.waitUntilSettled()
2598
2599 self.assertEqual(A.data['status'], 'MERGED')
2600 self.assertEqual(A.reported, 2)
2601 self.assertEqual(B.data['status'], 'NEW')
2602 self.assertEqual(B.reported, 2)
2603 self.assertEqual(self.history[0].result, 'SUCCESS')
2604 self.assertEqual(self.history[0].name, 'project-merge')
2605 self.assertEqual(self.history[1].result, 'FAILURE')
2606 self.assertEqual(self.history[1].name, 'project-merge')
2607 self.assertEqual(self.history[2].result, 'SUCCESS')
2608 self.assertEqual(self.history[3].result, 'SUCCESS')
2609 self.assertEqual(self.history[4].result, 'SUCCESS')
2610 self.assertEqual(len(self.history), 5)
2611
James E. Blairec056492016-07-22 09:45:56 -07002612 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002613 def test_live_reconfiguration_failed_job(self):
2614 # Test that a change with a removed failing job does not
2615 # disrupt reconfiguration. If a change has a failed job and
2616 # that job is removed during a reconfiguration, we observed a
2617 # bug where the code to re-set build statuses would run on
2618 # that build and raise an exception because the job no longer
2619 # existed.
James E. Blair08d19992016-08-10 15:25:31 -07002620 self.launch_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002621
2622 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2623
2624 # This change will fail and later be removed by the reconfiguration.
James E. Blair08d19992016-08-10 15:25:31 -07002625 self.launch_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002626
2627 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2628 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002629 self.launch_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002630 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002631 self.launch_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002632 self.waitUntilSettled()
2633
2634 self.assertEqual(A.data['status'], 'NEW')
2635 self.assertEqual(A.reported, 0)
2636
2637 self.assertEqual(self.getJobFromHistory('project-merge').result,
2638 'SUCCESS')
2639 self.assertEqual(self.getJobFromHistory('project-test1').result,
2640 'FAILURE')
2641 self.assertEqual(len(self.history), 2)
2642
2643 # Remove the test1 job.
James E. Blairf84026c2015-12-08 16:11:46 -08002644 self.updateConfigLayout(
2645 'tests/fixtures/layout-live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002646 self.sched.reconfigure(self.config)
2647 self.waitUntilSettled()
2648
James E. Blair08d19992016-08-10 15:25:31 -07002649 self.launch_server.hold_jobs_in_build = False
2650 self.launch_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002651 self.waitUntilSettled()
2652
2653 self.assertEqual(self.getJobFromHistory('project-test2').result,
2654 'SUCCESS')
2655 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2656 'SUCCESS')
2657 self.assertEqual(len(self.history), 4)
2658
2659 self.assertEqual(A.data['status'], 'NEW')
2660 self.assertEqual(A.reported, 1)
2661 self.assertIn('Build succeeded', A.messages[0])
2662 # Ensure the removed job was not included in the report.
2663 self.assertNotIn('project-test1', A.messages[0])
2664
James E. Blairec056492016-07-22 09:45:56 -07002665 @skip("Disabled for early v3 development")
James E. Blairfe707d12015-08-05 15:18:15 -07002666 def test_live_reconfiguration_shared_queue(self):
2667 # Test that a change with a failing job which was removed from
2668 # this project but otherwise still exists in the system does
2669 # not disrupt reconfiguration.
2670
James E. Blair08d19992016-08-10 15:25:31 -07002671 self.launch_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002672
2673 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2674
James E. Blair08d19992016-08-10 15:25:31 -07002675 self.launch_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002676
2677 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2678 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002679 self.launch_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002680 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002681 self.launch_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002682 self.waitUntilSettled()
2683
2684 self.assertEqual(A.data['status'], 'NEW')
2685 self.assertEqual(A.reported, 0)
2686
2687 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2688 'SUCCESS')
2689 self.assertEqual(self.getJobFromHistory(
2690 'project1-project2-integration').result, 'FAILURE')
2691 self.assertEqual(len(self.history), 2)
2692
2693 # Remove the integration job.
James E. Blairf84026c2015-12-08 16:11:46 -08002694 self.updateConfigLayout(
2695 'tests/fixtures/layout-live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002696 self.sched.reconfigure(self.config)
2697 self.waitUntilSettled()
2698
James E. Blair08d19992016-08-10 15:25:31 -07002699 self.launch_server.hold_jobs_in_build = False
2700 self.launch_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002701 self.waitUntilSettled()
2702
2703 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2704 'SUCCESS')
2705 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2706 'SUCCESS')
2707 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2708 'SUCCESS')
2709 self.assertEqual(self.getJobFromHistory(
2710 'project1-project2-integration').result, 'FAILURE')
2711 self.assertEqual(len(self.history), 4)
2712
2713 self.assertEqual(A.data['status'], 'NEW')
2714 self.assertEqual(A.reported, 1)
2715 self.assertIn('Build succeeded', A.messages[0])
2716 # Ensure the removed job was not included in the report.
2717 self.assertNotIn('project1-project2-integration', A.messages[0])
2718
James E. Blairec056492016-07-22 09:45:56 -07002719 @skip("Disabled for early v3 development")
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002720 def test_double_live_reconfiguration_shared_queue(self):
2721 # This was a real-world regression. A change is added to
2722 # gate; a reconfigure happens, a second change which depends
2723 # on the first is added, and a second reconfiguration happens.
2724 # Ensure that both changes merge.
2725
2726 # A failure may indicate incorrect caching or cleaning up of
2727 # references during a reconfiguration.
James E. Blair08d19992016-08-10 15:25:31 -07002728 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002729
2730 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2731 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2732 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002733 A.addApproval('code-review', 2)
2734 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002735
2736 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002737 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002738 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002739 self.launch_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002740 self.waitUntilSettled()
2741
2742 # Reconfigure (with only one change in the pipeline).
2743 self.sched.reconfigure(self.config)
2744 self.waitUntilSettled()
2745
2746 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002747 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002748 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002749 self.launch_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002750 self.waitUntilSettled()
2751
2752 # Reconfigure (with both in the pipeline).
2753 self.sched.reconfigure(self.config)
2754 self.waitUntilSettled()
2755
James E. Blair08d19992016-08-10 15:25:31 -07002756 self.launch_server.hold_jobs_in_build = False
2757 self.launch_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002758 self.waitUntilSettled()
2759
2760 self.assertEqual(len(self.history), 8)
2761
2762 self.assertEqual(A.data['status'], 'MERGED')
2763 self.assertEqual(A.reported, 2)
2764 self.assertEqual(B.data['status'], 'MERGED')
2765 self.assertEqual(B.reported, 2)
2766
James E. Blairec056492016-07-22 09:45:56 -07002767 @skip("Disabled for early v3 development")
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002768 def test_live_reconfiguration_del_project(self):
2769 # Test project deletion from layout
2770 # while changes are enqueued
2771
James E. Blair08d19992016-08-10 15:25:31 -07002772 self.launch_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002773 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2774 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2775 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2776
2777 # A Depends-On: B
2778 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2779 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002780 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002781
2782 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2783 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2784 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002785 self.launch_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002786 self.waitUntilSettled()
2787 self.assertEqual(len(self.builds), 5)
2788
2789 # This layout defines only org/project, not org/project1
James E. Blairf84026c2015-12-08 16:11:46 -08002790 self.updateConfigLayout(
2791 'tests/fixtures/layout-live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002792 self.sched.reconfigure(self.config)
2793 self.waitUntilSettled()
2794
2795 # Builds for C aborted, builds for A succeed,
2796 # and have change B applied ahead
2797 job_c = self.getJobFromHistory('project1-test1')
2798 self.assertEqual(job_c.changes, '3,1')
2799 self.assertEqual(job_c.result, 'ABORTED')
2800
James E. Blair08d19992016-08-10 15:25:31 -07002801 self.launch_server.hold_jobs_in_build = False
2802 self.launch_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002803 self.waitUntilSettled()
2804
2805 self.assertEqual(self.getJobFromHistory('project-test1').changes,
2806 '2,1 1,1')
2807
2808 self.assertEqual(A.data['status'], 'NEW')
2809 self.assertEqual(B.data['status'], 'NEW')
2810 self.assertEqual(C.data['status'], 'NEW')
2811 self.assertEqual(A.reported, 1)
2812 self.assertEqual(B.reported, 0)
2813 self.assertEqual(C.reported, 0)
2814
2815 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
2816 self.assertIn('Build succeeded', A.messages[0])
2817
James E. Blairec056492016-07-22 09:45:56 -07002818 @skip("Disabled for early v3 development")
James E. Blaire712d9f2013-07-31 11:40:11 -07002819 def test_live_reconfiguration_functions(self):
2820 "Test live reconfiguration with a custom function"
2821 self.worker.registerFunction('build:node-project-test1:debian')
2822 self.worker.registerFunction('build:node-project-test1:wheezy')
2823 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002824 A.addApproval('code-review', 2)
2825 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002826 self.waitUntilSettled()
2827
2828 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2829 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2830 'debian')
2831 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2832
James E. Blairf84026c2015-12-08 16:11:46 -08002833 self.updateConfigLayout(
2834 'tests/fixtures/layout-live-reconfiguration-functions.yaml')
James E. Blaire712d9f2013-07-31 11:40:11 -07002835 self.sched.reconfigure(self.config)
2836 self.worker.build_history = []
2837
2838 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002839 B.addApproval('code-review', 2)
2840 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002841 self.waitUntilSettled()
2842
2843 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2844 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2845 'wheezy')
2846 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2847
James E. Blairec056492016-07-22 09:45:56 -07002848 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002849 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002850 self.updateConfigLayout(
2851 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002852 self.sched.reconfigure(self.config)
2853
2854 self.init_repo("org/new-project")
2855 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2856
James E. Blair8b5408c2016-08-08 15:37:46 -07002857 A.addApproval('code-review', 2)
2858 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002859 self.waitUntilSettled()
2860 self.assertEqual(self.getJobFromHistory('project-merge').result,
2861 'SUCCESS')
2862 self.assertEqual(self.getJobFromHistory('project-test1').result,
2863 'SUCCESS')
2864 self.assertEqual(self.getJobFromHistory('project-test2').result,
2865 'SUCCESS')
2866 self.assertEqual(A.data['status'], 'MERGED')
2867 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002868
Clark Boylan6dbbc482013-10-18 10:57:31 -07002869 def test_repo_deleted(self):
Paul Belanger66e95962016-11-11 12:11:06 -05002870 self.updateConfigLayout('layout-repo-deleted')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002871 self.sched.reconfigure(self.config)
2872
2873 self.init_repo("org/delete-project")
2874 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2875
James E. Blair8b5408c2016-08-08 15:37:46 -07002876 A.addApproval('code-review', 2)
2877 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002878 self.waitUntilSettled()
2879 self.assertEqual(self.getJobFromHistory('project-merge').result,
2880 'SUCCESS')
2881 self.assertEqual(self.getJobFromHistory('project-test1').result,
2882 'SUCCESS')
2883 self.assertEqual(self.getJobFromHistory('project-test2').result,
2884 'SUCCESS')
2885 self.assertEqual(A.data['status'], 'MERGED')
2886 self.assertEqual(A.reported, 2)
2887
2888 # Delete org/new-project zuul repo. Should be recloned.
2889 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
2890
2891 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2892
James E. Blair8b5408c2016-08-08 15:37:46 -07002893 B.addApproval('code-review', 2)
2894 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002895 self.waitUntilSettled()
2896 self.assertEqual(self.getJobFromHistory('project-merge').result,
2897 'SUCCESS')
2898 self.assertEqual(self.getJobFromHistory('project-test1').result,
2899 'SUCCESS')
2900 self.assertEqual(self.getJobFromHistory('project-test2').result,
2901 'SUCCESS')
2902 self.assertEqual(B.data['status'], 'MERGED')
2903 self.assertEqual(B.reported, 2)
2904
James E. Blairec056492016-07-22 09:45:56 -07002905 @skip("Disabled for early v3 development")
James E. Blair456f2fb2016-02-09 09:29:33 -08002906 def test_tags(self):
2907 "Test job tags"
2908 self.config.set('zuul', 'layout_config',
2909 'tests/fixtures/layout-tags.yaml')
2910 self.sched.reconfigure(self.config)
2911
2912 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2913 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2914 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2915 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2916 self.waitUntilSettled()
2917
2918 results = {'project1-merge': 'extratag merge project1',
2919 'project2-merge': 'merge'}
2920
2921 for build in self.history:
2922 self.assertEqual(results.get(build.name, ''),
2923 build.parameters.get('BUILD_TAGS'))
2924
James E. Blairec056492016-07-22 09:45:56 -07002925 @skip("Disabled for early v3 development")
James E. Blair63bb0ef2013-07-29 17:14:51 -07002926 def test_timer(self):
2927 "Test that a periodic job is triggered"
James E. Blair08d19992016-08-10 15:25:31 -07002928 self.launch_server.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08002929 self.updateConfigLayout(
2930 'tests/fixtures/layout-timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002931 self.sched.reconfigure(self.config)
2932 self.registerJobs()
2933
Clark Boylan3ee090a2014-04-03 20:55:09 -07002934 # The pipeline triggers every second, so we should have seen
2935 # several by now.
2936 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002937 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002938
2939 self.assertEqual(len(self.builds), 2)
2940
James E. Blair63bb0ef2013-07-29 17:14:51 -07002941 port = self.webapp.server.socket.getsockname()[1]
2942
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002943 req = urllib.request.Request("http://localhost:%s/status.json" % port)
2944 f = urllib.request.urlopen(req)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002945 data = f.read()
2946
James E. Blair08d19992016-08-10 15:25:31 -07002947 self.launch_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002948 # Stop queuing timer triggered jobs so that the assertions
2949 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002950 self.updateConfigLayout(
2951 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002952 self.sched.reconfigure(self.config)
2953 self.registerJobs()
James E. Blair08d19992016-08-10 15:25:31 -07002954 self.launch_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002955 self.waitUntilSettled()
2956
2957 self.assertEqual(self.getJobFromHistory(
2958 'project-bitrot-stable-old').result, 'SUCCESS')
2959 self.assertEqual(self.getJobFromHistory(
2960 'project-bitrot-stable-older').result, 'SUCCESS')
2961
2962 data = json.loads(data)
2963 status_jobs = set()
2964 for p in data['pipelines']:
2965 for q in p['change_queues']:
2966 for head in q['heads']:
2967 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07002968 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002969 for job in change['jobs']:
2970 status_jobs.add(job['name'])
2971 self.assertIn('project-bitrot-stable-old', status_jobs)
2972 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002973
James E. Blairec056492016-07-22 09:45:56 -07002974 @skip("Disabled for early v3 development")
James E. Blair4f6033c2014-03-27 15:49:09 -07002975 def test_idle(self):
2976 "Test that frequent periodic jobs work"
James E. Blair08d19992016-08-10 15:25:31 -07002977 self.launch_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002978
Clark Boylan3ee090a2014-04-03 20:55:09 -07002979 for x in range(1, 3):
2980 # Test that timer triggers periodic jobs even across
2981 # layout config reloads.
2982 # Start timer trigger
James E. Blairf84026c2015-12-08 16:11:46 -08002983 self.updateConfigLayout(
2984 'tests/fixtures/layout-idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002985 self.sched.reconfigure(self.config)
2986 self.registerJobs()
James E. Blair995fc0f2016-02-04 16:48:31 -08002987 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002988
Clark Boylan3ee090a2014-04-03 20:55:09 -07002989 # The pipeline triggers every second, so we should have seen
2990 # several by now.
2991 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002992
2993 # Stop queuing timer triggered jobs so that the assertions
2994 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002995 self.updateConfigLayout(
2996 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002997 self.sched.reconfigure(self.config)
2998 self.registerJobs()
James E. Blair995fc0f2016-02-04 16:48:31 -08002999 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07003000
3001 self.assertEqual(len(self.builds), 2)
James E. Blair08d19992016-08-10 15:25:31 -07003002 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003003 self.waitUntilSettled()
3004 self.assertEqual(len(self.builds), 0)
3005 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07003006
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003007 def test_check_smtp_pool(self):
Paul Belanger01be8ca2016-11-11 12:21:06 -05003008 self.updateConfigLayout('layout-smtp')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003009 self.sched.reconfigure(self.config)
3010
3011 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3012 self.waitUntilSettled()
3013
3014 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3015 self.waitUntilSettled()
3016
James E. Blairff80a2f2013-12-27 13:24:06 -08003017 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003018
3019 # A.messages only holds what FakeGerrit places in it. Thus we
3020 # work on the knowledge of what the first message should be as
3021 # it is only configured to go to SMTP.
3022
3023 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003024 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003025 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003026 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003027 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003028 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003029
3030 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003031 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003032 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003033 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003034 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003035 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003036
James E. Blairec056492016-07-22 09:45:56 -07003037 @skip("Disabled for early v3 development")
James E. Blaire5910202013-12-27 09:50:31 -08003038 def test_timer_smtp(self):
3039 "Test that a periodic job is triggered"
James E. Blair08d19992016-08-10 15:25:31 -07003040 self.launch_server.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08003041 self.updateConfigLayout(
3042 'tests/fixtures/layout-timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08003043 self.sched.reconfigure(self.config)
3044 self.registerJobs()
3045
Clark Boylan3ee090a2014-04-03 20:55:09 -07003046 # The pipeline triggers every second, so we should have seen
3047 # several by now.
3048 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003049 self.waitUntilSettled()
3050
Clark Boylan3ee090a2014-04-03 20:55:09 -07003051 self.assertEqual(len(self.builds), 2)
James E. Blair08d19992016-08-10 15:25:31 -07003052 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003053 self.waitUntilSettled()
3054 self.assertEqual(len(self.history), 2)
3055
James E. Blaire5910202013-12-27 09:50:31 -08003056 self.assertEqual(self.getJobFromHistory(
3057 'project-bitrot-stable-old').result, 'SUCCESS')
3058 self.assertEqual(self.getJobFromHistory(
3059 'project-bitrot-stable-older').result, 'SUCCESS')
3060
James E. Blairff80a2f2013-12-27 13:24:06 -08003061 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003062
3063 # A.messages only holds what FakeGerrit places in it. Thus we
3064 # work on the knowledge of what the first message should be as
3065 # it is only configured to go to SMTP.
3066
3067 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003068 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003069 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003070 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003071 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003072 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003073
Clark Boylan3ee090a2014-04-03 20:55:09 -07003074 # Stop queuing timer triggered jobs and let any that may have
3075 # queued through so that end of test assertions pass.
James E. Blairf84026c2015-12-08 16:11:46 -08003076 self.updateConfigLayout(
3077 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003078 self.sched.reconfigure(self.config)
3079 self.registerJobs()
James E. Blairf8058972014-08-15 16:09:16 -07003080 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003081 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003082 self.waitUntilSettled()
3083
James E. Blairec056492016-07-22 09:45:56 -07003084 @skip("Disabled for early v3 development")
James E. Blair91e34592015-07-31 16:45:59 -07003085 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003086 "Test that the RPC client can enqueue a change"
3087 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003088 A.addApproval('code-review', 2)
3089 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003090
3091 client = zuul.rpcclient.RPCClient('127.0.0.1',
3092 self.gearman_server.port)
3093 r = client.enqueue(pipeline='gate',
3094 project='org/project',
3095 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003096 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003097 self.waitUntilSettled()
3098 self.assertEqual(self.getJobFromHistory('project-merge').result,
3099 'SUCCESS')
3100 self.assertEqual(self.getJobFromHistory('project-test1').result,
3101 'SUCCESS')
3102 self.assertEqual(self.getJobFromHistory('project-test2').result,
3103 'SUCCESS')
3104 self.assertEqual(A.data['status'], 'MERGED')
3105 self.assertEqual(A.reported, 2)
3106 self.assertEqual(r, True)
3107
James E. Blairec056492016-07-22 09:45:56 -07003108 @skip("Disabled for early v3 development")
James E. Blair91e34592015-07-31 16:45:59 -07003109 def test_client_enqueue_ref(self):
3110 "Test that the RPC client can enqueue a ref"
3111
3112 client = zuul.rpcclient.RPCClient('127.0.0.1',
3113 self.gearman_server.port)
3114 r = client.enqueue_ref(
3115 pipeline='post',
3116 project='org/project',
3117 trigger='gerrit',
3118 ref='master',
3119 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
3120 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
3121 self.waitUntilSettled()
3122 job_names = [x.name for x in self.history]
3123 self.assertEqual(len(self.history), 1)
3124 self.assertIn('project-post', job_names)
3125 self.assertEqual(r, True)
3126
James E. Blairec056492016-07-22 09:45:56 -07003127 @skip("Disabled for early v3 development")
James E. Blairad28e912013-11-27 10:43:22 -08003128 def test_client_enqueue_negative(self):
3129 "Test that the RPC client returns errors"
3130 client = zuul.rpcclient.RPCClient('127.0.0.1',
3131 self.gearman_server.port)
3132 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3133 "Invalid project"):
3134 r = client.enqueue(pipeline='gate',
3135 project='project-does-not-exist',
3136 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003137 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003138 client.shutdown()
3139 self.assertEqual(r, False)
3140
3141 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3142 "Invalid pipeline"):
3143 r = client.enqueue(pipeline='pipeline-does-not-exist',
3144 project='org/project',
3145 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003146 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003147 client.shutdown()
3148 self.assertEqual(r, False)
3149
3150 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3151 "Invalid trigger"):
3152 r = client.enqueue(pipeline='gate',
3153 project='org/project',
3154 trigger='trigger-does-not-exist',
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 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3160 "Invalid change"):
3161 r = client.enqueue(pipeline='gate',
3162 project='org/project',
3163 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003164 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003165 client.shutdown()
3166 self.assertEqual(r, False)
3167
3168 self.waitUntilSettled()
3169 self.assertEqual(len(self.history), 0)
3170 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003171
James E. Blairec056492016-07-22 09:45:56 -07003172 @skip("Disabled for early v3 development")
James E. Blair36658cf2013-12-06 17:53:48 -08003173 def test_client_promote(self):
3174 "Test that the RPC client can promote a change"
James E. Blair08d19992016-08-10 15:25:31 -07003175 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003176 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3177 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3178 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003179 A.addApproval('code-review', 2)
3180 B.addApproval('code-review', 2)
3181 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003182
James E. Blair8b5408c2016-08-08 15:37:46 -07003183 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3184 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3185 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003186
3187 self.waitUntilSettled()
3188
Sean Daguef39b9ca2014-01-10 21:34:35 -05003189 items = self.sched.layout.pipelines['gate'].getAllItems()
3190 enqueue_times = {}
3191 for item in items:
3192 enqueue_times[str(item.change)] = item.enqueue_time
3193
James E. Blair36658cf2013-12-06 17:53:48 -08003194 client = zuul.rpcclient.RPCClient('127.0.0.1',
3195 self.gearman_server.port)
3196 r = client.promote(pipeline='gate',
3197 change_ids=['2,1', '3,1'])
3198
Sean Daguef39b9ca2014-01-10 21:34:35 -05003199 # ensure that enqueue times are durable
3200 items = self.sched.layout.pipelines['gate'].getAllItems()
3201 for item in items:
3202 self.assertEqual(
3203 enqueue_times[str(item.change)], item.enqueue_time)
3204
James E. Blair78acec92014-02-06 07:11:32 -08003205 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003206 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003207 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003208 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003209 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003210 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003211 self.waitUntilSettled()
3212
3213 self.assertEqual(len(self.builds), 6)
3214 self.assertEqual(self.builds[0].name, 'project-test1')
3215 self.assertEqual(self.builds[1].name, 'project-test2')
3216 self.assertEqual(self.builds[2].name, 'project-test1')
3217 self.assertEqual(self.builds[3].name, 'project-test2')
3218 self.assertEqual(self.builds[4].name, 'project-test1')
3219 self.assertEqual(self.builds[5].name, 'project-test2')
3220
3221 self.assertTrue(self.job_has_changes(self.builds[0], B))
3222 self.assertFalse(self.job_has_changes(self.builds[0], A))
3223 self.assertFalse(self.job_has_changes(self.builds[0], C))
3224
3225 self.assertTrue(self.job_has_changes(self.builds[2], B))
3226 self.assertTrue(self.job_has_changes(self.builds[2], C))
3227 self.assertFalse(self.job_has_changes(self.builds[2], A))
3228
3229 self.assertTrue(self.job_has_changes(self.builds[4], B))
3230 self.assertTrue(self.job_has_changes(self.builds[4], C))
3231 self.assertTrue(self.job_has_changes(self.builds[4], A))
3232
James E. Blair08d19992016-08-10 15:25:31 -07003233 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003234 self.waitUntilSettled()
3235
3236 self.assertEqual(A.data['status'], 'MERGED')
3237 self.assertEqual(A.reported, 2)
3238 self.assertEqual(B.data['status'], 'MERGED')
3239 self.assertEqual(B.reported, 2)
3240 self.assertEqual(C.data['status'], 'MERGED')
3241 self.assertEqual(C.reported, 2)
3242
3243 client.shutdown()
3244 self.assertEqual(r, True)
3245
James E. Blairec056492016-07-22 09:45:56 -07003246 @skip("Disabled for early v3 development")
James E. Blair36658cf2013-12-06 17:53:48 -08003247 def test_client_promote_dependent(self):
3248 "Test that the RPC client can promote a dependent change"
3249 # C (depends on B) -> B -> A ; then promote C to get:
3250 # A -> C (depends on B) -> B
James E. Blair08d19992016-08-10 15:25:31 -07003251 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003252 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3253 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3254 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3255
3256 C.setDependsOn(B, 1)
3257
James E. Blair8b5408c2016-08-08 15:37:46 -07003258 A.addApproval('code-review', 2)
3259 B.addApproval('code-review', 2)
3260 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003261
James E. Blair8b5408c2016-08-08 15:37:46 -07003262 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3263 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3264 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003265
3266 self.waitUntilSettled()
3267
3268 client = zuul.rpcclient.RPCClient('127.0.0.1',
3269 self.gearman_server.port)
3270 r = client.promote(pipeline='gate',
3271 change_ids=['3,1'])
3272
James E. Blair78acec92014-02-06 07:11:32 -08003273 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003274 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003275 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003276 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003277 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003278 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003279 self.waitUntilSettled()
3280
3281 self.assertEqual(len(self.builds), 6)
3282 self.assertEqual(self.builds[0].name, 'project-test1')
3283 self.assertEqual(self.builds[1].name, 'project-test2')
3284 self.assertEqual(self.builds[2].name, 'project-test1')
3285 self.assertEqual(self.builds[3].name, 'project-test2')
3286 self.assertEqual(self.builds[4].name, 'project-test1')
3287 self.assertEqual(self.builds[5].name, 'project-test2')
3288
3289 self.assertTrue(self.job_has_changes(self.builds[0], B))
3290 self.assertFalse(self.job_has_changes(self.builds[0], A))
3291 self.assertFalse(self.job_has_changes(self.builds[0], C))
3292
3293 self.assertTrue(self.job_has_changes(self.builds[2], B))
3294 self.assertTrue(self.job_has_changes(self.builds[2], C))
3295 self.assertFalse(self.job_has_changes(self.builds[2], A))
3296
3297 self.assertTrue(self.job_has_changes(self.builds[4], B))
3298 self.assertTrue(self.job_has_changes(self.builds[4], C))
3299 self.assertTrue(self.job_has_changes(self.builds[4], A))
3300
James E. Blair08d19992016-08-10 15:25:31 -07003301 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003302 self.waitUntilSettled()
3303
3304 self.assertEqual(A.data['status'], 'MERGED')
3305 self.assertEqual(A.reported, 2)
3306 self.assertEqual(B.data['status'], 'MERGED')
3307 self.assertEqual(B.reported, 2)
3308 self.assertEqual(C.data['status'], 'MERGED')
3309 self.assertEqual(C.reported, 2)
3310
3311 client.shutdown()
3312 self.assertEqual(r, True)
3313
James E. Blairec056492016-07-22 09:45:56 -07003314 @skip("Disabled for early v3 development")
James E. Blair36658cf2013-12-06 17:53:48 -08003315 def test_client_promote_negative(self):
3316 "Test that the RPC client returns errors for promotion"
James E. Blair08d19992016-08-10 15:25:31 -07003317 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003318 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003319 A.addApproval('code-review', 2)
3320 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003321 self.waitUntilSettled()
3322
3323 client = zuul.rpcclient.RPCClient('127.0.0.1',
3324 self.gearman_server.port)
3325
3326 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3327 r = client.promote(pipeline='nonexistent',
3328 change_ids=['2,1', '3,1'])
3329 client.shutdown()
3330 self.assertEqual(r, False)
3331
3332 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3333 r = client.promote(pipeline='gate',
3334 change_ids=['4,1'])
3335 client.shutdown()
3336 self.assertEqual(r, False)
3337
James E. Blair08d19992016-08-10 15:25:31 -07003338 self.launch_server.hold_jobs_in_build = False
3339 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003340 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003341
James E. Blairec056492016-07-22 09:45:56 -07003342 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003343 def test_queue_rate_limiting(self):
3344 "Test that DependentPipelines are rate limited with dep across window"
James E. Blairf84026c2015-12-08 16:11:46 -08003345 self.updateConfigLayout(
3346 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003347 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07003348 self.launch_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003349 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3350 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3351 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3352
3353 C.setDependsOn(B, 1)
James E. Blair08d19992016-08-10 15:25:31 -07003354 self.launch_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003355
James E. Blair8b5408c2016-08-08 15:37:46 -07003356 A.addApproval('code-review', 2)
3357 B.addApproval('code-review', 2)
3358 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003359
James E. Blair8b5408c2016-08-08 15:37:46 -07003360 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3361 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3362 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003363 self.waitUntilSettled()
3364
3365 # Only A and B will have their merge jobs queued because
3366 # window is 2.
3367 self.assertEqual(len(self.builds), 2)
3368 self.assertEqual(self.builds[0].name, 'project-merge')
3369 self.assertEqual(self.builds[1].name, 'project-merge')
3370
James E. Blair08d19992016-08-10 15:25:31 -07003371 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003372 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003373 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003374 self.waitUntilSettled()
3375
3376 # Only A and B will have their test jobs queued because
3377 # window is 2.
3378 self.assertEqual(len(self.builds), 4)
3379 self.assertEqual(self.builds[0].name, 'project-test1')
3380 self.assertEqual(self.builds[1].name, 'project-test2')
3381 self.assertEqual(self.builds[2].name, 'project-test1')
3382 self.assertEqual(self.builds[3].name, 'project-test2')
3383
James E. Blair08d19992016-08-10 15:25:31 -07003384 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003385 self.waitUntilSettled()
3386
3387 queue = self.sched.layout.pipelines['gate'].queues[0]
3388 # A failed so window is reduced by 1 to 1.
3389 self.assertEqual(queue.window, 1)
3390 self.assertEqual(queue.window_floor, 1)
3391 self.assertEqual(A.data['status'], 'NEW')
3392
3393 # Gate is reset and only B's merge job is queued because
3394 # window shrunk to 1.
3395 self.assertEqual(len(self.builds), 1)
3396 self.assertEqual(self.builds[0].name, 'project-merge')
3397
James E. Blair08d19992016-08-10 15:25:31 -07003398 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003399 self.waitUntilSettled()
3400
3401 # Only B's test jobs are queued because window is still 1.
3402 self.assertEqual(len(self.builds), 2)
3403 self.assertEqual(self.builds[0].name, 'project-test1')
3404 self.assertEqual(self.builds[1].name, 'project-test2')
3405
James E. Blair08d19992016-08-10 15:25:31 -07003406 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003407 self.waitUntilSettled()
3408
3409 # B was successfully merged so window is increased to 2.
3410 self.assertEqual(queue.window, 2)
3411 self.assertEqual(queue.window_floor, 1)
3412 self.assertEqual(B.data['status'], 'MERGED')
3413
3414 # Only C is left and its merge job is queued.
3415 self.assertEqual(len(self.builds), 1)
3416 self.assertEqual(self.builds[0].name, 'project-merge')
3417
James E. Blair08d19992016-08-10 15:25:31 -07003418 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003419 self.waitUntilSettled()
3420
3421 # After successful merge job the test jobs for C are queued.
3422 self.assertEqual(len(self.builds), 2)
3423 self.assertEqual(self.builds[0].name, 'project-test1')
3424 self.assertEqual(self.builds[1].name, 'project-test2')
3425
James E. Blair08d19992016-08-10 15:25:31 -07003426 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003427 self.waitUntilSettled()
3428
3429 # C successfully merged so window is bumped to 3.
3430 self.assertEqual(queue.window, 3)
3431 self.assertEqual(queue.window_floor, 1)
3432 self.assertEqual(C.data['status'], 'MERGED')
3433
James E. Blairec056492016-07-22 09:45:56 -07003434 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003435 def test_queue_rate_limiting_dependent(self):
3436 "Test that DependentPipelines are rate limited with dep in window"
James E. Blairf84026c2015-12-08 16:11:46 -08003437 self.updateConfigLayout(
3438 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003439 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07003440 self.launch_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003441 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3442 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3443 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3444
3445 B.setDependsOn(A, 1)
3446
James E. Blair08d19992016-08-10 15:25:31 -07003447 self.launch_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003448
James E. Blair8b5408c2016-08-08 15:37:46 -07003449 A.addApproval('code-review', 2)
3450 B.addApproval('code-review', 2)
3451 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003452
James E. Blair8b5408c2016-08-08 15:37:46 -07003453 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3454 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3455 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003456 self.waitUntilSettled()
3457
3458 # Only A and B will have their merge jobs queued because
3459 # window is 2.
3460 self.assertEqual(len(self.builds), 2)
3461 self.assertEqual(self.builds[0].name, 'project-merge')
3462 self.assertEqual(self.builds[1].name, 'project-merge')
3463
James E. Blair08d19992016-08-10 15:25:31 -07003464 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003465 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003466 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003467 self.waitUntilSettled()
3468
3469 # Only A and B will have their test jobs queued because
3470 # window is 2.
3471 self.assertEqual(len(self.builds), 4)
3472 self.assertEqual(self.builds[0].name, 'project-test1')
3473 self.assertEqual(self.builds[1].name, 'project-test2')
3474 self.assertEqual(self.builds[2].name, 'project-test1')
3475 self.assertEqual(self.builds[3].name, 'project-test2')
3476
James E. Blair08d19992016-08-10 15:25:31 -07003477 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003478 self.waitUntilSettled()
3479
3480 queue = self.sched.layout.pipelines['gate'].queues[0]
3481 # A failed so window is reduced by 1 to 1.
3482 self.assertEqual(queue.window, 1)
3483 self.assertEqual(queue.window_floor, 1)
3484 self.assertEqual(A.data['status'], 'NEW')
3485 self.assertEqual(B.data['status'], 'NEW')
3486
3487 # Gate is reset and only C's merge job is queued because
3488 # window shrunk to 1 and A and B were dequeued.
3489 self.assertEqual(len(self.builds), 1)
3490 self.assertEqual(self.builds[0].name, 'project-merge')
3491
James E. Blair08d19992016-08-10 15:25:31 -07003492 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003493 self.waitUntilSettled()
3494
3495 # Only C's test jobs are queued because window is still 1.
3496 self.assertEqual(len(self.builds), 2)
3497 self.assertEqual(self.builds[0].name, 'project-test1')
3498 self.assertEqual(self.builds[1].name, 'project-test2')
3499
James E. Blair08d19992016-08-10 15:25:31 -07003500 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003501 self.waitUntilSettled()
3502
3503 # C was successfully merged so window is increased to 2.
3504 self.assertEqual(queue.window, 2)
3505 self.assertEqual(queue.window_floor, 1)
3506 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003507
James E. Blairec056492016-07-22 09:45:56 -07003508 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003509 def test_worker_update_metadata(self):
3510 "Test if a worker can send back metadata about itself"
James E. Blair08d19992016-08-10 15:25:31 -07003511 self.launch_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003512
3513 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003514 A.addApproval('code-review', 2)
3515 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003516 self.waitUntilSettled()
3517
3518 self.assertEqual(len(self.launcher.builds), 1)
3519
3520 self.log.debug('Current builds:')
3521 self.log.debug(self.launcher.builds)
3522
3523 start = time.time()
3524 while True:
3525 if time.time() - start > 10:
3526 raise Exception("Timeout waiting for gearman server to report "
3527 + "back to the client")
3528 build = self.launcher.builds.values()[0]
3529 if build.worker.name == "My Worker":
3530 break
3531 else:
3532 time.sleep(0)
3533
3534 self.log.debug(build)
3535 self.assertEqual("My Worker", build.worker.name)
3536 self.assertEqual("localhost", build.worker.hostname)
3537 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3538 self.assertEqual("zuul.example.org", build.worker.fqdn)
3539 self.assertEqual("FakeBuilder", build.worker.program)
3540 self.assertEqual("v1.1", build.worker.version)
3541 self.assertEqual({'something': 'else'}, build.worker.extra)
3542
James E. Blair08d19992016-08-10 15:25:31 -07003543 self.launch_server.hold_jobs_in_build = False
3544 self.launch_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003545 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003546
James E. Blairec056492016-07-22 09:45:56 -07003547 @skip("Disabled for early v3 development")
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003548 def test_footer_message(self):
3549 "Test a pipeline's footer message is correctly added to the report."
James E. Blairf84026c2015-12-08 16:11:46 -08003550 self.updateConfigLayout(
3551 'tests/fixtures/layout-footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003552 self.sched.reconfigure(self.config)
3553 self.registerJobs()
3554
3555 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003556 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07003557 self.launch_server.failJob('test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003558 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003559 self.waitUntilSettled()
3560
3561 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003562 B.addApproval('code-review', 2)
3563 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003564 self.waitUntilSettled()
3565
3566 self.assertEqual(2, len(self.smtp_messages))
3567
3568 failure_body = """\
3569Build failed. For information on how to proceed, see \
3570http://wiki.example.org/Test_Failures
3571
3572- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
3573- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
3574
3575For CI problems and help debugging, contact ci@example.org"""
3576
3577 success_body = """\
3578Build succeeded.
3579
3580- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
3581- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
3582
3583For CI problems and help debugging, contact ci@example.org"""
3584
3585 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
3586 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11003587
James E. Blairec056492016-07-22 09:45:56 -07003588 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003589 def test_merge_failure_reporters(self):
3590 """Check that the config is set up correctly"""
3591
James E. Blairf84026c2015-12-08 16:11:46 -08003592 self.updateConfigLayout(
3593 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003594 self.sched.reconfigure(self.config)
3595 self.registerJobs()
3596
3597 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003598 "Merge Failed.\n\nThis change or one of its cross-repo "
3599 "dependencies was unable to be automatically merged with the "
3600 "current state of its repository. Please rebase the change and "
3601 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003602 self.sched.layout.pipelines['check'].merge_failure_message)
3603 self.assertEqual(
3604 "The merge failed! For more information...",
3605 self.sched.layout.pipelines['gate'].merge_failure_message)
3606
3607 self.assertEqual(
3608 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3609 self.assertEqual(
3610 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3611
3612 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003613 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3614 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003615
3616 self.assertTrue(
3617 (
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.smtp.SMTPReporter) 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.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003624 ) or (
3625 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003626 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003627 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003628 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003629 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003630 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003631 )
3632 )
3633
James E. Blairec056492016-07-22 09:45:56 -07003634 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003635 def test_merge_failure_reports(self):
3636 """Check that when a change fails to merge the correct message is sent
3637 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003638 self.updateConfigLayout(
3639 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003640 self.sched.reconfigure(self.config)
3641 self.registerJobs()
3642
3643 # Check a test failure isn't reported to SMTP
3644 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003645 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07003646 self.launch_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003647 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003648 self.waitUntilSettled()
3649
3650 self.assertEqual(3, len(self.history)) # 3 jobs
3651 self.assertEqual(0, len(self.smtp_messages))
3652
3653 # Check a merge failure is reported to SMTP
3654 # B should be merged, but C will conflict with B
3655 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3656 B.addPatchset(['conflict'])
3657 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3658 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003659 B.addApproval('code-review', 2)
3660 C.addApproval('code-review', 2)
3661 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3662 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003663 self.waitUntilSettled()
3664
3665 self.assertEqual(6, len(self.history)) # A and B jobs
3666 self.assertEqual(1, len(self.smtp_messages))
3667 self.assertEqual('The merge failed! For more information...',
3668 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003669
James E. Blairec056492016-07-22 09:45:56 -07003670 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003671 def test_default_merge_failure_reports(self):
3672 """Check that the default merge failure reports are correct."""
3673
3674 # A should report success, B should report merge failure.
3675 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3676 A.addPatchset(['conflict'])
3677 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3678 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003679 A.addApproval('code-review', 2)
3680 B.addApproval('code-review', 2)
3681 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3682 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003683 self.waitUntilSettled()
3684
3685 self.assertEqual(3, len(self.history)) # A jobs
3686 self.assertEqual(A.reported, 2)
3687 self.assertEqual(B.reported, 2)
3688 self.assertEqual(A.data['status'], 'MERGED')
3689 self.assertEqual(B.data['status'], 'NEW')
3690 self.assertIn('Build succeeded', A.messages[1])
3691 self.assertIn('Merge Failed', B.messages[1])
3692 self.assertIn('automatically merged', B.messages[1])
3693 self.assertNotIn('logs.example.com', B.messages[1])
3694 self.assertNotIn('SKIPPED', B.messages[1])
3695
James E. Blairec056492016-07-22 09:45:56 -07003696 @skip("Disabled for early v3 development")
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003697 def test_swift_instructions(self):
3698 "Test that the correct swift instructions are sent to the workers"
James E. Blairf84026c2015-12-08 16:11:46 -08003699 self.updateConfigLayout(
3700 'tests/fixtures/layout-swift.yaml')
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003701 self.sched.reconfigure(self.config)
3702 self.registerJobs()
3703
James E. Blair08d19992016-08-10 15:25:31 -07003704 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003705 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3706
James E. Blair8b5408c2016-08-08 15:37:46 -07003707 A.addApproval('code-review', 2)
3708 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003709 self.waitUntilSettled()
3710
3711 self.assertEqual(
3712 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
3713 "gate/test-merge/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003714 self.builds[0].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003715 self.assertEqual(5,
3716 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
3717 split('\n')))
3718 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
3719
3720 self.assertEqual(
3721 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
3722 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003723 self.builds[1].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003724 self.assertEqual(5,
3725 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
3726 split('\n')))
3727 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
3728
3729 self.assertEqual(
3730 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
3731 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003732 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003733 self.assertEqual(5,
3734 len(self.builds[1].
3735 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
3736 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
3737
James E. Blair08d19992016-08-10 15:25:31 -07003738 self.launch_server.hold_jobs_in_build = False
3739 self.launch_server.release()
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003740 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003741
James E. Blairec056492016-07-22 09:45:56 -07003742 @skip("Disabled for early v3 development")
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003743 def test_client_get_running_jobs(self):
3744 "Test that the RPC client can get a list of running jobs"
James E. Blair08d19992016-08-10 15:25:31 -07003745 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003746 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003747 A.addApproval('code-review', 2)
3748 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003749 self.waitUntilSettled()
3750
3751 client = zuul.rpcclient.RPCClient('127.0.0.1',
3752 self.gearman_server.port)
3753
3754 # Wait for gearman server to send the initial workData back to zuul
3755 start = time.time()
3756 while True:
3757 if time.time() - start > 10:
3758 raise Exception("Timeout waiting for gearman server to report "
3759 + "back to the client")
3760 build = self.launcher.builds.values()[0]
3761 if build.worker.name == "My Worker":
3762 break
3763 else:
3764 time.sleep(0)
3765
3766 running_items = client.get_running_jobs()
3767
3768 self.assertEqual(1, len(running_items))
3769 running_item = running_items[0]
3770 self.assertEqual([], running_item['failing_reasons'])
3771 self.assertEqual([], running_item['items_behind'])
3772 self.assertEqual('https://hostname/1', running_item['url'])
3773 self.assertEqual(None, running_item['item_ahead'])
3774 self.assertEqual('org/project', running_item['project'])
3775 self.assertEqual(None, running_item['remaining_time'])
3776 self.assertEqual(True, running_item['active'])
3777 self.assertEqual('1,1', running_item['id'])
3778
3779 self.assertEqual(3, len(running_item['jobs']))
3780 for job in running_item['jobs']:
3781 if job['name'] == 'project-merge':
3782 self.assertEqual('project-merge', job['name'])
3783 self.assertEqual('gate', job['pipeline'])
3784 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003785 self.assertEqual('https://server/job/project-merge/0/',
3786 job['url'])
3787 self.assertEqual(7, len(job['worker']))
3788 self.assertEqual(False, job['canceled'])
3789 self.assertEqual(True, job['voting'])
3790 self.assertEqual(None, job['result'])
3791 self.assertEqual('gate', job['pipeline'])
3792 break
3793
James E. Blair08d19992016-08-10 15:25:31 -07003794 self.launch_server.hold_jobs_in_build = False
3795 self.launch_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003796 self.waitUntilSettled()
3797
3798 running_items = client.get_running_jobs()
3799 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003800
3801 def test_nonvoting_pipeline(self):
3802 "Test that a nonvoting pipeline (experimental) can still report"
3803
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003804 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3805 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003806 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3807 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003808
3809 self.assertEqual(self.getJobFromHistory('project-merge').result,
3810 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003811 self.assertEqual(
3812 self.getJobFromHistory('experimental-project-test').result,
3813 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003814 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003815
3816 def test_crd_gate(self):
3817 "Test cross-repo dependencies"
3818 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3819 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003820 A.addApproval('code-review', 2)
3821 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003822
3823 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3824 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3825 AM2.setMerged()
3826 AM1.setMerged()
3827
3828 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3829 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3830 BM2.setMerged()
3831 BM1.setMerged()
3832
3833 # A -> AM1 -> AM2
3834 # B -> BM1 -> BM2
3835 # A Depends-On: B
3836 # M2 is here to make sure it is never queried. If it is, it
3837 # means zuul is walking down the entire history of merged
3838 # changes.
3839
3840 B.setDependsOn(BM1, 1)
3841 BM1.setDependsOn(BM2, 1)
3842
3843 A.setDependsOn(AM1, 1)
3844 AM1.setDependsOn(AM2, 1)
3845
3846 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3847 A.subject, B.data['id'])
3848
James E. Blair8b5408c2016-08-08 15:37:46 -07003849 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003850 self.waitUntilSettled()
3851
3852 self.assertEqual(A.data['status'], 'NEW')
3853 self.assertEqual(B.data['status'], 'NEW')
3854
Paul Belanger5dccbe72016-11-14 11:17:37 -05003855 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003856 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003857
James E. Blair08d19992016-08-10 15:25:31 -07003858 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003859 B.addApproval('approved', 1)
3860 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003861 self.waitUntilSettled()
3862
James E. Blair08d19992016-08-10 15:25:31 -07003863 self.launch_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003864 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003865 self.launch_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003866 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003867 self.launch_server.hold_jobs_in_build = False
3868 self.launch_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003869 self.waitUntilSettled()
3870
3871 self.assertEqual(AM2.queried, 0)
3872 self.assertEqual(BM2.queried, 0)
3873 self.assertEqual(A.data['status'], 'MERGED')
3874 self.assertEqual(B.data['status'], 'MERGED')
3875 self.assertEqual(A.reported, 2)
3876 self.assertEqual(B.reported, 2)
3877
Paul Belanger5dccbe72016-11-14 11:17:37 -05003878 changes = self.getJobFromHistory(
3879 'project-merge', 'org/project1').changes
3880 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003881
3882 def test_crd_branch(self):
3883 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003884
3885 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003886 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3887 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3888 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3889 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003890 A.addApproval('code-review', 2)
3891 B.addApproval('code-review', 2)
3892 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003893
3894 # A Depends-On: B+C
3895 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3896 A.subject, B.data['id'])
3897
James E. Blair08d19992016-08-10 15:25:31 -07003898 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003899 B.addApproval('approved', 1)
3900 C.addApproval('approved', 1)
3901 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003902 self.waitUntilSettled()
3903
James E. Blair08d19992016-08-10 15:25:31 -07003904 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003905 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003906 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003907 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003908 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003909 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003910 self.launch_server.hold_jobs_in_build = False
3911 self.launch_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003912 self.waitUntilSettled()
3913
3914 self.assertEqual(A.data['status'], 'MERGED')
3915 self.assertEqual(B.data['status'], 'MERGED')
3916 self.assertEqual(C.data['status'], 'MERGED')
3917 self.assertEqual(A.reported, 2)
3918 self.assertEqual(B.reported, 2)
3919 self.assertEqual(C.reported, 2)
3920
Paul Belanger6379db12016-11-14 13:57:54 -05003921 changes = self.getJobFromHistory(
3922 'project-merge', 'org/project1').changes
3923 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003924
3925 def test_crd_multiline(self):
3926 "Test multiple depends-on lines in commit"
3927 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3928 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3929 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003930 A.addApproval('code-review', 2)
3931 B.addApproval('code-review', 2)
3932 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003933
3934 # A Depends-On: B+C
3935 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3936 A.subject, B.data['id'], C.data['id'])
3937
James E. Blair08d19992016-08-10 15:25:31 -07003938 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003939 B.addApproval('approved', 1)
3940 C.addApproval('approved', 1)
3941 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003942 self.waitUntilSettled()
3943
James E. Blair08d19992016-08-10 15:25:31 -07003944 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003945 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003946 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003947 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003948 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003949 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003950 self.launch_server.hold_jobs_in_build = False
3951 self.launch_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003952 self.waitUntilSettled()
3953
3954 self.assertEqual(A.data['status'], 'MERGED')
3955 self.assertEqual(B.data['status'], 'MERGED')
3956 self.assertEqual(C.data['status'], 'MERGED')
3957 self.assertEqual(A.reported, 2)
3958 self.assertEqual(B.reported, 2)
3959 self.assertEqual(C.reported, 2)
3960
Paul Belanger5dccbe72016-11-14 11:17:37 -05003961 changes = self.getJobFromHistory(
3962 'project-merge', 'org/project1').changes
3963 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003964
3965 def test_crd_unshared_gate(self):
3966 "Test cross-repo dependencies in unshared gate queues"
3967 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3968 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003969 A.addApproval('code-review', 2)
3970 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003971
3972 # A Depends-On: B
3973 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3974 A.subject, B.data['id'])
3975
3976 # A and B do not share a queue, make sure that A is unable to
3977 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003978 B.addApproval('approved', 1)
3979 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003980 self.waitUntilSettled()
3981
3982 self.assertEqual(A.data['status'], 'NEW')
3983 self.assertEqual(B.data['status'], 'NEW')
3984 self.assertEqual(A.reported, 0)
3985 self.assertEqual(B.reported, 0)
3986 self.assertEqual(len(self.history), 0)
3987
3988 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003989 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003990 self.waitUntilSettled()
3991
3992 self.assertEqual(B.data['status'], 'MERGED')
3993 self.assertEqual(B.reported, 2)
3994
3995 # Now that B is merged, A should be able to be enqueued and
3996 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003997 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003998 self.waitUntilSettled()
3999
4000 self.assertEqual(A.data['status'], 'MERGED')
4001 self.assertEqual(A.reported, 2)
4002
James E. Blair96698e22015-04-02 07:48:21 -07004003 def test_crd_gate_reverse(self):
4004 "Test reverse cross-repo dependencies"
4005 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4006 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004007 A.addApproval('code-review', 2)
4008 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07004009
4010 # A Depends-On: B
4011
4012 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4013 A.subject, B.data['id'])
4014
James E. Blair8b5408c2016-08-08 15:37:46 -07004015 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004016 self.waitUntilSettled()
4017
4018 self.assertEqual(A.data['status'], 'NEW')
4019 self.assertEqual(B.data['status'], 'NEW')
4020
James E. Blair08d19992016-08-10 15:25:31 -07004021 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07004022 A.addApproval('approved', 1)
4023 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004024 self.waitUntilSettled()
4025
James E. Blair08d19992016-08-10 15:25:31 -07004026 self.launch_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004027 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07004028 self.launch_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004029 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07004030 self.launch_server.hold_jobs_in_build = False
4031 self.launch_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07004032 self.waitUntilSettled()
4033
4034 self.assertEqual(A.data['status'], 'MERGED')
4035 self.assertEqual(B.data['status'], 'MERGED')
4036 self.assertEqual(A.reported, 2)
4037 self.assertEqual(B.reported, 2)
4038
Paul Belanger5dccbe72016-11-14 11:17:37 -05004039 changes = self.getJobFromHistory(
4040 'project-merge', 'org/project1').changes
4041 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07004042
James E. Blair5ee24252014-12-30 10:12:29 -08004043 def test_crd_cycle(self):
4044 "Test cross-repo dependency cycles"
4045 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4046 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004047 A.addApproval('code-review', 2)
4048 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004049
4050 # A -> B -> A (via commit-depends)
4051
4052 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4053 A.subject, B.data['id'])
4054 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4055 B.subject, A.data['id'])
4056
James E. Blair8b5408c2016-08-08 15:37:46 -07004057 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004058 self.waitUntilSettled()
4059
4060 self.assertEqual(A.reported, 0)
4061 self.assertEqual(B.reported, 0)
4062 self.assertEqual(A.data['status'], 'NEW')
4063 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004064
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004065 def test_crd_gate_unknown(self):
4066 "Test unknown projects in dependent pipeline"
4067 self.init_repo("org/unknown")
4068 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4069 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004070 A.addApproval('code-review', 2)
4071 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004072
4073 # A Depends-On: B
4074 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4075 A.subject, B.data['id'])
4076
James E. Blair8b5408c2016-08-08 15:37:46 -07004077 B.addApproval('approved', 1)
4078 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004079 self.waitUntilSettled()
4080
4081 # Unknown projects cannot share a queue with any other
4082 # since they don't have common jobs with any other (they have no jobs).
4083 # Changes which depend on unknown project changes
4084 # should not be processed in dependent pipeline
4085 self.assertEqual(A.data['status'], 'NEW')
4086 self.assertEqual(B.data['status'], 'NEW')
4087 self.assertEqual(A.reported, 0)
4088 self.assertEqual(B.reported, 0)
4089 self.assertEqual(len(self.history), 0)
4090
4091 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07004092 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004093 B.setMerged()
4094 self.waitUntilSettled()
4095 self.assertEqual(len(self.history), 0)
4096
4097 # Now that B is merged, A should be able to be enqueued and
4098 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07004099 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004100 self.waitUntilSettled()
4101
4102 self.assertEqual(A.data['status'], 'MERGED')
4103 self.assertEqual(A.reported, 2)
4104 self.assertEqual(B.data['status'], 'MERGED')
4105 self.assertEqual(B.reported, 0)
4106
James E. Blairbfb8e042014-12-30 17:01:44 -08004107 def test_crd_check(self):
4108 "Test cross-repo dependencies in independent pipelines"
4109
Paul Belangerb30342b2016-11-14 12:30:43 -05004110 self.launch_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004111 self.gearman_server.hold_jobs_in_queue = True
4112 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4113 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4114
4115 # A Depends-On: B
4116 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4117 A.subject, B.data['id'])
4118
4119 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4120 self.waitUntilSettled()
4121
4122 queue = self.gearman_server.getQueue()
4123 ref = self.getParameter(queue[-1], 'ZUUL_REF')
4124 self.gearman_server.hold_jobs_in_queue = False
4125 self.gearman_server.release()
4126 self.waitUntilSettled()
4127
Paul Belangerb30342b2016-11-14 12:30:43 -05004128 self.launch_server.release('.*-merge')
4129 self.waitUntilSettled()
4130
4131 path = os.path.join(self.builds[0].jobdir.git_root, "org/project1")
James E. Blairbfb8e042014-12-30 17:01:44 -08004132 repo = git.Repo(path)
4133 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4134 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004135 correct_messages = [
4136 'initial commit', 'add content from fixture', 'A-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004137 self.assertEqual(repo_messages, correct_messages)
4138
Paul Belangerb30342b2016-11-14 12:30:43 -05004139 path = os.path.join(self.builds[0].jobdir.git_root, "org/project2")
James E. Blairbfb8e042014-12-30 17:01:44 -08004140 repo = git.Repo(path)
4141 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4142 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004143 correct_messages = [
4144 'initial commit', 'add content from fixture', 'B-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004145 self.assertEqual(repo_messages, correct_messages)
4146
Paul Belangerb30342b2016-11-14 12:30:43 -05004147 self.launch_server.hold_jobs_in_build = False
4148 self.launch_server.release()
4149 self.waitUntilSettled()
4150
James E. Blairbfb8e042014-12-30 17:01:44 -08004151 self.assertEqual(A.data['status'], 'NEW')
4152 self.assertEqual(B.data['status'], 'NEW')
4153 self.assertEqual(A.reported, 1)
4154 self.assertEqual(B.reported, 0)
4155
4156 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004157 tenant = self.sched.abide.tenants.get('tenant-one')
4158 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004159
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004160 def test_crd_check_git_depends(self):
4161 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004162 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004163 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4164 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4165
4166 # Add two git-dependent changes and make sure they both report
4167 # success.
4168 B.setDependsOn(A, 1)
4169 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4170 self.waitUntilSettled()
4171 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4172 self.waitUntilSettled()
4173
James E. Blairb8c16472015-05-05 14:55:26 -07004174 self.orderedRelease()
4175 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004176 self.waitUntilSettled()
4177
4178 self.assertEqual(A.data['status'], 'NEW')
4179 self.assertEqual(B.data['status'], 'NEW')
4180 self.assertEqual(A.reported, 1)
4181 self.assertEqual(B.reported, 1)
4182
4183 self.assertEqual(self.history[0].changes, '1,1')
4184 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004185 tenant = self.sched.abide.tenants.get('tenant-one')
4186 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004187
4188 self.assertIn('Build succeeded', A.messages[0])
4189 self.assertIn('Build succeeded', B.messages[0])
4190
4191 def test_crd_check_duplicate(self):
4192 "Test duplicate check in independent pipelines"
James E. Blair08d19992016-08-10 15:25:31 -07004193 self.launch_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004194 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4195 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004196 tenant = self.sched.abide.tenants.get('tenant-one')
4197 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004198
4199 # Add two git-dependent changes...
4200 B.setDependsOn(A, 1)
4201 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4202 self.waitUntilSettled()
4203 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4204
4205 # ...make sure the live one is not duplicated...
4206 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4207 self.waitUntilSettled()
4208 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4209
4210 # ...but the non-live one is able to be.
4211 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4212 self.waitUntilSettled()
4213 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4214
Clark Boylandd849822015-03-02 12:38:14 -08004215 # Release jobs in order to avoid races with change A jobs
4216 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004217 self.orderedRelease()
James E. Blair08d19992016-08-10 15:25:31 -07004218 self.launch_server.hold_jobs_in_build = False
4219 self.launch_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004220 self.waitUntilSettled()
4221
4222 self.assertEqual(A.data['status'], 'NEW')
4223 self.assertEqual(B.data['status'], 'NEW')
4224 self.assertEqual(A.reported, 1)
4225 self.assertEqual(B.reported, 1)
4226
4227 self.assertEqual(self.history[0].changes, '1,1 2,1')
4228 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004229 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004230
4231 self.assertIn('Build succeeded', A.messages[0])
4232 self.assertIn('Build succeeded', B.messages[0])
4233
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004234 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004235 "Test cross-repo dependencies re-enqueued in independent pipelines"
4236
4237 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004238 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4239 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004240
4241 # A Depends-On: B
4242 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4243 A.subject, B.data['id'])
4244
4245 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4246 self.waitUntilSettled()
4247
4248 self.sched.reconfigure(self.config)
4249
4250 # Make sure the items still share a change queue, and the
4251 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004252 tenant = self.sched.abide.tenants.get('tenant-one')
4253 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4254 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004255 first_item = queue.queue[0]
4256 for item in queue.queue:
4257 self.assertEqual(item.queue, first_item.queue)
4258 self.assertFalse(first_item.live)
4259 self.assertTrue(queue.queue[1].live)
4260
4261 self.gearman_server.hold_jobs_in_queue = False
4262 self.gearman_server.release()
4263 self.waitUntilSettled()
4264
4265 self.assertEqual(A.data['status'], 'NEW')
4266 self.assertEqual(B.data['status'], 'NEW')
4267 self.assertEqual(A.reported, 1)
4268 self.assertEqual(B.reported, 0)
4269
4270 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004271 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004272
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004273 def test_crd_check_reconfiguration(self):
4274 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4275
4276 def test_crd_undefined_project(self):
4277 """Test that undefined projects in dependencies are handled for
4278 independent pipelines"""
4279 # It's a hack for fake gerrit,
4280 # as it implies repo creation upon the creation of any change
4281 self.init_repo("org/unknown")
4282 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4283
James E. Blairec056492016-07-22 09:45:56 -07004284 @skip("Disabled for early v3 development")
James E. Blair17dd6772015-02-09 14:45:18 -08004285 def test_crd_check_ignore_dependencies(self):
4286 "Test cross-repo dependencies can be ignored"
James E. Blairf84026c2015-12-08 16:11:46 -08004287 self.updateConfigLayout(
4288 'tests/fixtures/layout-ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004289 self.sched.reconfigure(self.config)
4290 self.registerJobs()
4291
4292 self.gearman_server.hold_jobs_in_queue = True
4293 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4294 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4295 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4296
4297 # A Depends-On: B
4298 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4299 A.subject, B.data['id'])
4300 # C git-depends on B
4301 C.setDependsOn(B, 1)
4302 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4303 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4304 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4305 self.waitUntilSettled()
4306
4307 # Make sure none of the items share a change queue, and all
4308 # are live.
4309 check_pipeline = self.sched.layout.pipelines['check']
4310 self.assertEqual(len(check_pipeline.queues), 3)
4311 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4312 for item in check_pipeline.getAllItems():
4313 self.assertTrue(item.live)
4314
4315 self.gearman_server.hold_jobs_in_queue = False
4316 self.gearman_server.release()
4317 self.waitUntilSettled()
4318
4319 self.assertEqual(A.data['status'], 'NEW')
4320 self.assertEqual(B.data['status'], 'NEW')
4321 self.assertEqual(C.data['status'], 'NEW')
4322 self.assertEqual(A.reported, 1)
4323 self.assertEqual(B.reported, 1)
4324 self.assertEqual(C.reported, 1)
4325
4326 # Each job should have tested exactly one change
4327 for job in self.history:
4328 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004329
James E. Blairec056492016-07-22 09:45:56 -07004330 @skip("Disabled for early v3 development")
James E. Blair96698e22015-04-02 07:48:21 -07004331 def test_crd_check_transitive(self):
4332 "Test transitive cross-repo dependencies"
4333 # Specifically, if A -> B -> C, and C gets a new patchset and
4334 # A gets a new patchset, ensure the test of A,2 includes B,1
4335 # and C,2 (not C,1 which would indicate stale data in the
4336 # cache for B).
4337 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4338 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4339 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4340
4341 # A Depends-On: B
4342 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4343 A.subject, B.data['id'])
4344
4345 # B Depends-On: C
4346 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4347 B.subject, C.data['id'])
4348
4349 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4350 self.waitUntilSettled()
4351 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4352
4353 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4354 self.waitUntilSettled()
4355 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4356
4357 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4358 self.waitUntilSettled()
4359 self.assertEqual(self.history[-1].changes, '3,1')
4360
4361 C.addPatchset()
4362 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4363 self.waitUntilSettled()
4364 self.assertEqual(self.history[-1].changes, '3,2')
4365
4366 A.addPatchset()
4367 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4368 self.waitUntilSettled()
4369 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004370
James E. Blaira8b90b32016-08-24 15:18:50 -07004371 def test_crd_check_unknown(self):
4372 "Test unknown projects in independent pipeline"
4373 self.init_repo("org/unknown")
4374 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4375 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4376 # A Depends-On: B
4377 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4378 A.subject, B.data['id'])
4379
4380 # Make sure zuul has seen an event on B.
4381 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4382 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4383 self.waitUntilSettled()
4384
4385 self.assertEqual(A.data['status'], 'NEW')
4386 self.assertEqual(A.reported, 1)
4387 self.assertEqual(B.data['status'], 'NEW')
4388 self.assertEqual(B.reported, 0)
4389
James E. Blair92464a22016-04-05 10:21:26 -07004390 def test_crd_cycle_join(self):
4391 "Test an updated change creates a cycle"
4392 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4393
4394 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4395 self.waitUntilSettled()
4396
4397 # Create B->A
4398 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4399 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4400 B.subject, A.data['id'])
4401 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4402 self.waitUntilSettled()
4403
4404 # Update A to add A->B (a cycle).
4405 A.addPatchset()
4406 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4407 A.subject, B.data['id'])
4408 # Normally we would submit the patchset-created event for
4409 # processing here, however, we have no way of noting whether
4410 # the dependency cycle detection correctly raised an
4411 # exception, so instead, we reach into the source driver and
4412 # call the method that would ultimately be called by the event
4413 # processing.
4414
Paul Belanger0e155e22016-11-14 14:12:23 -05004415 tenant = self.sched.abide.tenants.get('tenant-one')
4416 source = tenant.layout.pipelines['gate'].source
4417
4418 # TODO(pabelanger): As we add more source / trigger APIs we should make
4419 # it easier for users to create events for testing.
4420 event = zuul.model.TriggerEvent()
4421 event.trigger_name = 'gerrit'
4422 event.change_number = '1'
4423 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004424 with testtools.ExpectedException(
4425 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004426 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004427 self.log.debug("Got expected dependency cycle exception")
4428
4429 # Now if we update B to remove the depends-on, everything
4430 # should be okay. B; A->B
4431
4432 B.addPatchset()
4433 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004434
4435 source.getChange(event, True)
4436 event.change_number = '2'
4437 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004438
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004439 def test_disable_at(self):
4440 "Test a pipeline will only report to the disabled trigger when failing"
4441
Paul Belanger7dc76e82016-11-11 16:51:08 -05004442 self.updateConfigLayout('layout-disabled-at')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004443 self.sched.reconfigure(self.config)
4444
Paul Belanger7dc76e82016-11-11 16:51:08 -05004445 tenant = self.sched.abide.tenants.get('openstack')
4446 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004447 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004448 0, tenant.layout.pipelines['check']._consecutive_failures)
4449 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004450
4451 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4452 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4453 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4454 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4455 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4456 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4457 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4458 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4459 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4460 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4461 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4462
James E. Blair08d19992016-08-10 15:25:31 -07004463 self.launch_server.failJob('project-test1', A)
4464 self.launch_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004465 # Let C pass, resetting the counter
James E. Blair08d19992016-08-10 15:25:31 -07004466 self.launch_server.failJob('project-test1', D)
4467 self.launch_server.failJob('project-test1', E)
4468 self.launch_server.failJob('project-test1', F)
4469 self.launch_server.failJob('project-test1', G)
4470 self.launch_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004471 # I also passes but should only report to the disabled reporters
James E. Blair08d19992016-08-10 15:25:31 -07004472 self.launch_server.failJob('project-test1', J)
4473 self.launch_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004474
4475 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4476 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4477 self.waitUntilSettled()
4478
4479 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004480 2, 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(C.getPatchsetCreatedEvent(1))
4484 self.waitUntilSettled()
4485
4486 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004487 0, tenant.layout.pipelines['check']._consecutive_failures)
4488 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004489
4490 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4491 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4492 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4493 self.waitUntilSettled()
4494
4495 # We should be disabled now
4496 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004497 3, tenant.layout.pipelines['check']._consecutive_failures)
4498 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004499
4500 # We need to wait between each of these patches to make sure the
4501 # smtp messages come back in an expected order
4502 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4503 self.waitUntilSettled()
4504 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4505 self.waitUntilSettled()
4506 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4507 self.waitUntilSettled()
4508
4509 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4510 # leaving a message on each change
4511 self.assertEqual(1, len(A.messages))
4512 self.assertIn('Build failed.', A.messages[0])
4513 self.assertEqual(1, len(B.messages))
4514 self.assertIn('Build failed.', B.messages[0])
4515 self.assertEqual(1, len(C.messages))
4516 self.assertIn('Build succeeded.', C.messages[0])
4517 self.assertEqual(1, len(D.messages))
4518 self.assertIn('Build failed.', D.messages[0])
4519 self.assertEqual(1, len(E.messages))
4520 self.assertIn('Build failed.', E.messages[0])
4521 self.assertEqual(1, len(F.messages))
4522 self.assertIn('Build failed.', F.messages[0])
4523
4524 # The last 3 (GHI) would have only reported via smtp.
4525 self.assertEqual(3, len(self.smtp_messages))
4526 self.assertEqual(0, len(G.messages))
4527 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004528 self.assertIn(
4529 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004530 self.assertEqual(0, len(H.messages))
4531 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004532 self.assertIn(
4533 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004534 self.assertEqual(0, len(I.messages))
4535 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004536 self.assertIn(
4537 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004538
4539 # Now reload the configuration (simulate a HUP) to check the pipeline
4540 # comes out of disabled
4541 self.sched.reconfigure(self.config)
4542
Paul Belanger7dc76e82016-11-11 16:51:08 -05004543 tenant = self.sched.abide.tenants.get('openstack')
4544
4545 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004546 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004547 0, tenant.layout.pipelines['check']._consecutive_failures)
4548 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004549
4550 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4551 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4552 self.waitUntilSettled()
4553
4554 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004555 2, tenant.layout.pipelines['check']._consecutive_failures)
4556 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004557
4558 # J and K went back to gerrit
4559 self.assertEqual(1, len(J.messages))
4560 self.assertIn('Build failed.', J.messages[0])
4561 self.assertEqual(1, len(K.messages))
4562 self.assertIn('Build failed.', K.messages[0])
4563 # No more messages reported via smtp
4564 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004565
James E. Blairec056492016-07-22 09:45:56 -07004566 @skip("Disabled for early v3 development")
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004567 def test_success_pattern(self):
4568 "Ensure bad build params are ignored"
4569
4570 # Use SMTP reporter to grab the result message easier
4571 self.init_repo("org/docs")
4572 self.config.set('zuul', 'layout_config',
4573 'tests/fixtures/layout-success-pattern.yaml')
4574 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07004575 self.launch_server.hold_jobs_in_build = True
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004576 self.registerJobs()
4577
4578 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4579 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4580 self.waitUntilSettled()
4581
4582 # Grab build id
4583 self.assertEqual(len(self.builds), 1)
4584 uuid = self.builds[0].unique[:7]
4585
James E. Blair08d19992016-08-10 15:25:31 -07004586 self.launch_server.hold_jobs_in_build = False
4587 self.launch_server.release()
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004588 self.waitUntilSettled()
4589
4590 self.assertEqual(len(self.smtp_messages), 1)
4591 body = self.smtp_messages[0]['body'].splitlines()
4592 self.assertEqual('Build succeeded.', body[0])
4593
4594 self.assertIn(
4595 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4596 'docs-draft-test/%s/publish-docs/' % uuid,
4597 body[2])
4598 self.assertIn(
4599 '- docs-draft-test2 https://server/job/docs-draft-test2/1/',
4600 body[3])
Arieb6f068c2016-10-09 13:11:06 +03004601
4602
4603class TestDuplicatePipeline(ZuulTestCase):
4604 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4605
4606 def test_duplicate_pipelines(self):
4607 "Test that a change matching multiple pipelines works"
4608
4609 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4610 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4611 self.waitUntilSettled()
4612
4613 self.assertHistory([
4614 dict(name='project-test1', result='SUCCESS', changes='1,1',
4615 pipeline='dup1'),
4616 dict(name='project-test1', result='SUCCESS', changes='1,1',
4617 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004618 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004619
4620 self.assertEqual(len(A.messages), 2)
4621
Paul Belangera46a3742016-11-09 14:23:03 -05004622 if 'dup1' in A.messages[0]:
4623 self.assertIn('dup1', A.messages[0])
4624 self.assertNotIn('dup2', A.messages[0])
4625 self.assertIn('project-test1', A.messages[0])
4626 self.assertIn('dup2', A.messages[1])
4627 self.assertNotIn('dup1', A.messages[1])
4628 self.assertIn('project-test1', A.messages[1])
4629 else:
4630 self.assertIn('dup1', A.messages[1])
4631 self.assertNotIn('dup2', A.messages[1])
4632 self.assertIn('project-test1', A.messages[1])
4633 self.assertIn('dup2', A.messages[0])
4634 self.assertNotIn('dup1', A.messages[0])
4635 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004636
4637
4638class TestSchedulerOneJobProject(ZuulTestCase):
4639 tenant_config_file = 'config/one-job-project/main.yaml'
4640
4641 def test_one_job_project(self):
4642 "Test that queueing works with one job"
4643 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4644 'master', 'A')
4645 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4646 'master', 'B')
4647 A.addApproval('code-review', 2)
4648 B.addApproval('code-review', 2)
4649 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4650 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4651 self.waitUntilSettled()
4652
4653 self.assertEqual(A.data['status'], 'MERGED')
4654 self.assertEqual(A.reported, 2)
4655 self.assertEqual(B.data['status'], 'MERGED')
4656 self.assertEqual(B.reported, 2)
Paul Belanger86085b32016-11-03 12:48:57 -04004657
4658
4659class TestSchedulerTemplatedProject(ZuulTestCase):
4660 tenant_config_file = 'config/templated-project/main.yaml'
4661
4662 def test_job_from_templates_launched(self):
4663 "Test whether a job generated via a template can be launched"
4664
4665 A = self.fake_gerrit.addFakeChange(
4666 'org/templated-project', 'master', 'A')
4667 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4668 self.waitUntilSettled()
4669
4670 self.assertEqual(self.getJobFromHistory('project-test1').result,
4671 'SUCCESS')
4672 self.assertEqual(self.getJobFromHistory('project-test2').result,
4673 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004674
4675 def test_layered_templates(self):
4676 "Test whether a job generated via a template can be launched"
4677
4678 A = self.fake_gerrit.addFakeChange(
4679 'org/layered-project', 'master', 'A')
4680 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4681 self.waitUntilSettled()
4682
4683 self.assertEqual(self.getJobFromHistory('project-test1').result,
4684 'SUCCESS')
4685 self.assertEqual(self.getJobFromHistory('project-test2').result,
4686 'SUCCESS')
4687 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4688 ).result, 'SUCCESS')
4689 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4690 ).result, 'SUCCESS')
4691 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4692 ).result, 'SUCCESS')
4693 self.assertEqual(self.getJobFromHistory('project-test6').result,
4694 'SUCCESS')