blob: acbc144e02bfd582135cf2d3b58580e69f14683f [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. Blaird466dc42012-07-31 10:42:56 -0700508 def test_failed_change_at_head_with_queue(self):
509 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700510
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700511 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700512 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
513 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
514 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700515 A.addApproval('code-review', 2)
516 B.addApproval('code-review', 2)
517 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700518
James E. Blair08d19992016-08-10 15:25:31 -0700519 self.launch_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700520
James E. Blair8b5408c2016-08-08 15:37:46 -0700521 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
522 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
523 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700524
525 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700526 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400527 self.assertEqual(len(self.builds), 0)
528 self.assertEqual(len(queue), 1)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800529 self.assertEqual(queue[0].name, 'launcher:launch')
530 job_args = json.loads(queue[0].arguments)
531 self.assertEqual(job_args['job'], 'project-merge')
532 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700533
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700534 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700535 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700536 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700537 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700538 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700539 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700540 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700541
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400542 self.assertEqual(len(self.builds), 0)
543 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800544
545 self.assertEqual(
546 json.loads(queue[0].arguments)['job'], 'project-test1')
547 self.assertEqual(
548 json.loads(queue[1].arguments)['job'], 'project-test2')
549 self.assertEqual(
550 json.loads(queue[2].arguments)['job'], 'project-test1')
551 self.assertEqual(
552 json.loads(queue[3].arguments)['job'], 'project-test2')
553 self.assertEqual(
554 json.loads(queue[4].arguments)['job'], 'project-test1')
555 self.assertEqual(
556 json.loads(queue[5].arguments)['job'], 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700557
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700558 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700559 self.waitUntilSettled()
560
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400561 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700562 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400563 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
564 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700565
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700566 self.gearman_server.hold_jobs_in_queue = False
567 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700568 self.waitUntilSettled()
569
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400570 self.assertEqual(len(self.builds), 0)
571 self.assertEqual(len(self.history), 11)
572 self.assertEqual(A.data['status'], 'NEW')
573 self.assertEqual(B.data['status'], 'MERGED')
574 self.assertEqual(C.data['status'], 'MERGED')
575 self.assertEqual(A.reported, 2)
576 self.assertEqual(B.reported, 2)
577 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700578
James E. Blairec056492016-07-22 09:45:56 -0700579 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700580 def _test_time_database(self, iteration):
James E. Blair08d19992016-08-10 15:25:31 -0700581 self.launch_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700582 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -0700583 A.addApproval('code-review', 2)
584 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700585 self.waitUntilSettled()
586 time.sleep(2)
587
588 data = json.loads(self.sched.formatStatusJSON())
589 found_job = None
590 for pipeline in data['pipelines']:
591 if pipeline['name'] != 'gate':
592 continue
593 for queue in pipeline['change_queues']:
594 for head in queue['heads']:
595 for item in head:
596 for job in item['jobs']:
597 if job['name'] == 'project-merge':
598 found_job = job
599 break
600
601 self.assertIsNotNone(found_job)
602 if iteration == 1:
603 self.assertIsNotNone(found_job['estimated_time'])
604 self.assertIsNone(found_job['remaining_time'])
605 else:
606 self.assertIsNotNone(found_job['estimated_time'])
607 self.assertTrue(found_job['estimated_time'] >= 2)
608 self.assertIsNotNone(found_job['remaining_time'])
609
James E. Blair08d19992016-08-10 15:25:31 -0700610 self.launch_server.hold_jobs_in_build = False
611 self.launch_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700612 self.waitUntilSettled()
613
James E. Blairec056492016-07-22 09:45:56 -0700614 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700615 def test_time_database(self):
616 "Test the time database"
617
618 self._test_time_database(1)
619 self._test_time_database(2)
620
James E. Blairfef71632013-09-23 11:15:47 -0700621 def test_two_failed_changes_at_head(self):
622 "Test that changes are reparented correctly if 2 fail at head"
623
James E. Blair08d19992016-08-10 15:25:31 -0700624 self.launch_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700625 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
626 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
627 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700628 A.addApproval('code-review', 2)
629 B.addApproval('code-review', 2)
630 C.addApproval('code-review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700631
James E. Blair08d19992016-08-10 15:25:31 -0700632 self.launch_server.failJob('project-test1', A)
633 self.launch_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700634
James E. Blair8b5408c2016-08-08 15:37:46 -0700635 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
636 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
637 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700638 self.waitUntilSettled()
639
James E. Blair08d19992016-08-10 15:25:31 -0700640 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700641 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700642 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700643 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700644 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700645 self.waitUntilSettled()
646
647 self.assertEqual(len(self.builds), 6)
648 self.assertEqual(self.builds[0].name, 'project-test1')
649 self.assertEqual(self.builds[1].name, 'project-test2')
650 self.assertEqual(self.builds[2].name, 'project-test1')
651 self.assertEqual(self.builds[3].name, 'project-test2')
652 self.assertEqual(self.builds[4].name, 'project-test1')
653 self.assertEqual(self.builds[5].name, 'project-test2')
654
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400655 self.assertTrue(self.builds[0].hasChanges(A))
656 self.assertTrue(self.builds[2].hasChanges(A))
657 self.assertTrue(self.builds[2].hasChanges(B))
658 self.assertTrue(self.builds[4].hasChanges(A))
659 self.assertTrue(self.builds[4].hasChanges(B))
660 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700661
662 # Fail change B first
663 self.release(self.builds[2])
664 self.waitUntilSettled()
665
666 # restart of C after B failure
James E. Blair08d19992016-08-10 15:25:31 -0700667 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700668 self.waitUntilSettled()
669
670 self.assertEqual(len(self.builds), 5)
671 self.assertEqual(self.builds[0].name, 'project-test1')
672 self.assertEqual(self.builds[1].name, 'project-test2')
673 self.assertEqual(self.builds[2].name, 'project-test2')
674 self.assertEqual(self.builds[3].name, 'project-test1')
675 self.assertEqual(self.builds[4].name, 'project-test2')
676
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400677 self.assertTrue(self.builds[1].hasChanges(A))
678 self.assertTrue(self.builds[2].hasChanges(A))
679 self.assertTrue(self.builds[2].hasChanges(B))
680 self.assertTrue(self.builds[4].hasChanges(A))
681 self.assertFalse(self.builds[4].hasChanges(B))
682 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700683
684 # Finish running all passing jobs for change A
685 self.release(self.builds[1])
686 self.waitUntilSettled()
687 # Fail and report change A
688 self.release(self.builds[0])
689 self.waitUntilSettled()
690
691 # restart of B,C after A failure
James E. Blair08d19992016-08-10 15:25:31 -0700692 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700693 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700694 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700695 self.waitUntilSettled()
696
697 self.assertEqual(len(self.builds), 4)
698 self.assertEqual(self.builds[0].name, 'project-test1') # B
699 self.assertEqual(self.builds[1].name, 'project-test2') # B
700 self.assertEqual(self.builds[2].name, 'project-test1') # C
701 self.assertEqual(self.builds[3].name, 'project-test2') # C
702
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400703 self.assertFalse(self.builds[1].hasChanges(A))
704 self.assertTrue(self.builds[1].hasChanges(B))
705 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700706
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400707 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700708 # After A failed and B and C restarted, B should be back in
709 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400710 self.assertTrue(self.builds[2].hasChanges(B))
711 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700712
James E. Blair08d19992016-08-10 15:25:31 -0700713 self.launch_server.hold_jobs_in_build = False
714 self.launch_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700715 self.waitUntilSettled()
716
717 self.assertEqual(len(self.builds), 0)
718 self.assertEqual(len(self.history), 21)
719 self.assertEqual(A.data['status'], 'NEW')
720 self.assertEqual(B.data['status'], 'NEW')
721 self.assertEqual(C.data['status'], 'MERGED')
722 self.assertEqual(A.reported, 2)
723 self.assertEqual(B.reported, 2)
724 self.assertEqual(C.reported, 2)
725
James E. Blairec056492016-07-22 09:45:56 -0700726 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700727 def test_parse_skip_if(self):
728 job_yaml = """
729jobs:
730 - name: job_name
731 skip-if:
732 - project: ^project_name$
733 branch: ^stable/icehouse$
734 all-files-match-any:
735 - ^filename$
736 - project: ^project2_name$
737 all-files-match-any:
738 - ^filename2$
739 """.strip()
740 data = yaml.load(job_yaml)
741 config_job = data.get('jobs')[0]
742 cm = zuul.change_matcher
743 expected = cm.MatchAny([
744 cm.MatchAll([
745 cm.ProjectMatcher('^project_name$'),
746 cm.BranchMatcher('^stable/icehouse$'),
747 cm.MatchAllFiles([cm.FileMatcher('^filename$')]),
748 ]),
749 cm.MatchAll([
750 cm.ProjectMatcher('^project2_name$'),
751 cm.MatchAllFiles([cm.FileMatcher('^filename2$')]),
752 ]),
753 ])
754 matcher = self.sched._parseSkipIf(config_job)
755 self.assertEqual(expected, matcher)
756
James E. Blair8c803f82012-07-31 16:25:42 -0700757 def test_patch_order(self):
758 "Test that dependent patches are tested in the right order"
759 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
760 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
761 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700762 A.addApproval('code-review', 2)
763 B.addApproval('code-review', 2)
764 C.addApproval('code-review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700765
766 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
767 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
768 M2.setMerged()
769 M1.setMerged()
770
771 # C -> B -> A -> M1 -> M2
772 # M2 is here to make sure it is never queried. If it is, it
773 # means zuul is walking down the entire history of merged
774 # changes.
775
776 C.setDependsOn(B, 1)
777 B.setDependsOn(A, 1)
778 A.setDependsOn(M1, 1)
779 M1.setDependsOn(M2, 1)
780
James E. Blair8b5408c2016-08-08 15:37:46 -0700781 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700782
783 self.waitUntilSettled()
784
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400785 self.assertEqual(A.data['status'], 'NEW')
786 self.assertEqual(B.data['status'], 'NEW')
787 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700788
James E. Blair8b5408c2016-08-08 15:37:46 -0700789 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
790 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700791
792 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400793 self.assertEqual(M2.queried, 0)
794 self.assertEqual(A.data['status'], 'MERGED')
795 self.assertEqual(B.data['status'], 'MERGED')
796 self.assertEqual(C.data['status'], 'MERGED')
797 self.assertEqual(A.reported, 2)
798 self.assertEqual(B.reported, 2)
799 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700800
James E. Blair063672f2015-01-29 13:09:12 -0800801 def test_needed_changes_enqueue(self):
802 "Test that a needed change is enqueued ahead"
803 # A Given a git tree like this, if we enqueue
804 # / \ change C, we should walk up and down the tree
805 # B G and enqueue changes in the order ABCDEFG.
806 # /|\ This is also the order that you would get if
807 # *C E F you enqueued changes in the order ABCDEFG, so
808 # / the ordering is stable across re-enqueue events.
809 # D
810
811 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
812 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
813 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
814 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
815 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
816 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
817 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
818 B.setDependsOn(A, 1)
819 C.setDependsOn(B, 1)
820 D.setDependsOn(C, 1)
821 E.setDependsOn(B, 1)
822 F.setDependsOn(B, 1)
823 G.setDependsOn(A, 1)
824
James E. Blair8b5408c2016-08-08 15:37:46 -0700825 A.addApproval('code-review', 2)
826 B.addApproval('code-review', 2)
827 C.addApproval('code-review', 2)
828 D.addApproval('code-review', 2)
829 E.addApproval('code-review', 2)
830 F.addApproval('code-review', 2)
831 G.addApproval('code-review', 2)
832 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800833
834 self.waitUntilSettled()
835
836 self.assertEqual(A.data['status'], 'NEW')
837 self.assertEqual(B.data['status'], 'NEW')
838 self.assertEqual(C.data['status'], 'NEW')
839 self.assertEqual(D.data['status'], 'NEW')
840 self.assertEqual(E.data['status'], 'NEW')
841 self.assertEqual(F.data['status'], 'NEW')
842 self.assertEqual(G.data['status'], 'NEW')
843
844 # We're about to add approvals to changes without adding the
845 # triggering events to Zuul, so that we can be sure that it is
846 # enqueing the changes based on dependencies, not because of
847 # triggering events. Since it will have the changes cached
848 # already (without approvals), we need to clear the cache
849 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400850 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100851 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800852
James E. Blair08d19992016-08-10 15:25:31 -0700853 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -0700854 A.addApproval('approved', 1)
855 B.addApproval('approved', 1)
856 D.addApproval('approved', 1)
857 E.addApproval('approved', 1)
858 F.addApproval('approved', 1)
859 G.addApproval('approved', 1)
860 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800861
862 for x in range(8):
James E. Blair08d19992016-08-10 15:25:31 -0700863 self.launch_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800864 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700865 self.launch_server.hold_jobs_in_build = False
866 self.launch_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800867 self.waitUntilSettled()
868
869 self.assertEqual(A.data['status'], 'MERGED')
870 self.assertEqual(B.data['status'], 'MERGED')
871 self.assertEqual(C.data['status'], 'MERGED')
872 self.assertEqual(D.data['status'], 'MERGED')
873 self.assertEqual(E.data['status'], 'MERGED')
874 self.assertEqual(F.data['status'], 'MERGED')
875 self.assertEqual(G.data['status'], 'MERGED')
876 self.assertEqual(A.reported, 2)
877 self.assertEqual(B.reported, 2)
878 self.assertEqual(C.reported, 2)
879 self.assertEqual(D.reported, 2)
880 self.assertEqual(E.reported, 2)
881 self.assertEqual(F.reported, 2)
882 self.assertEqual(G.reported, 2)
883 self.assertEqual(self.history[6].changes,
884 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
885
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100886 def test_source_cache(self):
887 "Test that the source cache operates correctly"
James E. Blair08d19992016-08-10 15:25:31 -0700888 self.launch_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700889
890 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
891 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
892 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
James E. Blair8b5408c2016-08-08 15:37:46 -0700893 A.addApproval('code-review', 2)
894 B.addApproval('code-review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700895
896 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
897 M1.setMerged()
898
899 B.setDependsOn(A, 1)
900 A.setDependsOn(M1, 1)
901
James E. Blair8b5408c2016-08-08 15:37:46 -0700902 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700903 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
904
905 self.waitUntilSettled()
906
907 for build in self.builds:
908 if build.parameters['ZUUL_PIPELINE'] == 'check':
909 build.release()
910 self.waitUntilSettled()
911 for build in self.builds:
912 if build.parameters['ZUUL_PIPELINE'] == 'check':
913 build.release()
914 self.waitUntilSettled()
915
James E. Blair8b5408c2016-08-08 15:37:46 -0700916 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700917 self.waitUntilSettled()
918
Joshua Hesketh352264b2015-08-11 23:42:08 +1000919 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700920 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000921 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700922
James E. Blair08d19992016-08-10 15:25:31 -0700923 self.launch_server.hold_jobs_in_build = False
924 self.launch_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700925 self.waitUntilSettled()
926
927 self.assertEqual(A.data['status'], 'MERGED')
928 self.assertEqual(B.data['status'], 'MERGED')
929 self.assertEqual(A.queried, 2) # Initial and isMerged
930 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
931
James E. Blair8c803f82012-07-31 16:25:42 -0700932 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700933 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700934 # TODO: move to test_gerrit (this is a unit test!)
935 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400936 tenant = self.sched.abide.tenants.get('tenant-one')
937 source = tenant.layout.pipelines['gate'].source
938
939 # TODO(pabelanger): As we add more source / trigger APIs we should make
940 # it easier for users to create events for testing.
941 event = zuul.model.TriggerEvent()
942 event.trigger_name = 'gerrit'
943 event.change_number = '1'
944 event.patch_number = '2'
945
946 a = source.getChange(event)
947 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700948 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700949
James E. Blair8b5408c2016-08-08 15:37:46 -0700950 A.addApproval('code-review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400951 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700952 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700953
James E. Blair8b5408c2016-08-08 15:37:46 -0700954 A.addApproval('approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400955 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700956 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700957
James E. Blairec056492016-07-22 09:45:56 -0700958 @skip("Disabled for early v3 development")
James E. Blair4886cc12012-07-18 15:39:41 -0700959 def test_build_configuration(self):
960 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700961
962 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -0700963 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
964 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
965 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700966 A.addApproval('code-review', 2)
967 B.addApproval('code-review', 2)
968 C.addApproval('code-review', 2)
969 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
970 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
971 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair4886cc12012-07-18 15:39:41 -0700972 self.waitUntilSettled()
973
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700974 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -0700975 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700976 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -0700977 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700978 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -0700979 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700980 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700981 ref = self.getParameter(queue[-1], 'ZUUL_REF')
982 self.gearman_server.hold_jobs_in_queue = False
983 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700984 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -0700985
Monty Taylorbc758832013-06-17 17:22:42 -0400986 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -0700987 repo = git.Repo(path)
988 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
989 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -0700990 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400991 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -0700992
James E. Blairec056492016-07-22 09:45:56 -0700993 @skip("Disabled for early v3 development")
James E. Blair973721f2012-08-15 10:19:43 -0700994 def test_build_configuration_conflict(self):
995 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700996
997 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -0700998 A = self.fake_gerrit.addFakeChange('org/conflict-project',
999 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001000 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001001 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1002 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001003 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001004 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1005 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001006 A.addApproval('code-review', 2)
1007 B.addApproval('code-review', 2)
1008 C.addApproval('code-review', 2)
1009 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1010 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1011 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -07001012 self.waitUntilSettled()
1013
James E. Blair6736beb2013-07-11 15:18:15 -07001014 self.assertEqual(A.reported, 1)
1015 self.assertEqual(B.reported, 1)
1016 self.assertEqual(C.reported, 1)
1017
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001018 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001019 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001020 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001021 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001022 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001023 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001024
1025 self.assertEqual(len(self.history), 2) # A and C merge jobs
1026
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001027 self.gearman_server.hold_jobs_in_queue = False
1028 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001029 self.waitUntilSettled()
1030
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001031 self.assertEqual(A.data['status'], 'MERGED')
1032 self.assertEqual(B.data['status'], 'NEW')
1033 self.assertEqual(C.data['status'], 'MERGED')
1034 self.assertEqual(A.reported, 2)
1035 self.assertEqual(B.reported, 2)
1036 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001037 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -07001038
James E. Blairec056492016-07-22 09:45:56 -07001039 @skip("Disabled for early v3 development")
James E. Blairdaabed22012-08-15 15:38:57 -07001040 def test_post(self):
1041 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001042
Zhongyue Luo5d556072012-09-21 02:00:47 +09001043 e = {
1044 "type": "ref-updated",
1045 "submitter": {
1046 "name": "User Name",
1047 },
1048 "refUpdate": {
1049 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1050 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1051 "refName": "master",
1052 "project": "org/project",
1053 }
1054 }
James E. Blairdaabed22012-08-15 15:38:57 -07001055 self.fake_gerrit.addEvent(e)
1056 self.waitUntilSettled()
1057
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001058 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001059 self.assertEqual(len(self.history), 1)
1060 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001061
James E. Blairec056492016-07-22 09:45:56 -07001062 @skip("Disabled for early v3 development")
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001063 def test_post_ignore_deletes(self):
1064 "Test that deleting refs does not trigger post jobs"
1065
1066 e = {
1067 "type": "ref-updated",
1068 "submitter": {
1069 "name": "User Name",
1070 },
1071 "refUpdate": {
1072 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1073 "newRev": "0000000000000000000000000000000000000000",
1074 "refName": "master",
1075 "project": "org/project",
1076 }
1077 }
1078 self.fake_gerrit.addEvent(e)
1079 self.waitUntilSettled()
1080
1081 job_names = [x.name for x in self.history]
1082 self.assertEqual(len(self.history), 0)
1083 self.assertNotIn('project-post', job_names)
1084
James E. Blairec056492016-07-22 09:45:56 -07001085 @skip("Disabled for early v3 development")
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001086 def test_post_ignore_deletes_negative(self):
1087 "Test that deleting refs does trigger post jobs"
1088
James E. Blairf84026c2015-12-08 16:11:46 -08001089 self.updateConfigLayout(
1090 'tests/fixtures/layout-dont-ignore-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001091 self.sched.reconfigure(self.config)
1092
1093 e = {
1094 "type": "ref-updated",
1095 "submitter": {
1096 "name": "User Name",
1097 },
1098 "refUpdate": {
1099 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1100 "newRev": "0000000000000000000000000000000000000000",
1101 "refName": "master",
1102 "project": "org/project",
1103 }
1104 }
1105 self.fake_gerrit.addEvent(e)
1106 self.waitUntilSettled()
1107
1108 job_names = [x.name for x in self.history]
1109 self.assertEqual(len(self.history), 1)
1110 self.assertIn('project-post', job_names)
1111
James E. Blairec056492016-07-22 09:45:56 -07001112 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001113 def test_build_configuration_branch(self):
1114 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001115
1116 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001117 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1118 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1119 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001120 A.addApproval('code-review', 2)
1121 B.addApproval('code-review', 2)
1122 C.addApproval('code-review', 2)
1123 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1124 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1125 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairc6294a52012-08-17 10:19:48 -07001126 self.waitUntilSettled()
1127
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001128 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001129 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001130 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001131 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001132 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001133 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001134 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001135 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1136 self.gearman_server.hold_jobs_in_queue = False
1137 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001138 self.waitUntilSettled()
1139
Monty Taylorbc758832013-06-17 17:22:42 -04001140 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001141 repo = git.Repo(path)
1142 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1143 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001144 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001145 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001146
James E. Blairec056492016-07-22 09:45:56 -07001147 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001148 def test_build_configuration_branch_interaction(self):
1149 "Test that switching between branches works"
1150 self.test_build_configuration()
1151 self.test_build_configuration_branch()
1152 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001153 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001154 repo = git.Repo(path)
1155 repo.heads.master.commit = repo.commit('init')
1156 self.test_build_configuration()
1157
James E. Blairec056492016-07-22 09:45:56 -07001158 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001159 def test_build_configuration_multi_branch(self):
1160 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001161
1162 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001163 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1164 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1165 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001166 A.addApproval('code-review', 2)
1167 B.addApproval('code-review', 2)
1168 C.addApproval('code-review', 2)
1169 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1170 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1171 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairc6294a52012-08-17 10:19:48 -07001172 self.waitUntilSettled()
James E. Blairbb1fe502014-03-04 10:15:06 -08001173 queue = self.gearman_server.getQueue()
1174 job_A = None
1175 for job in queue:
1176 if 'project-merge' in job.name:
1177 job_A = job
1178 ref_A = self.getParameter(job_A, 'ZUUL_REF')
1179 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
1180 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
1181 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
James E. Blairc6294a52012-08-17 10:19:48 -07001182
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001183 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001184 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001185 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001186 job_B = None
1187 for job in queue:
1188 if 'project-merge' in job.name:
1189 job_B = job
1190 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001191 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001192 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blairbb1fe502014-03-04 10:15:06 -08001193 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
1194
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001195 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001196 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001197 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001198 for job in queue:
1199 if 'project-merge' in job.name:
1200 job_C = job
1201 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001202 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001203 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blairbb1fe502014-03-04 10:15:06 -08001204 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001205 self.gearman_server.hold_jobs_in_queue = False
1206 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001207 self.waitUntilSettled()
1208
Monty Taylorbc758832013-06-17 17:22:42 -04001209 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001210 repo = git.Repo(path)
1211
1212 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001213 for c in repo.iter_commits(ref_C)]
James E. Blairbb1fe502014-03-04 10:15:06 -08001214 repo_shas = [c.hexsha for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001215 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001216 correct_messages = ['initial commit', 'A-1', 'C-1']
James E. Blairbb1fe502014-03-04 10:15:06 -08001217 # Ensure the right commits are in the history for this ref
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001218 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08001219 # Ensure ZUUL_REF -> ZUUL_COMMIT
1220 self.assertEqual(repo_shas[0], commit_C)
James E. Blairc6294a52012-08-17 10:19:48 -07001221
1222 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001223 for c in repo.iter_commits(ref_B)]
James E. Blairbb1fe502014-03-04 10:15:06 -08001224 repo_shas = [c.hexsha for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001225 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001226 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001227 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08001228 self.assertEqual(repo_shas[0], commit_B)
1229
1230 repo_messages = [c.message.strip()
1231 for c in repo.iter_commits(ref_A)]
1232 repo_shas = [c.hexsha for c in repo.iter_commits(ref_A)]
1233 repo_messages.reverse()
1234 correct_messages = ['initial commit', 'A-1']
1235 self.assertEqual(repo_messages, correct_messages)
1236 self.assertEqual(repo_shas[0], commit_A)
1237
1238 self.assertNotEqual(ref_A, ref_B, ref_C)
1239 self.assertNotEqual(commit_A, commit_B, commit_C)
James E. Blair7f71c802012-08-22 13:04:32 -07001240
James E. Blaircaec0c52012-08-22 14:52:22 -07001241 def test_dependent_changes_dequeue(self):
1242 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001243
James E. Blaircaec0c52012-08-22 14:52:22 -07001244 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1245 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1246 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001247 A.addApproval('code-review', 2)
1248 B.addApproval('code-review', 2)
1249 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001250
1251 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1252 M1.setMerged()
1253
1254 # C -> B -> A -> M1
1255
1256 C.setDependsOn(B, 1)
1257 B.setDependsOn(A, 1)
1258 A.setDependsOn(M1, 1)
1259
James E. Blair08d19992016-08-10 15:25:31 -07001260 self.launch_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001261
James E. Blair8b5408c2016-08-08 15:37:46 -07001262 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1263 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1264 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001265
1266 self.waitUntilSettled()
1267
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001268 self.assertEqual(A.data['status'], 'NEW')
1269 self.assertEqual(A.reported, 2)
1270 self.assertEqual(B.data['status'], 'NEW')
1271 self.assertEqual(B.reported, 2)
1272 self.assertEqual(C.data['status'], 'NEW')
1273 self.assertEqual(C.reported, 2)
1274 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001275
James E. Blair972e3c72013-08-29 12:04:55 -07001276 def test_failing_dependent_changes(self):
1277 "Test that failing dependent patches are taken out of stream"
James E. Blair08d19992016-08-10 15:25:31 -07001278 self.launch_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001279 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1280 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1281 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1282 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1283 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001284 A.addApproval('code-review', 2)
1285 B.addApproval('code-review', 2)
1286 C.addApproval('code-review', 2)
1287 D.addApproval('code-review', 2)
1288 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001289
1290 # E, D -> C -> B, A
1291
1292 D.setDependsOn(C, 1)
1293 C.setDependsOn(B, 1)
1294
James E. Blair08d19992016-08-10 15:25:31 -07001295 self.launch_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001296
James E. Blair8b5408c2016-08-08 15:37:46 -07001297 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1298 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1299 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1300 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1301 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001302
1303 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()
James E. Blair08d19992016-08-10 15:25:31 -07001306 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001307 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001308 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001309 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001310 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001311 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001312 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001313 self.waitUntilSettled()
1314
James E. Blair08d19992016-08-10 15:25:31 -07001315 self.launch_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001316 for build in self.builds:
1317 if build.parameters['ZUUL_CHANGE'] != '1':
1318 build.release()
1319 self.waitUntilSettled()
1320
James E. Blair08d19992016-08-10 15:25:31 -07001321 self.launch_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001322 self.waitUntilSettled()
1323
1324 self.assertEqual(A.data['status'], 'MERGED')
1325 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001326 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001327 self.assertEqual(B.data['status'], 'NEW')
1328 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001329 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001330 self.assertEqual(C.data['status'], 'NEW')
1331 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001332 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001333 self.assertEqual(D.data['status'], 'NEW')
1334 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001335 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001336 self.assertEqual(E.data['status'], 'MERGED')
1337 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001338 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001339 self.assertEqual(len(self.history), 18)
1340
James E. Blairec590122012-08-22 15:19:31 -07001341 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001342 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001343 # If it's dequeued more than once, we should see extra
1344 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001345
James E. Blair08d19992016-08-10 15:25:31 -07001346 self.launch_server.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001347 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1348 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1349 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001350 A.addApproval('code-review', 2)
1351 B.addApproval('code-review', 2)
1352 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001353
Paul Belanger58bf6912016-11-11 19:36:01 -05001354 self.launch_server.failJob('project-test1', A)
1355 self.launch_server.failJob('project-test2', A)
James E. Blair08d19992016-08-10 15:25:31 -07001356 self.launch_server.failJob('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001357
James E. Blair8b5408c2016-08-08 15:37:46 -07001358 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1359 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1360 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001361
1362 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001363
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001364 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001365 self.assertEqual(self.builds[0].name, 'project-merge')
1366 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001367
James E. Blair08d19992016-08-10 15:25:31 -07001368 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001369 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001370 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001371 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001372 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001373 self.waitUntilSettled()
1374
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001375 self.assertEqual(len(self.builds), 9)
Paul Belanger58bf6912016-11-11 19:36:01 -05001376 self.assertEqual(self.builds[0].name, 'project-test1')
1377 self.assertEqual(self.builds[1].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001378 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001379 self.assertEqual(self.builds[3].name, 'project-test1')
1380 self.assertEqual(self.builds[4].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001381 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001382 self.assertEqual(self.builds[6].name, 'project-test1')
1383 self.assertEqual(self.builds[7].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001384 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001385
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001386 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001387 self.waitUntilSettled()
1388
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001389 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1390 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001391
James E. Blair08d19992016-08-10 15:25:31 -07001392 self.launch_server.hold_jobs_in_build = False
1393 self.launch_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001394 self.waitUntilSettled()
1395
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001396 self.assertEqual(len(self.builds), 0)
1397 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001398
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001399 self.assertEqual(A.data['status'], 'NEW')
1400 self.assertEqual(B.data['status'], 'MERGED')
1401 self.assertEqual(C.data['status'], 'MERGED')
1402 self.assertEqual(A.reported, 2)
1403 self.assertEqual(B.reported, 2)
1404 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001405
James E. Blairec056492016-07-22 09:45:56 -07001406 @skip("Disabled for early v3 development")
James E. Blair4ec821f2012-08-23 15:28:28 -07001407 def test_nonvoting_job(self):
1408 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001409
James E. Blair4ec821f2012-08-23 15:28:28 -07001410 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1411 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001412 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07001413 self.launch_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001414 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001415
1416 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001417
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001418 self.assertEqual(A.data['status'], 'MERGED')
1419 self.assertEqual(A.reported, 2)
1420 self.assertEqual(
1421 self.getJobFromHistory('nonvoting-project-merge').result,
1422 'SUCCESS')
1423 self.assertEqual(
1424 self.getJobFromHistory('nonvoting-project-test1').result,
1425 'SUCCESS')
1426 self.assertEqual(
1427 self.getJobFromHistory('nonvoting-project-test2').result,
1428 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001429
James E. Blair5821bd92015-09-16 08:48:15 -07001430 for build in self.builds:
1431 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1432
James E. Blaire0487072012-08-29 17:38:31 -07001433 def test_check_queue_success(self):
1434 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001435
James E. Blaire0487072012-08-29 17:38:31 -07001436 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1437 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1438
1439 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001440
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001441 self.assertEqual(A.data['status'], 'NEW')
1442 self.assertEqual(A.reported, 1)
1443 self.assertEqual(self.getJobFromHistory('project-merge').result,
1444 'SUCCESS')
1445 self.assertEqual(self.getJobFromHistory('project-test1').result,
1446 'SUCCESS')
1447 self.assertEqual(self.getJobFromHistory('project-test2').result,
1448 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001449
1450 def test_check_queue_failure(self):
1451 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001452
James E. Blaire0487072012-08-29 17:38:31 -07001453 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair08d19992016-08-10 15:25:31 -07001454 self.launch_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001455 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1456
1457 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001458
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001459 self.assertEqual(A.data['status'], 'NEW')
1460 self.assertEqual(A.reported, 1)
1461 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001462 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001463 self.assertEqual(self.getJobFromHistory('project-test1').result,
1464 'SUCCESS')
1465 self.assertEqual(self.getJobFromHistory('project-test2').result,
1466 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001467
James E. Blairec056492016-07-22 09:45:56 -07001468 @skip("Disabled for early v3 development")
James E. Blair127bc182012-08-28 15:55:15 -07001469 def test_dependent_behind_dequeue(self):
1470 "test that dependent changes behind dequeued changes work"
1471 # This complicated test is a reproduction of a real life bug
1472 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001473
James E. Blair08d19992016-08-10 15:25:31 -07001474 self.launch_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001475 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1476 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1477 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1478 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1479 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1480 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1481 D.setDependsOn(C, 1)
1482 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001483 A.addApproval('code-review', 2)
1484 B.addApproval('code-review', 2)
1485 C.addApproval('code-review', 2)
1486 D.addApproval('code-review', 2)
1487 E.addApproval('code-review', 2)
1488 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001489
1490 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001491
1492 # Change object re-use in the gerrit trigger is hidden if
1493 # changes are added in quick succession; waiting makes it more
1494 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001495 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001496 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001497 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001498 self.waitUntilSettled()
1499
James E. Blair08d19992016-08-10 15:25:31 -07001500 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001501 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001502 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001503 self.waitUntilSettled()
1504
James E. Blair8b5408c2016-08-08 15:37:46 -07001505 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001506 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001507 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001508 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001509 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001510 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001511 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001512 self.waitUntilSettled()
1513
James E. Blair08d19992016-08-10 15:25:31 -07001514 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001515 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001516 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001517 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001518 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001519 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001520 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001521 self.waitUntilSettled()
1522
1523 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001524
1525 # Grab pointers to the jobs we want to release before
1526 # releasing any, because list indexes may change as
1527 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001528 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001529 a.release()
1530 b.release()
1531 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001532 self.waitUntilSettled()
1533
James E. Blair08d19992016-08-10 15:25:31 -07001534 self.launch_server.hold_jobs_in_build = False
1535 self.launch_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001536 self.waitUntilSettled()
1537
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001538 self.assertEqual(A.data['status'], 'NEW')
1539 self.assertEqual(B.data['status'], 'MERGED')
1540 self.assertEqual(C.data['status'], 'MERGED')
1541 self.assertEqual(D.data['status'], 'MERGED')
1542 self.assertEqual(E.data['status'], 'MERGED')
1543 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001544
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001545 self.assertEqual(A.reported, 2)
1546 self.assertEqual(B.reported, 2)
1547 self.assertEqual(C.reported, 2)
1548 self.assertEqual(D.reported, 2)
1549 self.assertEqual(E.reported, 2)
1550 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001551
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001552 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1553 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001554
1555 def test_merger_repack(self):
1556 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001557
James E. Blair05fed602012-09-07 12:45:24 -07001558 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001559 A.addApproval('code-review', 2)
1560 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001561 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001562 self.assertEqual(self.getJobFromHistory('project-merge').result,
1563 'SUCCESS')
1564 self.assertEqual(self.getJobFromHistory('project-test1').result,
1565 'SUCCESS')
1566 self.assertEqual(self.getJobFromHistory('project-test2').result,
1567 'SUCCESS')
1568 self.assertEqual(A.data['status'], 'MERGED')
1569 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001570 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001571 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001572
Monty Taylorbc758832013-06-17 17:22:42 -04001573 path = os.path.join(self.git_root, "org/project")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001574 print(repack_repo(path))
James E. Blair05fed602012-09-07 12:45:24 -07001575
1576 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001577 A.addApproval('code-review', 2)
1578 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001579 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001580 self.assertEqual(self.getJobFromHistory('project-merge').result,
1581 'SUCCESS')
1582 self.assertEqual(self.getJobFromHistory('project-test1').result,
1583 'SUCCESS')
1584 self.assertEqual(self.getJobFromHistory('project-test2').result,
1585 'SUCCESS')
1586 self.assertEqual(A.data['status'], 'MERGED')
1587 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001588
James E. Blair4886f282012-11-15 09:27:33 -08001589 def test_merger_repack_large_change(self):
1590 "Test that the merger works with large changes after a repack"
1591 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001592 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001593 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Hesketh352264b2015-08-11 23:42:08 +10001594 url = self.fake_gerrit.getGitUrl(
Paul Belanger503bded2016-11-11 19:43:38 -05001595 tenant.layout.project_configs.get('org/project1'))
James E. Blair4076e2b2014-01-28 12:42:20 -08001596 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08001597 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1598 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001599 path = os.path.join(self.upstream_root, "org/project1")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001600 print(repack_repo(path))
Monty Taylorbc758832013-06-17 17:22:42 -04001601 path = os.path.join(self.git_root, "org/project1")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001602 print(repack_repo(path))
James E. Blair4886f282012-11-15 09:27:33 -08001603
James E. Blair8b5408c2016-08-08 15:37:46 -07001604 A.addApproval('code-review', 2)
1605 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001606 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001607 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001608 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001609 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001610 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001611 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001612 'SUCCESS')
1613 self.assertEqual(A.data['status'], 'MERGED')
1614 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001615
James E. Blairec056492016-07-22 09:45:56 -07001616 @skip("Disabled for early v3 development")
James E. Blair7ee88a22012-09-12 18:59:31 +02001617 def test_nonexistent_job(self):
1618 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001619 # Set to the state immediately after a restart
1620 self.resetGearmanServer()
1621 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02001622
1623 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001624 A.addApproval('code-review', 2)
1625 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair7ee88a22012-09-12 18:59:31 +02001626 # There may be a thread about to report a lost change
1627 while A.reported < 2:
1628 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001629 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001630 self.assertFalse(job_names)
1631 self.assertEqual(A.data['status'], 'NEW')
1632 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001633 self.assertEmptyQueues()
1634
1635 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001636 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02001637 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001638 A.addApproval('code-review', 2)
1639 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair7ee88a22012-09-12 18:59:31 +02001640 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001641 self.assertEqual(self.getJobFromHistory('project-merge').result,
1642 'SUCCESS')
1643 self.assertEqual(self.getJobFromHistory('project-test1').result,
1644 'SUCCESS')
1645 self.assertEqual(self.getJobFromHistory('project-test2').result,
1646 'SUCCESS')
1647 self.assertEqual(A.data['status'], 'MERGED')
1648 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08001649
James E. Blair2fa50962013-01-30 21:50:41 -08001650 def test_new_patchset_dequeues_old(self):
1651 "Test that a new patchset causes the old to be dequeued"
1652 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair08d19992016-08-10 15:25:31 -07001653 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001654 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1655 M.setMerged()
1656
1657 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1658 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1659 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1660 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001661 A.addApproval('code-review', 2)
1662 B.addApproval('code-review', 2)
1663 C.addApproval('code-review', 2)
1664 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001665
1666 C.setDependsOn(B, 1)
1667 B.setDependsOn(A, 1)
1668 A.setDependsOn(M, 1)
1669
James E. Blair8b5408c2016-08-08 15:37:46 -07001670 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1671 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1672 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1673 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001674 self.waitUntilSettled()
1675
1676 B.addPatchset()
1677 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1678 self.waitUntilSettled()
1679
James E. Blair08d19992016-08-10 15:25:31 -07001680 self.launch_server.hold_jobs_in_build = False
1681 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001682 self.waitUntilSettled()
1683
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001684 self.assertEqual(A.data['status'], 'MERGED')
1685 self.assertEqual(A.reported, 2)
1686 self.assertEqual(B.data['status'], 'NEW')
1687 self.assertEqual(B.reported, 2)
1688 self.assertEqual(C.data['status'], 'NEW')
1689 self.assertEqual(C.reported, 2)
1690 self.assertEqual(D.data['status'], 'MERGED')
1691 self.assertEqual(D.reported, 2)
1692 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001693
James E. Blairba437362015-02-07 11:41:52 -08001694 def test_new_patchset_check(self):
1695 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001696
James E. Blair08d19992016-08-10 15:25:31 -07001697 self.launch_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001698
1699 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001700 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001701 tenant = self.sched.abide.tenants.get('tenant-one')
1702 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001703
1704 # Add two git-dependent changes
1705 B.setDependsOn(A, 1)
1706 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1707 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001708 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1709 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001710
1711 # A live item, and a non-live/live pair
1712 items = check_pipeline.getAllItems()
1713 self.assertEqual(len(items), 3)
1714
1715 self.assertEqual(items[0].change.number, '1')
1716 self.assertEqual(items[0].change.patchset, '1')
1717 self.assertFalse(items[0].live)
1718
1719 self.assertEqual(items[1].change.number, '2')
1720 self.assertEqual(items[1].change.patchset, '1')
1721 self.assertTrue(items[1].live)
1722
1723 self.assertEqual(items[2].change.number, '1')
1724 self.assertEqual(items[2].change.patchset, '1')
1725 self.assertTrue(items[2].live)
1726
1727 # Add a new patchset to A
1728 A.addPatchset()
1729 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1730 self.waitUntilSettled()
1731
1732 # The live copy of A,1 should be gone, but the non-live and B
1733 # should continue, and we should have a new A,2
1734 items = check_pipeline.getAllItems()
1735 self.assertEqual(len(items), 3)
1736
1737 self.assertEqual(items[0].change.number, '1')
1738 self.assertEqual(items[0].change.patchset, '1')
1739 self.assertFalse(items[0].live)
1740
1741 self.assertEqual(items[1].change.number, '2')
1742 self.assertEqual(items[1].change.patchset, '1')
1743 self.assertTrue(items[1].live)
1744
1745 self.assertEqual(items[2].change.number, '1')
1746 self.assertEqual(items[2].change.patchset, '2')
1747 self.assertTrue(items[2].live)
1748
1749 # Add a new patchset to B
1750 B.addPatchset()
1751 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1752 self.waitUntilSettled()
1753
1754 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1755 # but we should have a new B,2 (still based on A,1)
1756 items = check_pipeline.getAllItems()
1757 self.assertEqual(len(items), 3)
1758
1759 self.assertEqual(items[0].change.number, '1')
1760 self.assertEqual(items[0].change.patchset, '2')
1761 self.assertTrue(items[0].live)
1762
1763 self.assertEqual(items[1].change.number, '1')
1764 self.assertEqual(items[1].change.patchset, '1')
1765 self.assertFalse(items[1].live)
1766
1767 self.assertEqual(items[2].change.number, '2')
1768 self.assertEqual(items[2].change.patchset, '2')
1769 self.assertTrue(items[2].live)
1770
1771 self.builds[0].release()
1772 self.waitUntilSettled()
1773 self.builds[0].release()
1774 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001775 self.launch_server.hold_jobs_in_build = False
1776 self.launch_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001777 self.waitUntilSettled()
1778
1779 self.assertEqual(A.reported, 1)
1780 self.assertEqual(B.reported, 1)
1781 self.assertEqual(self.history[0].result, 'ABORTED')
1782 self.assertEqual(self.history[0].changes, '1,1')
1783 self.assertEqual(self.history[1].result, 'ABORTED')
1784 self.assertEqual(self.history[1].changes, '1,1 2,1')
1785 self.assertEqual(self.history[2].result, 'SUCCESS')
1786 self.assertEqual(self.history[2].changes, '1,2')
1787 self.assertEqual(self.history[3].result, 'SUCCESS')
1788 self.assertEqual(self.history[3].changes, '1,1 2,2')
1789
1790 def test_abandoned_gate(self):
1791 "Test that an abandoned change is dequeued from gate"
1792
James E. Blair08d19992016-08-10 15:25:31 -07001793 self.launch_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001794
1795 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001796 A.addApproval('code-review', 2)
1797 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001798 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001799 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1800 self.assertEqual(self.builds[0].name, 'project-merge')
1801
1802 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1803 self.waitUntilSettled()
1804
James E. Blair08d19992016-08-10 15:25:31 -07001805 self.launch_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001806 self.waitUntilSettled()
1807
Arie5b4048c2016-10-05 16:12:06 +03001808 self.assertBuilds([])
1809 self.assertHistory([
1810 dict(name='project-merge', result='ABORTED', changes='1,1')],
1811 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001812 self.assertEqual(A.reported, 1,
1813 "Abandoned gate change should report only start")
1814
1815 def test_abandoned_check(self):
1816 "Test that an abandoned change is dequeued from check"
1817
James E. Blair08d19992016-08-10 15:25:31 -07001818 self.launch_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001819
1820 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1821 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001822 tenant = self.sched.abide.tenants.get('tenant-one')
1823 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001824
1825 # Add two git-dependent changes
1826 B.setDependsOn(A, 1)
1827 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1828 self.waitUntilSettled()
1829 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1830 self.waitUntilSettled()
1831 # A live item, and a non-live/live pair
1832 items = check_pipeline.getAllItems()
1833 self.assertEqual(len(items), 3)
1834
1835 self.assertEqual(items[0].change.number, '1')
1836 self.assertFalse(items[0].live)
1837
1838 self.assertEqual(items[1].change.number, '2')
1839 self.assertTrue(items[1].live)
1840
1841 self.assertEqual(items[2].change.number, '1')
1842 self.assertTrue(items[2].live)
1843
1844 # Abandon A
1845 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1846 self.waitUntilSettled()
1847
1848 # The live copy of A should be gone, but the non-live and B
1849 # should continue
1850 items = check_pipeline.getAllItems()
1851 self.assertEqual(len(items), 2)
1852
1853 self.assertEqual(items[0].change.number, '1')
1854 self.assertFalse(items[0].live)
1855
1856 self.assertEqual(items[1].change.number, '2')
1857 self.assertTrue(items[1].live)
1858
James E. Blair08d19992016-08-10 15:25:31 -07001859 self.launch_server.hold_jobs_in_build = False
1860 self.launch_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001861 self.waitUntilSettled()
1862
1863 self.assertEqual(len(self.history), 4)
1864 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001865 'Build should have been aborted')
1866 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001867 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001868
James E. Blairec056492016-07-22 09:45:56 -07001869 @skip("Disabled for early v3 development")
Steve Varnau7b78b312015-04-03 14:49:46 -07001870 def test_abandoned_not_timer(self):
1871 "Test that an abandoned change does not cancel timer jobs"
1872
James E. Blair08d19992016-08-10 15:25:31 -07001873 self.launch_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001874
1875 # Start timer trigger - also org/project
James E. Blairf84026c2015-12-08 16:11:46 -08001876 self.updateConfigLayout(
1877 'tests/fixtures/layout-idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001878 self.sched.reconfigure(self.config)
1879 self.registerJobs()
1880 # The pipeline triggers every second, so we should have seen
1881 # several by now.
1882 time.sleep(5)
1883 self.waitUntilSettled()
1884 # Stop queuing timer triggered jobs so that the assertions
1885 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08001886 self.updateConfigLayout(
1887 'tests/fixtures/layout-no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001888 self.sched.reconfigure(self.config)
1889 self.registerJobs()
1890 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1891
1892 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1893 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1894 self.waitUntilSettled()
1895 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1896
1897 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1898 self.waitUntilSettled()
1899
1900 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1901
James E. Blair08d19992016-08-10 15:25:31 -07001902 self.launch_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001903 self.waitUntilSettled()
1904
Arx Cruzb1b010d2013-10-28 19:49:59 -02001905 def test_zuul_url_return(self):
1906 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001907 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
James E. Blair08d19992016-08-10 15:25:31 -07001908 self.launch_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001909
1910 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001911 A.addApproval('code-review', 2)
1912 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001913 self.waitUntilSettled()
1914
1915 self.assertEqual(len(self.builds), 1)
1916 for build in self.builds:
1917 self.assertTrue('ZUUL_URL' in build.parameters)
1918
James E. Blair08d19992016-08-10 15:25:31 -07001919 self.launch_server.hold_jobs_in_build = False
1920 self.launch_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001921 self.waitUntilSettled()
1922
James E. Blair2fa50962013-01-30 21:50:41 -08001923 def test_new_patchset_dequeues_old_on_head(self):
1924 "Test that a new patchset causes the old to be dequeued (at head)"
1925 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair08d19992016-08-10 15:25:31 -07001926 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001927 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1928 M.setMerged()
1929 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1930 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1931 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1932 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001933 A.addApproval('code-review', 2)
1934 B.addApproval('code-review', 2)
1935 C.addApproval('code-review', 2)
1936 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001937
1938 C.setDependsOn(B, 1)
1939 B.setDependsOn(A, 1)
1940 A.setDependsOn(M, 1)
1941
James E. Blair8b5408c2016-08-08 15:37:46 -07001942 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1943 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1944 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1945 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001946 self.waitUntilSettled()
1947
1948 A.addPatchset()
1949 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1950 self.waitUntilSettled()
1951
James E. Blair08d19992016-08-10 15:25:31 -07001952 self.launch_server.hold_jobs_in_build = False
1953 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001954 self.waitUntilSettled()
1955
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001956 self.assertEqual(A.data['status'], 'NEW')
1957 self.assertEqual(A.reported, 2)
1958 self.assertEqual(B.data['status'], 'NEW')
1959 self.assertEqual(B.reported, 2)
1960 self.assertEqual(C.data['status'], 'NEW')
1961 self.assertEqual(C.reported, 2)
1962 self.assertEqual(D.data['status'], 'MERGED')
1963 self.assertEqual(D.reported, 2)
1964 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001965
1966 def test_new_patchset_dequeues_old_without_dependents(self):
1967 "Test that a new patchset causes only the old to be dequeued"
James E. Blair08d19992016-08-10 15:25:31 -07001968 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001969 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1970 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1971 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001972 A.addApproval('code-review', 2)
1973 B.addApproval('code-review', 2)
1974 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001975
James E. Blair8b5408c2016-08-08 15:37:46 -07001976 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1977 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1978 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001979 self.waitUntilSettled()
1980
1981 B.addPatchset()
1982 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1983 self.waitUntilSettled()
1984
James E. Blair08d19992016-08-10 15:25:31 -07001985 self.launch_server.hold_jobs_in_build = False
1986 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001987 self.waitUntilSettled()
1988
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001989 self.assertEqual(A.data['status'], 'MERGED')
1990 self.assertEqual(A.reported, 2)
1991 self.assertEqual(B.data['status'], 'NEW')
1992 self.assertEqual(B.reported, 2)
1993 self.assertEqual(C.data['status'], 'MERGED')
1994 self.assertEqual(C.reported, 2)
1995 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001996
1997 def test_new_patchset_dequeues_old_independent_queue(self):
1998 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair08d19992016-08-10 15:25:31 -07001999 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002000 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2001 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2002 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2003 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2004 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2005 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2006 self.waitUntilSettled()
2007
2008 B.addPatchset()
2009 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2010 self.waitUntilSettled()
2011
James E. Blair08d19992016-08-10 15:25:31 -07002012 self.launch_server.hold_jobs_in_build = False
2013 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002014 self.waitUntilSettled()
2015
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002016 self.assertEqual(A.data['status'], 'NEW')
2017 self.assertEqual(A.reported, 1)
2018 self.assertEqual(B.data['status'], 'NEW')
2019 self.assertEqual(B.reported, 1)
2020 self.assertEqual(C.data['status'], 'NEW')
2021 self.assertEqual(C.reported, 1)
2022 self.assertEqual(len(self.history), 10)
2023 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002024
James E. Blair18c64442014-03-18 10:14:45 -07002025 def test_noop_job(self):
2026 "Test that the internal noop job works"
2027 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002028 A.addApproval('code-review', 2)
2029 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07002030 self.waitUntilSettled()
2031
2032 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2033 self.assertTrue(self.sched._areAllBuildsComplete())
2034 self.assertEqual(len(self.history), 0)
2035 self.assertEqual(A.data['status'], 'MERGED')
2036 self.assertEqual(A.reported, 2)
2037
James E. Blairec056492016-07-22 09:45:56 -07002038 @skip("Disabled for early v3 development")
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002039 def test_no_job_project(self):
2040 "Test that reports with no jobs don't get sent"
2041 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
2042 'master', 'A')
2043 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2044 self.waitUntilSettled()
2045
2046 # Change wasn't reported to
2047 self.assertEqual(A.reported, False)
2048
2049 # Check queue is empty afterwards
2050 check_pipeline = self.sched.layout.pipelines['check']
2051 items = check_pipeline.getAllItems()
2052 self.assertEqual(len(items), 0)
2053
2054 self.assertEqual(len(self.history), 0)
2055
James E. Blair7d0dedc2013-02-21 17:26:09 -08002056 def test_zuul_refs(self):
2057 "Test that zuul refs exist and have the right changes"
James E. Blair08d19992016-08-10 15:25:31 -07002058 self.launch_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002059 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2060 M1.setMerged()
2061 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2062 M2.setMerged()
2063
2064 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2065 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2066 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2067 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07002068 A.addApproval('code-review', 2)
2069 B.addApproval('code-review', 2)
2070 C.addApproval('code-review', 2)
2071 D.addApproval('code-review', 2)
2072 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2073 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
2074 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
2075 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002076
2077 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002078 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002079 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002080 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002081 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002082 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002083 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002084 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002085 self.waitUntilSettled()
2086
James E. Blair7d0dedc2013-02-21 17:26:09 -08002087 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08002088 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002089 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002090 if x.parameters['ZUUL_CHANGE'] == '3':
2091 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002092 a_build = x
2093 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002094 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002095 b_build = x
2096 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002097 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002098 c_build = x
2099 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002100 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002101 d_build = x
2102 if a_build and b_build and c_build and d_build:
2103 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002104
2105 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002106 self.assertIsNotNone(a_zref)
2107 self.assertIsNotNone(b_zref)
2108 self.assertIsNotNone(c_zref)
2109 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002110
2111 # And they should all be different
2112 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002113 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002114
Clint Byrum3343e3e2016-11-15 16:05:03 -08002115 # should have a, not b, and should not be in project2
2116 self.assertTrue(a_build.hasChanges(A))
2117 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002118
Clint Byrum3343e3e2016-11-15 16:05:03 -08002119 # should have a and b, and should not be in project2
2120 self.assertTrue(b_build.hasChanges(A, B))
2121 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002122
Clint Byrum3343e3e2016-11-15 16:05:03 -08002123 # should have a and b in 1, c in 2
2124 self.assertTrue(c_build.hasChanges(A, B, C))
2125 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002126
Clint Byrum3343e3e2016-11-15 16:05:03 -08002127 # should have a and b in 1, c and d in 2
2128 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002129
James E. Blair08d19992016-08-10 15:25:31 -07002130 self.launch_server.hold_jobs_in_build = False
2131 self.launch_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002132 self.waitUntilSettled()
2133
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002134 self.assertEqual(A.data['status'], 'MERGED')
2135 self.assertEqual(A.reported, 2)
2136 self.assertEqual(B.data['status'], 'MERGED')
2137 self.assertEqual(B.reported, 2)
2138 self.assertEqual(C.data['status'], 'MERGED')
2139 self.assertEqual(C.reported, 2)
2140 self.assertEqual(D.data['status'], 'MERGED')
2141 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002142
James E. Blairec056492016-07-22 09:45:56 -07002143 @skip("Disabled for early v3 development")
James E. Blair4a28a882013-08-23 15:17:33 -07002144 def test_rerun_on_error(self):
2145 "Test that if a worker fails to run a job, it is run again"
James E. Blair08d19992016-08-10 15:25:31 -07002146 self.launch_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002147 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002148 A.addApproval('code-review', 2)
2149 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002150 self.waitUntilSettled()
2151
2152 self.builds[0].run_error = True
James E. Blair08d19992016-08-10 15:25:31 -07002153 self.launch_server.hold_jobs_in_build = False
2154 self.launch_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002155 self.waitUntilSettled()
2156 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2157 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2158
James E. Blair412e5582013-04-22 15:50:12 -07002159 def test_statsd(self):
2160 "Test each of the statsd methods used in the scheduler"
2161 import extras
2162 statsd = extras.try_import('statsd.statsd')
2163 statsd.incr('test-incr')
2164 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002165 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002166 self.assertReportedStat('test-incr', '1|c')
2167 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002168 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002169
James E. Blairec056492016-07-22 09:45:56 -07002170 @skip("Disabled for early v3 development")
James E. Blairdad52252014-02-07 16:59:17 -08002171 def test_stuck_job_cleanup(self):
2172 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07002173 # This job won't be registered at startup because it is not in
2174 # the standard layout, but we need it to already be registerd
2175 # for when we reconfigure, as that is when Zuul will attempt
2176 # to run the new job.
2177 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002178 self.gearman_server.hold_jobs_in_queue = True
2179 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002180 A.addApproval('code-review', 2)
2181 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002182 self.waitUntilSettled()
2183 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2184
James E. Blairf84026c2015-12-08 16:11:46 -08002185 self.updateConfigLayout(
2186 'tests/fixtures/layout-no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002187 self.sched.reconfigure(self.config)
2188 self.waitUntilSettled()
2189
James E. Blair18c64442014-03-18 10:14:45 -07002190 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002191 self.waitUntilSettled()
2192 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2193 self.assertTrue(self.sched._areAllBuildsComplete())
2194
2195 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002196 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002197 self.assertEqual(self.history[0].result, 'SUCCESS')
2198
James E. Blair879dafb2015-07-17 14:04:49 -07002199 def test_file_head(self):
2200 # This is a regression test for an observed bug. A change
2201 # with a file named "HEAD" in the root directory of the repo
2202 # was processed by a merger. It then was unable to reset the
2203 # repo because of:
2204 # GitCommandError: 'git reset --hard HEAD' returned
2205 # with exit code 128
2206 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2207 # and filename
2208 # Use '--' to separate filenames from revisions'
2209
2210 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002211 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002212 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2213
2214 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2215 self.waitUntilSettled()
2216
2217 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2218 self.waitUntilSettled()
2219
2220 self.assertIn('Build succeeded', A.messages[0])
2221 self.assertIn('Build succeeded', B.messages[0])
2222
James E. Blairec056492016-07-22 09:45:56 -07002223 @skip("Disabled for early v3 development")
James E. Blair70c71582013-03-06 08:50:50 -08002224 def test_file_jobs(self):
2225 "Test that file jobs run only when appropriate"
2226 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2227 A.addPatchset(['pip-requires'])
2228 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002229 A.addApproval('code-review', 2)
2230 B.addApproval('code-review', 2)
2231 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2232 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002233 self.waitUntilSettled()
2234
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002235 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002236 if x.name == 'project-testfile']
2237
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002238 self.assertEqual(len(testfile_jobs), 1)
2239 self.assertEqual(testfile_jobs[0].changes, '1,2')
2240 self.assertEqual(A.data['status'], 'MERGED')
2241 self.assertEqual(A.reported, 2)
2242 self.assertEqual(B.data['status'], 'MERGED')
2243 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002244
James E. Blairec056492016-07-22 09:45:56 -07002245 @skip("Disabled for early v3 development")
Maru Newby3fe5f852015-01-13 04:22:14 +00002246 def _test_skip_if_jobs(self, branch, should_skip):
2247 "Test that jobs with a skip-if filter run only when appropriate"
James E. Blairf84026c2015-12-08 16:11:46 -08002248 self.updateConfigLayout(
2249 'tests/fixtures/layout-skip-if.yaml')
Maru Newby3fe5f852015-01-13 04:22:14 +00002250 self.sched.reconfigure(self.config)
2251 self.registerJobs()
2252
2253 change = self.fake_gerrit.addFakeChange('org/project',
2254 branch,
2255 'test skip-if')
2256 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2257 self.waitUntilSettled()
2258
2259 tested_change_ids = [x.changes[0] for x in self.history
2260 if x.name == 'project-test-skip-if']
2261
2262 if should_skip:
2263 self.assertEqual([], tested_change_ids)
2264 else:
2265 self.assertIn(change.data['number'], tested_change_ids)
2266
James E. Blairec056492016-07-22 09:45:56 -07002267 @skip("Disabled for early v3 development")
Maru Newby3fe5f852015-01-13 04:22:14 +00002268 def test_skip_if_match_skips_job(self):
2269 self._test_skip_if_jobs(branch='master', should_skip=True)
2270
James E. Blairec056492016-07-22 09:45:56 -07002271 @skip("Disabled for early v3 development")
Maru Newby3fe5f852015-01-13 04:22:14 +00002272 def test_skip_if_no_match_runs_job(self):
2273 self._test_skip_if_jobs(branch='mp', should_skip=False)
2274
James E. Blairec056492016-07-22 09:45:56 -07002275 @skip("Disabled for early v3 development")
James E. Blair3c5e5b52013-04-26 11:17:03 -07002276 def test_test_config(self):
2277 "Test that we can test the config"
James E. Blairf84026c2015-12-08 16:11:46 -08002278 self.sched.testConfig(self.config.get('zuul', 'tenant_config'),
Joshua Hesketh352264b2015-08-11 23:42:08 +10002279 self.connections)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002280
James E. Blairec056492016-07-22 09:45:56 -07002281 @skip("Disabled for early v3 development")
James E. Blairc8a1e052014-02-25 09:29:26 -08002282 def test_queue_names(self):
2283 "Test shared change queue names"
2284 project1 = self.sched.layout.projects['org/project1']
2285 project2 = self.sched.layout.projects['org/project2']
2286 q1 = self.sched.layout.pipelines['gate'].getQueue(project1)
2287 q2 = self.sched.layout.pipelines['gate'].getQueue(project2)
2288 self.assertEqual(q1.name, 'integration')
2289 self.assertEqual(q2.name, 'integration')
2290
James E. Blairf84026c2015-12-08 16:11:46 -08002291 self.updateConfigLayout(
2292 'tests/fixtures/layout-bad-queue.yaml')
James E. Blairc8a1e052014-02-25 09:29:26 -08002293 with testtools.ExpectedException(
2294 Exception, "More than one name assigned to change queue"):
2295 self.sched.reconfigure(self.config)
2296
James E. Blair64ed6f22013-07-10 14:07:23 -07002297 def test_queue_precedence(self):
2298 "Test that queue precedence works"
2299
2300 self.gearman_server.hold_jobs_in_queue = True
James E. Blair08d19992016-08-10 15:25:31 -07002301 self.launch_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002302 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2303 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002304 A.addApproval('code-review', 2)
2305 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002306
2307 self.waitUntilSettled()
2308 self.gearman_server.hold_jobs_in_queue = False
2309 self.gearman_server.release()
2310 self.waitUntilSettled()
2311
James E. Blair8de58bd2013-07-18 16:23:33 -07002312 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002313 self.orderedRelease()
James E. Blair08d19992016-08-10 15:25:31 -07002314 self.launch_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002315 self.waitUntilSettled()
2316
James E. Blair64ed6f22013-07-10 14:07:23 -07002317 self.log.debug(self.history)
2318 self.assertEqual(self.history[0].pipeline, 'gate')
2319 self.assertEqual(self.history[1].pipeline, 'check')
2320 self.assertEqual(self.history[2].pipeline, 'gate')
2321 self.assertEqual(self.history[3].pipeline, 'gate')
2322 self.assertEqual(self.history[4].pipeline, 'check')
2323 self.assertEqual(self.history[5].pipeline, 'check')
2324
James E. Blairec056492016-07-22 09:45:56 -07002325 @skip("Disabled for early v3 development")
Clark Boylana5edbe42014-06-03 16:39:10 -07002326 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002327 "Test that we can retrieve JSON status info"
James E. Blair08d19992016-08-10 15:25:31 -07002328 self.launch_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002329 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002330 A.addApproval('code-review', 2)
2331 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002332 self.waitUntilSettled()
2333
James E. Blair08d19992016-08-10 15:25:31 -07002334 self.launch_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002335 self.waitUntilSettled()
2336
James E. Blair1843a552013-07-03 14:19:52 -07002337 port = self.webapp.server.socket.getsockname()[1]
2338
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002339 req = urllib.request.Request("http://localhost:%s/status.json" % port)
2340 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002341 headers = f.info()
2342 self.assertIn('Content-Length', headers)
2343 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002344 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2345 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002346 self.assertIn('Access-Control-Allow-Origin', headers)
2347 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002348 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002349 self.assertIn('Expires', headers)
James E. Blair1843a552013-07-03 14:19:52 -07002350 data = f.read()
2351
James E. Blair08d19992016-08-10 15:25:31 -07002352 self.launch_server.hold_jobs_in_build = False
2353 self.launch_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002354 self.waitUntilSettled()
2355
2356 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002357 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002358 for p in data['pipelines']:
2359 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002360 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002361 self.assertEqual(q['window'], 20)
2362 else:
2363 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002364 for head in q['heads']:
2365 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002366 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002367 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002368 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002369 status_jobs.append(job)
2370 self.assertEqual('project-merge', status_jobs[0]['name'])
2371 self.assertEqual('https://server/job/project-merge/0/',
2372 status_jobs[0]['url'])
2373 self.assertEqual('http://logs.example.com/1/1/gate/project-merge/0',
2374 status_jobs[0]['report_url'])
2375
2376 self.assertEqual('project-test1', status_jobs[1]['name'])
2377 self.assertEqual('https://server/job/project-test1/1/',
2378 status_jobs[1]['url'])
2379 self.assertEqual('http://logs.example.com/1/1/gate/project-test1/1',
2380 status_jobs[1]['report_url'])
2381
2382 self.assertEqual('project-test2', status_jobs[2]['name'])
2383 self.assertEqual('https://server/job/project-test2/2/',
2384 status_jobs[2]['url'])
2385 self.assertEqual('http://logs.example.com/1/1/gate/project-test2/2',
2386 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002387
James E. Blairec056492016-07-22 09:45:56 -07002388 @skip("Disabled for early v3 development")
James E. Blairc3d428e2013-12-03 15:06:48 -08002389 def test_merging_queues(self):
2390 "Test that transitively-connected change queues are merged"
James E. Blairf84026c2015-12-08 16:11:46 -08002391 self.updateConfigLayout(
2392 'tests/fixtures/layout-merge-queues.yaml')
James E. Blairc3d428e2013-12-03 15:06:48 -08002393 self.sched.reconfigure(self.config)
2394 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
2395
James E. Blairec056492016-07-22 09:45:56 -07002396 @skip("Disabled for early v3 development")
James E. Blairaf17a972016-02-03 15:07:18 -08002397 def test_mutex(self):
2398 "Test job mutexes"
2399 self.config.set('zuul', 'layout_config',
2400 'tests/fixtures/layout-mutex.yaml')
2401 self.sched.reconfigure(self.config)
2402
James E. Blair08d19992016-08-10 15:25:31 -07002403 self.launch_server.hold_jobs_in_build = True
James E. Blairaf17a972016-02-03 15:07:18 -08002404 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2405 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2406 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2407
2408 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2409 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2410 self.waitUntilSettled()
2411 self.assertEqual(len(self.builds), 3)
2412 self.assertEqual(self.builds[0].name, 'project-test1')
2413 self.assertEqual(self.builds[1].name, 'mutex-one')
2414 self.assertEqual(self.builds[2].name, 'project-test1')
2415
James E. Blair08d19992016-08-10 15:25:31 -07002416 self.launch_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002417 self.waitUntilSettled()
2418
2419 self.assertEqual(len(self.builds), 3)
2420 self.assertEqual(self.builds[0].name, 'project-test1')
2421 self.assertEqual(self.builds[1].name, 'project-test1')
2422 self.assertEqual(self.builds[2].name, 'mutex-two')
2423 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2424
James E. Blair08d19992016-08-10 15:25:31 -07002425 self.launch_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002426 self.waitUntilSettled()
2427
2428 self.assertEqual(len(self.builds), 3)
2429 self.assertEqual(self.builds[0].name, 'project-test1')
2430 self.assertEqual(self.builds[1].name, 'project-test1')
2431 self.assertEqual(self.builds[2].name, 'mutex-one')
2432 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2433
James E. Blair08d19992016-08-10 15:25:31 -07002434 self.launch_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002435 self.waitUntilSettled()
2436
2437 self.assertEqual(len(self.builds), 3)
2438 self.assertEqual(self.builds[0].name, 'project-test1')
2439 self.assertEqual(self.builds[1].name, 'project-test1')
2440 self.assertEqual(self.builds[2].name, 'mutex-two')
2441 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2442
James E. Blair08d19992016-08-10 15:25:31 -07002443 self.launch_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002444 self.waitUntilSettled()
2445
2446 self.assertEqual(len(self.builds), 2)
2447 self.assertEqual(self.builds[0].name, 'project-test1')
2448 self.assertEqual(self.builds[1].name, 'project-test1')
2449 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2450
James E. Blair08d19992016-08-10 15:25:31 -07002451 self.launch_server.hold_jobs_in_build = False
2452 self.launch_server.release()
James E. Blairaf17a972016-02-03 15:07:18 -08002453
2454 self.waitUntilSettled()
2455 self.assertEqual(len(self.builds), 0)
2456
2457 self.assertEqual(A.reported, 1)
2458 self.assertEqual(B.reported, 1)
2459 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2460
James E. Blairec056492016-07-22 09:45:56 -07002461 @skip("Disabled for early v3 development")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002462 def test_node_label(self):
2463 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002464 self.worker.registerFunction('build:node-project-test1:debian')
2465
2466 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002467 A.addApproval('code-review', 2)
2468 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002469 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002470
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002471 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2472 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2473 'debian')
2474 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002475
2476 def test_live_reconfiguration(self):
2477 "Test that live reconfiguration works"
James E. Blair08d19992016-08-10 15:25:31 -07002478 self.launch_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002479 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002480 A.addApproval('code-review', 2)
2481 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002482 self.waitUntilSettled()
2483
2484 self.sched.reconfigure(self.config)
2485
James E. Blair08d19992016-08-10 15:25:31 -07002486 self.launch_server.hold_jobs_in_build = False
2487 self.launch_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002488 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002489 self.assertEqual(self.getJobFromHistory('project-merge').result,
2490 'SUCCESS')
2491 self.assertEqual(self.getJobFromHistory('project-test1').result,
2492 'SUCCESS')
2493 self.assertEqual(self.getJobFromHistory('project-test2').result,
2494 'SUCCESS')
2495 self.assertEqual(A.data['status'], 'MERGED')
2496 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002497
James E. Blairec056492016-07-22 09:45:56 -07002498 @skip("Disabled for early v3 development")
James E. Blair6bc782d2015-07-17 16:20:21 -07002499 def test_live_reconfiguration_merge_conflict(self):
2500 # A real-world bug: a change in a gate queue has a merge
2501 # conflict and a job is added to its project while it's
2502 # sitting in the queue. The job gets added to the change and
2503 # enqueued and the change gets stuck.
2504 self.worker.registerFunction('build:project-test3')
James E. Blair08d19992016-08-10 15:25:31 -07002505 self.launch_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002506
2507 # This change is fine. It's here to stop the queue long
2508 # enough for the next change to be subject to the
2509 # reconfiguration, as well as to provide a conflict for the
2510 # next change. This change will succeed and merge.
2511 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2512 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002513 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002514
2515 # This change will be in merge conflict. During the
2516 # reconfiguration, we will add a job. We want to make sure
2517 # that doesn't cause it to get stuck.
2518 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2519 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002520 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002521
James E. Blair8b5408c2016-08-08 15:37:46 -07002522 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2523 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002524
2525 self.waitUntilSettled()
2526
2527 # No jobs have run yet
2528 self.assertEqual(A.data['status'], 'NEW')
2529 self.assertEqual(A.reported, 1)
2530 self.assertEqual(B.data['status'], 'NEW')
2531 self.assertEqual(B.reported, 1)
2532 self.assertEqual(len(self.history), 0)
2533
2534 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002535 self.updateConfigLayout(
2536 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002537 self.sched.reconfigure(self.config)
2538 self.waitUntilSettled()
2539
James E. Blair08d19992016-08-10 15:25:31 -07002540 self.launch_server.hold_jobs_in_build = False
2541 self.launch_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002542 self.waitUntilSettled()
2543
2544 self.assertEqual(A.data['status'], 'MERGED')
2545 self.assertEqual(A.reported, 2)
2546 self.assertEqual(B.data['status'], 'NEW')
2547 self.assertEqual(B.reported, 2)
2548 self.assertEqual(self.getJobFromHistory('project-merge').result,
2549 'SUCCESS')
2550 self.assertEqual(self.getJobFromHistory('project-test1').result,
2551 'SUCCESS')
2552 self.assertEqual(self.getJobFromHistory('project-test2').result,
2553 'SUCCESS')
2554 self.assertEqual(self.getJobFromHistory('project-test3').result,
2555 'SUCCESS')
2556 self.assertEqual(len(self.history), 4)
2557
James E. Blairec056492016-07-22 09:45:56 -07002558 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002559 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002560 # An extrapolation of test_live_reconfiguration_merge_conflict
2561 # that tests a job added to a job tree with a failed root does
2562 # not run.
2563 self.worker.registerFunction('build:project-test3')
James E. Blair08d19992016-08-10 15:25:31 -07002564 self.launch_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002565
2566 # This change is fine. It's here to stop the queue long
2567 # enough for the next change to be subject to the
2568 # reconfiguration. This change will succeed and merge.
2569 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2570 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002571 A.addApproval('code-review', 2)
2572 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002573 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002574 self.launch_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002575 self.waitUntilSettled()
2576
2577 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair08d19992016-08-10 15:25:31 -07002578 self.launch_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002579 B.addApproval('code-review', 2)
2580 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002581 self.waitUntilSettled()
2582
James E. Blair08d19992016-08-10 15:25:31 -07002583 self.launch_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002584 self.waitUntilSettled()
2585
2586 # Both -merge jobs have run, but no others.
2587 self.assertEqual(A.data['status'], 'NEW')
2588 self.assertEqual(A.reported, 1)
2589 self.assertEqual(B.data['status'], 'NEW')
2590 self.assertEqual(B.reported, 1)
2591 self.assertEqual(self.history[0].result, 'SUCCESS')
2592 self.assertEqual(self.history[0].name, 'project-merge')
2593 self.assertEqual(self.history[1].result, 'FAILURE')
2594 self.assertEqual(self.history[1].name, 'project-merge')
2595 self.assertEqual(len(self.history), 2)
2596
2597 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002598 self.updateConfigLayout(
2599 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002600 self.sched.reconfigure(self.config)
2601 self.waitUntilSettled()
2602
James E. Blair08d19992016-08-10 15:25:31 -07002603 self.launch_server.hold_jobs_in_build = False
2604 self.launch_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002605 self.waitUntilSettled()
2606
2607 self.assertEqual(A.data['status'], 'MERGED')
2608 self.assertEqual(A.reported, 2)
2609 self.assertEqual(B.data['status'], 'NEW')
2610 self.assertEqual(B.reported, 2)
2611 self.assertEqual(self.history[0].result, 'SUCCESS')
2612 self.assertEqual(self.history[0].name, 'project-merge')
2613 self.assertEqual(self.history[1].result, 'FAILURE')
2614 self.assertEqual(self.history[1].name, 'project-merge')
2615 self.assertEqual(self.history[2].result, 'SUCCESS')
2616 self.assertEqual(self.history[3].result, 'SUCCESS')
2617 self.assertEqual(self.history[4].result, 'SUCCESS')
2618 self.assertEqual(len(self.history), 5)
2619
James E. Blairec056492016-07-22 09:45:56 -07002620 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002621 def test_live_reconfiguration_failed_job(self):
2622 # Test that a change with a removed failing job does not
2623 # disrupt reconfiguration. If a change has a failed job and
2624 # that job is removed during a reconfiguration, we observed a
2625 # bug where the code to re-set build statuses would run on
2626 # that build and raise an exception because the job no longer
2627 # existed.
James E. Blair08d19992016-08-10 15:25:31 -07002628 self.launch_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002629
2630 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2631
2632 # This change will fail and later be removed by the reconfiguration.
James E. Blair08d19992016-08-10 15:25:31 -07002633 self.launch_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002634
2635 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2636 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002637 self.launch_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002638 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002639 self.launch_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002640 self.waitUntilSettled()
2641
2642 self.assertEqual(A.data['status'], 'NEW')
2643 self.assertEqual(A.reported, 0)
2644
2645 self.assertEqual(self.getJobFromHistory('project-merge').result,
2646 'SUCCESS')
2647 self.assertEqual(self.getJobFromHistory('project-test1').result,
2648 'FAILURE')
2649 self.assertEqual(len(self.history), 2)
2650
2651 # Remove the test1 job.
James E. Blairf84026c2015-12-08 16:11:46 -08002652 self.updateConfigLayout(
2653 'tests/fixtures/layout-live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002654 self.sched.reconfigure(self.config)
2655 self.waitUntilSettled()
2656
James E. Blair08d19992016-08-10 15:25:31 -07002657 self.launch_server.hold_jobs_in_build = False
2658 self.launch_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002659 self.waitUntilSettled()
2660
2661 self.assertEqual(self.getJobFromHistory('project-test2').result,
2662 'SUCCESS')
2663 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2664 'SUCCESS')
2665 self.assertEqual(len(self.history), 4)
2666
2667 self.assertEqual(A.data['status'], 'NEW')
2668 self.assertEqual(A.reported, 1)
2669 self.assertIn('Build succeeded', A.messages[0])
2670 # Ensure the removed job was not included in the report.
2671 self.assertNotIn('project-test1', A.messages[0])
2672
James E. Blairec056492016-07-22 09:45:56 -07002673 @skip("Disabled for early v3 development")
James E. Blairfe707d12015-08-05 15:18:15 -07002674 def test_live_reconfiguration_shared_queue(self):
2675 # Test that a change with a failing job which was removed from
2676 # this project but otherwise still exists in the system does
2677 # not disrupt reconfiguration.
2678
James E. Blair08d19992016-08-10 15:25:31 -07002679 self.launch_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002680
2681 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2682
James E. Blair08d19992016-08-10 15:25:31 -07002683 self.launch_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002684
2685 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2686 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002687 self.launch_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002688 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002689 self.launch_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002690 self.waitUntilSettled()
2691
2692 self.assertEqual(A.data['status'], 'NEW')
2693 self.assertEqual(A.reported, 0)
2694
2695 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2696 'SUCCESS')
2697 self.assertEqual(self.getJobFromHistory(
2698 'project1-project2-integration').result, 'FAILURE')
2699 self.assertEqual(len(self.history), 2)
2700
2701 # Remove the integration job.
James E. Blairf84026c2015-12-08 16:11:46 -08002702 self.updateConfigLayout(
2703 'tests/fixtures/layout-live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002704 self.sched.reconfigure(self.config)
2705 self.waitUntilSettled()
2706
James E. Blair08d19992016-08-10 15:25:31 -07002707 self.launch_server.hold_jobs_in_build = False
2708 self.launch_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002709 self.waitUntilSettled()
2710
2711 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2712 'SUCCESS')
2713 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2714 'SUCCESS')
2715 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2716 'SUCCESS')
2717 self.assertEqual(self.getJobFromHistory(
2718 'project1-project2-integration').result, 'FAILURE')
2719 self.assertEqual(len(self.history), 4)
2720
2721 self.assertEqual(A.data['status'], 'NEW')
2722 self.assertEqual(A.reported, 1)
2723 self.assertIn('Build succeeded', A.messages[0])
2724 # Ensure the removed job was not included in the report.
2725 self.assertNotIn('project1-project2-integration', A.messages[0])
2726
James E. Blairec056492016-07-22 09:45:56 -07002727 @skip("Disabled for early v3 development")
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002728 def test_double_live_reconfiguration_shared_queue(self):
2729 # This was a real-world regression. A change is added to
2730 # gate; a reconfigure happens, a second change which depends
2731 # on the first is added, and a second reconfiguration happens.
2732 # Ensure that both changes merge.
2733
2734 # A failure may indicate incorrect caching or cleaning up of
2735 # references during a reconfiguration.
James E. Blair08d19992016-08-10 15:25:31 -07002736 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002737
2738 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2739 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2740 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002741 A.addApproval('code-review', 2)
2742 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002743
2744 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002745 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002746 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002747 self.launch_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002748 self.waitUntilSettled()
2749
2750 # Reconfigure (with only one change in the pipeline).
2751 self.sched.reconfigure(self.config)
2752 self.waitUntilSettled()
2753
2754 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002755 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002756 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002757 self.launch_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002758 self.waitUntilSettled()
2759
2760 # Reconfigure (with both in the pipeline).
2761 self.sched.reconfigure(self.config)
2762 self.waitUntilSettled()
2763
James E. Blair08d19992016-08-10 15:25:31 -07002764 self.launch_server.hold_jobs_in_build = False
2765 self.launch_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002766 self.waitUntilSettled()
2767
2768 self.assertEqual(len(self.history), 8)
2769
2770 self.assertEqual(A.data['status'], 'MERGED')
2771 self.assertEqual(A.reported, 2)
2772 self.assertEqual(B.data['status'], 'MERGED')
2773 self.assertEqual(B.reported, 2)
2774
James E. Blairec056492016-07-22 09:45:56 -07002775 @skip("Disabled for early v3 development")
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002776 def test_live_reconfiguration_del_project(self):
2777 # Test project deletion from layout
2778 # while changes are enqueued
2779
James E. Blair08d19992016-08-10 15:25:31 -07002780 self.launch_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002781 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2782 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2783 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2784
2785 # A Depends-On: B
2786 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2787 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002788 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002789
2790 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2791 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2792 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002793 self.launch_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002794 self.waitUntilSettled()
2795 self.assertEqual(len(self.builds), 5)
2796
2797 # This layout defines only org/project, not org/project1
James E. Blairf84026c2015-12-08 16:11:46 -08002798 self.updateConfigLayout(
2799 'tests/fixtures/layout-live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002800 self.sched.reconfigure(self.config)
2801 self.waitUntilSettled()
2802
2803 # Builds for C aborted, builds for A succeed,
2804 # and have change B applied ahead
2805 job_c = self.getJobFromHistory('project1-test1')
2806 self.assertEqual(job_c.changes, '3,1')
2807 self.assertEqual(job_c.result, 'ABORTED')
2808
James E. Blair08d19992016-08-10 15:25:31 -07002809 self.launch_server.hold_jobs_in_build = False
2810 self.launch_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002811 self.waitUntilSettled()
2812
2813 self.assertEqual(self.getJobFromHistory('project-test1').changes,
2814 '2,1 1,1')
2815
2816 self.assertEqual(A.data['status'], 'NEW')
2817 self.assertEqual(B.data['status'], 'NEW')
2818 self.assertEqual(C.data['status'], 'NEW')
2819 self.assertEqual(A.reported, 1)
2820 self.assertEqual(B.reported, 0)
2821 self.assertEqual(C.reported, 0)
2822
2823 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
2824 self.assertIn('Build succeeded', A.messages[0])
2825
James E. Blairec056492016-07-22 09:45:56 -07002826 @skip("Disabled for early v3 development")
James E. Blaire712d9f2013-07-31 11:40:11 -07002827 def test_live_reconfiguration_functions(self):
2828 "Test live reconfiguration with a custom function"
2829 self.worker.registerFunction('build:node-project-test1:debian')
2830 self.worker.registerFunction('build:node-project-test1:wheezy')
2831 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002832 A.addApproval('code-review', 2)
2833 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002834 self.waitUntilSettled()
2835
2836 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2837 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2838 'debian')
2839 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2840
James E. Blairf84026c2015-12-08 16:11:46 -08002841 self.updateConfigLayout(
2842 'tests/fixtures/layout-live-reconfiguration-functions.yaml')
James E. Blaire712d9f2013-07-31 11:40:11 -07002843 self.sched.reconfigure(self.config)
2844 self.worker.build_history = []
2845
2846 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002847 B.addApproval('code-review', 2)
2848 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002849 self.waitUntilSettled()
2850
2851 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2852 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2853 'wheezy')
2854 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2855
James E. Blairec056492016-07-22 09:45:56 -07002856 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002857 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002858 self.updateConfigLayout(
2859 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002860 self.sched.reconfigure(self.config)
2861
2862 self.init_repo("org/new-project")
2863 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2864
James E. Blair8b5408c2016-08-08 15:37:46 -07002865 A.addApproval('code-review', 2)
2866 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002867 self.waitUntilSettled()
2868 self.assertEqual(self.getJobFromHistory('project-merge').result,
2869 'SUCCESS')
2870 self.assertEqual(self.getJobFromHistory('project-test1').result,
2871 'SUCCESS')
2872 self.assertEqual(self.getJobFromHistory('project-test2').result,
2873 'SUCCESS')
2874 self.assertEqual(A.data['status'], 'MERGED')
2875 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002876
Clark Boylan6dbbc482013-10-18 10:57:31 -07002877 def test_repo_deleted(self):
Paul Belanger66e95962016-11-11 12:11:06 -05002878 self.updateConfigLayout('layout-repo-deleted')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002879 self.sched.reconfigure(self.config)
2880
2881 self.init_repo("org/delete-project")
2882 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2883
James E. Blair8b5408c2016-08-08 15:37:46 -07002884 A.addApproval('code-review', 2)
2885 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002886 self.waitUntilSettled()
2887 self.assertEqual(self.getJobFromHistory('project-merge').result,
2888 'SUCCESS')
2889 self.assertEqual(self.getJobFromHistory('project-test1').result,
2890 'SUCCESS')
2891 self.assertEqual(self.getJobFromHistory('project-test2').result,
2892 'SUCCESS')
2893 self.assertEqual(A.data['status'], 'MERGED')
2894 self.assertEqual(A.reported, 2)
2895
2896 # Delete org/new-project zuul repo. Should be recloned.
2897 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
2898
2899 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2900
James E. Blair8b5408c2016-08-08 15:37:46 -07002901 B.addApproval('code-review', 2)
2902 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002903 self.waitUntilSettled()
2904 self.assertEqual(self.getJobFromHistory('project-merge').result,
2905 'SUCCESS')
2906 self.assertEqual(self.getJobFromHistory('project-test1').result,
2907 'SUCCESS')
2908 self.assertEqual(self.getJobFromHistory('project-test2').result,
2909 'SUCCESS')
2910 self.assertEqual(B.data['status'], 'MERGED')
2911 self.assertEqual(B.reported, 2)
2912
James E. Blairec056492016-07-22 09:45:56 -07002913 @skip("Disabled for early v3 development")
James E. Blair456f2fb2016-02-09 09:29:33 -08002914 def test_tags(self):
2915 "Test job tags"
2916 self.config.set('zuul', 'layout_config',
2917 'tests/fixtures/layout-tags.yaml')
2918 self.sched.reconfigure(self.config)
2919
2920 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2921 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2922 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2923 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2924 self.waitUntilSettled()
2925
2926 results = {'project1-merge': 'extratag merge project1',
2927 'project2-merge': 'merge'}
2928
2929 for build in self.history:
2930 self.assertEqual(results.get(build.name, ''),
2931 build.parameters.get('BUILD_TAGS'))
2932
James E. Blairec056492016-07-22 09:45:56 -07002933 @skip("Disabled for early v3 development")
James E. Blair63bb0ef2013-07-29 17:14:51 -07002934 def test_timer(self):
2935 "Test that a periodic job is triggered"
James E. Blair08d19992016-08-10 15:25:31 -07002936 self.launch_server.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08002937 self.updateConfigLayout(
2938 'tests/fixtures/layout-timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002939 self.sched.reconfigure(self.config)
2940 self.registerJobs()
2941
Clark Boylan3ee090a2014-04-03 20:55:09 -07002942 # The pipeline triggers every second, so we should have seen
2943 # several by now.
2944 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002945 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002946
2947 self.assertEqual(len(self.builds), 2)
2948
James E. Blair63bb0ef2013-07-29 17:14:51 -07002949 port = self.webapp.server.socket.getsockname()[1]
2950
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002951 req = urllib.request.Request("http://localhost:%s/status.json" % port)
2952 f = urllib.request.urlopen(req)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002953 data = f.read()
2954
James E. Blair08d19992016-08-10 15:25:31 -07002955 self.launch_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002956 # Stop queuing timer triggered jobs so that the assertions
2957 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002958 self.updateConfigLayout(
2959 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002960 self.sched.reconfigure(self.config)
2961 self.registerJobs()
James E. Blair08d19992016-08-10 15:25:31 -07002962 self.launch_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002963 self.waitUntilSettled()
2964
2965 self.assertEqual(self.getJobFromHistory(
2966 'project-bitrot-stable-old').result, 'SUCCESS')
2967 self.assertEqual(self.getJobFromHistory(
2968 'project-bitrot-stable-older').result, 'SUCCESS')
2969
2970 data = json.loads(data)
2971 status_jobs = set()
2972 for p in data['pipelines']:
2973 for q in p['change_queues']:
2974 for head in q['heads']:
2975 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07002976 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002977 for job in change['jobs']:
2978 status_jobs.add(job['name'])
2979 self.assertIn('project-bitrot-stable-old', status_jobs)
2980 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002981
James E. Blairec056492016-07-22 09:45:56 -07002982 @skip("Disabled for early v3 development")
James E. Blair4f6033c2014-03-27 15:49:09 -07002983 def test_idle(self):
2984 "Test that frequent periodic jobs work"
James E. Blair08d19992016-08-10 15:25:31 -07002985 self.launch_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002986
Clark Boylan3ee090a2014-04-03 20:55:09 -07002987 for x in range(1, 3):
2988 # Test that timer triggers periodic jobs even across
2989 # layout config reloads.
2990 # Start timer trigger
James E. Blairf84026c2015-12-08 16:11:46 -08002991 self.updateConfigLayout(
2992 'tests/fixtures/layout-idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002993 self.sched.reconfigure(self.config)
2994 self.registerJobs()
James E. Blair995fc0f2016-02-04 16:48:31 -08002995 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002996
Clark Boylan3ee090a2014-04-03 20:55:09 -07002997 # The pipeline triggers every second, so we should have seen
2998 # several by now.
2999 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07003000
3001 # Stop queuing timer triggered jobs so that the assertions
3002 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08003003 self.updateConfigLayout(
3004 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003005 self.sched.reconfigure(self.config)
3006 self.registerJobs()
James E. Blair995fc0f2016-02-04 16:48:31 -08003007 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07003008
3009 self.assertEqual(len(self.builds), 2)
James E. Blair08d19992016-08-10 15:25:31 -07003010 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003011 self.waitUntilSettled()
3012 self.assertEqual(len(self.builds), 0)
3013 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07003014
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003015 def test_check_smtp_pool(self):
Paul Belanger01be8ca2016-11-11 12:21:06 -05003016 self.updateConfigLayout('layout-smtp')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003017 self.sched.reconfigure(self.config)
3018
3019 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3020 self.waitUntilSettled()
3021
3022 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3023 self.waitUntilSettled()
3024
James E. Blairff80a2f2013-12-27 13:24:06 -08003025 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003026
3027 # A.messages only holds what FakeGerrit places in it. Thus we
3028 # work on the knowledge of what the first message should be as
3029 # it is only configured to go to SMTP.
3030
3031 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003032 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003033 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003034 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003035 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003036 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003037
3038 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003039 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003040 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003041 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003042 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003043 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003044
James E. Blairec056492016-07-22 09:45:56 -07003045 @skip("Disabled for early v3 development")
James E. Blaire5910202013-12-27 09:50:31 -08003046 def test_timer_smtp(self):
3047 "Test that a periodic job is triggered"
James E. Blair08d19992016-08-10 15:25:31 -07003048 self.launch_server.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08003049 self.updateConfigLayout(
3050 'tests/fixtures/layout-timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08003051 self.sched.reconfigure(self.config)
3052 self.registerJobs()
3053
Clark Boylan3ee090a2014-04-03 20:55:09 -07003054 # The pipeline triggers every second, so we should have seen
3055 # several by now.
3056 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003057 self.waitUntilSettled()
3058
Clark Boylan3ee090a2014-04-03 20:55:09 -07003059 self.assertEqual(len(self.builds), 2)
James E. Blair08d19992016-08-10 15:25:31 -07003060 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003061 self.waitUntilSettled()
3062 self.assertEqual(len(self.history), 2)
3063
James E. Blaire5910202013-12-27 09:50:31 -08003064 self.assertEqual(self.getJobFromHistory(
3065 'project-bitrot-stable-old').result, 'SUCCESS')
3066 self.assertEqual(self.getJobFromHistory(
3067 'project-bitrot-stable-older').result, 'SUCCESS')
3068
James E. Blairff80a2f2013-12-27 13:24:06 -08003069 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003070
3071 # A.messages only holds what FakeGerrit places in it. Thus we
3072 # work on the knowledge of what the first message should be as
3073 # it is only configured to go to SMTP.
3074
3075 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003076 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003077 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003078 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003079 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003080 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003081
Clark Boylan3ee090a2014-04-03 20:55:09 -07003082 # Stop queuing timer triggered jobs and let any that may have
3083 # queued through so that end of test assertions pass.
James E. Blairf84026c2015-12-08 16:11:46 -08003084 self.updateConfigLayout(
3085 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003086 self.sched.reconfigure(self.config)
3087 self.registerJobs()
James E. Blairf8058972014-08-15 16:09:16 -07003088 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003089 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003090 self.waitUntilSettled()
3091
James E. Blairec056492016-07-22 09:45:56 -07003092 @skip("Disabled for early v3 development")
James E. Blair91e34592015-07-31 16:45:59 -07003093 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003094 "Test that the RPC client can enqueue a change"
3095 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003096 A.addApproval('code-review', 2)
3097 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003098
3099 client = zuul.rpcclient.RPCClient('127.0.0.1',
3100 self.gearman_server.port)
3101 r = client.enqueue(pipeline='gate',
3102 project='org/project',
3103 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003104 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003105 self.waitUntilSettled()
3106 self.assertEqual(self.getJobFromHistory('project-merge').result,
3107 'SUCCESS')
3108 self.assertEqual(self.getJobFromHistory('project-test1').result,
3109 'SUCCESS')
3110 self.assertEqual(self.getJobFromHistory('project-test2').result,
3111 'SUCCESS')
3112 self.assertEqual(A.data['status'], 'MERGED')
3113 self.assertEqual(A.reported, 2)
3114 self.assertEqual(r, True)
3115
James E. Blairec056492016-07-22 09:45:56 -07003116 @skip("Disabled for early v3 development")
James E. Blair91e34592015-07-31 16:45:59 -07003117 def test_client_enqueue_ref(self):
3118 "Test that the RPC client can enqueue a ref"
3119
3120 client = zuul.rpcclient.RPCClient('127.0.0.1',
3121 self.gearman_server.port)
3122 r = client.enqueue_ref(
3123 pipeline='post',
3124 project='org/project',
3125 trigger='gerrit',
3126 ref='master',
3127 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
3128 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
3129 self.waitUntilSettled()
3130 job_names = [x.name for x in self.history]
3131 self.assertEqual(len(self.history), 1)
3132 self.assertIn('project-post', job_names)
3133 self.assertEqual(r, True)
3134
James E. Blairec056492016-07-22 09:45:56 -07003135 @skip("Disabled for early v3 development")
James E. Blairad28e912013-11-27 10:43:22 -08003136 def test_client_enqueue_negative(self):
3137 "Test that the RPC client returns errors"
3138 client = zuul.rpcclient.RPCClient('127.0.0.1',
3139 self.gearman_server.port)
3140 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3141 "Invalid project"):
3142 r = client.enqueue(pipeline='gate',
3143 project='project-does-not-exist',
3144 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003145 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003146 client.shutdown()
3147 self.assertEqual(r, False)
3148
3149 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3150 "Invalid pipeline"):
3151 r = client.enqueue(pipeline='pipeline-does-not-exist',
3152 project='org/project',
3153 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003154 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003155 client.shutdown()
3156 self.assertEqual(r, False)
3157
3158 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3159 "Invalid trigger"):
3160 r = client.enqueue(pipeline='gate',
3161 project='org/project',
3162 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003163 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003164 client.shutdown()
3165 self.assertEqual(r, False)
3166
3167 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3168 "Invalid change"):
3169 r = client.enqueue(pipeline='gate',
3170 project='org/project',
3171 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003172 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003173 client.shutdown()
3174 self.assertEqual(r, False)
3175
3176 self.waitUntilSettled()
3177 self.assertEqual(len(self.history), 0)
3178 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003179
James E. Blairec056492016-07-22 09:45:56 -07003180 @skip("Disabled for early v3 development")
James E. Blair36658cf2013-12-06 17:53:48 -08003181 def test_client_promote(self):
3182 "Test that the RPC client can promote a change"
James E. Blair08d19992016-08-10 15:25:31 -07003183 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003184 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3185 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3186 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003187 A.addApproval('code-review', 2)
3188 B.addApproval('code-review', 2)
3189 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003190
James E. Blair8b5408c2016-08-08 15:37:46 -07003191 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3192 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3193 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003194
3195 self.waitUntilSettled()
3196
Sean Daguef39b9ca2014-01-10 21:34:35 -05003197 items = self.sched.layout.pipelines['gate'].getAllItems()
3198 enqueue_times = {}
3199 for item in items:
3200 enqueue_times[str(item.change)] = item.enqueue_time
3201
James E. Blair36658cf2013-12-06 17:53:48 -08003202 client = zuul.rpcclient.RPCClient('127.0.0.1',
3203 self.gearman_server.port)
3204 r = client.promote(pipeline='gate',
3205 change_ids=['2,1', '3,1'])
3206
Sean Daguef39b9ca2014-01-10 21:34:35 -05003207 # ensure that enqueue times are durable
3208 items = self.sched.layout.pipelines['gate'].getAllItems()
3209 for item in items:
3210 self.assertEqual(
3211 enqueue_times[str(item.change)], item.enqueue_time)
3212
James E. Blair78acec92014-02-06 07:11:32 -08003213 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003214 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003215 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003216 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003217 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003218 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003219 self.waitUntilSettled()
3220
3221 self.assertEqual(len(self.builds), 6)
3222 self.assertEqual(self.builds[0].name, 'project-test1')
3223 self.assertEqual(self.builds[1].name, 'project-test2')
3224 self.assertEqual(self.builds[2].name, 'project-test1')
3225 self.assertEqual(self.builds[3].name, 'project-test2')
3226 self.assertEqual(self.builds[4].name, 'project-test1')
3227 self.assertEqual(self.builds[5].name, 'project-test2')
3228
3229 self.assertTrue(self.job_has_changes(self.builds[0], B))
3230 self.assertFalse(self.job_has_changes(self.builds[0], A))
3231 self.assertFalse(self.job_has_changes(self.builds[0], C))
3232
3233 self.assertTrue(self.job_has_changes(self.builds[2], B))
3234 self.assertTrue(self.job_has_changes(self.builds[2], C))
3235 self.assertFalse(self.job_has_changes(self.builds[2], A))
3236
3237 self.assertTrue(self.job_has_changes(self.builds[4], B))
3238 self.assertTrue(self.job_has_changes(self.builds[4], C))
3239 self.assertTrue(self.job_has_changes(self.builds[4], A))
3240
James E. Blair08d19992016-08-10 15:25:31 -07003241 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003242 self.waitUntilSettled()
3243
3244 self.assertEqual(A.data['status'], 'MERGED')
3245 self.assertEqual(A.reported, 2)
3246 self.assertEqual(B.data['status'], 'MERGED')
3247 self.assertEqual(B.reported, 2)
3248 self.assertEqual(C.data['status'], 'MERGED')
3249 self.assertEqual(C.reported, 2)
3250
3251 client.shutdown()
3252 self.assertEqual(r, True)
3253
James E. Blairec056492016-07-22 09:45:56 -07003254 @skip("Disabled for early v3 development")
James E. Blair36658cf2013-12-06 17:53:48 -08003255 def test_client_promote_dependent(self):
3256 "Test that the RPC client can promote a dependent change"
3257 # C (depends on B) -> B -> A ; then promote C to get:
3258 # A -> C (depends on B) -> B
James E. Blair08d19992016-08-10 15:25:31 -07003259 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003260 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3261 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3262 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3263
3264 C.setDependsOn(B, 1)
3265
James E. Blair8b5408c2016-08-08 15:37:46 -07003266 A.addApproval('code-review', 2)
3267 B.addApproval('code-review', 2)
3268 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003269
James E. Blair8b5408c2016-08-08 15:37:46 -07003270 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3271 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3272 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003273
3274 self.waitUntilSettled()
3275
3276 client = zuul.rpcclient.RPCClient('127.0.0.1',
3277 self.gearman_server.port)
3278 r = client.promote(pipeline='gate',
3279 change_ids=['3,1'])
3280
James E. Blair78acec92014-02-06 07:11:32 -08003281 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003282 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003283 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003284 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003285 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003286 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003287 self.waitUntilSettled()
3288
3289 self.assertEqual(len(self.builds), 6)
3290 self.assertEqual(self.builds[0].name, 'project-test1')
3291 self.assertEqual(self.builds[1].name, 'project-test2')
3292 self.assertEqual(self.builds[2].name, 'project-test1')
3293 self.assertEqual(self.builds[3].name, 'project-test2')
3294 self.assertEqual(self.builds[4].name, 'project-test1')
3295 self.assertEqual(self.builds[5].name, 'project-test2')
3296
3297 self.assertTrue(self.job_has_changes(self.builds[0], B))
3298 self.assertFalse(self.job_has_changes(self.builds[0], A))
3299 self.assertFalse(self.job_has_changes(self.builds[0], C))
3300
3301 self.assertTrue(self.job_has_changes(self.builds[2], B))
3302 self.assertTrue(self.job_has_changes(self.builds[2], C))
3303 self.assertFalse(self.job_has_changes(self.builds[2], A))
3304
3305 self.assertTrue(self.job_has_changes(self.builds[4], B))
3306 self.assertTrue(self.job_has_changes(self.builds[4], C))
3307 self.assertTrue(self.job_has_changes(self.builds[4], A))
3308
James E. Blair08d19992016-08-10 15:25:31 -07003309 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003310 self.waitUntilSettled()
3311
3312 self.assertEqual(A.data['status'], 'MERGED')
3313 self.assertEqual(A.reported, 2)
3314 self.assertEqual(B.data['status'], 'MERGED')
3315 self.assertEqual(B.reported, 2)
3316 self.assertEqual(C.data['status'], 'MERGED')
3317 self.assertEqual(C.reported, 2)
3318
3319 client.shutdown()
3320 self.assertEqual(r, True)
3321
James E. Blairec056492016-07-22 09:45:56 -07003322 @skip("Disabled for early v3 development")
James E. Blair36658cf2013-12-06 17:53:48 -08003323 def test_client_promote_negative(self):
3324 "Test that the RPC client returns errors for promotion"
James E. Blair08d19992016-08-10 15:25:31 -07003325 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003326 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003327 A.addApproval('code-review', 2)
3328 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003329 self.waitUntilSettled()
3330
3331 client = zuul.rpcclient.RPCClient('127.0.0.1',
3332 self.gearman_server.port)
3333
3334 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3335 r = client.promote(pipeline='nonexistent',
3336 change_ids=['2,1', '3,1'])
3337 client.shutdown()
3338 self.assertEqual(r, False)
3339
3340 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3341 r = client.promote(pipeline='gate',
3342 change_ids=['4,1'])
3343 client.shutdown()
3344 self.assertEqual(r, False)
3345
James E. Blair08d19992016-08-10 15:25:31 -07003346 self.launch_server.hold_jobs_in_build = False
3347 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003348 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003349
James E. Blairec056492016-07-22 09:45:56 -07003350 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003351 def test_queue_rate_limiting(self):
3352 "Test that DependentPipelines are rate limited with dep across window"
James E. Blairf84026c2015-12-08 16:11:46 -08003353 self.updateConfigLayout(
3354 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003355 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07003356 self.launch_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003357 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3358 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3359 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3360
3361 C.setDependsOn(B, 1)
James E. Blair08d19992016-08-10 15:25:31 -07003362 self.launch_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003363
James E. Blair8b5408c2016-08-08 15:37:46 -07003364 A.addApproval('code-review', 2)
3365 B.addApproval('code-review', 2)
3366 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003367
James E. Blair8b5408c2016-08-08 15:37:46 -07003368 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3369 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3370 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003371 self.waitUntilSettled()
3372
3373 # Only A and B will have their merge jobs queued because
3374 # window is 2.
3375 self.assertEqual(len(self.builds), 2)
3376 self.assertEqual(self.builds[0].name, 'project-merge')
3377 self.assertEqual(self.builds[1].name, 'project-merge')
3378
James E. Blair08d19992016-08-10 15:25:31 -07003379 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003380 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003381 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003382 self.waitUntilSettled()
3383
3384 # Only A and B will have their test jobs queued because
3385 # window is 2.
3386 self.assertEqual(len(self.builds), 4)
3387 self.assertEqual(self.builds[0].name, 'project-test1')
3388 self.assertEqual(self.builds[1].name, 'project-test2')
3389 self.assertEqual(self.builds[2].name, 'project-test1')
3390 self.assertEqual(self.builds[3].name, 'project-test2')
3391
James E. Blair08d19992016-08-10 15:25:31 -07003392 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003393 self.waitUntilSettled()
3394
3395 queue = self.sched.layout.pipelines['gate'].queues[0]
3396 # A failed so window is reduced by 1 to 1.
3397 self.assertEqual(queue.window, 1)
3398 self.assertEqual(queue.window_floor, 1)
3399 self.assertEqual(A.data['status'], 'NEW')
3400
3401 # Gate is reset and only B's merge job is queued because
3402 # window shrunk to 1.
3403 self.assertEqual(len(self.builds), 1)
3404 self.assertEqual(self.builds[0].name, 'project-merge')
3405
James E. Blair08d19992016-08-10 15:25:31 -07003406 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003407 self.waitUntilSettled()
3408
3409 # Only B's test jobs are queued because window is still 1.
3410 self.assertEqual(len(self.builds), 2)
3411 self.assertEqual(self.builds[0].name, 'project-test1')
3412 self.assertEqual(self.builds[1].name, 'project-test2')
3413
James E. Blair08d19992016-08-10 15:25:31 -07003414 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003415 self.waitUntilSettled()
3416
3417 # B was successfully merged so window is increased to 2.
3418 self.assertEqual(queue.window, 2)
3419 self.assertEqual(queue.window_floor, 1)
3420 self.assertEqual(B.data['status'], 'MERGED')
3421
3422 # Only C is left and its merge job is queued.
3423 self.assertEqual(len(self.builds), 1)
3424 self.assertEqual(self.builds[0].name, 'project-merge')
3425
James E. Blair08d19992016-08-10 15:25:31 -07003426 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003427 self.waitUntilSettled()
3428
3429 # After successful merge job the test jobs for C are queued.
3430 self.assertEqual(len(self.builds), 2)
3431 self.assertEqual(self.builds[0].name, 'project-test1')
3432 self.assertEqual(self.builds[1].name, 'project-test2')
3433
James E. Blair08d19992016-08-10 15:25:31 -07003434 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003435 self.waitUntilSettled()
3436
3437 # C successfully merged so window is bumped to 3.
3438 self.assertEqual(queue.window, 3)
3439 self.assertEqual(queue.window_floor, 1)
3440 self.assertEqual(C.data['status'], 'MERGED')
3441
James E. Blairec056492016-07-22 09:45:56 -07003442 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003443 def test_queue_rate_limiting_dependent(self):
3444 "Test that DependentPipelines are rate limited with dep in window"
James E. Blairf84026c2015-12-08 16:11:46 -08003445 self.updateConfigLayout(
3446 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003447 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07003448 self.launch_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003449 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3450 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3451 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3452
3453 B.setDependsOn(A, 1)
3454
James E. Blair08d19992016-08-10 15:25:31 -07003455 self.launch_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003456
James E. Blair8b5408c2016-08-08 15:37:46 -07003457 A.addApproval('code-review', 2)
3458 B.addApproval('code-review', 2)
3459 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003460
James E. Blair8b5408c2016-08-08 15:37:46 -07003461 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3462 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3463 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003464 self.waitUntilSettled()
3465
3466 # Only A and B will have their merge jobs queued because
3467 # window is 2.
3468 self.assertEqual(len(self.builds), 2)
3469 self.assertEqual(self.builds[0].name, 'project-merge')
3470 self.assertEqual(self.builds[1].name, 'project-merge')
3471
James E. Blair08d19992016-08-10 15:25:31 -07003472 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003473 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003474 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003475 self.waitUntilSettled()
3476
3477 # Only A and B will have their test jobs queued because
3478 # window is 2.
3479 self.assertEqual(len(self.builds), 4)
3480 self.assertEqual(self.builds[0].name, 'project-test1')
3481 self.assertEqual(self.builds[1].name, 'project-test2')
3482 self.assertEqual(self.builds[2].name, 'project-test1')
3483 self.assertEqual(self.builds[3].name, 'project-test2')
3484
James E. Blair08d19992016-08-10 15:25:31 -07003485 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003486 self.waitUntilSettled()
3487
3488 queue = self.sched.layout.pipelines['gate'].queues[0]
3489 # A failed so window is reduced by 1 to 1.
3490 self.assertEqual(queue.window, 1)
3491 self.assertEqual(queue.window_floor, 1)
3492 self.assertEqual(A.data['status'], 'NEW')
3493 self.assertEqual(B.data['status'], 'NEW')
3494
3495 # Gate is reset and only C's merge job is queued because
3496 # window shrunk to 1 and A and B were dequeued.
3497 self.assertEqual(len(self.builds), 1)
3498 self.assertEqual(self.builds[0].name, 'project-merge')
3499
James E. Blair08d19992016-08-10 15:25:31 -07003500 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003501 self.waitUntilSettled()
3502
3503 # Only C's test jobs are queued because window is still 1.
3504 self.assertEqual(len(self.builds), 2)
3505 self.assertEqual(self.builds[0].name, 'project-test1')
3506 self.assertEqual(self.builds[1].name, 'project-test2')
3507
James E. Blair08d19992016-08-10 15:25:31 -07003508 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003509 self.waitUntilSettled()
3510
3511 # C was successfully merged so window is increased to 2.
3512 self.assertEqual(queue.window, 2)
3513 self.assertEqual(queue.window_floor, 1)
3514 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003515
James E. Blairec056492016-07-22 09:45:56 -07003516 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003517 def test_worker_update_metadata(self):
3518 "Test if a worker can send back metadata about itself"
James E. Blair08d19992016-08-10 15:25:31 -07003519 self.launch_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003520
3521 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003522 A.addApproval('code-review', 2)
3523 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003524 self.waitUntilSettled()
3525
3526 self.assertEqual(len(self.launcher.builds), 1)
3527
3528 self.log.debug('Current builds:')
3529 self.log.debug(self.launcher.builds)
3530
3531 start = time.time()
3532 while True:
3533 if time.time() - start > 10:
3534 raise Exception("Timeout waiting for gearman server to report "
3535 + "back to the client")
3536 build = self.launcher.builds.values()[0]
3537 if build.worker.name == "My Worker":
3538 break
3539 else:
3540 time.sleep(0)
3541
3542 self.log.debug(build)
3543 self.assertEqual("My Worker", build.worker.name)
3544 self.assertEqual("localhost", build.worker.hostname)
3545 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3546 self.assertEqual("zuul.example.org", build.worker.fqdn)
3547 self.assertEqual("FakeBuilder", build.worker.program)
3548 self.assertEqual("v1.1", build.worker.version)
3549 self.assertEqual({'something': 'else'}, build.worker.extra)
3550
James E. Blair08d19992016-08-10 15:25:31 -07003551 self.launch_server.hold_jobs_in_build = False
3552 self.launch_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003553 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003554
James E. Blairec056492016-07-22 09:45:56 -07003555 @skip("Disabled for early v3 development")
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003556 def test_footer_message(self):
3557 "Test a pipeline's footer message is correctly added to the report."
James E. Blairf84026c2015-12-08 16:11:46 -08003558 self.updateConfigLayout(
3559 'tests/fixtures/layout-footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003560 self.sched.reconfigure(self.config)
3561 self.registerJobs()
3562
3563 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003564 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07003565 self.launch_server.failJob('test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003566 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003567 self.waitUntilSettled()
3568
3569 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003570 B.addApproval('code-review', 2)
3571 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003572 self.waitUntilSettled()
3573
3574 self.assertEqual(2, len(self.smtp_messages))
3575
3576 failure_body = """\
3577Build failed. For information on how to proceed, see \
3578http://wiki.example.org/Test_Failures
3579
3580- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
3581- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
3582
3583For CI problems and help debugging, contact ci@example.org"""
3584
3585 success_body = """\
3586Build succeeded.
3587
3588- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
3589- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
3590
3591For CI problems and help debugging, contact ci@example.org"""
3592
3593 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
3594 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11003595
James E. Blairec056492016-07-22 09:45:56 -07003596 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003597 def test_merge_failure_reporters(self):
3598 """Check that the config is set up correctly"""
3599
James E. Blairf84026c2015-12-08 16:11:46 -08003600 self.updateConfigLayout(
3601 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003602 self.sched.reconfigure(self.config)
3603 self.registerJobs()
3604
3605 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003606 "Merge Failed.\n\nThis change or one of its cross-repo "
3607 "dependencies was unable to be automatically merged with the "
3608 "current state of its repository. Please rebase the change and "
3609 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003610 self.sched.layout.pipelines['check'].merge_failure_message)
3611 self.assertEqual(
3612 "The merge failed! For more information...",
3613 self.sched.layout.pipelines['gate'].merge_failure_message)
3614
3615 self.assertEqual(
3616 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3617 self.assertEqual(
3618 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3619
3620 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003621 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3622 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003623
3624 self.assertTrue(
3625 (
3626 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003627 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003628 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003629 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003630 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003631 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003632 ) or (
3633 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003634 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003635 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003636 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003637 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003638 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003639 )
3640 )
3641
James E. Blairec056492016-07-22 09:45:56 -07003642 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003643 def test_merge_failure_reports(self):
3644 """Check that when a change fails to merge the correct message is sent
3645 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003646 self.updateConfigLayout(
3647 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003648 self.sched.reconfigure(self.config)
3649 self.registerJobs()
3650
3651 # Check a test failure isn't reported to SMTP
3652 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003653 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07003654 self.launch_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003655 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003656 self.waitUntilSettled()
3657
3658 self.assertEqual(3, len(self.history)) # 3 jobs
3659 self.assertEqual(0, len(self.smtp_messages))
3660
3661 # Check a merge failure is reported to SMTP
3662 # B should be merged, but C will conflict with B
3663 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3664 B.addPatchset(['conflict'])
3665 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3666 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003667 B.addApproval('code-review', 2)
3668 C.addApproval('code-review', 2)
3669 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3670 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003671 self.waitUntilSettled()
3672
3673 self.assertEqual(6, len(self.history)) # A and B jobs
3674 self.assertEqual(1, len(self.smtp_messages))
3675 self.assertEqual('The merge failed! For more information...',
3676 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003677
James E. Blairec056492016-07-22 09:45:56 -07003678 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003679 def test_default_merge_failure_reports(self):
3680 """Check that the default merge failure reports are correct."""
3681
3682 # A should report success, B should report merge failure.
3683 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3684 A.addPatchset(['conflict'])
3685 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3686 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003687 A.addApproval('code-review', 2)
3688 B.addApproval('code-review', 2)
3689 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3690 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003691 self.waitUntilSettled()
3692
3693 self.assertEqual(3, len(self.history)) # A jobs
3694 self.assertEqual(A.reported, 2)
3695 self.assertEqual(B.reported, 2)
3696 self.assertEqual(A.data['status'], 'MERGED')
3697 self.assertEqual(B.data['status'], 'NEW')
3698 self.assertIn('Build succeeded', A.messages[1])
3699 self.assertIn('Merge Failed', B.messages[1])
3700 self.assertIn('automatically merged', B.messages[1])
3701 self.assertNotIn('logs.example.com', B.messages[1])
3702 self.assertNotIn('SKIPPED', B.messages[1])
3703
James E. Blairec056492016-07-22 09:45:56 -07003704 @skip("Disabled for early v3 development")
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003705 def test_swift_instructions(self):
3706 "Test that the correct swift instructions are sent to the workers"
James E. Blairf84026c2015-12-08 16:11:46 -08003707 self.updateConfigLayout(
3708 'tests/fixtures/layout-swift.yaml')
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003709 self.sched.reconfigure(self.config)
3710 self.registerJobs()
3711
James E. Blair08d19992016-08-10 15:25:31 -07003712 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003713 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3714
James E. Blair8b5408c2016-08-08 15:37:46 -07003715 A.addApproval('code-review', 2)
3716 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003717 self.waitUntilSettled()
3718
3719 self.assertEqual(
3720 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
3721 "gate/test-merge/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003722 self.builds[0].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003723 self.assertEqual(5,
3724 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
3725 split('\n')))
3726 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
3727
3728 self.assertEqual(
3729 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
3730 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003731 self.builds[1].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003732 self.assertEqual(5,
3733 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
3734 split('\n')))
3735 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
3736
3737 self.assertEqual(
3738 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
3739 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003740 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003741 self.assertEqual(5,
3742 len(self.builds[1].
3743 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
3744 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
3745
James E. Blair08d19992016-08-10 15:25:31 -07003746 self.launch_server.hold_jobs_in_build = False
3747 self.launch_server.release()
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003748 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003749
James E. Blairec056492016-07-22 09:45:56 -07003750 @skip("Disabled for early v3 development")
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003751 def test_client_get_running_jobs(self):
3752 "Test that the RPC client can get a list of running jobs"
James E. Blair08d19992016-08-10 15:25:31 -07003753 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003754 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003755 A.addApproval('code-review', 2)
3756 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003757 self.waitUntilSettled()
3758
3759 client = zuul.rpcclient.RPCClient('127.0.0.1',
3760 self.gearman_server.port)
3761
3762 # Wait for gearman server to send the initial workData back to zuul
3763 start = time.time()
3764 while True:
3765 if time.time() - start > 10:
3766 raise Exception("Timeout waiting for gearman server to report "
3767 + "back to the client")
3768 build = self.launcher.builds.values()[0]
3769 if build.worker.name == "My Worker":
3770 break
3771 else:
3772 time.sleep(0)
3773
3774 running_items = client.get_running_jobs()
3775
3776 self.assertEqual(1, len(running_items))
3777 running_item = running_items[0]
3778 self.assertEqual([], running_item['failing_reasons'])
3779 self.assertEqual([], running_item['items_behind'])
3780 self.assertEqual('https://hostname/1', running_item['url'])
3781 self.assertEqual(None, running_item['item_ahead'])
3782 self.assertEqual('org/project', running_item['project'])
3783 self.assertEqual(None, running_item['remaining_time'])
3784 self.assertEqual(True, running_item['active'])
3785 self.assertEqual('1,1', running_item['id'])
3786
3787 self.assertEqual(3, len(running_item['jobs']))
3788 for job in running_item['jobs']:
3789 if job['name'] == 'project-merge':
3790 self.assertEqual('project-merge', job['name'])
3791 self.assertEqual('gate', job['pipeline'])
3792 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003793 self.assertEqual('https://server/job/project-merge/0/',
3794 job['url'])
3795 self.assertEqual(7, len(job['worker']))
3796 self.assertEqual(False, job['canceled'])
3797 self.assertEqual(True, job['voting'])
3798 self.assertEqual(None, job['result'])
3799 self.assertEqual('gate', job['pipeline'])
3800 break
3801
James E. Blair08d19992016-08-10 15:25:31 -07003802 self.launch_server.hold_jobs_in_build = False
3803 self.launch_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003804 self.waitUntilSettled()
3805
3806 running_items = client.get_running_jobs()
3807 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003808
3809 def test_nonvoting_pipeline(self):
3810 "Test that a nonvoting pipeline (experimental) can still report"
3811
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003812 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3813 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003814 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3815 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003816
3817 self.assertEqual(self.getJobFromHistory('project-merge').result,
3818 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003819 self.assertEqual(
3820 self.getJobFromHistory('experimental-project-test').result,
3821 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003822 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003823
3824 def test_crd_gate(self):
3825 "Test cross-repo dependencies"
3826 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3827 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003828 A.addApproval('code-review', 2)
3829 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003830
3831 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3832 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3833 AM2.setMerged()
3834 AM1.setMerged()
3835
3836 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3837 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3838 BM2.setMerged()
3839 BM1.setMerged()
3840
3841 # A -> AM1 -> AM2
3842 # B -> BM1 -> BM2
3843 # A Depends-On: B
3844 # M2 is here to make sure it is never queried. If it is, it
3845 # means zuul is walking down the entire history of merged
3846 # changes.
3847
3848 B.setDependsOn(BM1, 1)
3849 BM1.setDependsOn(BM2, 1)
3850
3851 A.setDependsOn(AM1, 1)
3852 AM1.setDependsOn(AM2, 1)
3853
3854 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3855 A.subject, B.data['id'])
3856
James E. Blair8b5408c2016-08-08 15:37:46 -07003857 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003858 self.waitUntilSettled()
3859
3860 self.assertEqual(A.data['status'], 'NEW')
3861 self.assertEqual(B.data['status'], 'NEW')
3862
Paul Belanger5dccbe72016-11-14 11:17:37 -05003863 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003864 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003865
James E. Blair08d19992016-08-10 15:25:31 -07003866 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003867 B.addApproval('approved', 1)
3868 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003869 self.waitUntilSettled()
3870
James E. Blair08d19992016-08-10 15:25:31 -07003871 self.launch_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003872 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003873 self.launch_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003874 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003875 self.launch_server.hold_jobs_in_build = False
3876 self.launch_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003877 self.waitUntilSettled()
3878
3879 self.assertEqual(AM2.queried, 0)
3880 self.assertEqual(BM2.queried, 0)
3881 self.assertEqual(A.data['status'], 'MERGED')
3882 self.assertEqual(B.data['status'], 'MERGED')
3883 self.assertEqual(A.reported, 2)
3884 self.assertEqual(B.reported, 2)
3885
Paul Belanger5dccbe72016-11-14 11:17:37 -05003886 changes = self.getJobFromHistory(
3887 'project-merge', 'org/project1').changes
3888 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003889
3890 def test_crd_branch(self):
3891 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003892
3893 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003894 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3895 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3896 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3897 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003898 A.addApproval('code-review', 2)
3899 B.addApproval('code-review', 2)
3900 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003901
3902 # A Depends-On: B+C
3903 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3904 A.subject, B.data['id'])
3905
James E. Blair08d19992016-08-10 15:25:31 -07003906 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003907 B.addApproval('approved', 1)
3908 C.addApproval('approved', 1)
3909 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003910 self.waitUntilSettled()
3911
James E. Blair08d19992016-08-10 15:25:31 -07003912 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003913 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003914 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003915 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003916 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003917 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003918 self.launch_server.hold_jobs_in_build = False
3919 self.launch_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003920 self.waitUntilSettled()
3921
3922 self.assertEqual(A.data['status'], 'MERGED')
3923 self.assertEqual(B.data['status'], 'MERGED')
3924 self.assertEqual(C.data['status'], 'MERGED')
3925 self.assertEqual(A.reported, 2)
3926 self.assertEqual(B.reported, 2)
3927 self.assertEqual(C.reported, 2)
3928
Paul Belanger6379db12016-11-14 13:57:54 -05003929 changes = self.getJobFromHistory(
3930 'project-merge', 'org/project1').changes
3931 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003932
3933 def test_crd_multiline(self):
3934 "Test multiple depends-on lines in commit"
3935 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3936 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3937 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003938 A.addApproval('code-review', 2)
3939 B.addApproval('code-review', 2)
3940 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003941
3942 # A Depends-On: B+C
3943 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3944 A.subject, B.data['id'], C.data['id'])
3945
James E. Blair08d19992016-08-10 15:25:31 -07003946 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003947 B.addApproval('approved', 1)
3948 C.addApproval('approved', 1)
3949 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003950 self.waitUntilSettled()
3951
James E. Blair08d19992016-08-10 15:25:31 -07003952 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003953 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003954 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003955 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003956 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003957 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003958 self.launch_server.hold_jobs_in_build = False
3959 self.launch_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003960 self.waitUntilSettled()
3961
3962 self.assertEqual(A.data['status'], 'MERGED')
3963 self.assertEqual(B.data['status'], 'MERGED')
3964 self.assertEqual(C.data['status'], 'MERGED')
3965 self.assertEqual(A.reported, 2)
3966 self.assertEqual(B.reported, 2)
3967 self.assertEqual(C.reported, 2)
3968
Paul Belanger5dccbe72016-11-14 11:17:37 -05003969 changes = self.getJobFromHistory(
3970 'project-merge', 'org/project1').changes
3971 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003972
3973 def test_crd_unshared_gate(self):
3974 "Test cross-repo dependencies in unshared gate queues"
3975 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3976 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003977 A.addApproval('code-review', 2)
3978 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003979
3980 # A Depends-On: B
3981 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3982 A.subject, B.data['id'])
3983
3984 # A and B do not share a queue, make sure that A is unable to
3985 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003986 B.addApproval('approved', 1)
3987 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003988 self.waitUntilSettled()
3989
3990 self.assertEqual(A.data['status'], 'NEW')
3991 self.assertEqual(B.data['status'], 'NEW')
3992 self.assertEqual(A.reported, 0)
3993 self.assertEqual(B.reported, 0)
3994 self.assertEqual(len(self.history), 0)
3995
3996 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003997 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003998 self.waitUntilSettled()
3999
4000 self.assertEqual(B.data['status'], 'MERGED')
4001 self.assertEqual(B.reported, 2)
4002
4003 # Now that B is merged, A should be able to be enqueued and
4004 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07004005 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004006 self.waitUntilSettled()
4007
4008 self.assertEqual(A.data['status'], 'MERGED')
4009 self.assertEqual(A.reported, 2)
4010
James E. Blair96698e22015-04-02 07:48:21 -07004011 def test_crd_gate_reverse(self):
4012 "Test reverse cross-repo dependencies"
4013 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4014 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004015 A.addApproval('code-review', 2)
4016 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07004017
4018 # A Depends-On: B
4019
4020 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4021 A.subject, B.data['id'])
4022
James E. Blair8b5408c2016-08-08 15:37:46 -07004023 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004024 self.waitUntilSettled()
4025
4026 self.assertEqual(A.data['status'], 'NEW')
4027 self.assertEqual(B.data['status'], 'NEW')
4028
James E. Blair08d19992016-08-10 15:25:31 -07004029 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07004030 A.addApproval('approved', 1)
4031 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004032 self.waitUntilSettled()
4033
James E. Blair08d19992016-08-10 15:25:31 -07004034 self.launch_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004035 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07004036 self.launch_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004037 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07004038 self.launch_server.hold_jobs_in_build = False
4039 self.launch_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07004040 self.waitUntilSettled()
4041
4042 self.assertEqual(A.data['status'], 'MERGED')
4043 self.assertEqual(B.data['status'], 'MERGED')
4044 self.assertEqual(A.reported, 2)
4045 self.assertEqual(B.reported, 2)
4046
Paul Belanger5dccbe72016-11-14 11:17:37 -05004047 changes = self.getJobFromHistory(
4048 'project-merge', 'org/project1').changes
4049 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07004050
James E. Blair5ee24252014-12-30 10:12:29 -08004051 def test_crd_cycle(self):
4052 "Test cross-repo dependency cycles"
4053 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4054 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004055 A.addApproval('code-review', 2)
4056 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004057
4058 # A -> B -> A (via commit-depends)
4059
4060 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4061 A.subject, B.data['id'])
4062 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4063 B.subject, A.data['id'])
4064
James E. Blair8b5408c2016-08-08 15:37:46 -07004065 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004066 self.waitUntilSettled()
4067
4068 self.assertEqual(A.reported, 0)
4069 self.assertEqual(B.reported, 0)
4070 self.assertEqual(A.data['status'], 'NEW')
4071 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004072
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004073 def test_crd_gate_unknown(self):
4074 "Test unknown projects in dependent pipeline"
4075 self.init_repo("org/unknown")
4076 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4077 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004078 A.addApproval('code-review', 2)
4079 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004080
4081 # A Depends-On: B
4082 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4083 A.subject, B.data['id'])
4084
James E. Blair8b5408c2016-08-08 15:37:46 -07004085 B.addApproval('approved', 1)
4086 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004087 self.waitUntilSettled()
4088
4089 # Unknown projects cannot share a queue with any other
4090 # since they don't have common jobs with any other (they have no jobs).
4091 # Changes which depend on unknown project changes
4092 # should not be processed in dependent pipeline
4093 self.assertEqual(A.data['status'], 'NEW')
4094 self.assertEqual(B.data['status'], 'NEW')
4095 self.assertEqual(A.reported, 0)
4096 self.assertEqual(B.reported, 0)
4097 self.assertEqual(len(self.history), 0)
4098
4099 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07004100 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004101 B.setMerged()
4102 self.waitUntilSettled()
4103 self.assertEqual(len(self.history), 0)
4104
4105 # Now that B is merged, A should be able to be enqueued and
4106 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07004107 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004108 self.waitUntilSettled()
4109
4110 self.assertEqual(A.data['status'], 'MERGED')
4111 self.assertEqual(A.reported, 2)
4112 self.assertEqual(B.data['status'], 'MERGED')
4113 self.assertEqual(B.reported, 0)
4114
James E. Blairbfb8e042014-12-30 17:01:44 -08004115 def test_crd_check(self):
4116 "Test cross-repo dependencies in independent pipelines"
4117
Paul Belangerb30342b2016-11-14 12:30:43 -05004118 self.launch_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004119 self.gearman_server.hold_jobs_in_queue = True
4120 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4121 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4122
4123 # A Depends-On: B
4124 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4125 A.subject, B.data['id'])
4126
4127 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4128 self.waitUntilSettled()
4129
4130 queue = self.gearman_server.getQueue()
4131 ref = self.getParameter(queue[-1], 'ZUUL_REF')
4132 self.gearman_server.hold_jobs_in_queue = False
4133 self.gearman_server.release()
4134 self.waitUntilSettled()
4135
Paul Belangerb30342b2016-11-14 12:30:43 -05004136 self.launch_server.release('.*-merge')
4137 self.waitUntilSettled()
4138
4139 path = os.path.join(self.builds[0].jobdir.git_root, "org/project1")
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', 'A-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 path = os.path.join(self.builds[0].jobdir.git_root, "org/project2")
James E. Blairbfb8e042014-12-30 17:01:44 -08004148 repo = git.Repo(path)
4149 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4150 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004151 correct_messages = [
4152 'initial commit', 'add content from fixture', 'B-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004153 self.assertEqual(repo_messages, correct_messages)
4154
Paul Belangerb30342b2016-11-14 12:30:43 -05004155 self.launch_server.hold_jobs_in_build = False
4156 self.launch_server.release()
4157 self.waitUntilSettled()
4158
James E. Blairbfb8e042014-12-30 17:01:44 -08004159 self.assertEqual(A.data['status'], 'NEW')
4160 self.assertEqual(B.data['status'], 'NEW')
4161 self.assertEqual(A.reported, 1)
4162 self.assertEqual(B.reported, 0)
4163
4164 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004165 tenant = self.sched.abide.tenants.get('tenant-one')
4166 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004167
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004168 def test_crd_check_git_depends(self):
4169 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004170 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004171 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4172 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4173
4174 # Add two git-dependent changes and make sure they both report
4175 # success.
4176 B.setDependsOn(A, 1)
4177 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4178 self.waitUntilSettled()
4179 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4180 self.waitUntilSettled()
4181
James E. Blairb8c16472015-05-05 14:55:26 -07004182 self.orderedRelease()
4183 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004184 self.waitUntilSettled()
4185
4186 self.assertEqual(A.data['status'], 'NEW')
4187 self.assertEqual(B.data['status'], 'NEW')
4188 self.assertEqual(A.reported, 1)
4189 self.assertEqual(B.reported, 1)
4190
4191 self.assertEqual(self.history[0].changes, '1,1')
4192 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004193 tenant = self.sched.abide.tenants.get('tenant-one')
4194 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004195
4196 self.assertIn('Build succeeded', A.messages[0])
4197 self.assertIn('Build succeeded', B.messages[0])
4198
4199 def test_crd_check_duplicate(self):
4200 "Test duplicate check in independent pipelines"
James E. Blair08d19992016-08-10 15:25:31 -07004201 self.launch_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004202 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4203 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004204 tenant = self.sched.abide.tenants.get('tenant-one')
4205 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004206
4207 # Add two git-dependent changes...
4208 B.setDependsOn(A, 1)
4209 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4210 self.waitUntilSettled()
4211 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4212
4213 # ...make sure the live one is not duplicated...
4214 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4215 self.waitUntilSettled()
4216 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4217
4218 # ...but the non-live one is able to be.
4219 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4220 self.waitUntilSettled()
4221 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4222
Clark Boylandd849822015-03-02 12:38:14 -08004223 # Release jobs in order to avoid races with change A jobs
4224 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004225 self.orderedRelease()
James E. Blair08d19992016-08-10 15:25:31 -07004226 self.launch_server.hold_jobs_in_build = False
4227 self.launch_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004228 self.waitUntilSettled()
4229
4230 self.assertEqual(A.data['status'], 'NEW')
4231 self.assertEqual(B.data['status'], 'NEW')
4232 self.assertEqual(A.reported, 1)
4233 self.assertEqual(B.reported, 1)
4234
4235 self.assertEqual(self.history[0].changes, '1,1 2,1')
4236 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004237 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004238
4239 self.assertIn('Build succeeded', A.messages[0])
4240 self.assertIn('Build succeeded', B.messages[0])
4241
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004242 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004243 "Test cross-repo dependencies re-enqueued in independent pipelines"
4244
4245 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004246 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4247 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004248
4249 # A Depends-On: B
4250 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4251 A.subject, B.data['id'])
4252
4253 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4254 self.waitUntilSettled()
4255
4256 self.sched.reconfigure(self.config)
4257
4258 # Make sure the items still share a change queue, and the
4259 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004260 tenant = self.sched.abide.tenants.get('tenant-one')
4261 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4262 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004263 first_item = queue.queue[0]
4264 for item in queue.queue:
4265 self.assertEqual(item.queue, first_item.queue)
4266 self.assertFalse(first_item.live)
4267 self.assertTrue(queue.queue[1].live)
4268
4269 self.gearman_server.hold_jobs_in_queue = False
4270 self.gearman_server.release()
4271 self.waitUntilSettled()
4272
4273 self.assertEqual(A.data['status'], 'NEW')
4274 self.assertEqual(B.data['status'], 'NEW')
4275 self.assertEqual(A.reported, 1)
4276 self.assertEqual(B.reported, 0)
4277
4278 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004279 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004280
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004281 def test_crd_check_reconfiguration(self):
4282 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4283
4284 def test_crd_undefined_project(self):
4285 """Test that undefined projects in dependencies are handled for
4286 independent pipelines"""
4287 # It's a hack for fake gerrit,
4288 # as it implies repo creation upon the creation of any change
4289 self.init_repo("org/unknown")
4290 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4291
James E. Blairec056492016-07-22 09:45:56 -07004292 @skip("Disabled for early v3 development")
James E. Blair17dd6772015-02-09 14:45:18 -08004293 def test_crd_check_ignore_dependencies(self):
4294 "Test cross-repo dependencies can be ignored"
James E. Blairf84026c2015-12-08 16:11:46 -08004295 self.updateConfigLayout(
4296 'tests/fixtures/layout-ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004297 self.sched.reconfigure(self.config)
4298 self.registerJobs()
4299
4300 self.gearman_server.hold_jobs_in_queue = True
4301 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4302 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4303 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4304
4305 # A Depends-On: B
4306 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4307 A.subject, B.data['id'])
4308 # C git-depends on B
4309 C.setDependsOn(B, 1)
4310 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4311 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4312 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4313 self.waitUntilSettled()
4314
4315 # Make sure none of the items share a change queue, and all
4316 # are live.
4317 check_pipeline = self.sched.layout.pipelines['check']
4318 self.assertEqual(len(check_pipeline.queues), 3)
4319 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4320 for item in check_pipeline.getAllItems():
4321 self.assertTrue(item.live)
4322
4323 self.gearman_server.hold_jobs_in_queue = False
4324 self.gearman_server.release()
4325 self.waitUntilSettled()
4326
4327 self.assertEqual(A.data['status'], 'NEW')
4328 self.assertEqual(B.data['status'], 'NEW')
4329 self.assertEqual(C.data['status'], 'NEW')
4330 self.assertEqual(A.reported, 1)
4331 self.assertEqual(B.reported, 1)
4332 self.assertEqual(C.reported, 1)
4333
4334 # Each job should have tested exactly one change
4335 for job in self.history:
4336 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004337
James E. Blairec056492016-07-22 09:45:56 -07004338 @skip("Disabled for early v3 development")
James E. Blair96698e22015-04-02 07:48:21 -07004339 def test_crd_check_transitive(self):
4340 "Test transitive cross-repo dependencies"
4341 # Specifically, if A -> B -> C, and C gets a new patchset and
4342 # A gets a new patchset, ensure the test of A,2 includes B,1
4343 # and C,2 (not C,1 which would indicate stale data in the
4344 # cache for B).
4345 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4346 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4347 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4348
4349 # A Depends-On: B
4350 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4351 A.subject, B.data['id'])
4352
4353 # B Depends-On: C
4354 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4355 B.subject, C.data['id'])
4356
4357 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4358 self.waitUntilSettled()
4359 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4360
4361 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4362 self.waitUntilSettled()
4363 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4364
4365 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4366 self.waitUntilSettled()
4367 self.assertEqual(self.history[-1].changes, '3,1')
4368
4369 C.addPatchset()
4370 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4371 self.waitUntilSettled()
4372 self.assertEqual(self.history[-1].changes, '3,2')
4373
4374 A.addPatchset()
4375 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4376 self.waitUntilSettled()
4377 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004378
James E. Blaira8b90b32016-08-24 15:18:50 -07004379 def test_crd_check_unknown(self):
4380 "Test unknown projects in independent pipeline"
4381 self.init_repo("org/unknown")
4382 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4383 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4384 # A Depends-On: B
4385 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4386 A.subject, B.data['id'])
4387
4388 # Make sure zuul has seen an event on B.
4389 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4390 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4391 self.waitUntilSettled()
4392
4393 self.assertEqual(A.data['status'], 'NEW')
4394 self.assertEqual(A.reported, 1)
4395 self.assertEqual(B.data['status'], 'NEW')
4396 self.assertEqual(B.reported, 0)
4397
James E. Blair92464a22016-04-05 10:21:26 -07004398 def test_crd_cycle_join(self):
4399 "Test an updated change creates a cycle"
4400 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4401
4402 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4403 self.waitUntilSettled()
4404
4405 # Create B->A
4406 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4407 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4408 B.subject, A.data['id'])
4409 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4410 self.waitUntilSettled()
4411
4412 # Update A to add A->B (a cycle).
4413 A.addPatchset()
4414 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4415 A.subject, B.data['id'])
4416 # Normally we would submit the patchset-created event for
4417 # processing here, however, we have no way of noting whether
4418 # the dependency cycle detection correctly raised an
4419 # exception, so instead, we reach into the source driver and
4420 # call the method that would ultimately be called by the event
4421 # processing.
4422
Paul Belanger0e155e22016-11-14 14:12:23 -05004423 tenant = self.sched.abide.tenants.get('tenant-one')
4424 source = tenant.layout.pipelines['gate'].source
4425
4426 # TODO(pabelanger): As we add more source / trigger APIs we should make
4427 # it easier for users to create events for testing.
4428 event = zuul.model.TriggerEvent()
4429 event.trigger_name = 'gerrit'
4430 event.change_number = '1'
4431 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004432 with testtools.ExpectedException(
4433 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004434 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004435 self.log.debug("Got expected dependency cycle exception")
4436
4437 # Now if we update B to remove the depends-on, everything
4438 # should be okay. B; A->B
4439
4440 B.addPatchset()
4441 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004442
4443 source.getChange(event, True)
4444 event.change_number = '2'
4445 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004446
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004447 def test_disable_at(self):
4448 "Test a pipeline will only report to the disabled trigger when failing"
4449
Paul Belanger7dc76e82016-11-11 16:51:08 -05004450 self.updateConfigLayout('layout-disabled-at')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004451 self.sched.reconfigure(self.config)
4452
Paul Belanger7dc76e82016-11-11 16:51:08 -05004453 tenant = self.sched.abide.tenants.get('openstack')
4454 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004455 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004456 0, tenant.layout.pipelines['check']._consecutive_failures)
4457 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004458
4459 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4460 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4461 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4462 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4463 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4464 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4465 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4466 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4467 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4468 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4469 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4470
James E. Blair08d19992016-08-10 15:25:31 -07004471 self.launch_server.failJob('project-test1', A)
4472 self.launch_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004473 # Let C pass, resetting the counter
James E. Blair08d19992016-08-10 15:25:31 -07004474 self.launch_server.failJob('project-test1', D)
4475 self.launch_server.failJob('project-test1', E)
4476 self.launch_server.failJob('project-test1', F)
4477 self.launch_server.failJob('project-test1', G)
4478 self.launch_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004479 # I also passes but should only report to the disabled reporters
James E. Blair08d19992016-08-10 15:25:31 -07004480 self.launch_server.failJob('project-test1', J)
4481 self.launch_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004482
4483 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4484 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4485 self.waitUntilSettled()
4486
4487 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004488 2, tenant.layout.pipelines['check']._consecutive_failures)
4489 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004490
4491 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4492 self.waitUntilSettled()
4493
4494 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004495 0, tenant.layout.pipelines['check']._consecutive_failures)
4496 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004497
4498 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4499 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4500 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4501 self.waitUntilSettled()
4502
4503 # We should be disabled now
4504 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004505 3, tenant.layout.pipelines['check']._consecutive_failures)
4506 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004507
4508 # We need to wait between each of these patches to make sure the
4509 # smtp messages come back in an expected order
4510 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4511 self.waitUntilSettled()
4512 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4513 self.waitUntilSettled()
4514 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4515 self.waitUntilSettled()
4516
4517 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4518 # leaving a message on each change
4519 self.assertEqual(1, len(A.messages))
4520 self.assertIn('Build failed.', A.messages[0])
4521 self.assertEqual(1, len(B.messages))
4522 self.assertIn('Build failed.', B.messages[0])
4523 self.assertEqual(1, len(C.messages))
4524 self.assertIn('Build succeeded.', C.messages[0])
4525 self.assertEqual(1, len(D.messages))
4526 self.assertIn('Build failed.', D.messages[0])
4527 self.assertEqual(1, len(E.messages))
4528 self.assertIn('Build failed.', E.messages[0])
4529 self.assertEqual(1, len(F.messages))
4530 self.assertIn('Build failed.', F.messages[0])
4531
4532 # The last 3 (GHI) would have only reported via smtp.
4533 self.assertEqual(3, len(self.smtp_messages))
4534 self.assertEqual(0, len(G.messages))
4535 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004536 self.assertIn(
4537 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004538 self.assertEqual(0, len(H.messages))
4539 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004540 self.assertIn(
4541 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004542 self.assertEqual(0, len(I.messages))
4543 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004544 self.assertIn(
4545 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004546
4547 # Now reload the configuration (simulate a HUP) to check the pipeline
4548 # comes out of disabled
4549 self.sched.reconfigure(self.config)
4550
Paul Belanger7dc76e82016-11-11 16:51:08 -05004551 tenant = self.sched.abide.tenants.get('openstack')
4552
4553 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004554 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004555 0, tenant.layout.pipelines['check']._consecutive_failures)
4556 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004557
4558 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4559 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4560 self.waitUntilSettled()
4561
4562 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004563 2, tenant.layout.pipelines['check']._consecutive_failures)
4564 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004565
4566 # J and K went back to gerrit
4567 self.assertEqual(1, len(J.messages))
4568 self.assertIn('Build failed.', J.messages[0])
4569 self.assertEqual(1, len(K.messages))
4570 self.assertIn('Build failed.', K.messages[0])
4571 # No more messages reported via smtp
4572 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004573
James E. Blairec056492016-07-22 09:45:56 -07004574 @skip("Disabled for early v3 development")
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004575 def test_success_pattern(self):
4576 "Ensure bad build params are ignored"
4577
4578 # Use SMTP reporter to grab the result message easier
4579 self.init_repo("org/docs")
4580 self.config.set('zuul', 'layout_config',
4581 'tests/fixtures/layout-success-pattern.yaml')
4582 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07004583 self.launch_server.hold_jobs_in_build = True
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004584 self.registerJobs()
4585
4586 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4587 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4588 self.waitUntilSettled()
4589
4590 # Grab build id
4591 self.assertEqual(len(self.builds), 1)
4592 uuid = self.builds[0].unique[:7]
4593
James E. Blair08d19992016-08-10 15:25:31 -07004594 self.launch_server.hold_jobs_in_build = False
4595 self.launch_server.release()
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004596 self.waitUntilSettled()
4597
4598 self.assertEqual(len(self.smtp_messages), 1)
4599 body = self.smtp_messages[0]['body'].splitlines()
4600 self.assertEqual('Build succeeded.', body[0])
4601
4602 self.assertIn(
4603 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4604 'docs-draft-test/%s/publish-docs/' % uuid,
4605 body[2])
4606 self.assertIn(
4607 '- docs-draft-test2 https://server/job/docs-draft-test2/1/',
4608 body[3])
Arieb6f068c2016-10-09 13:11:06 +03004609
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004610 @skip("Disabled for early v3 development")
Paul Belanger71d98172016-11-08 10:56:31 -05004611 def test_rerun_on_abort(self):
4612 "Test that if a worker fails to run a job, it is run again"
4613
4614 self.config.set('zuul', 'layout_config',
4615 'tests/fixtures/layout-abort-attempts.yaml')
4616 self.sched.reconfigure(self.config)
4617 self.worker.hold_jobs_in_build = True
4618 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4619 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4620 self.waitUntilSettled()
4621
4622 self.worker.release('.*-merge')
4623 self.waitUntilSettled()
4624
4625 self.assertEqual(len(self.builds), 2)
4626 self.builds[0].requeue = True
4627 self.worker.release('.*-test*')
4628 self.waitUntilSettled()
4629
4630 for x in range(3):
4631 self.assertEqual(len(self.builds), 1)
4632 self.builds[0].requeue = True
4633 self.worker.release('.*-test1')
4634 self.waitUntilSettled()
4635
4636 self.worker.hold_jobs_in_build = False
4637 self.worker.release()
4638 self.waitUntilSettled()
4639 self.assertEqual(len(self.history), 6)
4640 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4641 self.assertEqual(A.reported, 1)
4642 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004643
Arieb6f068c2016-10-09 13:11:06 +03004644
4645class TestDuplicatePipeline(ZuulTestCase):
4646 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4647
4648 def test_duplicate_pipelines(self):
4649 "Test that a change matching multiple pipelines works"
4650
4651 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4652 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4653 self.waitUntilSettled()
4654
4655 self.assertHistory([
4656 dict(name='project-test1', result='SUCCESS', changes='1,1',
4657 pipeline='dup1'),
4658 dict(name='project-test1', result='SUCCESS', changes='1,1',
4659 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004660 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004661
4662 self.assertEqual(len(A.messages), 2)
4663
Paul Belangera46a3742016-11-09 14:23:03 -05004664 if 'dup1' in A.messages[0]:
4665 self.assertIn('dup1', A.messages[0])
4666 self.assertNotIn('dup2', A.messages[0])
4667 self.assertIn('project-test1', A.messages[0])
4668 self.assertIn('dup2', A.messages[1])
4669 self.assertNotIn('dup1', A.messages[1])
4670 self.assertIn('project-test1', A.messages[1])
4671 else:
4672 self.assertIn('dup1', A.messages[1])
4673 self.assertNotIn('dup2', A.messages[1])
4674 self.assertIn('project-test1', A.messages[1])
4675 self.assertIn('dup2', A.messages[0])
4676 self.assertNotIn('dup1', A.messages[0])
4677 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004678
4679
4680class TestSchedulerOneJobProject(ZuulTestCase):
4681 tenant_config_file = 'config/one-job-project/main.yaml'
4682
4683 def test_one_job_project(self):
4684 "Test that queueing works with one job"
4685 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4686 'master', 'A')
4687 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4688 'master', 'B')
4689 A.addApproval('code-review', 2)
4690 B.addApproval('code-review', 2)
4691 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4692 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4693 self.waitUntilSettled()
4694
4695 self.assertEqual(A.data['status'], 'MERGED')
4696 self.assertEqual(A.reported, 2)
4697 self.assertEqual(B.data['status'], 'MERGED')
4698 self.assertEqual(B.reported, 2)
Paul Belanger86085b32016-11-03 12:48:57 -04004699
4700
4701class TestSchedulerTemplatedProject(ZuulTestCase):
4702 tenant_config_file = 'config/templated-project/main.yaml'
4703
4704 def test_job_from_templates_launched(self):
4705 "Test whether a job generated via a template can be launched"
4706
4707 A = self.fake_gerrit.addFakeChange(
4708 'org/templated-project', 'master', 'A')
4709 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4710 self.waitUntilSettled()
4711
4712 self.assertEqual(self.getJobFromHistory('project-test1').result,
4713 'SUCCESS')
4714 self.assertEqual(self.getJobFromHistory('project-test2').result,
4715 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004716
4717 def test_layered_templates(self):
4718 "Test whether a job generated via a template can be launched"
4719
4720 A = self.fake_gerrit.addFakeChange(
4721 'org/layered-project', 'master', 'A')
4722 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4723 self.waitUntilSettled()
4724
4725 self.assertEqual(self.getJobFromHistory('project-test1').result,
4726 'SUCCESS')
4727 self.assertEqual(self.getJobFromHistory('project-test2').result,
4728 'SUCCESS')
4729 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4730 ).result, 'SUCCESS')
4731 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4732 ).result, 'SUCCESS')
4733 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4734 ).result, 'SUCCESS')
4735 self.assertEqual(self.getJobFromHistory('project-test6').result,
4736 'SUCCESS')