blob: ebb5e1c8501f25a18034bd8973a59039ee5d43e1 [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
Jesse Keating64d29012017-09-06 12:27:49 -070020from unittest import skip
Gregory Haynes4fc12542015-04-22 20:38:06 -070021
James E. Blair21037782017-07-19 11:56:55 -070022import git
23
Wayne1a78c612015-06-11 17:14:13 -070024from tests.base import ZuulTestCase, simple_layout, random_sha1
Gregory Haynes4fc12542015-04-22 20:38:06 -070025
Gregory Haynes4fc12542015-04-22 20:38:06 -070026
27class TestGithubDriver(ZuulTestCase):
28 config_file = 'zuul-github-driver.conf'
29
30 @simple_layout('layouts/basic-github.yaml', driver='github')
31 def test_pull_event(self):
32 self.executor_server.hold_jobs_in_build = True
33
Jan Hruban37615e52015-11-19 14:30:49 +010034 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
35 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Gregory Haynes4fc12542015-04-22 20:38:06 -070036 self.waitUntilSettled()
37
Gregory Haynes4fc12542015-04-22 20:38:06 -070038 self.executor_server.hold_jobs_in_build = False
39 self.executor_server.release()
40 self.waitUntilSettled()
41
42 self.assertEqual('SUCCESS',
43 self.getJobFromHistory('project-test1').result)
44 self.assertEqual('SUCCESS',
45 self.getJobFromHistory('project-test2').result)
46
47 job = self.getJobFromHistory('project-test2')
Monty Taylord13bc362017-06-30 13:11:37 -050048 zuulvars = job.parameters['zuul']
James E. Blaire3db2952017-07-21 15:03:36 -070049 self.assertEqual(str(A.number), zuulvars['change'])
50 self.assertEqual(str(A.head_sha), zuulvars['patchset'])
James E. Blair3b222492017-07-21 15:17:37 -070051 self.assertEqual('master', zuulvars['branch'])
Jan Hruban37615e52015-11-19 14:30:49 +010052 self.assertEqual(1, len(A.comments))
Jan Hruband4edee82015-12-16 12:49:51 +010053 self.assertEqual(2, len(self.history))
54
55 # test_pull_unmatched_branch_event(self):
56 self.create_branch('org/project', 'unmatched_branch')
57 B = self.fake_github.openFakePullRequest(
58 'org/project', 'unmatched_branch', 'B')
59 self.fake_github.emitEvent(B.getPullRequestOpenedEvent())
60 self.waitUntilSettled()
61
62 self.assertEqual(2, len(self.history))
Wayne1a78c612015-06-11 17:14:13 -070063
Jan Hruban570d01c2016-03-10 21:51:32 +010064 @simple_layout('layouts/files-github.yaml', driver='github')
65 def test_pull_matched_file_event(self):
66 A = self.fake_github.openFakePullRequest(
67 'org/project', 'master', 'A',
68 files=['random.txt', 'build-requires'])
69 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
70 self.waitUntilSettled()
71 self.assertEqual(1, len(self.history))
72
73 # test_pull_unmatched_file_event
74 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B',
75 files=['random.txt'])
76 self.fake_github.emitEvent(B.getPullRequestOpenedEvent())
77 self.waitUntilSettled()
78 self.assertEqual(1, len(self.history))
79
Jan Hrubanc7ab1602015-10-14 15:29:33 +020080 @simple_layout('layouts/basic-github.yaml', driver='github')
81 def test_comment_event(self):
Jan Hruban37615e52015-11-19 14:30:49 +010082 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
83 self.fake_github.emitEvent(A.getCommentAddedEvent('test me'))
Jan Hrubanc7ab1602015-10-14 15:29:33 +020084 self.waitUntilSettled()
85 self.assertEqual(2, len(self.history))
86
87 # Test an unmatched comment, history should remain the same
Jan Hruban37615e52015-11-19 14:30:49 +010088 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
89 self.fake_github.emitEvent(B.getCommentAddedEvent('casual comment'))
Jan Hrubanc7ab1602015-10-14 15:29:33 +020090 self.waitUntilSettled()
91 self.assertEqual(2, len(self.history))
92
Wayne1a78c612015-06-11 17:14:13 -070093 @simple_layout('layouts/push-tag-github.yaml', driver='github')
94 def test_tag_event(self):
95 self.executor_server.hold_jobs_in_build = True
96
James E. Blair21037782017-07-19 11:56:55 -070097 self.create_branch('org/project', 'tagbranch')
98 files = {'README.txt': 'test'}
99 self.addCommitToRepo('org/project', 'test tag',
100 files, branch='tagbranch', tag='newtag')
101 path = os.path.join(self.upstream_root, 'org/project')
102 repo = git.Repo(path)
103 tag = repo.tags['newtag']
104 sha = tag.commit.hexsha
105 del repo
106
Wayne1a78c612015-06-11 17:14:13 -0700107 self.fake_github.emitEvent(
108 self.fake_github.getPushEvent('org/project', 'refs/tags/newtag',
109 new_rev=sha))
110 self.waitUntilSettled()
111
112 build_params = self.builds[0].parameters
James E. Blaira438c172017-07-21 14:54:42 -0700113 self.assertEqual('refs/tags/newtag', build_params['zuul']['ref'])
James E. Blairb8203e42017-08-02 17:00:14 -0700114 self.assertFalse('oldrev' in build_params['zuul'])
James E. Blaired8b0012017-07-21 14:49:29 -0700115 self.assertEqual(sha, build_params['zuul']['newrev'])
Wayne1a78c612015-06-11 17:14:13 -0700116 self.executor_server.hold_jobs_in_build = False
117 self.executor_server.release()
118 self.waitUntilSettled()
119
120 self.assertEqual('SUCCESS',
121 self.getJobFromHistory('project-tag').result)
122
123 @simple_layout('layouts/push-tag-github.yaml', driver='github')
124 def test_push_event(self):
125 self.executor_server.hold_jobs_in_build = True
126
James E. Blair289f5932017-07-27 15:02:29 -0700127 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
128 old_sha = '0' * 40
129 new_sha = A.head_sha
130 A.setMerged("merging A")
131 pevent = self.fake_github.getPushEvent(project='org/project',
132 ref='refs/heads/master',
133 old_rev=old_sha,
134 new_rev=new_sha)
135 self.fake_github.emitEvent(pevent)
Wayne1a78c612015-06-11 17:14:13 -0700136 self.waitUntilSettled()
137
138 build_params = self.builds[0].parameters
James E. Blaira438c172017-07-21 14:54:42 -0700139 self.assertEqual('refs/heads/master', build_params['zuul']['ref'])
James E. Blair289f5932017-07-27 15:02:29 -0700140 self.assertFalse('oldrev' in build_params['zuul'])
James E. Blaired8b0012017-07-21 14:49:29 -0700141 self.assertEqual(new_sha, build_params['zuul']['newrev'])
Wayne1a78c612015-06-11 17:14:13 -0700142
143 self.executor_server.hold_jobs_in_build = False
144 self.executor_server.release()
145 self.waitUntilSettled()
146
147 self.assertEqual('SUCCESS',
148 self.getJobFromHistory('project-post').result)
Jan Hruband4edee82015-12-16 12:49:51 +0100149 self.assertEqual(1, len(self.history))
150
151 # test unmatched push event
152 old_sha = random_sha1()
153 new_sha = random_sha1()
154 self.fake_github.emitEvent(
155 self.fake_github.getPushEvent('org/project',
156 'refs/heads/unmatched_branch',
157 old_sha, new_sha))
158 self.waitUntilSettled()
159
160 self.assertEqual(1, len(self.history))
Jan Hruban6d53c5e2015-10-24 03:03:34 +0200161
Jan Hruban16ad31f2015-11-07 14:39:07 +0100162 @simple_layout('layouts/labeling-github.yaml', driver='github')
163 def test_labels(self):
Jan Hruban37615e52015-11-19 14:30:49 +0100164 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
Jan Hruban16ad31f2015-11-07 14:39:07 +0100165 self.fake_github.emitEvent(A.addLabel('test'))
166 self.waitUntilSettled()
167 self.assertEqual(1, len(self.history))
168 self.assertEqual('project-labels', self.history[0].name)
169 self.assertEqual(['tests passed'], A.labels)
170
171 # test label removed
Jan Hruban37615e52015-11-19 14:30:49 +0100172 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
Jan Hruban16ad31f2015-11-07 14:39:07 +0100173 B.addLabel('do not test')
174 self.fake_github.emitEvent(B.removeLabel('do not test'))
175 self.waitUntilSettled()
176 self.assertEqual(2, len(self.history))
177 self.assertEqual('project-labels', self.history[1].name)
178 self.assertEqual(['tests passed'], B.labels)
179
180 # test unmatched label
Jan Hruban37615e52015-11-19 14:30:49 +0100181 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
Jan Hruban16ad31f2015-11-07 14:39:07 +0100182 self.fake_github.emitEvent(C.addLabel('other label'))
183 self.waitUntilSettled()
184 self.assertEqual(2, len(self.history))
185 self.assertEqual(['other label'], C.labels)
186
Jesse Keating5c05a9f2017-01-12 14:44:58 -0800187 @simple_layout('layouts/reviews-github.yaml', driver='github')
188 def test_review_event(self):
189 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
190 self.fake_github.emitEvent(A.getReviewAddedEvent('approve'))
191 self.waitUntilSettled()
192 self.assertEqual(1, len(self.history))
193 self.assertEqual('project-reviews', self.history[0].name)
194 self.assertEqual(['tests passed'], A.labels)
195
196 # test_review_unmatched_event
197 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
198 self.fake_github.emitEvent(B.getReviewAddedEvent('comment'))
199 self.waitUntilSettled()
200 self.assertEqual(1, len(self.history))
201
Jan Hruban324ca5b2015-11-05 19:28:54 +0100202 @simple_layout('layouts/dequeue-github.yaml', driver='github')
203 def test_dequeue_pull_synchronized(self):
204 self.executor_server.hold_jobs_in_build = True
205
Jan Hruban37615e52015-11-19 14:30:49 +0100206 A = self.fake_github.openFakePullRequest(
207 'org/one-job-project', 'master', 'A')
208 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100209 self.waitUntilSettled()
210
211 # event update stamp has resolution one second, wait so the latter
212 # one has newer timestamp
213 time.sleep(1)
Jan Hruban37615e52015-11-19 14:30:49 +0100214 A.addCommit()
215 self.fake_github.emitEvent(A.getPullRequestSynchronizeEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100216 self.waitUntilSettled()
217
218 self.executor_server.hold_jobs_in_build = False
219 self.executor_server.release()
220 self.waitUntilSettled()
221
222 self.assertEqual(2, len(self.history))
223 self.assertEqual(1, self.countJobResults(self.history, 'ABORTED'))
224
225 @simple_layout('layouts/dequeue-github.yaml', driver='github')
226 def test_dequeue_pull_abandoned(self):
227 self.executor_server.hold_jobs_in_build = True
228
Jan Hruban37615e52015-11-19 14:30:49 +0100229 A = self.fake_github.openFakePullRequest(
230 'org/one-job-project', 'master', 'A')
231 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100232 self.waitUntilSettled()
Jan Hruban37615e52015-11-19 14:30:49 +0100233 self.fake_github.emitEvent(A.getPullRequestClosedEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100234 self.waitUntilSettled()
235
236 self.executor_server.hold_jobs_in_build = False
237 self.executor_server.release()
238 self.waitUntilSettled()
239
240 self.assertEqual(1, len(self.history))
241 self.assertEqual(1, self.countJobResults(self.history, 'ABORTED'))
242
Jan Hruban6d53c5e2015-10-24 03:03:34 +0200243 @simple_layout('layouts/basic-github.yaml', driver='github')
244 def test_git_https_url(self):
245 """Test that git_ssh option gives git url with ssh"""
246 url = self.fake_github.real_getGitUrl('org/project')
247 self.assertEqual('https://github.com/org/project', url)
248
249 @simple_layout('layouts/basic-github.yaml', driver='github')
250 def test_git_ssh_url(self):
251 """Test that git_ssh option gives git url with ssh"""
252 url = self.fake_github_ssh.real_getGitUrl('org/project')
253 self.assertEqual('ssh://git@github.com/org/project.git', url)
Jan Hrubane252a732017-01-03 15:03:09 +0100254
Jesse Keatingbe4ef8a2016-12-06 11:29:13 -0800255 @simple_layout('layouts/basic-github.yaml', driver='github')
256 def test_git_enterprise_url(self):
257 """Test that git_url option gives git url with proper host"""
258 url = self.fake_github_ent.real_getGitUrl('org/project')
259 self.assertEqual('ssh://git@github.enterprise.io/org/project.git', url)
260
Jan Hrubane252a732017-01-03 15:03:09 +0100261 @simple_layout('layouts/reporting-github.yaml', driver='github')
262 def test_reporting(self):
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700263 project = 'org/project'
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200264 github = self.fake_github.github_client
265
Jan Hrubane252a732017-01-03 15:03:09 +0100266 # pipeline reports pull status both on start and success
267 self.executor_server.hold_jobs_in_build = True
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700268 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
Jan Hruban37615e52015-11-19 14:30:49 +0100269 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Jan Hrubane252a732017-01-03 15:03:09 +0100270 self.waitUntilSettled()
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200271
Jesse Keatingd96e5882017-01-19 13:55:50 -0800272 # We should have a status container for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200273 self.assertIn(
274 A.head_sha, github.repo_from_project(project)._commits.keys())
275 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
276
Jesse Keatingd96e5882017-01-19 13:55:50 -0800277 # We should only have one status for the head sha
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700278 self.assertEqual(1, len(statuses))
279 check_status = statuses[0]
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100280 check_url = ('http://zuul.example.com/status/#%s,%s' %
281 (A.number, A.head_sha))
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000282 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700283 self.assertEqual('check status: pending',
284 check_status['description'])
Jan Hrubane252a732017-01-03 15:03:09 +0100285 self.assertEqual('pending', check_status['state'])
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100286 self.assertEqual(check_url, check_status['url'])
Jan Hruban37615e52015-11-19 14:30:49 +0100287 self.assertEqual(0, len(A.comments))
Jan Hrubane252a732017-01-03 15:03:09 +0100288
289 self.executor_server.hold_jobs_in_build = False
290 self.executor_server.release()
291 self.waitUntilSettled()
Jesse Keatingd96e5882017-01-19 13:55:50 -0800292 # We should only have two statuses for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200293 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700294 self.assertEqual(2, len(statuses))
295 check_status = statuses[0]
Jesse Keatingd96e5882017-01-19 13:55:50 -0800296 check_url = ('http://zuul.example.com/status/#%s,%s' %
297 (A.number, A.head_sha))
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000298 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700299 self.assertEqual('check status: success',
300 check_status['description'])
Jan Hrubane252a732017-01-03 15:03:09 +0100301 self.assertEqual('success', check_status['state'])
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100302 self.assertEqual(check_url, check_status['url'])
Jan Hruban37615e52015-11-19 14:30:49 +0100303 self.assertEqual(1, len(A.comments))
304 self.assertThat(A.comments[0],
Jan Hrubane252a732017-01-03 15:03:09 +0100305 MatchesRegex('.*Build succeeded.*', re.DOTALL))
306
307 # pipeline does not report any status but does comment
308 self.executor_server.hold_jobs_in_build = True
309 self.fake_github.emitEvent(
Jan Hruban37615e52015-11-19 14:30:49 +0100310 A.getCommentAddedEvent('reporting check'))
Jan Hrubane252a732017-01-03 15:03:09 +0100311 self.waitUntilSettled()
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200312 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700313 self.assertEqual(2, len(statuses))
Jan Hrubane252a732017-01-03 15:03:09 +0100314 # comments increased by one for the start message
Jan Hruban37615e52015-11-19 14:30:49 +0100315 self.assertEqual(2, len(A.comments))
316 self.assertThat(A.comments[1],
Jan Hrubane252a732017-01-03 15:03:09 +0100317 MatchesRegex('.*Starting reporting jobs.*', re.DOTALL))
318 self.executor_server.hold_jobs_in_build = False
319 self.executor_server.release()
320 self.waitUntilSettled()
Jesse Keatingd96e5882017-01-19 13:55:50 -0800321 # pipeline reports success status
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200322 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700323 self.assertEqual(3, len(statuses))
324 report_status = statuses[0]
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000325 self.assertEqual('tenant-one/reporting', report_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700326 self.assertEqual('reporting status: success',
327 report_status['description'])
Jesse Keatingd96e5882017-01-19 13:55:50 -0800328 self.assertEqual('success', report_status['state'])
Jan Hruban37615e52015-11-19 14:30:49 +0100329 self.assertEqual(2, len(A.comments))
Jamie Lennox3f16de52017-05-09 14:24:11 +1000330
331 base = 'http://logs.example.com/tenant-one/reporting/%s/%s/' % (
332 A.project, A.number)
333
334 # Deconstructing the URL because we don't save the BuildSet UUID
335 # anywhere to do a direct comparison and doing regexp matches on a full
336 # URL is painful.
337
338 # The first part of the URL matches the easy base string
339 self.assertThat(report_status['url'], StartsWith(base))
340
341 # The rest of the URL is a UUID and a trailing slash.
342 self.assertThat(report_status['url'][len(base):],
343 MatchesRegex('^[a-fA-F0-9]{32}\/$'))
Jan Hruban49bff072015-11-03 11:45:46 +0100344
Jesse Keating08dab8f2017-06-21 12:59:23 +0100345 @simple_layout('layouts/reporting-github.yaml', driver='github')
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700346 def test_truncated_status_description(self):
347 project = 'org/project'
348 # pipeline reports pull status both on start and success
349 self.executor_server.hold_jobs_in_build = True
350 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
351 self.fake_github.emitEvent(
352 A.getCommentAddedEvent('long pipeline'))
353 self.waitUntilSettled()
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200354 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700355 self.assertEqual(1, len(statuses))
356 check_status = statuses[0]
357 # Status is truncated due to long pipeline name
358 self.assertEqual('status: pending',
359 check_status['description'])
360
361 self.executor_server.hold_jobs_in_build = False
362 self.executor_server.release()
363 self.waitUntilSettled()
364 # We should only have two statuses for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200365 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700366 self.assertEqual(2, len(statuses))
367 check_status = statuses[0]
368 # Status is truncated due to long pipeline name
369 self.assertEqual('status: success',
370 check_status['description'])
371
372 @simple_layout('layouts/reporting-github.yaml', driver='github')
Jesse Keating08dab8f2017-06-21 12:59:23 +0100373 def test_push_reporting(self):
374 project = 'org/project2'
375 # pipeline reports pull status both on start and success
376 self.executor_server.hold_jobs_in_build = True
Jesse Keating08dab8f2017-06-21 12:59:23 +0100377
James E. Blair289f5932017-07-27 15:02:29 -0700378 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
379 old_sha = '0' * 40
380 new_sha = A.head_sha
381 A.setMerged("merging A")
382 pevent = self.fake_github.getPushEvent(project=project,
383 ref='refs/heads/master',
384 old_rev=old_sha,
385 new_rev=new_sha)
Jesse Keating08dab8f2017-06-21 12:59:23 +0100386 self.fake_github.emitEvent(pevent)
387 self.waitUntilSettled()
388
389 # there should only be one report, a status
390 self.assertEqual(1, len(self.fake_github.reports))
391 # Verify the user/context/state of the status
392 status = ('zuul', 'tenant-one/push-reporting', 'pending')
393 self.assertEqual(status, self.fake_github.reports[0][-1])
394
395 # free the executor, allow the build to finish
396 self.executor_server.hold_jobs_in_build = False
397 self.executor_server.release()
398 self.waitUntilSettled()
399
400 # Now there should be a second report, the success of the build
401 self.assertEqual(2, len(self.fake_github.reports))
402 # Verify the user/context/state of the status
403 status = ('zuul', 'tenant-one/push-reporting', 'success')
404 self.assertEqual(status, self.fake_github.reports[-1][-1])
405
406 # now make a PR which should also comment
407 self.executor_server.hold_jobs_in_build = True
408 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
409 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
410 self.waitUntilSettled()
411
412 # Now there should be a four reports, a new comment
413 # and status
414 self.assertEqual(4, len(self.fake_github.reports))
415 self.executor_server.release()
416 self.waitUntilSettled()
417
Jan Hruban49bff072015-11-03 11:45:46 +0100418 @simple_layout('layouts/merging-github.yaml', driver='github')
419 def test_report_pull_merge(self):
420 # pipeline merges the pull request on success
Jan Hruban3b415922016-02-03 13:10:22 +0100421 A = self.fake_github.openFakePullRequest('org/project', 'master',
422 'PR title')
Jan Hruban49bff072015-11-03 11:45:46 +0100423 self.fake_github.emitEvent(A.getCommentAddedEvent('merge me'))
424 self.waitUntilSettled()
425 self.assertTrue(A.is_merged)
Jan Hruban3b415922016-02-03 13:10:22 +0100426 self.assertThat(A.merge_message,
427 MatchesRegex('.*PR title.*Reviewed-by.*', re.DOTALL))
Jan Hruban49bff072015-11-03 11:45:46 +0100428
429 # pipeline merges the pull request on success after failure
430 self.fake_github.merge_failure = True
Jan Hruban37615e52015-11-19 14:30:49 +0100431 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
Jan Hruban49bff072015-11-03 11:45:46 +0100432 self.fake_github.emitEvent(B.getCommentAddedEvent('merge me'))
433 self.waitUntilSettled()
434 self.assertFalse(B.is_merged)
435 self.fake_github.merge_failure = False
436
437 # pipeline merges the pull request on second run of merge
438 # first merge failed on 405 Method Not Allowed error
439 self.fake_github.merge_not_allowed_count = 1
Jan Hruban37615e52015-11-19 14:30:49 +0100440 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
Jan Hruban49bff072015-11-03 11:45:46 +0100441 self.fake_github.emitEvent(C.getCommentAddedEvent('merge me'))
442 self.waitUntilSettled()
443 self.assertTrue(C.is_merged)
444
445 # pipeline does not merge the pull request
446 # merge failed on 405 Method Not Allowed error - twice
447 self.fake_github.merge_not_allowed_count = 2
Jan Hruban37615e52015-11-19 14:30:49 +0100448 D = self.fake_github.openFakePullRequest('org/project', 'master', 'D')
Jan Hruban49bff072015-11-03 11:45:46 +0100449 self.fake_github.emitEvent(D.getCommentAddedEvent('merge me'))
450 self.waitUntilSettled()
451 self.assertFalse(D.is_merged)
Adam Gandelman62198cb2017-02-14 16:11:02 -0800452 self.assertEqual(len(D.comments), 1)
453 self.assertEqual(D.comments[0], 'Merge failed')
Jan Hruban37615e52015-11-19 14:30:49 +0100454
Jesse Keating28434312017-07-31 11:32:48 -0700455 @simple_layout('layouts/reporting-multiple-github.yaml', driver='github')
456 def test_reporting_multiple_github(self):
457 project = 'org/project1'
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200458 github = self.fake_github.github_client
459
Jesse Keating28434312017-07-31 11:32:48 -0700460 # pipeline reports pull status both on start and success
461 self.executor_server.hold_jobs_in_build = True
462 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
463 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
464 # open one on B as well, which should not effect A reporting
465 B = self.fake_github.openFakePullRequest('org/project2', 'master',
466 'B')
467 self.fake_github.emitEvent(B.getPullRequestOpenedEvent())
468 self.waitUntilSettled()
469 # We should have a status container for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200470 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
471 self.assertIn(
472 A.head_sha, github.repo_from_project(project)._commits.keys())
Jesse Keating28434312017-07-31 11:32:48 -0700473 # We should only have one status for the head sha
474 self.assertEqual(1, len(statuses))
475 check_status = statuses[0]
476 check_url = ('http://zuul.example.com/status/#%s,%s' %
477 (A.number, A.head_sha))
478 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700479 self.assertEqual('check status: pending', check_status['description'])
Jesse Keating28434312017-07-31 11:32:48 -0700480 self.assertEqual('pending', check_status['state'])
481 self.assertEqual(check_url, check_status['url'])
482 self.assertEqual(0, len(A.comments))
483
484 self.executor_server.hold_jobs_in_build = False
485 self.executor_server.release()
486 self.waitUntilSettled()
487 # We should only have two statuses for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200488 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating28434312017-07-31 11:32:48 -0700489 self.assertEqual(2, len(statuses))
490 check_status = statuses[0]
491 check_url = ('http://zuul.example.com/status/#%s,%s' %
492 (A.number, A.head_sha))
493 self.assertEqual('tenant-one/check', check_status['context'])
494 self.assertEqual('success', check_status['state'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700495 self.assertEqual('check status: success', check_status['description'])
Jesse Keating28434312017-07-31 11:32:48 -0700496 self.assertEqual(check_url, check_status['url'])
497 self.assertEqual(1, len(A.comments))
498 self.assertThat(A.comments[0],
499 MatchesRegex('.*Build succeeded.*', re.DOTALL))
500
Jan Hruban37615e52015-11-19 14:30:49 +0100501 @simple_layout('layouts/dependent-github.yaml', driver='github')
502 def test_parallel_changes(self):
503 "Test that changes are tested in parallel and merged in series"
504
505 self.executor_server.hold_jobs_in_build = True
506 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
507 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
508 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
509
510 self.fake_github.emitEvent(A.addLabel('merge'))
511 self.fake_github.emitEvent(B.addLabel('merge'))
512 self.fake_github.emitEvent(C.addLabel('merge'))
513
514 self.waitUntilSettled()
515 self.assertEqual(len(self.builds), 1)
516 self.assertEqual(self.builds[0].name, 'project-merge')
517 self.assertTrue(self.builds[0].hasChanges(A))
518
519 self.executor_server.release('.*-merge')
520 self.waitUntilSettled()
521 self.assertEqual(len(self.builds), 3)
522 self.assertEqual(self.builds[0].name, 'project-test1')
523 self.assertTrue(self.builds[0].hasChanges(A))
524 self.assertEqual(self.builds[1].name, 'project-test2')
525 self.assertTrue(self.builds[1].hasChanges(A))
526 self.assertEqual(self.builds[2].name, 'project-merge')
527 self.assertTrue(self.builds[2].hasChanges(A, B))
528
529 self.executor_server.release('.*-merge')
530 self.waitUntilSettled()
531 self.assertEqual(len(self.builds), 5)
532 self.assertEqual(self.builds[0].name, 'project-test1')
533 self.assertTrue(self.builds[0].hasChanges(A))
534 self.assertEqual(self.builds[1].name, 'project-test2')
535 self.assertTrue(self.builds[1].hasChanges(A))
536
537 self.assertEqual(self.builds[2].name, 'project-test1')
538 self.assertTrue(self.builds[2].hasChanges(A))
539 self.assertEqual(self.builds[3].name, 'project-test2')
540 self.assertTrue(self.builds[3].hasChanges(A, B))
541
542 self.assertEqual(self.builds[4].name, 'project-merge')
543 self.assertTrue(self.builds[4].hasChanges(A, B, C))
544
545 self.executor_server.release('.*-merge')
546 self.waitUntilSettled()
547 self.assertEqual(len(self.builds), 6)
548 self.assertEqual(self.builds[0].name, 'project-test1')
549 self.assertTrue(self.builds[0].hasChanges(A))
550 self.assertEqual(self.builds[1].name, 'project-test2')
551 self.assertTrue(self.builds[1].hasChanges(A))
552
553 self.assertEqual(self.builds[2].name, 'project-test1')
554 self.assertTrue(self.builds[2].hasChanges(A, B))
555 self.assertEqual(self.builds[3].name, 'project-test2')
556 self.assertTrue(self.builds[3].hasChanges(A, B))
557
558 self.assertEqual(self.builds[4].name, 'project-test1')
559 self.assertTrue(self.builds[4].hasChanges(A, B, C))
560 self.assertEqual(self.builds[5].name, 'project-test2')
561 self.assertTrue(self.builds[5].hasChanges(A, B, C))
562
563 all_builds = self.builds[:]
564 self.release(all_builds[2])
565 self.release(all_builds[3])
566 self.waitUntilSettled()
567 self.assertFalse(A.is_merged)
568 self.assertFalse(B.is_merged)
569 self.assertFalse(C.is_merged)
570
571 self.release(all_builds[0])
572 self.release(all_builds[1])
573 self.waitUntilSettled()
574 self.assertTrue(A.is_merged)
575 self.assertTrue(B.is_merged)
576 self.assertFalse(C.is_merged)
577
578 self.executor_server.hold_jobs_in_build = False
579 self.executor_server.release()
580 self.waitUntilSettled()
581 self.assertEqual(len(self.builds), 0)
582 self.assertEqual(len(self.history), 9)
583 self.assertTrue(C.is_merged)
584
585 self.assertNotIn('merge', A.labels)
586 self.assertNotIn('merge', B.labels)
587 self.assertNotIn('merge', C.labels)
588
589 @simple_layout('layouts/dependent-github.yaml', driver='github')
590 def test_failed_changes(self):
591 "Test that a change behind a failed change is retested"
592 self.executor_server.hold_jobs_in_build = True
593
594 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
595 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
596
597 self.executor_server.failJob('project-test1', A)
598
599 self.fake_github.emitEvent(A.addLabel('merge'))
600 self.fake_github.emitEvent(B.addLabel('merge'))
601 self.waitUntilSettled()
602
603 self.executor_server.release('.*-merge')
604 self.waitUntilSettled()
605
606 self.executor_server.hold_jobs_in_build = False
607 self.executor_server.release()
608
609 self.waitUntilSettled()
610 # It's certain that the merge job for change 2 will run, but
611 # the test1 and test2 jobs may or may not run.
612 self.assertTrue(len(self.history) > 6)
613 self.assertFalse(A.is_merged)
614 self.assertTrue(B.is_merged)
615 self.assertNotIn('merge', A.labels)
616 self.assertNotIn('merge', B.labels)
617
618 @simple_layout('layouts/dependent-github.yaml', driver='github')
619 def test_failed_change_at_head(self):
620 "Test that if a change at the head fails, jobs behind it are canceled"
621
622 self.executor_server.hold_jobs_in_build = True
623 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
624 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
625 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
626
627 self.executor_server.failJob('project-test1', A)
628
629 self.fake_github.emitEvent(A.addLabel('merge'))
630 self.fake_github.emitEvent(B.addLabel('merge'))
631 self.fake_github.emitEvent(C.addLabel('merge'))
632
633 self.waitUntilSettled()
634
635 self.assertEqual(len(self.builds), 1)
636 self.assertEqual(self.builds[0].name, 'project-merge')
637 self.assertTrue(self.builds[0].hasChanges(A))
638
639 self.executor_server.release('.*-merge')
640 self.waitUntilSettled()
641 self.executor_server.release('.*-merge')
642 self.waitUntilSettled()
643 self.executor_server.release('.*-merge')
644 self.waitUntilSettled()
645
646 self.assertEqual(len(self.builds), 6)
647 self.assertEqual(self.builds[0].name, 'project-test1')
648 self.assertEqual(self.builds[1].name, 'project-test2')
649 self.assertEqual(self.builds[2].name, 'project-test1')
650 self.assertEqual(self.builds[3].name, 'project-test2')
651 self.assertEqual(self.builds[4].name, 'project-test1')
652 self.assertEqual(self.builds[5].name, 'project-test2')
653
654 self.release(self.builds[0])
655 self.waitUntilSettled()
656
657 # project-test2, project-merge for B
658 self.assertEqual(len(self.builds), 2)
659 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
660
661 self.executor_server.hold_jobs_in_build = False
662 self.executor_server.release()
663 self.waitUntilSettled()
664
665 self.assertEqual(len(self.builds), 0)
666 self.assertEqual(len(self.history), 15)
667 self.assertFalse(A.is_merged)
668 self.assertTrue(B.is_merged)
669 self.assertTrue(C.is_merged)
670 self.assertNotIn('merge', A.labels)
671 self.assertNotIn('merge', B.labels)
672 self.assertNotIn('merge', C.labels)
Jesse Keating71a47ff2017-06-06 11:36:43 -0700673
674 @simple_layout('layouts/basic-github.yaml', driver='github')
675 def test_push_event_reconfigure(self):
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200676 pevent = self.fake_github.getPushEvent(project='org/common-config',
Jesse Keating71a47ff2017-06-06 11:36:43 -0700677 ref='refs/heads/master',
678 modified_files=['zuul.yaml'])
679
680 # record previous tenant reconfiguration time, which may not be set
681 old = self.sched.tenant_last_reconfigured.get('tenant-one', 0)
682 time.sleep(1)
683 self.fake_github.emitEvent(pevent)
684 self.waitUntilSettled()
685 new = self.sched.tenant_last_reconfigured.get('tenant-one', 0)
686 # New timestamp should be greater than the old timestamp
687 self.assertLess(old, new)
Tristan Cacqueray2bafb1f2017-06-12 07:10:26 +0000688
Jesse Keating64d29012017-09-06 12:27:49 -0700689 # TODO(jlk): Make this a more generic test for unknown project
690 @skip("Skipped for rewrite of webhook handler")
Tristan Cacqueray2bafb1f2017-06-12 07:10:26 +0000691 @simple_layout('layouts/basic-github.yaml', driver='github')
692 def test_ping_event(self):
693 # Test valid ping
694 pevent = {'repository': {'full_name': 'org/project'}}
695 req = self.fake_github.emitEvent(('ping', pevent))
696 self.assertEqual(req.status, 200, "Ping event didn't succeed")
697
698 # Test invalid ping
699 pevent = {'repository': {'full_name': 'unknown-project'}}
700 self.assertRaises(
701 urllib.error.HTTPError,
702 self.fake_github.emitEvent,
703 ('ping', pevent),
704 )
Tobias Henkeleca46202017-08-02 20:27:10 +0200705
706
707class TestGithubUnprotectedBranches(ZuulTestCase):
708 config_file = 'zuul-github-driver.conf'
709 tenant_config_file = 'config/unprotected-branches/main.yaml'
710
711 def test_unprotected_branches(self):
712 tenant = self.sched.abide.tenants.get('tenant-one')
713
714 project1 = tenant.untrusted_projects[0]
715 project2 = tenant.untrusted_projects[1]
716
717 # project1 should have parsed master
718 self.assertIn('master', project1.unparsed_branch_config.keys())
719
720 # project2 should have no parsed branch
721 self.assertEqual(0, len(project2.unparsed_branch_config.keys()))