blob: a0882364896bdeb804169e3cd4b6df5c7f7431c3 [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'])
James E. Blairb8203e42017-08-02 17:00:14 -0700113 self.assertFalse('oldrev' in build_params['zuul'])
James E. Blaired8b0012017-07-21 14:49:29 -0700114 self.assertEqual(sha, build_params['zuul']['newrev'])
Wayne1a78c612015-06-11 17:14:13 -0700115 self.executor_server.hold_jobs_in_build = False
116 self.executor_server.release()
117 self.waitUntilSettled()
118
119 self.assertEqual('SUCCESS',
120 self.getJobFromHistory('project-tag').result)
121
122 @simple_layout('layouts/push-tag-github.yaml', driver='github')
123 def test_push_event(self):
124 self.executor_server.hold_jobs_in_build = True
125
James E. Blair289f5932017-07-27 15:02:29 -0700126 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
127 old_sha = '0' * 40
128 new_sha = A.head_sha
129 A.setMerged("merging A")
130 pevent = self.fake_github.getPushEvent(project='org/project',
131 ref='refs/heads/master',
132 old_rev=old_sha,
133 new_rev=new_sha)
134 self.fake_github.emitEvent(pevent)
Wayne1a78c612015-06-11 17:14:13 -0700135 self.waitUntilSettled()
136
137 build_params = self.builds[0].parameters
James E. Blaira438c172017-07-21 14:54:42 -0700138 self.assertEqual('refs/heads/master', build_params['zuul']['ref'])
James E. Blair289f5932017-07-27 15:02:29 -0700139 self.assertFalse('oldrev' in build_params['zuul'])
James E. Blaired8b0012017-07-21 14:49:29 -0700140 self.assertEqual(new_sha, build_params['zuul']['newrev'])
Wayne1a78c612015-06-11 17:14:13 -0700141
142 self.executor_server.hold_jobs_in_build = False
143 self.executor_server.release()
144 self.waitUntilSettled()
145
146 self.assertEqual('SUCCESS',
147 self.getJobFromHistory('project-post').result)
Jan Hruband4edee82015-12-16 12:49:51 +0100148 self.assertEqual(1, len(self.history))
149
150 # test unmatched push event
151 old_sha = random_sha1()
152 new_sha = random_sha1()
153 self.fake_github.emitEvent(
154 self.fake_github.getPushEvent('org/project',
155 'refs/heads/unmatched_branch',
156 old_sha, new_sha))
157 self.waitUntilSettled()
158
159 self.assertEqual(1, len(self.history))
Jan Hruban6d53c5e2015-10-24 03:03:34 +0200160
Jan Hruban16ad31f2015-11-07 14:39:07 +0100161 @simple_layout('layouts/labeling-github.yaml', driver='github')
162 def test_labels(self):
Jan Hruban37615e52015-11-19 14:30:49 +0100163 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
Jan Hruban16ad31f2015-11-07 14:39:07 +0100164 self.fake_github.emitEvent(A.addLabel('test'))
165 self.waitUntilSettled()
166 self.assertEqual(1, len(self.history))
167 self.assertEqual('project-labels', self.history[0].name)
168 self.assertEqual(['tests passed'], A.labels)
169
170 # test label removed
Jan Hruban37615e52015-11-19 14:30:49 +0100171 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
Jan Hruban16ad31f2015-11-07 14:39:07 +0100172 B.addLabel('do not test')
173 self.fake_github.emitEvent(B.removeLabel('do not test'))
174 self.waitUntilSettled()
175 self.assertEqual(2, len(self.history))
176 self.assertEqual('project-labels', self.history[1].name)
177 self.assertEqual(['tests passed'], B.labels)
178
179 # test unmatched label
Jan Hruban37615e52015-11-19 14:30:49 +0100180 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
Jan Hruban16ad31f2015-11-07 14:39:07 +0100181 self.fake_github.emitEvent(C.addLabel('other label'))
182 self.waitUntilSettled()
183 self.assertEqual(2, len(self.history))
184 self.assertEqual(['other label'], C.labels)
185
Jesse Keating5c05a9f2017-01-12 14:44:58 -0800186 @simple_layout('layouts/reviews-github.yaml', driver='github')
187 def test_review_event(self):
188 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
189 self.fake_github.emitEvent(A.getReviewAddedEvent('approve'))
190 self.waitUntilSettled()
191 self.assertEqual(1, len(self.history))
192 self.assertEqual('project-reviews', self.history[0].name)
193 self.assertEqual(['tests passed'], A.labels)
194
195 # test_review_unmatched_event
196 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
197 self.fake_github.emitEvent(B.getReviewAddedEvent('comment'))
198 self.waitUntilSettled()
199 self.assertEqual(1, len(self.history))
200
Jan Hruban324ca5b2015-11-05 19:28:54 +0100201 @simple_layout('layouts/dequeue-github.yaml', driver='github')
202 def test_dequeue_pull_synchronized(self):
203 self.executor_server.hold_jobs_in_build = True
204
Jan Hruban37615e52015-11-19 14:30:49 +0100205 A = self.fake_github.openFakePullRequest(
206 'org/one-job-project', 'master', 'A')
207 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100208 self.waitUntilSettled()
209
210 # event update stamp has resolution one second, wait so the latter
211 # one has newer timestamp
212 time.sleep(1)
Jan Hruban37615e52015-11-19 14:30:49 +0100213 A.addCommit()
214 self.fake_github.emitEvent(A.getPullRequestSynchronizeEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100215 self.waitUntilSettled()
216
217 self.executor_server.hold_jobs_in_build = False
218 self.executor_server.release()
219 self.waitUntilSettled()
220
221 self.assertEqual(2, len(self.history))
222 self.assertEqual(1, self.countJobResults(self.history, 'ABORTED'))
223
224 @simple_layout('layouts/dequeue-github.yaml', driver='github')
225 def test_dequeue_pull_abandoned(self):
226 self.executor_server.hold_jobs_in_build = True
227
Jan Hruban37615e52015-11-19 14:30:49 +0100228 A = self.fake_github.openFakePullRequest(
229 'org/one-job-project', 'master', 'A')
230 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100231 self.waitUntilSettled()
Jan Hruban37615e52015-11-19 14:30:49 +0100232 self.fake_github.emitEvent(A.getPullRequestClosedEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100233 self.waitUntilSettled()
234
235 self.executor_server.hold_jobs_in_build = False
236 self.executor_server.release()
237 self.waitUntilSettled()
238
239 self.assertEqual(1, len(self.history))
240 self.assertEqual(1, self.countJobResults(self.history, 'ABORTED'))
241
Jan Hruban6d53c5e2015-10-24 03:03:34 +0200242 @simple_layout('layouts/basic-github.yaml', driver='github')
243 def test_git_https_url(self):
244 """Test that git_ssh option gives git url with ssh"""
245 url = self.fake_github.real_getGitUrl('org/project')
246 self.assertEqual('https://github.com/org/project', url)
247
248 @simple_layout('layouts/basic-github.yaml', driver='github')
249 def test_git_ssh_url(self):
250 """Test that git_ssh option gives git url with ssh"""
251 url = self.fake_github_ssh.real_getGitUrl('org/project')
252 self.assertEqual('ssh://git@github.com/org/project.git', url)
Jan Hrubane252a732017-01-03 15:03:09 +0100253
Jesse Keatingbe4ef8a2016-12-06 11:29:13 -0800254 @simple_layout('layouts/basic-github.yaml', driver='github')
255 def test_git_enterprise_url(self):
256 """Test that git_url option gives git url with proper host"""
257 url = self.fake_github_ent.real_getGitUrl('org/project')
258 self.assertEqual('ssh://git@github.enterprise.io/org/project.git', url)
259
Jan Hrubane252a732017-01-03 15:03:09 +0100260 @simple_layout('layouts/reporting-github.yaml', driver='github')
261 def test_reporting(self):
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700262 project = 'org/project'
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200263 github = self.fake_github.github_client
264
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()
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200270
Jesse Keatingd96e5882017-01-19 13:55:50 -0800271 # We should have a status container for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200272 self.assertIn(
273 A.head_sha, github.repo_from_project(project)._commits.keys())
274 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
275
Jesse Keatingd96e5882017-01-19 13:55:50 -0800276 # We should only have one status for the head sha
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700277 self.assertEqual(1, len(statuses))
278 check_status = statuses[0]
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100279 check_url = ('http://zuul.example.com/status/#%s,%s' %
280 (A.number, A.head_sha))
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000281 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700282 self.assertEqual('check status: pending',
283 check_status['description'])
Jan Hrubane252a732017-01-03 15:03:09 +0100284 self.assertEqual('pending', check_status['state'])
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100285 self.assertEqual(check_url, check_status['url'])
Jan Hruban37615e52015-11-19 14:30:49 +0100286 self.assertEqual(0, len(A.comments))
Jan Hrubane252a732017-01-03 15:03:09 +0100287
288 self.executor_server.hold_jobs_in_build = False
289 self.executor_server.release()
290 self.waitUntilSettled()
Jesse Keatingd96e5882017-01-19 13:55:50 -0800291 # We should only have two statuses for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200292 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700293 self.assertEqual(2, len(statuses))
294 check_status = statuses[0]
Jesse Keatingd96e5882017-01-19 13:55:50 -0800295 check_url = ('http://zuul.example.com/status/#%s,%s' %
296 (A.number, A.head_sha))
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000297 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700298 self.assertEqual('check status: success',
299 check_status['description'])
Jan Hrubane252a732017-01-03 15:03:09 +0100300 self.assertEqual('success', check_status['state'])
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100301 self.assertEqual(check_url, check_status['url'])
Jan Hruban37615e52015-11-19 14:30:49 +0100302 self.assertEqual(1, len(A.comments))
303 self.assertThat(A.comments[0],
Jan Hrubane252a732017-01-03 15:03:09 +0100304 MatchesRegex('.*Build succeeded.*', re.DOTALL))
305
306 # pipeline does not report any status but does comment
307 self.executor_server.hold_jobs_in_build = True
308 self.fake_github.emitEvent(
Jan Hruban37615e52015-11-19 14:30:49 +0100309 A.getCommentAddedEvent('reporting check'))
Jan Hrubane252a732017-01-03 15:03:09 +0100310 self.waitUntilSettled()
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200311 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700312 self.assertEqual(2, len(statuses))
Jan Hrubane252a732017-01-03 15:03:09 +0100313 # comments increased by one for the start message
Jan Hruban37615e52015-11-19 14:30:49 +0100314 self.assertEqual(2, len(A.comments))
315 self.assertThat(A.comments[1],
Jan Hrubane252a732017-01-03 15:03:09 +0100316 MatchesRegex('.*Starting reporting jobs.*', re.DOTALL))
317 self.executor_server.hold_jobs_in_build = False
318 self.executor_server.release()
319 self.waitUntilSettled()
Jesse Keatingd96e5882017-01-19 13:55:50 -0800320 # pipeline reports success status
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200321 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700322 self.assertEqual(3, len(statuses))
323 report_status = statuses[0]
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000324 self.assertEqual('tenant-one/reporting', report_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700325 self.assertEqual('reporting status: success',
326 report_status['description'])
Jesse Keatingd96e5882017-01-19 13:55:50 -0800327 self.assertEqual('success', report_status['state'])
Jan Hruban37615e52015-11-19 14:30:49 +0100328 self.assertEqual(2, len(A.comments))
Jamie Lennox3f16de52017-05-09 14:24:11 +1000329
330 base = 'http://logs.example.com/tenant-one/reporting/%s/%s/' % (
331 A.project, A.number)
332
333 # Deconstructing the URL because we don't save the BuildSet UUID
334 # anywhere to do a direct comparison and doing regexp matches on a full
335 # URL is painful.
336
337 # The first part of the URL matches the easy base string
338 self.assertThat(report_status['url'], StartsWith(base))
339
340 # The rest of the URL is a UUID and a trailing slash.
341 self.assertThat(report_status['url'][len(base):],
342 MatchesRegex('^[a-fA-F0-9]{32}\/$'))
Jan Hruban49bff072015-11-03 11:45:46 +0100343
Jesse Keating08dab8f2017-06-21 12:59:23 +0100344 @simple_layout('layouts/reporting-github.yaml', driver='github')
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700345 def test_truncated_status_description(self):
346 project = 'org/project'
347 # pipeline reports pull status both on start and success
348 self.executor_server.hold_jobs_in_build = True
349 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
350 self.fake_github.emitEvent(
351 A.getCommentAddedEvent('long pipeline'))
352 self.waitUntilSettled()
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200353 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700354 self.assertEqual(1, len(statuses))
355 check_status = statuses[0]
356 # Status is truncated due to long pipeline name
357 self.assertEqual('status: pending',
358 check_status['description'])
359
360 self.executor_server.hold_jobs_in_build = False
361 self.executor_server.release()
362 self.waitUntilSettled()
363 # We should only have two statuses for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200364 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700365 self.assertEqual(2, len(statuses))
366 check_status = statuses[0]
367 # Status is truncated due to long pipeline name
368 self.assertEqual('status: success',
369 check_status['description'])
370
371 @simple_layout('layouts/reporting-github.yaml', driver='github')
Jesse Keating08dab8f2017-06-21 12:59:23 +0100372 def test_push_reporting(self):
373 project = 'org/project2'
374 # pipeline reports pull status both on start and success
375 self.executor_server.hold_jobs_in_build = True
Jesse Keating08dab8f2017-06-21 12:59:23 +0100376
James E. Blair289f5932017-07-27 15:02:29 -0700377 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
378 old_sha = '0' * 40
379 new_sha = A.head_sha
380 A.setMerged("merging A")
381 pevent = self.fake_github.getPushEvent(project=project,
382 ref='refs/heads/master',
383 old_rev=old_sha,
384 new_rev=new_sha)
Jesse Keating08dab8f2017-06-21 12:59:23 +0100385 self.fake_github.emitEvent(pevent)
386 self.waitUntilSettled()
387
388 # there should only be one report, a status
389 self.assertEqual(1, len(self.fake_github.reports))
390 # Verify the user/context/state of the status
391 status = ('zuul', 'tenant-one/push-reporting', 'pending')
392 self.assertEqual(status, self.fake_github.reports[0][-1])
393
394 # free the executor, allow the build to finish
395 self.executor_server.hold_jobs_in_build = False
396 self.executor_server.release()
397 self.waitUntilSettled()
398
399 # Now there should be a second report, the success of the build
400 self.assertEqual(2, len(self.fake_github.reports))
401 # Verify the user/context/state of the status
402 status = ('zuul', 'tenant-one/push-reporting', 'success')
403 self.assertEqual(status, self.fake_github.reports[-1][-1])
404
405 # now make a PR which should also comment
406 self.executor_server.hold_jobs_in_build = True
407 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
408 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
409 self.waitUntilSettled()
410
411 # Now there should be a four reports, a new comment
412 # and status
413 self.assertEqual(4, len(self.fake_github.reports))
414 self.executor_server.release()
415 self.waitUntilSettled()
416
Jan Hruban49bff072015-11-03 11:45:46 +0100417 @simple_layout('layouts/merging-github.yaml', driver='github')
418 def test_report_pull_merge(self):
419 # pipeline merges the pull request on success
Jan Hruban3b415922016-02-03 13:10:22 +0100420 A = self.fake_github.openFakePullRequest('org/project', 'master',
421 'PR title')
Jan Hruban49bff072015-11-03 11:45:46 +0100422 self.fake_github.emitEvent(A.getCommentAddedEvent('merge me'))
423 self.waitUntilSettled()
424 self.assertTrue(A.is_merged)
Jan Hruban3b415922016-02-03 13:10:22 +0100425 self.assertThat(A.merge_message,
426 MatchesRegex('.*PR title.*Reviewed-by.*', re.DOTALL))
Jan Hruban49bff072015-11-03 11:45:46 +0100427
428 # pipeline merges the pull request on success after failure
429 self.fake_github.merge_failure = True
Jan Hruban37615e52015-11-19 14:30:49 +0100430 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
Jan Hruban49bff072015-11-03 11:45:46 +0100431 self.fake_github.emitEvent(B.getCommentAddedEvent('merge me'))
432 self.waitUntilSettled()
433 self.assertFalse(B.is_merged)
434 self.fake_github.merge_failure = False
435
436 # pipeline merges the pull request on second run of merge
437 # first merge failed on 405 Method Not Allowed error
438 self.fake_github.merge_not_allowed_count = 1
Jan Hruban37615e52015-11-19 14:30:49 +0100439 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
Jan Hruban49bff072015-11-03 11:45:46 +0100440 self.fake_github.emitEvent(C.getCommentAddedEvent('merge me'))
441 self.waitUntilSettled()
442 self.assertTrue(C.is_merged)
443
444 # pipeline does not merge the pull request
445 # merge failed on 405 Method Not Allowed error - twice
446 self.fake_github.merge_not_allowed_count = 2
Jan Hruban37615e52015-11-19 14:30:49 +0100447 D = self.fake_github.openFakePullRequest('org/project', 'master', 'D')
Jan Hruban49bff072015-11-03 11:45:46 +0100448 self.fake_github.emitEvent(D.getCommentAddedEvent('merge me'))
449 self.waitUntilSettled()
450 self.assertFalse(D.is_merged)
Adam Gandelman62198cb2017-02-14 16:11:02 -0800451 self.assertEqual(len(D.comments), 1)
452 self.assertEqual(D.comments[0], 'Merge failed')
Jan Hruban37615e52015-11-19 14:30:49 +0100453
Jesse Keating28434312017-07-31 11:32:48 -0700454 @simple_layout('layouts/reporting-multiple-github.yaml', driver='github')
455 def test_reporting_multiple_github(self):
456 project = 'org/project1'
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200457 github = self.fake_github.github_client
458
Jesse Keating28434312017-07-31 11:32:48 -0700459 # pipeline reports pull status both on start and success
460 self.executor_server.hold_jobs_in_build = True
461 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
462 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
463 # open one on B as well, which should not effect A reporting
464 B = self.fake_github.openFakePullRequest('org/project2', 'master',
465 'B')
466 self.fake_github.emitEvent(B.getPullRequestOpenedEvent())
467 self.waitUntilSettled()
468 # We should have a status container for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200469 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
470 self.assertIn(
471 A.head_sha, github.repo_from_project(project)._commits.keys())
Jesse Keating28434312017-07-31 11:32:48 -0700472 # We should only have one status for the head sha
473 self.assertEqual(1, len(statuses))
474 check_status = statuses[0]
475 check_url = ('http://zuul.example.com/status/#%s,%s' %
476 (A.number, A.head_sha))
477 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700478 self.assertEqual('check status: pending', check_status['description'])
Jesse Keating28434312017-07-31 11:32:48 -0700479 self.assertEqual('pending', check_status['state'])
480 self.assertEqual(check_url, check_status['url'])
481 self.assertEqual(0, len(A.comments))
482
483 self.executor_server.hold_jobs_in_build = False
484 self.executor_server.release()
485 self.waitUntilSettled()
486 # We should only have two statuses for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200487 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating28434312017-07-31 11:32:48 -0700488 self.assertEqual(2, len(statuses))
489 check_status = statuses[0]
490 check_url = ('http://zuul.example.com/status/#%s,%s' %
491 (A.number, A.head_sha))
492 self.assertEqual('tenant-one/check', check_status['context'])
493 self.assertEqual('success', check_status['state'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700494 self.assertEqual('check status: success', check_status['description'])
Jesse Keating28434312017-07-31 11:32:48 -0700495 self.assertEqual(check_url, check_status['url'])
496 self.assertEqual(1, len(A.comments))
497 self.assertThat(A.comments[0],
498 MatchesRegex('.*Build succeeded.*', re.DOTALL))
499
Jan Hruban37615e52015-11-19 14:30:49 +0100500 @simple_layout('layouts/dependent-github.yaml', driver='github')
501 def test_parallel_changes(self):
502 "Test that changes are tested in parallel and merged in series"
503
504 self.executor_server.hold_jobs_in_build = True
505 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
506 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
507 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
508
509 self.fake_github.emitEvent(A.addLabel('merge'))
510 self.fake_github.emitEvent(B.addLabel('merge'))
511 self.fake_github.emitEvent(C.addLabel('merge'))
512
513 self.waitUntilSettled()
514 self.assertEqual(len(self.builds), 1)
515 self.assertEqual(self.builds[0].name, 'project-merge')
516 self.assertTrue(self.builds[0].hasChanges(A))
517
518 self.executor_server.release('.*-merge')
519 self.waitUntilSettled()
520 self.assertEqual(len(self.builds), 3)
521 self.assertEqual(self.builds[0].name, 'project-test1')
522 self.assertTrue(self.builds[0].hasChanges(A))
523 self.assertEqual(self.builds[1].name, 'project-test2')
524 self.assertTrue(self.builds[1].hasChanges(A))
525 self.assertEqual(self.builds[2].name, 'project-merge')
526 self.assertTrue(self.builds[2].hasChanges(A, B))
527
528 self.executor_server.release('.*-merge')
529 self.waitUntilSettled()
530 self.assertEqual(len(self.builds), 5)
531 self.assertEqual(self.builds[0].name, 'project-test1')
532 self.assertTrue(self.builds[0].hasChanges(A))
533 self.assertEqual(self.builds[1].name, 'project-test2')
534 self.assertTrue(self.builds[1].hasChanges(A))
535
536 self.assertEqual(self.builds[2].name, 'project-test1')
537 self.assertTrue(self.builds[2].hasChanges(A))
538 self.assertEqual(self.builds[3].name, 'project-test2')
539 self.assertTrue(self.builds[3].hasChanges(A, B))
540
541 self.assertEqual(self.builds[4].name, 'project-merge')
542 self.assertTrue(self.builds[4].hasChanges(A, B, C))
543
544 self.executor_server.release('.*-merge')
545 self.waitUntilSettled()
546 self.assertEqual(len(self.builds), 6)
547 self.assertEqual(self.builds[0].name, 'project-test1')
548 self.assertTrue(self.builds[0].hasChanges(A))
549 self.assertEqual(self.builds[1].name, 'project-test2')
550 self.assertTrue(self.builds[1].hasChanges(A))
551
552 self.assertEqual(self.builds[2].name, 'project-test1')
553 self.assertTrue(self.builds[2].hasChanges(A, B))
554 self.assertEqual(self.builds[3].name, 'project-test2')
555 self.assertTrue(self.builds[3].hasChanges(A, B))
556
557 self.assertEqual(self.builds[4].name, 'project-test1')
558 self.assertTrue(self.builds[4].hasChanges(A, B, C))
559 self.assertEqual(self.builds[5].name, 'project-test2')
560 self.assertTrue(self.builds[5].hasChanges(A, B, C))
561
562 all_builds = self.builds[:]
563 self.release(all_builds[2])
564 self.release(all_builds[3])
565 self.waitUntilSettled()
566 self.assertFalse(A.is_merged)
567 self.assertFalse(B.is_merged)
568 self.assertFalse(C.is_merged)
569
570 self.release(all_builds[0])
571 self.release(all_builds[1])
572 self.waitUntilSettled()
573 self.assertTrue(A.is_merged)
574 self.assertTrue(B.is_merged)
575 self.assertFalse(C.is_merged)
576
577 self.executor_server.hold_jobs_in_build = False
578 self.executor_server.release()
579 self.waitUntilSettled()
580 self.assertEqual(len(self.builds), 0)
581 self.assertEqual(len(self.history), 9)
582 self.assertTrue(C.is_merged)
583
584 self.assertNotIn('merge', A.labels)
585 self.assertNotIn('merge', B.labels)
586 self.assertNotIn('merge', C.labels)
587
588 @simple_layout('layouts/dependent-github.yaml', driver='github')
589 def test_failed_changes(self):
590 "Test that a change behind a failed change is retested"
591 self.executor_server.hold_jobs_in_build = True
592
593 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
594 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
595
596 self.executor_server.failJob('project-test1', A)
597
598 self.fake_github.emitEvent(A.addLabel('merge'))
599 self.fake_github.emitEvent(B.addLabel('merge'))
600 self.waitUntilSettled()
601
602 self.executor_server.release('.*-merge')
603 self.waitUntilSettled()
604
605 self.executor_server.hold_jobs_in_build = False
606 self.executor_server.release()
607
608 self.waitUntilSettled()
609 # It's certain that the merge job for change 2 will run, but
610 # the test1 and test2 jobs may or may not run.
611 self.assertTrue(len(self.history) > 6)
612 self.assertFalse(A.is_merged)
613 self.assertTrue(B.is_merged)
614 self.assertNotIn('merge', A.labels)
615 self.assertNotIn('merge', B.labels)
616
617 @simple_layout('layouts/dependent-github.yaml', driver='github')
618 def test_failed_change_at_head(self):
619 "Test that if a change at the head fails, jobs behind it are canceled"
620
621 self.executor_server.hold_jobs_in_build = True
622 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
623 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
624 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
625
626 self.executor_server.failJob('project-test1', A)
627
628 self.fake_github.emitEvent(A.addLabel('merge'))
629 self.fake_github.emitEvent(B.addLabel('merge'))
630 self.fake_github.emitEvent(C.addLabel('merge'))
631
632 self.waitUntilSettled()
633
634 self.assertEqual(len(self.builds), 1)
635 self.assertEqual(self.builds[0].name, 'project-merge')
636 self.assertTrue(self.builds[0].hasChanges(A))
637
638 self.executor_server.release('.*-merge')
639 self.waitUntilSettled()
640 self.executor_server.release('.*-merge')
641 self.waitUntilSettled()
642 self.executor_server.release('.*-merge')
643 self.waitUntilSettled()
644
645 self.assertEqual(len(self.builds), 6)
646 self.assertEqual(self.builds[0].name, 'project-test1')
647 self.assertEqual(self.builds[1].name, 'project-test2')
648 self.assertEqual(self.builds[2].name, 'project-test1')
649 self.assertEqual(self.builds[3].name, 'project-test2')
650 self.assertEqual(self.builds[4].name, 'project-test1')
651 self.assertEqual(self.builds[5].name, 'project-test2')
652
653 self.release(self.builds[0])
654 self.waitUntilSettled()
655
656 # project-test2, project-merge for B
657 self.assertEqual(len(self.builds), 2)
658 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
659
660 self.executor_server.hold_jobs_in_build = False
661 self.executor_server.release()
662 self.waitUntilSettled()
663
664 self.assertEqual(len(self.builds), 0)
665 self.assertEqual(len(self.history), 15)
666 self.assertFalse(A.is_merged)
667 self.assertTrue(B.is_merged)
668 self.assertTrue(C.is_merged)
669 self.assertNotIn('merge', A.labels)
670 self.assertNotIn('merge', B.labels)
671 self.assertNotIn('merge', C.labels)
Jesse Keating71a47ff2017-06-06 11:36:43 -0700672
673 @simple_layout('layouts/basic-github.yaml', driver='github')
674 def test_push_event_reconfigure(self):
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200675 pevent = self.fake_github.getPushEvent(project='org/common-config',
Jesse Keating71a47ff2017-06-06 11:36:43 -0700676 ref='refs/heads/master',
677 modified_files=['zuul.yaml'])
678
679 # record previous tenant reconfiguration time, which may not be set
680 old = self.sched.tenant_last_reconfigured.get('tenant-one', 0)
681 time.sleep(1)
682 self.fake_github.emitEvent(pevent)
683 self.waitUntilSettled()
684 new = self.sched.tenant_last_reconfigured.get('tenant-one', 0)
685 # New timestamp should be greater than the old timestamp
686 self.assertLess(old, new)
Tristan Cacqueray2bafb1f2017-06-12 07:10:26 +0000687
688 @simple_layout('layouts/basic-github.yaml', driver='github')
689 def test_ping_event(self):
690 # Test valid ping
691 pevent = {'repository': {'full_name': 'org/project'}}
692 req = self.fake_github.emitEvent(('ping', pevent))
693 self.assertEqual(req.status, 200, "Ping event didn't succeed")
694
695 # Test invalid ping
696 pevent = {'repository': {'full_name': 'unknown-project'}}
697 self.assertRaises(
698 urllib.error.HTTPError,
699 self.fake_github.emitEvent,
700 ('ping', pevent),
701 )
Tobias Henkeleca46202017-08-02 20:27:10 +0200702
703
704class TestGithubUnprotectedBranches(ZuulTestCase):
705 config_file = 'zuul-github-driver.conf'
706 tenant_config_file = 'config/unprotected-branches/main.yaml'
707
708 def test_unprotected_branches(self):
709 tenant = self.sched.abide.tenants.get('tenant-one')
710
711 project1 = tenant.untrusted_projects[0]
712 project2 = tenant.untrusted_projects[1]
713
714 # project1 should have parsed master
715 self.assertIn('master', project1.unparsed_branch_config.keys())
716
717 # project2 should have no parsed branch
718 self.assertEqual(0, len(project2.unparsed_branch_config.keys()))