blob: 4077cca2d0041e2a62b77ca94743231d08a83880 [file] [log] [blame]
Gregory Haynes4fc12542015-04-22 20:38:06 -07001# Copyright 2015 GoodData
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
James E. Blair21037782017-07-19 11:56:55 -070015import os
Jan Hrubane252a732017-01-03 15:03:09 +010016import re
Jamie Lennox3f16de52017-05-09 14:24:11 +100017from testtools.matchers import MatchesRegex, StartsWith
Tristan Cacqueray2bafb1f2017-06-12 07:10:26 +000018import urllib
Jan Hruban324ca5b2015-11-05 19:28:54 +010019import time
Gregory Haynes4fc12542015-04-22 20:38:06 -070020
James E. Blair21037782017-07-19 11:56:55 -070021import git
22
Wayne1a78c612015-06-11 17:14:13 -070023from tests.base import ZuulTestCase, simple_layout, random_sha1
Gregory Haynes4fc12542015-04-22 20:38:06 -070024
Gregory Haynes4fc12542015-04-22 20:38:06 -070025
26class TestGithubDriver(ZuulTestCase):
27 config_file = 'zuul-github-driver.conf'
28
29 @simple_layout('layouts/basic-github.yaml', driver='github')
30 def test_pull_event(self):
31 self.executor_server.hold_jobs_in_build = True
32
Jan Hruban37615e52015-11-19 14:30:49 +010033 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
34 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Gregory Haynes4fc12542015-04-22 20:38:06 -070035 self.waitUntilSettled()
36
Gregory Haynes4fc12542015-04-22 20:38:06 -070037 self.executor_server.hold_jobs_in_build = False
38 self.executor_server.release()
39 self.waitUntilSettled()
40
41 self.assertEqual('SUCCESS',
42 self.getJobFromHistory('project-test1').result)
43 self.assertEqual('SUCCESS',
44 self.getJobFromHistory('project-test2').result)
45
46 job = self.getJobFromHistory('project-test2')
Monty Taylord13bc362017-06-30 13:11:37 -050047 zuulvars = job.parameters['zuul']
James E. Blaire3db2952017-07-21 15:03:36 -070048 self.assertEqual(str(A.number), zuulvars['change'])
49 self.assertEqual(str(A.head_sha), zuulvars['patchset'])
James E. Blair3b222492017-07-21 15:17:37 -070050 self.assertEqual('master', zuulvars['branch'])
Jan Hruban37615e52015-11-19 14:30:49 +010051 self.assertEqual(1, len(A.comments))
Jan Hruband4edee82015-12-16 12:49:51 +010052 self.assertEqual(2, len(self.history))
53
54 # test_pull_unmatched_branch_event(self):
55 self.create_branch('org/project', 'unmatched_branch')
56 B = self.fake_github.openFakePullRequest(
57 'org/project', 'unmatched_branch', 'B')
58 self.fake_github.emitEvent(B.getPullRequestOpenedEvent())
59 self.waitUntilSettled()
60
61 self.assertEqual(2, len(self.history))
Wayne1a78c612015-06-11 17:14:13 -070062
Jan Hruban570d01c2016-03-10 21:51:32 +010063 @simple_layout('layouts/files-github.yaml', driver='github')
64 def test_pull_matched_file_event(self):
65 A = self.fake_github.openFakePullRequest(
66 'org/project', 'master', 'A',
67 files=['random.txt', 'build-requires'])
68 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
69 self.waitUntilSettled()
70 self.assertEqual(1, len(self.history))
71
72 # test_pull_unmatched_file_event
73 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B',
74 files=['random.txt'])
75 self.fake_github.emitEvent(B.getPullRequestOpenedEvent())
76 self.waitUntilSettled()
77 self.assertEqual(1, len(self.history))
78
Jan Hrubanc7ab1602015-10-14 15:29:33 +020079 @simple_layout('layouts/basic-github.yaml', driver='github')
80 def test_comment_event(self):
Jan Hruban37615e52015-11-19 14:30:49 +010081 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
82 self.fake_github.emitEvent(A.getCommentAddedEvent('test me'))
Jan Hrubanc7ab1602015-10-14 15:29:33 +020083 self.waitUntilSettled()
84 self.assertEqual(2, len(self.history))
85
86 # Test an unmatched comment, history should remain the same
Jan Hruban37615e52015-11-19 14:30:49 +010087 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
88 self.fake_github.emitEvent(B.getCommentAddedEvent('casual comment'))
Jan Hrubanc7ab1602015-10-14 15:29:33 +020089 self.waitUntilSettled()
90 self.assertEqual(2, len(self.history))
91
Wayne1a78c612015-06-11 17:14:13 -070092 @simple_layout('layouts/push-tag-github.yaml', driver='github')
93 def test_tag_event(self):
94 self.executor_server.hold_jobs_in_build = True
95
James E. Blair21037782017-07-19 11:56:55 -070096 self.create_branch('org/project', 'tagbranch')
97 files = {'README.txt': 'test'}
98 self.addCommitToRepo('org/project', 'test tag',
99 files, branch='tagbranch', tag='newtag')
100 path = os.path.join(self.upstream_root, 'org/project')
101 repo = git.Repo(path)
102 tag = repo.tags['newtag']
103 sha = tag.commit.hexsha
104 del repo
105
Wayne1a78c612015-06-11 17:14:13 -0700106 self.fake_github.emitEvent(
107 self.fake_github.getPushEvent('org/project', 'refs/tags/newtag',
108 new_rev=sha))
109 self.waitUntilSettled()
110
111 build_params = self.builds[0].parameters
James E. Blaira438c172017-07-21 14:54:42 -0700112 self.assertEqual('refs/tags/newtag', build_params['zuul']['ref'])
Wayne1a78c612015-06-11 17:14:13 -0700113 self.assertEqual('00000000000000000000000000000000',
James E. Blaired8b0012017-07-21 14:49:29 -0700114 build_params['zuul']['oldrev'])
115 self.assertEqual(sha, build_params['zuul']['newrev'])
Wayne1a78c612015-06-11 17:14:13 -0700116
117 self.executor_server.hold_jobs_in_build = False
118 self.executor_server.release()
119 self.waitUntilSettled()
120
121 self.assertEqual('SUCCESS',
122 self.getJobFromHistory('project-tag').result)
123
124 @simple_layout('layouts/push-tag-github.yaml', driver='github')
125 def test_push_event(self):
126 self.executor_server.hold_jobs_in_build = True
127
James E. Blair289f5932017-07-27 15:02:29 -0700128 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
129 old_sha = '0' * 40
130 new_sha = A.head_sha
131 A.setMerged("merging A")
132 pevent = self.fake_github.getPushEvent(project='org/project',
133 ref='refs/heads/master',
134 old_rev=old_sha,
135 new_rev=new_sha)
136 self.fake_github.emitEvent(pevent)
Wayne1a78c612015-06-11 17:14:13 -0700137 self.waitUntilSettled()
138
139 build_params = self.builds[0].parameters
James E. Blaira438c172017-07-21 14:54:42 -0700140 self.assertEqual('refs/heads/master', build_params['zuul']['ref'])
James E. Blair289f5932017-07-27 15:02:29 -0700141 self.assertFalse('oldrev' in build_params['zuul'])
James E. Blaired8b0012017-07-21 14:49:29 -0700142 self.assertEqual(new_sha, build_params['zuul']['newrev'])
Wayne1a78c612015-06-11 17:14:13 -0700143
144 self.executor_server.hold_jobs_in_build = False
145 self.executor_server.release()
146 self.waitUntilSettled()
147
148 self.assertEqual('SUCCESS',
149 self.getJobFromHistory('project-post').result)
Jan Hruband4edee82015-12-16 12:49:51 +0100150 self.assertEqual(1, len(self.history))
151
152 # test unmatched push event
153 old_sha = random_sha1()
154 new_sha = random_sha1()
155 self.fake_github.emitEvent(
156 self.fake_github.getPushEvent('org/project',
157 'refs/heads/unmatched_branch',
158 old_sha, new_sha))
159 self.waitUntilSettled()
160
161 self.assertEqual(1, len(self.history))
Jan Hruban6d53c5e2015-10-24 03:03:34 +0200162
Jan Hruban16ad31f2015-11-07 14:39:07 +0100163 @simple_layout('layouts/labeling-github.yaml', driver='github')
164 def test_labels(self):
Jan Hruban37615e52015-11-19 14:30:49 +0100165 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
Jan Hruban16ad31f2015-11-07 14:39:07 +0100166 self.fake_github.emitEvent(A.addLabel('test'))
167 self.waitUntilSettled()
168 self.assertEqual(1, len(self.history))
169 self.assertEqual('project-labels', self.history[0].name)
170 self.assertEqual(['tests passed'], A.labels)
171
172 # test label removed
Jan Hruban37615e52015-11-19 14:30:49 +0100173 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
Jan Hruban16ad31f2015-11-07 14:39:07 +0100174 B.addLabel('do not test')
175 self.fake_github.emitEvent(B.removeLabel('do not test'))
176 self.waitUntilSettled()
177 self.assertEqual(2, len(self.history))
178 self.assertEqual('project-labels', self.history[1].name)
179 self.assertEqual(['tests passed'], B.labels)
180
181 # test unmatched label
Jan Hruban37615e52015-11-19 14:30:49 +0100182 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
Jan Hruban16ad31f2015-11-07 14:39:07 +0100183 self.fake_github.emitEvent(C.addLabel('other label'))
184 self.waitUntilSettled()
185 self.assertEqual(2, len(self.history))
186 self.assertEqual(['other label'], C.labels)
187
Jesse Keating5c05a9f2017-01-12 14:44:58 -0800188 @simple_layout('layouts/reviews-github.yaml', driver='github')
189 def test_review_event(self):
190 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
191 self.fake_github.emitEvent(A.getReviewAddedEvent('approve'))
192 self.waitUntilSettled()
193 self.assertEqual(1, len(self.history))
194 self.assertEqual('project-reviews', self.history[0].name)
195 self.assertEqual(['tests passed'], A.labels)
196
197 # test_review_unmatched_event
198 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
199 self.fake_github.emitEvent(B.getReviewAddedEvent('comment'))
200 self.waitUntilSettled()
201 self.assertEqual(1, len(self.history))
202
Jan Hruban324ca5b2015-11-05 19:28:54 +0100203 @simple_layout('layouts/dequeue-github.yaml', driver='github')
204 def test_dequeue_pull_synchronized(self):
205 self.executor_server.hold_jobs_in_build = True
206
Jan Hruban37615e52015-11-19 14:30:49 +0100207 A = self.fake_github.openFakePullRequest(
208 'org/one-job-project', 'master', 'A')
209 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100210 self.waitUntilSettled()
211
212 # event update stamp has resolution one second, wait so the latter
213 # one has newer timestamp
214 time.sleep(1)
Jan Hruban37615e52015-11-19 14:30:49 +0100215 A.addCommit()
216 self.fake_github.emitEvent(A.getPullRequestSynchronizeEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100217 self.waitUntilSettled()
218
219 self.executor_server.hold_jobs_in_build = False
220 self.executor_server.release()
221 self.waitUntilSettled()
222
223 self.assertEqual(2, len(self.history))
224 self.assertEqual(1, self.countJobResults(self.history, 'ABORTED'))
225
226 @simple_layout('layouts/dequeue-github.yaml', driver='github')
227 def test_dequeue_pull_abandoned(self):
228 self.executor_server.hold_jobs_in_build = True
229
Jan Hruban37615e52015-11-19 14:30:49 +0100230 A = self.fake_github.openFakePullRequest(
231 'org/one-job-project', 'master', 'A')
232 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100233 self.waitUntilSettled()
Jan Hruban37615e52015-11-19 14:30:49 +0100234 self.fake_github.emitEvent(A.getPullRequestClosedEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100235 self.waitUntilSettled()
236
237 self.executor_server.hold_jobs_in_build = False
238 self.executor_server.release()
239 self.waitUntilSettled()
240
241 self.assertEqual(1, len(self.history))
242 self.assertEqual(1, self.countJobResults(self.history, 'ABORTED'))
243
Jan Hruban6d53c5e2015-10-24 03:03:34 +0200244 @simple_layout('layouts/basic-github.yaml', driver='github')
245 def test_git_https_url(self):
246 """Test that git_ssh option gives git url with ssh"""
247 url = self.fake_github.real_getGitUrl('org/project')
248 self.assertEqual('https://github.com/org/project', url)
249
250 @simple_layout('layouts/basic-github.yaml', driver='github')
251 def test_git_ssh_url(self):
252 """Test that git_ssh option gives git url with ssh"""
253 url = self.fake_github_ssh.real_getGitUrl('org/project')
254 self.assertEqual('ssh://git@github.com/org/project.git', url)
Jan Hrubane252a732017-01-03 15:03:09 +0100255
Jesse Keatingbe4ef8a2016-12-06 11:29:13 -0800256 @simple_layout('layouts/basic-github.yaml', driver='github')
257 def test_git_enterprise_url(self):
258 """Test that git_url option gives git url with proper host"""
259 url = self.fake_github_ent.real_getGitUrl('org/project')
260 self.assertEqual('ssh://git@github.enterprise.io/org/project.git', url)
261
Jan Hrubane252a732017-01-03 15:03:09 +0100262 @simple_layout('layouts/reporting-github.yaml', driver='github')
263 def test_reporting(self):
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700264 project = 'org/project'
Jan Hrubane252a732017-01-03 15:03:09 +0100265 # pipeline reports pull status both on start and success
266 self.executor_server.hold_jobs_in_build = True
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700267 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
Jan Hruban37615e52015-11-19 14:30:49 +0100268 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Jan Hrubane252a732017-01-03 15:03:09 +0100269 self.waitUntilSettled()
Jesse Keatingd96e5882017-01-19 13:55:50 -0800270 # We should have a status container for the head sha
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700271 statuses = self.fake_github.statuses[project][A.head_sha]
272 self.assertIn(A.head_sha, self.fake_github.statuses[project].keys())
Jesse Keatingd96e5882017-01-19 13:55:50 -0800273 # We should only have one status for the head sha
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700274 self.assertEqual(1, len(statuses))
275 check_status = statuses[0]
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100276 check_url = ('http://zuul.example.com/status/#%s,%s' %
277 (A.number, A.head_sha))
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000278 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700279 self.assertEqual('check status: pending',
280 check_status['description'])
Jan Hrubane252a732017-01-03 15:03:09 +0100281 self.assertEqual('pending', check_status['state'])
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100282 self.assertEqual(check_url, check_status['url'])
Jan Hruban37615e52015-11-19 14:30:49 +0100283 self.assertEqual(0, len(A.comments))
Jan Hrubane252a732017-01-03 15:03:09 +0100284
285 self.executor_server.hold_jobs_in_build = False
286 self.executor_server.release()
287 self.waitUntilSettled()
Jesse Keatingd96e5882017-01-19 13:55:50 -0800288 # We should only have two statuses for the head sha
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700289 statuses = self.fake_github.statuses[project][A.head_sha]
290 self.assertEqual(2, len(statuses))
291 check_status = statuses[0]
Jesse Keatingd96e5882017-01-19 13:55:50 -0800292 check_url = ('http://zuul.example.com/status/#%s,%s' %
293 (A.number, A.head_sha))
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000294 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700295 self.assertEqual('check status: success',
296 check_status['description'])
Jan Hrubane252a732017-01-03 15:03:09 +0100297 self.assertEqual('success', check_status['state'])
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100298 self.assertEqual(check_url, check_status['url'])
Jan Hruban37615e52015-11-19 14:30:49 +0100299 self.assertEqual(1, len(A.comments))
300 self.assertThat(A.comments[0],
Jan Hrubane252a732017-01-03 15:03:09 +0100301 MatchesRegex('.*Build succeeded.*', re.DOTALL))
302
303 # pipeline does not report any status but does comment
304 self.executor_server.hold_jobs_in_build = True
305 self.fake_github.emitEvent(
Jan Hruban37615e52015-11-19 14:30:49 +0100306 A.getCommentAddedEvent('reporting check'))
Jan Hrubane252a732017-01-03 15:03:09 +0100307 self.waitUntilSettled()
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700308 statuses = self.fake_github.statuses[project][A.head_sha]
309 self.assertEqual(2, len(statuses))
Jan Hrubane252a732017-01-03 15:03:09 +0100310 # comments increased by one for the start message
Jan Hruban37615e52015-11-19 14:30:49 +0100311 self.assertEqual(2, len(A.comments))
312 self.assertThat(A.comments[1],
Jan Hrubane252a732017-01-03 15:03:09 +0100313 MatchesRegex('.*Starting reporting jobs.*', re.DOTALL))
314 self.executor_server.hold_jobs_in_build = False
315 self.executor_server.release()
316 self.waitUntilSettled()
Jesse Keatingd96e5882017-01-19 13:55:50 -0800317 # pipeline reports success status
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700318 statuses = self.fake_github.statuses[project][A.head_sha]
319 self.assertEqual(3, len(statuses))
320 report_status = statuses[0]
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000321 self.assertEqual('tenant-one/reporting', report_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700322 self.assertEqual('reporting status: success',
323 report_status['description'])
Jesse Keatingd96e5882017-01-19 13:55:50 -0800324 self.assertEqual('success', report_status['state'])
Jan Hruban37615e52015-11-19 14:30:49 +0100325 self.assertEqual(2, len(A.comments))
Jamie Lennox3f16de52017-05-09 14:24:11 +1000326
327 base = 'http://logs.example.com/tenant-one/reporting/%s/%s/' % (
328 A.project, A.number)
329
330 # Deconstructing the URL because we don't save the BuildSet UUID
331 # anywhere to do a direct comparison and doing regexp matches on a full
332 # URL is painful.
333
334 # The first part of the URL matches the easy base string
335 self.assertThat(report_status['url'], StartsWith(base))
336
337 # The rest of the URL is a UUID and a trailing slash.
338 self.assertThat(report_status['url'][len(base):],
339 MatchesRegex('^[a-fA-F0-9]{32}\/$'))
Jan Hruban49bff072015-11-03 11:45:46 +0100340
Jesse Keating08dab8f2017-06-21 12:59:23 +0100341 @simple_layout('layouts/reporting-github.yaml', driver='github')
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700342 def test_truncated_status_description(self):
343 project = 'org/project'
344 # pipeline reports pull status both on start and success
345 self.executor_server.hold_jobs_in_build = True
346 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
347 self.fake_github.emitEvent(
348 A.getCommentAddedEvent('long pipeline'))
349 self.waitUntilSettled()
350 statuses = self.fake_github.statuses[project][A.head_sha]
351 self.assertEqual(1, len(statuses))
352 check_status = statuses[0]
353 # Status is truncated due to long pipeline name
354 self.assertEqual('status: pending',
355 check_status['description'])
356
357 self.executor_server.hold_jobs_in_build = False
358 self.executor_server.release()
359 self.waitUntilSettled()
360 # We should only have two statuses for the head sha
361 statuses = self.fake_github.statuses[project][A.head_sha]
362 self.assertEqual(2, len(statuses))
363 check_status = statuses[0]
364 # Status is truncated due to long pipeline name
365 self.assertEqual('status: success',
366 check_status['description'])
367
368 @simple_layout('layouts/reporting-github.yaml', driver='github')
Jesse Keating08dab8f2017-06-21 12:59:23 +0100369 def test_push_reporting(self):
370 project = 'org/project2'
371 # pipeline reports pull status both on start and success
372 self.executor_server.hold_jobs_in_build = True
Jesse Keating08dab8f2017-06-21 12:59:23 +0100373
James E. Blair289f5932017-07-27 15:02:29 -0700374 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
375 old_sha = '0' * 40
376 new_sha = A.head_sha
377 A.setMerged("merging A")
378 pevent = self.fake_github.getPushEvent(project=project,
379 ref='refs/heads/master',
380 old_rev=old_sha,
381 new_rev=new_sha)
Jesse Keating08dab8f2017-06-21 12:59:23 +0100382 self.fake_github.emitEvent(pevent)
383 self.waitUntilSettled()
384
385 # there should only be one report, a status
386 self.assertEqual(1, len(self.fake_github.reports))
387 # Verify the user/context/state of the status
388 status = ('zuul', 'tenant-one/push-reporting', 'pending')
389 self.assertEqual(status, self.fake_github.reports[0][-1])
390
391 # free the executor, allow the build to finish
392 self.executor_server.hold_jobs_in_build = False
393 self.executor_server.release()
394 self.waitUntilSettled()
395
396 # Now there should be a second report, the success of the build
397 self.assertEqual(2, len(self.fake_github.reports))
398 # Verify the user/context/state of the status
399 status = ('zuul', 'tenant-one/push-reporting', 'success')
400 self.assertEqual(status, self.fake_github.reports[-1][-1])
401
402 # now make a PR which should also comment
403 self.executor_server.hold_jobs_in_build = True
404 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
405 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
406 self.waitUntilSettled()
407
408 # Now there should be a four reports, a new comment
409 # and status
410 self.assertEqual(4, len(self.fake_github.reports))
411 self.executor_server.release()
412 self.waitUntilSettled()
413
Jan Hruban49bff072015-11-03 11:45:46 +0100414 @simple_layout('layouts/merging-github.yaml', driver='github')
415 def test_report_pull_merge(self):
416 # pipeline merges the pull request on success
Jan Hruban3b415922016-02-03 13:10:22 +0100417 A = self.fake_github.openFakePullRequest('org/project', 'master',
418 'PR title')
Jan Hruban49bff072015-11-03 11:45:46 +0100419 self.fake_github.emitEvent(A.getCommentAddedEvent('merge me'))
420 self.waitUntilSettled()
421 self.assertTrue(A.is_merged)
Jan Hruban3b415922016-02-03 13:10:22 +0100422 self.assertThat(A.merge_message,
423 MatchesRegex('.*PR title.*Reviewed-by.*', re.DOTALL))
Jan Hruban49bff072015-11-03 11:45:46 +0100424
425 # pipeline merges the pull request on success after failure
426 self.fake_github.merge_failure = True
Jan Hruban37615e52015-11-19 14:30:49 +0100427 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
Jan Hruban49bff072015-11-03 11:45:46 +0100428 self.fake_github.emitEvent(B.getCommentAddedEvent('merge me'))
429 self.waitUntilSettled()
430 self.assertFalse(B.is_merged)
431 self.fake_github.merge_failure = False
432
433 # pipeline merges the pull request on second run of merge
434 # first merge failed on 405 Method Not Allowed error
435 self.fake_github.merge_not_allowed_count = 1
Jan Hruban37615e52015-11-19 14:30:49 +0100436 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
Jan Hruban49bff072015-11-03 11:45:46 +0100437 self.fake_github.emitEvent(C.getCommentAddedEvent('merge me'))
438 self.waitUntilSettled()
439 self.assertTrue(C.is_merged)
440
441 # pipeline does not merge the pull request
442 # merge failed on 405 Method Not Allowed error - twice
443 self.fake_github.merge_not_allowed_count = 2
Jan Hruban37615e52015-11-19 14:30:49 +0100444 D = self.fake_github.openFakePullRequest('org/project', 'master', 'D')
Jan Hruban49bff072015-11-03 11:45:46 +0100445 self.fake_github.emitEvent(D.getCommentAddedEvent('merge me'))
446 self.waitUntilSettled()
447 self.assertFalse(D.is_merged)
Adam Gandelman62198cb2017-02-14 16:11:02 -0800448 self.assertEqual(len(D.comments), 1)
449 self.assertEqual(D.comments[0], 'Merge failed')
Jan Hruban37615e52015-11-19 14:30:49 +0100450
Jesse Keating28434312017-07-31 11:32:48 -0700451 @simple_layout('layouts/reporting-multiple-github.yaml', driver='github')
452 def test_reporting_multiple_github(self):
453 project = 'org/project1'
454 # pipeline reports pull status both on start and success
455 self.executor_server.hold_jobs_in_build = True
456 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
457 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
458 # open one on B as well, which should not effect A reporting
459 B = self.fake_github.openFakePullRequest('org/project2', 'master',
460 'B')
461 self.fake_github.emitEvent(B.getPullRequestOpenedEvent())
462 self.waitUntilSettled()
463 # We should have a status container for the head sha
464 statuses = self.fake_github.statuses[project][A.head_sha]
465 self.assertIn(A.head_sha, self.fake_github.statuses[project].keys())
466 # We should only have one status for the head sha
467 self.assertEqual(1, len(statuses))
468 check_status = statuses[0]
469 check_url = ('http://zuul.example.com/status/#%s,%s' %
470 (A.number, A.head_sha))
471 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700472 self.assertEqual('check status: pending', check_status['description'])
Jesse Keating28434312017-07-31 11:32:48 -0700473 self.assertEqual('pending', check_status['state'])
474 self.assertEqual(check_url, check_status['url'])
475 self.assertEqual(0, len(A.comments))
476
477 self.executor_server.hold_jobs_in_build = False
478 self.executor_server.release()
479 self.waitUntilSettled()
480 # We should only have two statuses for the head sha
481 statuses = self.fake_github.statuses[project][A.head_sha]
482 self.assertEqual(2, len(statuses))
483 check_status = statuses[0]
484 check_url = ('http://zuul.example.com/status/#%s,%s' %
485 (A.number, A.head_sha))
486 self.assertEqual('tenant-one/check', check_status['context'])
487 self.assertEqual('success', check_status['state'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700488 self.assertEqual('check status: success', check_status['description'])
Jesse Keating28434312017-07-31 11:32:48 -0700489 self.assertEqual(check_url, check_status['url'])
490 self.assertEqual(1, len(A.comments))
491 self.assertThat(A.comments[0],
492 MatchesRegex('.*Build succeeded.*', re.DOTALL))
493
Jan Hruban37615e52015-11-19 14:30:49 +0100494 @simple_layout('layouts/dependent-github.yaml', driver='github')
495 def test_parallel_changes(self):
496 "Test that changes are tested in parallel and merged in series"
497
498 self.executor_server.hold_jobs_in_build = True
499 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
500 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
501 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
502
503 self.fake_github.emitEvent(A.addLabel('merge'))
504 self.fake_github.emitEvent(B.addLabel('merge'))
505 self.fake_github.emitEvent(C.addLabel('merge'))
506
507 self.waitUntilSettled()
508 self.assertEqual(len(self.builds), 1)
509 self.assertEqual(self.builds[0].name, 'project-merge')
510 self.assertTrue(self.builds[0].hasChanges(A))
511
512 self.executor_server.release('.*-merge')
513 self.waitUntilSettled()
514 self.assertEqual(len(self.builds), 3)
515 self.assertEqual(self.builds[0].name, 'project-test1')
516 self.assertTrue(self.builds[0].hasChanges(A))
517 self.assertEqual(self.builds[1].name, 'project-test2')
518 self.assertTrue(self.builds[1].hasChanges(A))
519 self.assertEqual(self.builds[2].name, 'project-merge')
520 self.assertTrue(self.builds[2].hasChanges(A, B))
521
522 self.executor_server.release('.*-merge')
523 self.waitUntilSettled()
524 self.assertEqual(len(self.builds), 5)
525 self.assertEqual(self.builds[0].name, 'project-test1')
526 self.assertTrue(self.builds[0].hasChanges(A))
527 self.assertEqual(self.builds[1].name, 'project-test2')
528 self.assertTrue(self.builds[1].hasChanges(A))
529
530 self.assertEqual(self.builds[2].name, 'project-test1')
531 self.assertTrue(self.builds[2].hasChanges(A))
532 self.assertEqual(self.builds[3].name, 'project-test2')
533 self.assertTrue(self.builds[3].hasChanges(A, B))
534
535 self.assertEqual(self.builds[4].name, 'project-merge')
536 self.assertTrue(self.builds[4].hasChanges(A, B, C))
537
538 self.executor_server.release('.*-merge')
539 self.waitUntilSettled()
540 self.assertEqual(len(self.builds), 6)
541 self.assertEqual(self.builds[0].name, 'project-test1')
542 self.assertTrue(self.builds[0].hasChanges(A))
543 self.assertEqual(self.builds[1].name, 'project-test2')
544 self.assertTrue(self.builds[1].hasChanges(A))
545
546 self.assertEqual(self.builds[2].name, 'project-test1')
547 self.assertTrue(self.builds[2].hasChanges(A, B))
548 self.assertEqual(self.builds[3].name, 'project-test2')
549 self.assertTrue(self.builds[3].hasChanges(A, B))
550
551 self.assertEqual(self.builds[4].name, 'project-test1')
552 self.assertTrue(self.builds[4].hasChanges(A, B, C))
553 self.assertEqual(self.builds[5].name, 'project-test2')
554 self.assertTrue(self.builds[5].hasChanges(A, B, C))
555
556 all_builds = self.builds[:]
557 self.release(all_builds[2])
558 self.release(all_builds[3])
559 self.waitUntilSettled()
560 self.assertFalse(A.is_merged)
561 self.assertFalse(B.is_merged)
562 self.assertFalse(C.is_merged)
563
564 self.release(all_builds[0])
565 self.release(all_builds[1])
566 self.waitUntilSettled()
567 self.assertTrue(A.is_merged)
568 self.assertTrue(B.is_merged)
569 self.assertFalse(C.is_merged)
570
571 self.executor_server.hold_jobs_in_build = False
572 self.executor_server.release()
573 self.waitUntilSettled()
574 self.assertEqual(len(self.builds), 0)
575 self.assertEqual(len(self.history), 9)
576 self.assertTrue(C.is_merged)
577
578 self.assertNotIn('merge', A.labels)
579 self.assertNotIn('merge', B.labels)
580 self.assertNotIn('merge', C.labels)
581
582 @simple_layout('layouts/dependent-github.yaml', driver='github')
583 def test_failed_changes(self):
584 "Test that a change behind a failed change is retested"
585 self.executor_server.hold_jobs_in_build = True
586
587 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
588 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
589
590 self.executor_server.failJob('project-test1', A)
591
592 self.fake_github.emitEvent(A.addLabel('merge'))
593 self.fake_github.emitEvent(B.addLabel('merge'))
594 self.waitUntilSettled()
595
596 self.executor_server.release('.*-merge')
597 self.waitUntilSettled()
598
599 self.executor_server.hold_jobs_in_build = False
600 self.executor_server.release()
601
602 self.waitUntilSettled()
603 # It's certain that the merge job for change 2 will run, but
604 # the test1 and test2 jobs may or may not run.
605 self.assertTrue(len(self.history) > 6)
606 self.assertFalse(A.is_merged)
607 self.assertTrue(B.is_merged)
608 self.assertNotIn('merge', A.labels)
609 self.assertNotIn('merge', B.labels)
610
611 @simple_layout('layouts/dependent-github.yaml', driver='github')
612 def test_failed_change_at_head(self):
613 "Test that if a change at the head fails, jobs behind it are canceled"
614
615 self.executor_server.hold_jobs_in_build = True
616 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
617 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
618 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
619
620 self.executor_server.failJob('project-test1', A)
621
622 self.fake_github.emitEvent(A.addLabel('merge'))
623 self.fake_github.emitEvent(B.addLabel('merge'))
624 self.fake_github.emitEvent(C.addLabel('merge'))
625
626 self.waitUntilSettled()
627
628 self.assertEqual(len(self.builds), 1)
629 self.assertEqual(self.builds[0].name, 'project-merge')
630 self.assertTrue(self.builds[0].hasChanges(A))
631
632 self.executor_server.release('.*-merge')
633 self.waitUntilSettled()
634 self.executor_server.release('.*-merge')
635 self.waitUntilSettled()
636 self.executor_server.release('.*-merge')
637 self.waitUntilSettled()
638
639 self.assertEqual(len(self.builds), 6)
640 self.assertEqual(self.builds[0].name, 'project-test1')
641 self.assertEqual(self.builds[1].name, 'project-test2')
642 self.assertEqual(self.builds[2].name, 'project-test1')
643 self.assertEqual(self.builds[3].name, 'project-test2')
644 self.assertEqual(self.builds[4].name, 'project-test1')
645 self.assertEqual(self.builds[5].name, 'project-test2')
646
647 self.release(self.builds[0])
648 self.waitUntilSettled()
649
650 # project-test2, project-merge for B
651 self.assertEqual(len(self.builds), 2)
652 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
653
654 self.executor_server.hold_jobs_in_build = False
655 self.executor_server.release()
656 self.waitUntilSettled()
657
658 self.assertEqual(len(self.builds), 0)
659 self.assertEqual(len(self.history), 15)
660 self.assertFalse(A.is_merged)
661 self.assertTrue(B.is_merged)
662 self.assertTrue(C.is_merged)
663 self.assertNotIn('merge', A.labels)
664 self.assertNotIn('merge', B.labels)
665 self.assertNotIn('merge', C.labels)
Jesse Keating71a47ff2017-06-06 11:36:43 -0700666
667 @simple_layout('layouts/basic-github.yaml', driver='github')
668 def test_push_event_reconfigure(self):
669 pevent = self.fake_github.getPushEvent(project='common-config',
670 ref='refs/heads/master',
671 modified_files=['zuul.yaml'])
672
673 # record previous tenant reconfiguration time, which may not be set
674 old = self.sched.tenant_last_reconfigured.get('tenant-one', 0)
675 time.sleep(1)
676 self.fake_github.emitEvent(pevent)
677 self.waitUntilSettled()
678 new = self.sched.tenant_last_reconfigured.get('tenant-one', 0)
679 # New timestamp should be greater than the old timestamp
680 self.assertLess(old, new)
Tristan Cacqueray2bafb1f2017-06-12 07:10:26 +0000681
682 @simple_layout('layouts/basic-github.yaml', driver='github')
683 def test_ping_event(self):
684 # Test valid ping
685 pevent = {'repository': {'full_name': 'org/project'}}
686 req = self.fake_github.emitEvent(('ping', pevent))
687 self.assertEqual(req.status, 200, "Ping event didn't succeed")
688
689 # Test invalid ping
690 pevent = {'repository': {'full_name': 'unknown-project'}}
691 self.assertRaises(
692 urllib.error.HTTPError,
693 self.fake_github.emitEvent,
694 ('ping', pevent),
695 )