blob: 7aca4284fbe5ca7f651cf147d0274b9837cdaa0a [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
Jesse Keating80730e62017-09-14 15:35:11 -060015import asyncio
16import threading
James E. Blair21037782017-07-19 11:56:55 -070017import os
Jan Hrubane252a732017-01-03 15:03:09 +010018import re
Jamie Lennox3f16de52017-05-09 14:24:11 +100019from testtools.matchers import MatchesRegex, StartsWith
Tristan Cacqueray2bafb1f2017-06-12 07:10:26 +000020import urllib
Jesse Keating80730e62017-09-14 15:35:11 -060021import socket
Jan Hruban324ca5b2015-11-05 19:28:54 +010022import time
Jesse Keating64d29012017-09-06 12:27:49 -070023from unittest import skip
Gregory Haynes4fc12542015-04-22 20:38:06 -070024
James E. Blair21037782017-07-19 11:56:55 -070025import git
26
Jesse Keating80730e62017-09-14 15:35:11 -060027import zuul.web
28
Wayne1a78c612015-06-11 17:14:13 -070029from tests.base import ZuulTestCase, simple_layout, random_sha1
Gregory Haynes4fc12542015-04-22 20:38:06 -070030
Gregory Haynes4fc12542015-04-22 20:38:06 -070031
32class TestGithubDriver(ZuulTestCase):
33 config_file = 'zuul-github-driver.conf'
34
35 @simple_layout('layouts/basic-github.yaml', driver='github')
36 def test_pull_event(self):
37 self.executor_server.hold_jobs_in_build = True
38
Jan Hruban37615e52015-11-19 14:30:49 +010039 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
40 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Gregory Haynes4fc12542015-04-22 20:38:06 -070041 self.waitUntilSettled()
42
Gregory Haynes4fc12542015-04-22 20:38:06 -070043 self.executor_server.hold_jobs_in_build = False
44 self.executor_server.release()
45 self.waitUntilSettled()
46
47 self.assertEqual('SUCCESS',
48 self.getJobFromHistory('project-test1').result)
49 self.assertEqual('SUCCESS',
50 self.getJobFromHistory('project-test2').result)
51
52 job = self.getJobFromHistory('project-test2')
Monty Taylord13bc362017-06-30 13:11:37 -050053 zuulvars = job.parameters['zuul']
James E. Blaire3db2952017-07-21 15:03:36 -070054 self.assertEqual(str(A.number), zuulvars['change'])
55 self.assertEqual(str(A.head_sha), zuulvars['patchset'])
James E. Blair3b222492017-07-21 15:17:37 -070056 self.assertEqual('master', zuulvars['branch'])
Jan Hruban37615e52015-11-19 14:30:49 +010057 self.assertEqual(1, len(A.comments))
liushengd3419e82018-01-06 19:52:03 +080058 self.assertThat(
59 A.comments[0],
60 MatchesRegex('.*\[project-test1 \]\(.*\).*', re.DOTALL))
61 self.assertThat(
62 A.comments[0],
63 MatchesRegex('.*\[project-test2 \]\(.*\).*', re.DOTALL))
Jan Hruband4edee82015-12-16 12:49:51 +010064 self.assertEqual(2, len(self.history))
65
66 # test_pull_unmatched_branch_event(self):
67 self.create_branch('org/project', 'unmatched_branch')
68 B = self.fake_github.openFakePullRequest(
69 'org/project', 'unmatched_branch', 'B')
70 self.fake_github.emitEvent(B.getPullRequestOpenedEvent())
71 self.waitUntilSettled()
72
73 self.assertEqual(2, len(self.history))
Wayne1a78c612015-06-11 17:14:13 -070074
Jan Hruban570d01c2016-03-10 21:51:32 +010075 @simple_layout('layouts/files-github.yaml', driver='github')
76 def test_pull_matched_file_event(self):
77 A = self.fake_github.openFakePullRequest(
78 'org/project', 'master', 'A',
79 files=['random.txt', 'build-requires'])
80 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
81 self.waitUntilSettled()
82 self.assertEqual(1, len(self.history))
83
84 # test_pull_unmatched_file_event
85 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B',
86 files=['random.txt'])
87 self.fake_github.emitEvent(B.getPullRequestOpenedEvent())
88 self.waitUntilSettled()
89 self.assertEqual(1, len(self.history))
90
Jan Hrubanc7ab1602015-10-14 15:29:33 +020091 @simple_layout('layouts/basic-github.yaml', driver='github')
92 def test_comment_event(self):
Jan Hruban37615e52015-11-19 14:30:49 +010093 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
94 self.fake_github.emitEvent(A.getCommentAddedEvent('test me'))
Jan Hrubanc7ab1602015-10-14 15:29:33 +020095 self.waitUntilSettled()
96 self.assertEqual(2, len(self.history))
97
98 # Test an unmatched comment, history should remain the same
Jan Hruban37615e52015-11-19 14:30:49 +010099 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
100 self.fake_github.emitEvent(B.getCommentAddedEvent('casual comment'))
Jan Hrubanc7ab1602015-10-14 15:29:33 +0200101 self.waitUntilSettled()
102 self.assertEqual(2, len(self.history))
103
Wayne1a78c612015-06-11 17:14:13 -0700104 @simple_layout('layouts/push-tag-github.yaml', driver='github')
105 def test_tag_event(self):
106 self.executor_server.hold_jobs_in_build = True
107
James E. Blair21037782017-07-19 11:56:55 -0700108 self.create_branch('org/project', 'tagbranch')
109 files = {'README.txt': 'test'}
110 self.addCommitToRepo('org/project', 'test tag',
111 files, branch='tagbranch', tag='newtag')
112 path = os.path.join(self.upstream_root, 'org/project')
113 repo = git.Repo(path)
114 tag = repo.tags['newtag']
115 sha = tag.commit.hexsha
116 del repo
117
Wayne1a78c612015-06-11 17:14:13 -0700118 self.fake_github.emitEvent(
119 self.fake_github.getPushEvent('org/project', 'refs/tags/newtag',
120 new_rev=sha))
121 self.waitUntilSettled()
122
123 build_params = self.builds[0].parameters
James E. Blaira438c172017-07-21 14:54:42 -0700124 self.assertEqual('refs/tags/newtag', build_params['zuul']['ref'])
James E. Blairb8203e42017-08-02 17:00:14 -0700125 self.assertFalse('oldrev' in build_params['zuul'])
James E. Blaired8b0012017-07-21 14:49:29 -0700126 self.assertEqual(sha, build_params['zuul']['newrev'])
Wayne1a78c612015-06-11 17:14:13 -0700127 self.executor_server.hold_jobs_in_build = False
128 self.executor_server.release()
129 self.waitUntilSettled()
130
131 self.assertEqual('SUCCESS',
132 self.getJobFromHistory('project-tag').result)
133
134 @simple_layout('layouts/push-tag-github.yaml', driver='github')
135 def test_push_event(self):
136 self.executor_server.hold_jobs_in_build = True
137
James E. Blair289f5932017-07-27 15:02:29 -0700138 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
139 old_sha = '0' * 40
140 new_sha = A.head_sha
141 A.setMerged("merging A")
142 pevent = self.fake_github.getPushEvent(project='org/project',
143 ref='refs/heads/master',
144 old_rev=old_sha,
145 new_rev=new_sha)
146 self.fake_github.emitEvent(pevent)
Wayne1a78c612015-06-11 17:14:13 -0700147 self.waitUntilSettled()
148
149 build_params = self.builds[0].parameters
James E. Blaira438c172017-07-21 14:54:42 -0700150 self.assertEqual('refs/heads/master', build_params['zuul']['ref'])
James E. Blair289f5932017-07-27 15:02:29 -0700151 self.assertFalse('oldrev' in build_params['zuul'])
James E. Blaired8b0012017-07-21 14:49:29 -0700152 self.assertEqual(new_sha, build_params['zuul']['newrev'])
Wayne1a78c612015-06-11 17:14:13 -0700153
154 self.executor_server.hold_jobs_in_build = False
155 self.executor_server.release()
156 self.waitUntilSettled()
157
158 self.assertEqual('SUCCESS',
159 self.getJobFromHistory('project-post').result)
Jan Hruband4edee82015-12-16 12:49:51 +0100160 self.assertEqual(1, len(self.history))
161
162 # test unmatched push event
163 old_sha = random_sha1()
164 new_sha = random_sha1()
165 self.fake_github.emitEvent(
166 self.fake_github.getPushEvent('org/project',
167 'refs/heads/unmatched_branch',
168 old_sha, new_sha))
169 self.waitUntilSettled()
170
171 self.assertEqual(1, len(self.history))
Jan Hruban6d53c5e2015-10-24 03:03:34 +0200172
Jan Hruban16ad31f2015-11-07 14:39:07 +0100173 @simple_layout('layouts/labeling-github.yaml', driver='github')
174 def test_labels(self):
Jan Hruban37615e52015-11-19 14:30:49 +0100175 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
Jan Hruban16ad31f2015-11-07 14:39:07 +0100176 self.fake_github.emitEvent(A.addLabel('test'))
177 self.waitUntilSettled()
178 self.assertEqual(1, len(self.history))
179 self.assertEqual('project-labels', self.history[0].name)
180 self.assertEqual(['tests passed'], A.labels)
181
182 # test label removed
Jan Hruban37615e52015-11-19 14:30:49 +0100183 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
Jan Hruban16ad31f2015-11-07 14:39:07 +0100184 B.addLabel('do not test')
185 self.fake_github.emitEvent(B.removeLabel('do not test'))
186 self.waitUntilSettled()
187 self.assertEqual(2, len(self.history))
188 self.assertEqual('project-labels', self.history[1].name)
189 self.assertEqual(['tests passed'], B.labels)
190
191 # test unmatched label
Jan Hruban37615e52015-11-19 14:30:49 +0100192 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
Jan Hruban16ad31f2015-11-07 14:39:07 +0100193 self.fake_github.emitEvent(C.addLabel('other label'))
194 self.waitUntilSettled()
195 self.assertEqual(2, len(self.history))
196 self.assertEqual(['other label'], C.labels)
197
Jesse Keating5c05a9f2017-01-12 14:44:58 -0800198 @simple_layout('layouts/reviews-github.yaml', driver='github')
199 def test_review_event(self):
200 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
201 self.fake_github.emitEvent(A.getReviewAddedEvent('approve'))
202 self.waitUntilSettled()
203 self.assertEqual(1, len(self.history))
204 self.assertEqual('project-reviews', self.history[0].name)
205 self.assertEqual(['tests passed'], A.labels)
206
207 # test_review_unmatched_event
208 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
209 self.fake_github.emitEvent(B.getReviewAddedEvent('comment'))
210 self.waitUntilSettled()
211 self.assertEqual(1, len(self.history))
212
Jan Hruban324ca5b2015-11-05 19:28:54 +0100213 @simple_layout('layouts/dequeue-github.yaml', driver='github')
214 def test_dequeue_pull_synchronized(self):
215 self.executor_server.hold_jobs_in_build = True
216
Jan Hruban37615e52015-11-19 14:30:49 +0100217 A = self.fake_github.openFakePullRequest(
218 'org/one-job-project', 'master', 'A')
219 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100220 self.waitUntilSettled()
221
222 # event update stamp has resolution one second, wait so the latter
223 # one has newer timestamp
224 time.sleep(1)
Jan Hruban37615e52015-11-19 14:30:49 +0100225 A.addCommit()
226 self.fake_github.emitEvent(A.getPullRequestSynchronizeEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100227 self.waitUntilSettled()
228
229 self.executor_server.hold_jobs_in_build = False
230 self.executor_server.release()
231 self.waitUntilSettled()
232
233 self.assertEqual(2, len(self.history))
234 self.assertEqual(1, self.countJobResults(self.history, 'ABORTED'))
235
236 @simple_layout('layouts/dequeue-github.yaml', driver='github')
237 def test_dequeue_pull_abandoned(self):
238 self.executor_server.hold_jobs_in_build = True
239
Jan Hruban37615e52015-11-19 14:30:49 +0100240 A = self.fake_github.openFakePullRequest(
241 'org/one-job-project', 'master', 'A')
242 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100243 self.waitUntilSettled()
Jan Hruban37615e52015-11-19 14:30:49 +0100244 self.fake_github.emitEvent(A.getPullRequestClosedEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100245 self.waitUntilSettled()
246
247 self.executor_server.hold_jobs_in_build = False
248 self.executor_server.release()
249 self.waitUntilSettled()
250
251 self.assertEqual(1, len(self.history))
252 self.assertEqual(1, self.countJobResults(self.history, 'ABORTED'))
253
Jan Hruban6d53c5e2015-10-24 03:03:34 +0200254 @simple_layout('layouts/basic-github.yaml', driver='github')
255 def test_git_https_url(self):
256 """Test that git_ssh option gives git url with ssh"""
Tobias Henkel42f7d6f2017-12-15 16:09:43 +0100257 tenant = self.sched.abide.tenants.get('tenant-one')
258 _, project = tenant.getProject('org/project')
259
260 url = self.fake_github.real_getGitUrl(project)
Jan Hruban6d53c5e2015-10-24 03:03:34 +0200261 self.assertEqual('https://github.com/org/project', url)
262
263 @simple_layout('layouts/basic-github.yaml', driver='github')
264 def test_git_ssh_url(self):
265 """Test that git_ssh option gives git url with ssh"""
Tobias Henkel42f7d6f2017-12-15 16:09:43 +0100266 tenant = self.sched.abide.tenants.get('tenant-one')
267 _, project = tenant.getProject('org/project')
268
269 url = self.fake_github_ssh.real_getGitUrl(project)
Jan Hruban6d53c5e2015-10-24 03:03:34 +0200270 self.assertEqual('ssh://git@github.com/org/project.git', url)
Jan Hrubane252a732017-01-03 15:03:09 +0100271
Jesse Keatingbe4ef8a2016-12-06 11:29:13 -0800272 @simple_layout('layouts/basic-github.yaml', driver='github')
273 def test_git_enterprise_url(self):
274 """Test that git_url option gives git url with proper host"""
Tobias Henkel42f7d6f2017-12-15 16:09:43 +0100275 tenant = self.sched.abide.tenants.get('tenant-one')
276 _, project = tenant.getProject('org/project')
277
278 url = self.fake_github_ent.real_getGitUrl(project)
Jesse Keatingbe4ef8a2016-12-06 11:29:13 -0800279 self.assertEqual('ssh://git@github.enterprise.io/org/project.git', url)
280
Jan Hrubane252a732017-01-03 15:03:09 +0100281 @simple_layout('layouts/reporting-github.yaml', driver='github')
282 def test_reporting(self):
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700283 project = 'org/project'
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200284 github = self.fake_github.github_client
285
Jan Hrubane252a732017-01-03 15:03:09 +0100286 # pipeline reports pull status both on start and success
287 self.executor_server.hold_jobs_in_build = True
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700288 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
Jan Hruban37615e52015-11-19 14:30:49 +0100289 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Jan Hrubane252a732017-01-03 15:03:09 +0100290 self.waitUntilSettled()
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200291
Jesse Keatingd96e5882017-01-19 13:55:50 -0800292 # We should have a status container for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200293 self.assertIn(
294 A.head_sha, github.repo_from_project(project)._commits.keys())
295 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
296
Jesse Keatingd96e5882017-01-19 13:55:50 -0800297 # We should only have one status for the head sha
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700298 self.assertEqual(1, len(statuses))
299 check_status = statuses[0]
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100300 check_url = ('http://zuul.example.com/status/#%s,%s' %
301 (A.number, A.head_sha))
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000302 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700303 self.assertEqual('check status: pending',
304 check_status['description'])
Jan Hrubane252a732017-01-03 15:03:09 +0100305 self.assertEqual('pending', check_status['state'])
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100306 self.assertEqual(check_url, check_status['url'])
Jan Hruban37615e52015-11-19 14:30:49 +0100307 self.assertEqual(0, len(A.comments))
Jan Hrubane252a732017-01-03 15:03:09 +0100308
309 self.executor_server.hold_jobs_in_build = False
310 self.executor_server.release()
311 self.waitUntilSettled()
Jesse Keatingd96e5882017-01-19 13:55:50 -0800312 # We should only have two statuses for the head sha
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))
315 check_status = statuses[0]
Jesse Keatingd96e5882017-01-19 13:55:50 -0800316 check_url = ('http://zuul.example.com/status/#%s,%s' %
317 (A.number, A.head_sha))
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000318 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700319 self.assertEqual('check status: success',
320 check_status['description'])
Jan Hrubane252a732017-01-03 15:03:09 +0100321 self.assertEqual('success', check_status['state'])
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100322 self.assertEqual(check_url, check_status['url'])
Jan Hruban37615e52015-11-19 14:30:49 +0100323 self.assertEqual(1, len(A.comments))
324 self.assertThat(A.comments[0],
Jan Hrubane252a732017-01-03 15:03:09 +0100325 MatchesRegex('.*Build succeeded.*', re.DOTALL))
326
327 # pipeline does not report any status but does comment
328 self.executor_server.hold_jobs_in_build = True
329 self.fake_github.emitEvent(
Jan Hruban37615e52015-11-19 14:30:49 +0100330 A.getCommentAddedEvent('reporting check'))
Jan Hrubane252a732017-01-03 15:03:09 +0100331 self.waitUntilSettled()
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200332 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700333 self.assertEqual(2, len(statuses))
Jan Hrubane252a732017-01-03 15:03:09 +0100334 # comments increased by one for the start message
Jan Hruban37615e52015-11-19 14:30:49 +0100335 self.assertEqual(2, len(A.comments))
336 self.assertThat(A.comments[1],
Jan Hrubane252a732017-01-03 15:03:09 +0100337 MatchesRegex('.*Starting reporting jobs.*', re.DOTALL))
338 self.executor_server.hold_jobs_in_build = False
339 self.executor_server.release()
340 self.waitUntilSettled()
Jesse Keatingd96e5882017-01-19 13:55:50 -0800341 # pipeline reports success status
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200342 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700343 self.assertEqual(3, len(statuses))
344 report_status = statuses[0]
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000345 self.assertEqual('tenant-one/reporting', report_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700346 self.assertEqual('reporting status: success',
347 report_status['description'])
Jesse Keatingd96e5882017-01-19 13:55:50 -0800348 self.assertEqual('success', report_status['state'])
Jan Hruban37615e52015-11-19 14:30:49 +0100349 self.assertEqual(2, len(A.comments))
Jamie Lennox3f16de52017-05-09 14:24:11 +1000350
351 base = 'http://logs.example.com/tenant-one/reporting/%s/%s/' % (
352 A.project, A.number)
353
354 # Deconstructing the URL because we don't save the BuildSet UUID
355 # anywhere to do a direct comparison and doing regexp matches on a full
356 # URL is painful.
357
358 # The first part of the URL matches the easy base string
359 self.assertThat(report_status['url'], StartsWith(base))
360
361 # The rest of the URL is a UUID and a trailing slash.
362 self.assertThat(report_status['url'][len(base):],
363 MatchesRegex('^[a-fA-F0-9]{32}\/$'))
Jan Hruban49bff072015-11-03 11:45:46 +0100364
Jesse Keating08dab8f2017-06-21 12:59:23 +0100365 @simple_layout('layouts/reporting-github.yaml', driver='github')
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700366 def test_truncated_status_description(self):
367 project = 'org/project'
368 # pipeline reports pull status both on start and success
369 self.executor_server.hold_jobs_in_build = True
370 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
371 self.fake_github.emitEvent(
372 A.getCommentAddedEvent('long pipeline'))
373 self.waitUntilSettled()
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200374 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700375 self.assertEqual(1, len(statuses))
376 check_status = statuses[0]
377 # Status is truncated due to long pipeline name
378 self.assertEqual('status: pending',
379 check_status['description'])
380
381 self.executor_server.hold_jobs_in_build = False
382 self.executor_server.release()
383 self.waitUntilSettled()
384 # We should only have two statuses for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200385 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700386 self.assertEqual(2, len(statuses))
387 check_status = statuses[0]
388 # Status is truncated due to long pipeline name
389 self.assertEqual('status: success',
390 check_status['description'])
391
392 @simple_layout('layouts/reporting-github.yaml', driver='github')
Jesse Keating08dab8f2017-06-21 12:59:23 +0100393 def test_push_reporting(self):
394 project = 'org/project2'
395 # pipeline reports pull status both on start and success
396 self.executor_server.hold_jobs_in_build = True
Jesse Keating08dab8f2017-06-21 12:59:23 +0100397
James E. Blair289f5932017-07-27 15:02:29 -0700398 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
399 old_sha = '0' * 40
400 new_sha = A.head_sha
401 A.setMerged("merging A")
402 pevent = self.fake_github.getPushEvent(project=project,
403 ref='refs/heads/master',
404 old_rev=old_sha,
405 new_rev=new_sha)
Jesse Keating08dab8f2017-06-21 12:59:23 +0100406 self.fake_github.emitEvent(pevent)
407 self.waitUntilSettled()
408
409 # there should only be one report, a status
410 self.assertEqual(1, len(self.fake_github.reports))
411 # Verify the user/context/state of the status
412 status = ('zuul', 'tenant-one/push-reporting', 'pending')
413 self.assertEqual(status, self.fake_github.reports[0][-1])
414
415 # free the executor, allow the build to finish
416 self.executor_server.hold_jobs_in_build = False
417 self.executor_server.release()
418 self.waitUntilSettled()
419
420 # Now there should be a second report, the success of the build
421 self.assertEqual(2, len(self.fake_github.reports))
422 # Verify the user/context/state of the status
423 status = ('zuul', 'tenant-one/push-reporting', 'success')
424 self.assertEqual(status, self.fake_github.reports[-1][-1])
425
426 # now make a PR which should also comment
427 self.executor_server.hold_jobs_in_build = True
428 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
429 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
430 self.waitUntilSettled()
431
432 # Now there should be a four reports, a new comment
433 # and status
434 self.assertEqual(4, len(self.fake_github.reports))
435 self.executor_server.release()
436 self.waitUntilSettled()
437
Jan Hruban49bff072015-11-03 11:45:46 +0100438 @simple_layout('layouts/merging-github.yaml', driver='github')
439 def test_report_pull_merge(self):
440 # pipeline merges the pull request on success
Jan Hruban3b415922016-02-03 13:10:22 +0100441 A = self.fake_github.openFakePullRequest('org/project', 'master',
442 'PR title')
Jan Hruban49bff072015-11-03 11:45:46 +0100443 self.fake_github.emitEvent(A.getCommentAddedEvent('merge me'))
444 self.waitUntilSettled()
445 self.assertTrue(A.is_merged)
Jan Hruban3b415922016-02-03 13:10:22 +0100446 self.assertThat(A.merge_message,
447 MatchesRegex('.*PR title.*Reviewed-by.*', re.DOTALL))
Jan Hruban49bff072015-11-03 11:45:46 +0100448
449 # pipeline merges the pull request on success after failure
450 self.fake_github.merge_failure = True
Jan Hruban37615e52015-11-19 14:30:49 +0100451 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
Jan Hruban49bff072015-11-03 11:45:46 +0100452 self.fake_github.emitEvent(B.getCommentAddedEvent('merge me'))
453 self.waitUntilSettled()
454 self.assertFalse(B.is_merged)
455 self.fake_github.merge_failure = False
456
457 # pipeline merges the pull request on second run of merge
458 # first merge failed on 405 Method Not Allowed error
459 self.fake_github.merge_not_allowed_count = 1
Jan Hruban37615e52015-11-19 14:30:49 +0100460 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
Jan Hruban49bff072015-11-03 11:45:46 +0100461 self.fake_github.emitEvent(C.getCommentAddedEvent('merge me'))
462 self.waitUntilSettled()
463 self.assertTrue(C.is_merged)
464
465 # pipeline does not merge the pull request
466 # merge failed on 405 Method Not Allowed error - twice
467 self.fake_github.merge_not_allowed_count = 2
Jan Hruban37615e52015-11-19 14:30:49 +0100468 D = self.fake_github.openFakePullRequest('org/project', 'master', 'D')
Jan Hruban49bff072015-11-03 11:45:46 +0100469 self.fake_github.emitEvent(D.getCommentAddedEvent('merge me'))
470 self.waitUntilSettled()
471 self.assertFalse(D.is_merged)
Adam Gandelman62198cb2017-02-14 16:11:02 -0800472 self.assertEqual(len(D.comments), 1)
473 self.assertEqual(D.comments[0], 'Merge failed')
Jan Hruban37615e52015-11-19 14:30:49 +0100474
Jesse Keating28434312017-07-31 11:32:48 -0700475 @simple_layout('layouts/reporting-multiple-github.yaml', driver='github')
476 def test_reporting_multiple_github(self):
477 project = 'org/project1'
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200478 github = self.fake_github.github_client
479
Jesse Keating28434312017-07-31 11:32:48 -0700480 # pipeline reports pull status both on start and success
481 self.executor_server.hold_jobs_in_build = True
482 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
483 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
484 # open one on B as well, which should not effect A reporting
485 B = self.fake_github.openFakePullRequest('org/project2', 'master',
486 'B')
487 self.fake_github.emitEvent(B.getPullRequestOpenedEvent())
488 self.waitUntilSettled()
489 # We should have a status container for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200490 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
491 self.assertIn(
492 A.head_sha, github.repo_from_project(project)._commits.keys())
Jesse Keating28434312017-07-31 11:32:48 -0700493 # We should only have one status for the head sha
494 self.assertEqual(1, len(statuses))
495 check_status = statuses[0]
496 check_url = ('http://zuul.example.com/status/#%s,%s' %
497 (A.number, A.head_sha))
498 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700499 self.assertEqual('check status: pending', check_status['description'])
Jesse Keating28434312017-07-31 11:32:48 -0700500 self.assertEqual('pending', check_status['state'])
501 self.assertEqual(check_url, check_status['url'])
502 self.assertEqual(0, len(A.comments))
503
504 self.executor_server.hold_jobs_in_build = False
505 self.executor_server.release()
506 self.waitUntilSettled()
507 # We should only have two statuses for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200508 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating28434312017-07-31 11:32:48 -0700509 self.assertEqual(2, len(statuses))
510 check_status = statuses[0]
511 check_url = ('http://zuul.example.com/status/#%s,%s' %
512 (A.number, A.head_sha))
513 self.assertEqual('tenant-one/check', check_status['context'])
514 self.assertEqual('success', check_status['state'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700515 self.assertEqual('check status: success', check_status['description'])
Jesse Keating28434312017-07-31 11:32:48 -0700516 self.assertEqual(check_url, check_status['url'])
517 self.assertEqual(1, len(A.comments))
518 self.assertThat(A.comments[0],
519 MatchesRegex('.*Build succeeded.*', re.DOTALL))
520
Jan Hruban37615e52015-11-19 14:30:49 +0100521 @simple_layout('layouts/dependent-github.yaml', driver='github')
522 def test_parallel_changes(self):
523 "Test that changes are tested in parallel and merged in series"
524
525 self.executor_server.hold_jobs_in_build = True
526 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
527 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
528 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
529
530 self.fake_github.emitEvent(A.addLabel('merge'))
531 self.fake_github.emitEvent(B.addLabel('merge'))
532 self.fake_github.emitEvent(C.addLabel('merge'))
533
534 self.waitUntilSettled()
535 self.assertEqual(len(self.builds), 1)
536 self.assertEqual(self.builds[0].name, 'project-merge')
537 self.assertTrue(self.builds[0].hasChanges(A))
538
539 self.executor_server.release('.*-merge')
540 self.waitUntilSettled()
541 self.assertEqual(len(self.builds), 3)
542 self.assertEqual(self.builds[0].name, 'project-test1')
543 self.assertTrue(self.builds[0].hasChanges(A))
544 self.assertEqual(self.builds[1].name, 'project-test2')
545 self.assertTrue(self.builds[1].hasChanges(A))
546 self.assertEqual(self.builds[2].name, 'project-merge')
547 self.assertTrue(self.builds[2].hasChanges(A, B))
548
549 self.executor_server.release('.*-merge')
550 self.waitUntilSettled()
551 self.assertEqual(len(self.builds), 5)
552 self.assertEqual(self.builds[0].name, 'project-test1')
553 self.assertTrue(self.builds[0].hasChanges(A))
554 self.assertEqual(self.builds[1].name, 'project-test2')
555 self.assertTrue(self.builds[1].hasChanges(A))
556
557 self.assertEqual(self.builds[2].name, 'project-test1')
558 self.assertTrue(self.builds[2].hasChanges(A))
559 self.assertEqual(self.builds[3].name, 'project-test2')
560 self.assertTrue(self.builds[3].hasChanges(A, B))
561
562 self.assertEqual(self.builds[4].name, 'project-merge')
563 self.assertTrue(self.builds[4].hasChanges(A, B, C))
564
565 self.executor_server.release('.*-merge')
566 self.waitUntilSettled()
567 self.assertEqual(len(self.builds), 6)
568 self.assertEqual(self.builds[0].name, 'project-test1')
569 self.assertTrue(self.builds[0].hasChanges(A))
570 self.assertEqual(self.builds[1].name, 'project-test2')
571 self.assertTrue(self.builds[1].hasChanges(A))
572
573 self.assertEqual(self.builds[2].name, 'project-test1')
574 self.assertTrue(self.builds[2].hasChanges(A, B))
575 self.assertEqual(self.builds[3].name, 'project-test2')
576 self.assertTrue(self.builds[3].hasChanges(A, B))
577
578 self.assertEqual(self.builds[4].name, 'project-test1')
579 self.assertTrue(self.builds[4].hasChanges(A, B, C))
580 self.assertEqual(self.builds[5].name, 'project-test2')
581 self.assertTrue(self.builds[5].hasChanges(A, B, C))
582
583 all_builds = self.builds[:]
584 self.release(all_builds[2])
585 self.release(all_builds[3])
586 self.waitUntilSettled()
587 self.assertFalse(A.is_merged)
588 self.assertFalse(B.is_merged)
589 self.assertFalse(C.is_merged)
590
591 self.release(all_builds[0])
592 self.release(all_builds[1])
593 self.waitUntilSettled()
594 self.assertTrue(A.is_merged)
595 self.assertTrue(B.is_merged)
596 self.assertFalse(C.is_merged)
597
598 self.executor_server.hold_jobs_in_build = False
599 self.executor_server.release()
600 self.waitUntilSettled()
601 self.assertEqual(len(self.builds), 0)
602 self.assertEqual(len(self.history), 9)
603 self.assertTrue(C.is_merged)
604
605 self.assertNotIn('merge', A.labels)
606 self.assertNotIn('merge', B.labels)
607 self.assertNotIn('merge', C.labels)
608
609 @simple_layout('layouts/dependent-github.yaml', driver='github')
610 def test_failed_changes(self):
611 "Test that a change behind a failed change is retested"
612 self.executor_server.hold_jobs_in_build = True
613
614 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
615 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
616
617 self.executor_server.failJob('project-test1', A)
618
619 self.fake_github.emitEvent(A.addLabel('merge'))
620 self.fake_github.emitEvent(B.addLabel('merge'))
621 self.waitUntilSettled()
622
623 self.executor_server.release('.*-merge')
624 self.waitUntilSettled()
625
626 self.executor_server.hold_jobs_in_build = False
627 self.executor_server.release()
628
629 self.waitUntilSettled()
630 # It's certain that the merge job for change 2 will run, but
631 # the test1 and test2 jobs may or may not run.
632 self.assertTrue(len(self.history) > 6)
633 self.assertFalse(A.is_merged)
634 self.assertTrue(B.is_merged)
635 self.assertNotIn('merge', A.labels)
636 self.assertNotIn('merge', B.labels)
637
638 @simple_layout('layouts/dependent-github.yaml', driver='github')
639 def test_failed_change_at_head(self):
640 "Test that if a change at the head fails, jobs behind it are canceled"
641
642 self.executor_server.hold_jobs_in_build = True
643 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
644 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
645 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
646
647 self.executor_server.failJob('project-test1', A)
648
649 self.fake_github.emitEvent(A.addLabel('merge'))
650 self.fake_github.emitEvent(B.addLabel('merge'))
651 self.fake_github.emitEvent(C.addLabel('merge'))
652
653 self.waitUntilSettled()
654
655 self.assertEqual(len(self.builds), 1)
656 self.assertEqual(self.builds[0].name, 'project-merge')
657 self.assertTrue(self.builds[0].hasChanges(A))
658
659 self.executor_server.release('.*-merge')
660 self.waitUntilSettled()
661 self.executor_server.release('.*-merge')
662 self.waitUntilSettled()
663 self.executor_server.release('.*-merge')
664 self.waitUntilSettled()
665
666 self.assertEqual(len(self.builds), 6)
667 self.assertEqual(self.builds[0].name, 'project-test1')
668 self.assertEqual(self.builds[1].name, 'project-test2')
669 self.assertEqual(self.builds[2].name, 'project-test1')
670 self.assertEqual(self.builds[3].name, 'project-test2')
671 self.assertEqual(self.builds[4].name, 'project-test1')
672 self.assertEqual(self.builds[5].name, 'project-test2')
673
674 self.release(self.builds[0])
675 self.waitUntilSettled()
676
677 # project-test2, project-merge for B
678 self.assertEqual(len(self.builds), 2)
679 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
680
681 self.executor_server.hold_jobs_in_build = False
682 self.executor_server.release()
683 self.waitUntilSettled()
684
685 self.assertEqual(len(self.builds), 0)
686 self.assertEqual(len(self.history), 15)
687 self.assertFalse(A.is_merged)
688 self.assertTrue(B.is_merged)
689 self.assertTrue(C.is_merged)
690 self.assertNotIn('merge', A.labels)
691 self.assertNotIn('merge', B.labels)
692 self.assertNotIn('merge', C.labels)
Jesse Keating71a47ff2017-06-06 11:36:43 -0700693
694 @simple_layout('layouts/basic-github.yaml', driver='github')
695 def test_push_event_reconfigure(self):
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200696 pevent = self.fake_github.getPushEvent(project='org/common-config',
Jesse Keating71a47ff2017-06-06 11:36:43 -0700697 ref='refs/heads/master',
698 modified_files=['zuul.yaml'])
699
700 # record previous tenant reconfiguration time, which may not be set
701 old = self.sched.tenant_last_reconfigured.get('tenant-one', 0)
702 time.sleep(1)
703 self.fake_github.emitEvent(pevent)
704 self.waitUntilSettled()
705 new = self.sched.tenant_last_reconfigured.get('tenant-one', 0)
706 # New timestamp should be greater than the old timestamp
707 self.assertLess(old, new)
Tristan Cacqueray2bafb1f2017-06-12 07:10:26 +0000708
Jesse Keating64d29012017-09-06 12:27:49 -0700709 # TODO(jlk): Make this a more generic test for unknown project
710 @skip("Skipped for rewrite of webhook handler")
Tristan Cacqueray2bafb1f2017-06-12 07:10:26 +0000711 @simple_layout('layouts/basic-github.yaml', driver='github')
712 def test_ping_event(self):
713 # Test valid ping
714 pevent = {'repository': {'full_name': 'org/project'}}
715 req = self.fake_github.emitEvent(('ping', pevent))
716 self.assertEqual(req.status, 200, "Ping event didn't succeed")
717
718 # Test invalid ping
719 pevent = {'repository': {'full_name': 'unknown-project'}}
720 self.assertRaises(
721 urllib.error.HTTPError,
722 self.fake_github.emitEvent,
723 ('ping', pevent),
724 )
Tobias Henkeleca46202017-08-02 20:27:10 +0200725
726
727class TestGithubUnprotectedBranches(ZuulTestCase):
728 config_file = 'zuul-github-driver.conf'
729 tenant_config_file = 'config/unprotected-branches/main.yaml'
730
731 def test_unprotected_branches(self):
732 tenant = self.sched.abide.tenants.get('tenant-one')
733
734 project1 = tenant.untrusted_projects[0]
735 project2 = tenant.untrusted_projects[1]
736
737 # project1 should have parsed master
738 self.assertIn('master', project1.unparsed_branch_config.keys())
739
740 # project2 should have no parsed branch
741 self.assertEqual(0, len(project2.unparsed_branch_config.keys()))
Jesse Keating80730e62017-09-14 15:35:11 -0600742
743
744class TestGithubWebhook(ZuulTestCase):
745 config_file = 'zuul-github-driver.conf'
746
747 def setUp(self):
748 super(TestGithubWebhook, self).setUp()
749
750 # Start the web server
751 self.web = zuul.web.ZuulWeb(
752 listen_address='127.0.0.1', listen_port=0,
753 gear_server='127.0.0.1', gear_port=self.gearman_server.port,
754 github_connections={'github': self.fake_github})
755 loop = asyncio.new_event_loop()
756 loop.set_debug(True)
757 ws_thread = threading.Thread(target=self.web.run, args=(loop,))
758 ws_thread.start()
759 self.addCleanup(loop.close)
760 self.addCleanup(ws_thread.join)
761 self.addCleanup(self.web.stop)
762
763 host = '127.0.0.1'
764 # Wait until web server is started
765 while True:
766 time.sleep(0.1)
767 if self.web.server is None:
768 continue
769 port = self.web.server.sockets[0].getsockname()[1]
770 try:
771 with socket.create_connection((host, port)):
772 break
773 except ConnectionRefusedError:
774 pass
775
776 self.fake_github.setZuulWebPort(port)
777
778 def tearDown(self):
779 super(TestGithubWebhook, self).tearDown()
780
781 @simple_layout('layouts/basic-github.yaml', driver='github')
782 def test_webhook(self):
783 """Test that we can get github events via zuul-web."""
784
785 self.executor_server.hold_jobs_in_build = True
786
787 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
788 self.fake_github.emitEvent(A.getPullRequestOpenedEvent(),
789 use_zuulweb=True)
790 self.waitUntilSettled()
791
792 self.executor_server.hold_jobs_in_build = False
793 self.executor_server.release()
794 self.waitUntilSettled()
795
796 self.assertEqual('SUCCESS',
797 self.getJobFromHistory('project-test1').result)
798 self.assertEqual('SUCCESS',
799 self.getJobFromHistory('project-test2').result)
800
801 job = self.getJobFromHistory('project-test2')
802 zuulvars = job.parameters['zuul']
803 self.assertEqual(str(A.number), zuulvars['change'])
804 self.assertEqual(str(A.head_sha), zuulvars['patchset'])
805 self.assertEqual('master', zuulvars['branch'])
806 self.assertEqual(1, len(A.comments))
807 self.assertThat(
808 A.comments[0],
809 MatchesRegex('.*\[project-test1 \]\(.*\).*', re.DOTALL))
810 self.assertThat(
811 A.comments[0],
812 MatchesRegex('.*\[project-test2 \]\(.*\).*', re.DOTALL))
813 self.assertEqual(2, len(self.history))
814
815 # test_pull_unmatched_branch_event(self):
816 self.create_branch('org/project', 'unmatched_branch')
817 B = self.fake_github.openFakePullRequest(
818 'org/project', 'unmatched_branch', 'B')
819 self.fake_github.emitEvent(B.getPullRequestOpenedEvent(),
820 use_zuulweb=True)
821 self.waitUntilSettled()
822
823 self.assertEqual(2, len(self.history))