blob: 538de58662e8684ac4bd0a19499e86b2afb94506 [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'
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200265 github = self.fake_github.github_client
266
Jan Hrubane252a732017-01-03 15:03:09 +0100267 # pipeline reports pull status both on start and success
268 self.executor_server.hold_jobs_in_build = True
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700269 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
Jan Hruban37615e52015-11-19 14:30:49 +0100270 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Jan Hrubane252a732017-01-03 15:03:09 +0100271 self.waitUntilSettled()
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200272
Jesse Keatingd96e5882017-01-19 13:55:50 -0800273 # We should have a status container for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200274 self.assertIn(
275 A.head_sha, github.repo_from_project(project)._commits.keys())
276 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
277
Jesse Keatingd96e5882017-01-19 13:55:50 -0800278 # We should only have one status for the head sha
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700279 self.assertEqual(1, len(statuses))
280 check_status = statuses[0]
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100281 check_url = ('http://zuul.example.com/status/#%s,%s' %
282 (A.number, A.head_sha))
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000283 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700284 self.assertEqual('check status: pending',
285 check_status['description'])
Jan Hrubane252a732017-01-03 15:03:09 +0100286 self.assertEqual('pending', check_status['state'])
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100287 self.assertEqual(check_url, check_status['url'])
Jan Hruban37615e52015-11-19 14:30:49 +0100288 self.assertEqual(0, len(A.comments))
Jan Hrubane252a732017-01-03 15:03:09 +0100289
290 self.executor_server.hold_jobs_in_build = False
291 self.executor_server.release()
292 self.waitUntilSettled()
Jesse Keatingd96e5882017-01-19 13:55:50 -0800293 # We should only have two statuses for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200294 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700295 self.assertEqual(2, len(statuses))
296 check_status = statuses[0]
Jesse Keatingd96e5882017-01-19 13:55:50 -0800297 check_url = ('http://zuul.example.com/status/#%s,%s' %
298 (A.number, A.head_sha))
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000299 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700300 self.assertEqual('check status: success',
301 check_status['description'])
Jan Hrubane252a732017-01-03 15:03:09 +0100302 self.assertEqual('success', check_status['state'])
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100303 self.assertEqual(check_url, check_status['url'])
Jan Hruban37615e52015-11-19 14:30:49 +0100304 self.assertEqual(1, len(A.comments))
305 self.assertThat(A.comments[0],
Jan Hrubane252a732017-01-03 15:03:09 +0100306 MatchesRegex('.*Build succeeded.*', re.DOTALL))
307
308 # pipeline does not report any status but does comment
309 self.executor_server.hold_jobs_in_build = True
310 self.fake_github.emitEvent(
Jan Hruban37615e52015-11-19 14:30:49 +0100311 A.getCommentAddedEvent('reporting check'))
Jan Hrubane252a732017-01-03 15:03:09 +0100312 self.waitUntilSettled()
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200313 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700314 self.assertEqual(2, len(statuses))
Jan Hrubane252a732017-01-03 15:03:09 +0100315 # comments increased by one for the start message
Jan Hruban37615e52015-11-19 14:30:49 +0100316 self.assertEqual(2, len(A.comments))
317 self.assertThat(A.comments[1],
Jan Hrubane252a732017-01-03 15:03:09 +0100318 MatchesRegex('.*Starting reporting jobs.*', re.DOTALL))
319 self.executor_server.hold_jobs_in_build = False
320 self.executor_server.release()
321 self.waitUntilSettled()
Jesse Keatingd96e5882017-01-19 13:55:50 -0800322 # pipeline reports success status
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200323 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700324 self.assertEqual(3, len(statuses))
325 report_status = statuses[0]
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000326 self.assertEqual('tenant-one/reporting', report_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700327 self.assertEqual('reporting status: success',
328 report_status['description'])
Jesse Keatingd96e5882017-01-19 13:55:50 -0800329 self.assertEqual('success', report_status['state'])
Jan Hruban37615e52015-11-19 14:30:49 +0100330 self.assertEqual(2, len(A.comments))
Jamie Lennox3f16de52017-05-09 14:24:11 +1000331
332 base = 'http://logs.example.com/tenant-one/reporting/%s/%s/' % (
333 A.project, A.number)
334
335 # Deconstructing the URL because we don't save the BuildSet UUID
336 # anywhere to do a direct comparison and doing regexp matches on a full
337 # URL is painful.
338
339 # The first part of the URL matches the easy base string
340 self.assertThat(report_status['url'], StartsWith(base))
341
342 # The rest of the URL is a UUID and a trailing slash.
343 self.assertThat(report_status['url'][len(base):],
344 MatchesRegex('^[a-fA-F0-9]{32}\/$'))
Jan Hruban49bff072015-11-03 11:45:46 +0100345
Jesse Keating08dab8f2017-06-21 12:59:23 +0100346 @simple_layout('layouts/reporting-github.yaml', driver='github')
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700347 def test_truncated_status_description(self):
348 project = 'org/project'
349 # pipeline reports pull status both on start and success
350 self.executor_server.hold_jobs_in_build = True
351 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
352 self.fake_github.emitEvent(
353 A.getCommentAddedEvent('long pipeline'))
354 self.waitUntilSettled()
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200355 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700356 self.assertEqual(1, len(statuses))
357 check_status = statuses[0]
358 # Status is truncated due to long pipeline name
359 self.assertEqual('status: pending',
360 check_status['description'])
361
362 self.executor_server.hold_jobs_in_build = False
363 self.executor_server.release()
364 self.waitUntilSettled()
365 # We should only have two statuses for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200366 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700367 self.assertEqual(2, len(statuses))
368 check_status = statuses[0]
369 # Status is truncated due to long pipeline name
370 self.assertEqual('status: success',
371 check_status['description'])
372
373 @simple_layout('layouts/reporting-github.yaml', driver='github')
Jesse Keating08dab8f2017-06-21 12:59:23 +0100374 def test_push_reporting(self):
375 project = 'org/project2'
376 # pipeline reports pull status both on start and success
377 self.executor_server.hold_jobs_in_build = True
Jesse Keating08dab8f2017-06-21 12:59:23 +0100378
James E. Blair289f5932017-07-27 15:02:29 -0700379 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
380 old_sha = '0' * 40
381 new_sha = A.head_sha
382 A.setMerged("merging A")
383 pevent = self.fake_github.getPushEvent(project=project,
384 ref='refs/heads/master',
385 old_rev=old_sha,
386 new_rev=new_sha)
Jesse Keating08dab8f2017-06-21 12:59:23 +0100387 self.fake_github.emitEvent(pevent)
388 self.waitUntilSettled()
389
390 # there should only be one report, a status
391 self.assertEqual(1, len(self.fake_github.reports))
392 # Verify the user/context/state of the status
393 status = ('zuul', 'tenant-one/push-reporting', 'pending')
394 self.assertEqual(status, self.fake_github.reports[0][-1])
395
396 # free the executor, allow the build to finish
397 self.executor_server.hold_jobs_in_build = False
398 self.executor_server.release()
399 self.waitUntilSettled()
400
401 # Now there should be a second report, the success of the build
402 self.assertEqual(2, len(self.fake_github.reports))
403 # Verify the user/context/state of the status
404 status = ('zuul', 'tenant-one/push-reporting', 'success')
405 self.assertEqual(status, self.fake_github.reports[-1][-1])
406
407 # now make a PR which should also comment
408 self.executor_server.hold_jobs_in_build = True
409 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
410 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
411 self.waitUntilSettled()
412
413 # Now there should be a four reports, a new comment
414 # and status
415 self.assertEqual(4, len(self.fake_github.reports))
416 self.executor_server.release()
417 self.waitUntilSettled()
418
Jan Hruban49bff072015-11-03 11:45:46 +0100419 @simple_layout('layouts/merging-github.yaml', driver='github')
420 def test_report_pull_merge(self):
421 # pipeline merges the pull request on success
Jan Hruban3b415922016-02-03 13:10:22 +0100422 A = self.fake_github.openFakePullRequest('org/project', 'master',
423 'PR title')
Jan Hruban49bff072015-11-03 11:45:46 +0100424 self.fake_github.emitEvent(A.getCommentAddedEvent('merge me'))
425 self.waitUntilSettled()
426 self.assertTrue(A.is_merged)
Jan Hruban3b415922016-02-03 13:10:22 +0100427 self.assertThat(A.merge_message,
428 MatchesRegex('.*PR title.*Reviewed-by.*', re.DOTALL))
Jan Hruban49bff072015-11-03 11:45:46 +0100429
430 # pipeline merges the pull request on success after failure
431 self.fake_github.merge_failure = True
Jan Hruban37615e52015-11-19 14:30:49 +0100432 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
Jan Hruban49bff072015-11-03 11:45:46 +0100433 self.fake_github.emitEvent(B.getCommentAddedEvent('merge me'))
434 self.waitUntilSettled()
435 self.assertFalse(B.is_merged)
436 self.fake_github.merge_failure = False
437
438 # pipeline merges the pull request on second run of merge
439 # first merge failed on 405 Method Not Allowed error
440 self.fake_github.merge_not_allowed_count = 1
Jan Hruban37615e52015-11-19 14:30:49 +0100441 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
Jan Hruban49bff072015-11-03 11:45:46 +0100442 self.fake_github.emitEvent(C.getCommentAddedEvent('merge me'))
443 self.waitUntilSettled()
444 self.assertTrue(C.is_merged)
445
446 # pipeline does not merge the pull request
447 # merge failed on 405 Method Not Allowed error - twice
448 self.fake_github.merge_not_allowed_count = 2
Jan Hruban37615e52015-11-19 14:30:49 +0100449 D = self.fake_github.openFakePullRequest('org/project', 'master', 'D')
Jan Hruban49bff072015-11-03 11:45:46 +0100450 self.fake_github.emitEvent(D.getCommentAddedEvent('merge me'))
451 self.waitUntilSettled()
452 self.assertFalse(D.is_merged)
Adam Gandelman62198cb2017-02-14 16:11:02 -0800453 self.assertEqual(len(D.comments), 1)
454 self.assertEqual(D.comments[0], 'Merge failed')
Jan Hruban37615e52015-11-19 14:30:49 +0100455
Jesse Keating28434312017-07-31 11:32:48 -0700456 @simple_layout('layouts/reporting-multiple-github.yaml', driver='github')
457 def test_reporting_multiple_github(self):
458 project = 'org/project1'
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200459 github = self.fake_github.github_client
460
Jesse Keating28434312017-07-31 11:32:48 -0700461 # pipeline reports pull status both on start and success
462 self.executor_server.hold_jobs_in_build = True
463 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
464 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
465 # open one on B as well, which should not effect A reporting
466 B = self.fake_github.openFakePullRequest('org/project2', 'master',
467 'B')
468 self.fake_github.emitEvent(B.getPullRequestOpenedEvent())
469 self.waitUntilSettled()
470 # We should have a status container for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200471 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
472 self.assertIn(
473 A.head_sha, github.repo_from_project(project)._commits.keys())
Jesse Keating28434312017-07-31 11:32:48 -0700474 # We should only have one status for the head sha
475 self.assertEqual(1, len(statuses))
476 check_status = statuses[0]
477 check_url = ('http://zuul.example.com/status/#%s,%s' %
478 (A.number, A.head_sha))
479 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700480 self.assertEqual('check status: pending', check_status['description'])
Jesse Keating28434312017-07-31 11:32:48 -0700481 self.assertEqual('pending', check_status['state'])
482 self.assertEqual(check_url, check_status['url'])
483 self.assertEqual(0, len(A.comments))
484
485 self.executor_server.hold_jobs_in_build = False
486 self.executor_server.release()
487 self.waitUntilSettled()
488 # We should only have two statuses for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200489 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating28434312017-07-31 11:32:48 -0700490 self.assertEqual(2, len(statuses))
491 check_status = statuses[0]
492 check_url = ('http://zuul.example.com/status/#%s,%s' %
493 (A.number, A.head_sha))
494 self.assertEqual('tenant-one/check', check_status['context'])
495 self.assertEqual('success', check_status['state'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700496 self.assertEqual('check status: success', check_status['description'])
Jesse Keating28434312017-07-31 11:32:48 -0700497 self.assertEqual(check_url, check_status['url'])
498 self.assertEqual(1, len(A.comments))
499 self.assertThat(A.comments[0],
500 MatchesRegex('.*Build succeeded.*', re.DOTALL))
501
Jan Hruban37615e52015-11-19 14:30:49 +0100502 @simple_layout('layouts/dependent-github.yaml', driver='github')
503 def test_parallel_changes(self):
504 "Test that changes are tested in parallel and merged in series"
505
506 self.executor_server.hold_jobs_in_build = True
507 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
508 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
509 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
510
511 self.fake_github.emitEvent(A.addLabel('merge'))
512 self.fake_github.emitEvent(B.addLabel('merge'))
513 self.fake_github.emitEvent(C.addLabel('merge'))
514
515 self.waitUntilSettled()
516 self.assertEqual(len(self.builds), 1)
517 self.assertEqual(self.builds[0].name, 'project-merge')
518 self.assertTrue(self.builds[0].hasChanges(A))
519
520 self.executor_server.release('.*-merge')
521 self.waitUntilSettled()
522 self.assertEqual(len(self.builds), 3)
523 self.assertEqual(self.builds[0].name, 'project-test1')
524 self.assertTrue(self.builds[0].hasChanges(A))
525 self.assertEqual(self.builds[1].name, 'project-test2')
526 self.assertTrue(self.builds[1].hasChanges(A))
527 self.assertEqual(self.builds[2].name, 'project-merge')
528 self.assertTrue(self.builds[2].hasChanges(A, B))
529
530 self.executor_server.release('.*-merge')
531 self.waitUntilSettled()
532 self.assertEqual(len(self.builds), 5)
533 self.assertEqual(self.builds[0].name, 'project-test1')
534 self.assertTrue(self.builds[0].hasChanges(A))
535 self.assertEqual(self.builds[1].name, 'project-test2')
536 self.assertTrue(self.builds[1].hasChanges(A))
537
538 self.assertEqual(self.builds[2].name, 'project-test1')
539 self.assertTrue(self.builds[2].hasChanges(A))
540 self.assertEqual(self.builds[3].name, 'project-test2')
541 self.assertTrue(self.builds[3].hasChanges(A, B))
542
543 self.assertEqual(self.builds[4].name, 'project-merge')
544 self.assertTrue(self.builds[4].hasChanges(A, B, C))
545
546 self.executor_server.release('.*-merge')
547 self.waitUntilSettled()
548 self.assertEqual(len(self.builds), 6)
549 self.assertEqual(self.builds[0].name, 'project-test1')
550 self.assertTrue(self.builds[0].hasChanges(A))
551 self.assertEqual(self.builds[1].name, 'project-test2')
552 self.assertTrue(self.builds[1].hasChanges(A))
553
554 self.assertEqual(self.builds[2].name, 'project-test1')
555 self.assertTrue(self.builds[2].hasChanges(A, B))
556 self.assertEqual(self.builds[3].name, 'project-test2')
557 self.assertTrue(self.builds[3].hasChanges(A, B))
558
559 self.assertEqual(self.builds[4].name, 'project-test1')
560 self.assertTrue(self.builds[4].hasChanges(A, B, C))
561 self.assertEqual(self.builds[5].name, 'project-test2')
562 self.assertTrue(self.builds[5].hasChanges(A, B, C))
563
564 all_builds = self.builds[:]
565 self.release(all_builds[2])
566 self.release(all_builds[3])
567 self.waitUntilSettled()
568 self.assertFalse(A.is_merged)
569 self.assertFalse(B.is_merged)
570 self.assertFalse(C.is_merged)
571
572 self.release(all_builds[0])
573 self.release(all_builds[1])
574 self.waitUntilSettled()
575 self.assertTrue(A.is_merged)
576 self.assertTrue(B.is_merged)
577 self.assertFalse(C.is_merged)
578
579 self.executor_server.hold_jobs_in_build = False
580 self.executor_server.release()
581 self.waitUntilSettled()
582 self.assertEqual(len(self.builds), 0)
583 self.assertEqual(len(self.history), 9)
584 self.assertTrue(C.is_merged)
585
586 self.assertNotIn('merge', A.labels)
587 self.assertNotIn('merge', B.labels)
588 self.assertNotIn('merge', C.labels)
589
590 @simple_layout('layouts/dependent-github.yaml', driver='github')
591 def test_failed_changes(self):
592 "Test that a change behind a failed change is retested"
593 self.executor_server.hold_jobs_in_build = True
594
595 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
596 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
597
598 self.executor_server.failJob('project-test1', A)
599
600 self.fake_github.emitEvent(A.addLabel('merge'))
601 self.fake_github.emitEvent(B.addLabel('merge'))
602 self.waitUntilSettled()
603
604 self.executor_server.release('.*-merge')
605 self.waitUntilSettled()
606
607 self.executor_server.hold_jobs_in_build = False
608 self.executor_server.release()
609
610 self.waitUntilSettled()
611 # It's certain that the merge job for change 2 will run, but
612 # the test1 and test2 jobs may or may not run.
613 self.assertTrue(len(self.history) > 6)
614 self.assertFalse(A.is_merged)
615 self.assertTrue(B.is_merged)
616 self.assertNotIn('merge', A.labels)
617 self.assertNotIn('merge', B.labels)
618
619 @simple_layout('layouts/dependent-github.yaml', driver='github')
620 def test_failed_change_at_head(self):
621 "Test that if a change at the head fails, jobs behind it are canceled"
622
623 self.executor_server.hold_jobs_in_build = True
624 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
625 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
626 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
627
628 self.executor_server.failJob('project-test1', A)
629
630 self.fake_github.emitEvent(A.addLabel('merge'))
631 self.fake_github.emitEvent(B.addLabel('merge'))
632 self.fake_github.emitEvent(C.addLabel('merge'))
633
634 self.waitUntilSettled()
635
636 self.assertEqual(len(self.builds), 1)
637 self.assertEqual(self.builds[0].name, 'project-merge')
638 self.assertTrue(self.builds[0].hasChanges(A))
639
640 self.executor_server.release('.*-merge')
641 self.waitUntilSettled()
642 self.executor_server.release('.*-merge')
643 self.waitUntilSettled()
644 self.executor_server.release('.*-merge')
645 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
655 self.release(self.builds[0])
656 self.waitUntilSettled()
657
658 # project-test2, project-merge for B
659 self.assertEqual(len(self.builds), 2)
660 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
661
662 self.executor_server.hold_jobs_in_build = False
663 self.executor_server.release()
664 self.waitUntilSettled()
665
666 self.assertEqual(len(self.builds), 0)
667 self.assertEqual(len(self.history), 15)
668 self.assertFalse(A.is_merged)
669 self.assertTrue(B.is_merged)
670 self.assertTrue(C.is_merged)
671 self.assertNotIn('merge', A.labels)
672 self.assertNotIn('merge', B.labels)
673 self.assertNotIn('merge', C.labels)
Jesse Keating71a47ff2017-06-06 11:36:43 -0700674
675 @simple_layout('layouts/basic-github.yaml', driver='github')
676 def test_push_event_reconfigure(self):
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200677 pevent = self.fake_github.getPushEvent(project='org/common-config',
Jesse Keating71a47ff2017-06-06 11:36:43 -0700678 ref='refs/heads/master',
679 modified_files=['zuul.yaml'])
680
681 # record previous tenant reconfiguration time, which may not be set
682 old = self.sched.tenant_last_reconfigured.get('tenant-one', 0)
683 time.sleep(1)
684 self.fake_github.emitEvent(pevent)
685 self.waitUntilSettled()
686 new = self.sched.tenant_last_reconfigured.get('tenant-one', 0)
687 # New timestamp should be greater than the old timestamp
688 self.assertLess(old, new)
Tristan Cacqueray2bafb1f2017-06-12 07:10:26 +0000689
690 @simple_layout('layouts/basic-github.yaml', driver='github')
691 def test_ping_event(self):
692 # Test valid ping
693 pevent = {'repository': {'full_name': 'org/project'}}
694 req = self.fake_github.emitEvent(('ping', pevent))
695 self.assertEqual(req.status, 200, "Ping event didn't succeed")
696
697 # Test invalid ping
698 pevent = {'repository': {'full_name': 'unknown-project'}}
699 self.assertRaises(
700 urllib.error.HTTPError,
701 self.fake_github.emitEvent,
702 ('ping', pevent),
703 )
Tobias Henkeleca46202017-08-02 20:27:10 +0200704
705
706class TestGithubUnprotectedBranches(ZuulTestCase):
707 config_file = 'zuul-github-driver.conf'
708 tenant_config_file = 'config/unprotected-branches/main.yaml'
709
710 def test_unprotected_branches(self):
711 tenant = self.sched.abide.tenants.get('tenant-one')
712
713 project1 = tenant.untrusted_projects[0]
714 project2 = tenant.untrusted_projects[1]
715
716 # project1 should have parsed master
717 self.assertIn('master', project1.unparsed_branch_config.keys())
718
719 # project2 should have no parsed branch
720 self.assertEqual(0, len(project2.unparsed_branch_config.keys()))