blob: e404451f86a9c884ccca6f83a046ef3247ce4c9e [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))
liushengd3419e82018-01-06 19:52:03 +080053 self.assertThat(
54 A.comments[0],
55 MatchesRegex('.*\[project-test1 \]\(.*\).*', re.DOTALL))
56 self.assertThat(
57 A.comments[0],
58 MatchesRegex('.*\[project-test2 \]\(.*\).*', re.DOTALL))
Jan Hruband4edee82015-12-16 12:49:51 +010059 self.assertEqual(2, len(self.history))
60
61 # test_pull_unmatched_branch_event(self):
62 self.create_branch('org/project', 'unmatched_branch')
63 B = self.fake_github.openFakePullRequest(
64 'org/project', 'unmatched_branch', 'B')
65 self.fake_github.emitEvent(B.getPullRequestOpenedEvent())
66 self.waitUntilSettled()
67
68 self.assertEqual(2, len(self.history))
Wayne1a78c612015-06-11 17:14:13 -070069
Jan Hruban570d01c2016-03-10 21:51:32 +010070 @simple_layout('layouts/files-github.yaml', driver='github')
71 def test_pull_matched_file_event(self):
72 A = self.fake_github.openFakePullRequest(
73 'org/project', 'master', 'A',
74 files=['random.txt', 'build-requires'])
75 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
76 self.waitUntilSettled()
77 self.assertEqual(1, len(self.history))
78
79 # test_pull_unmatched_file_event
80 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B',
81 files=['random.txt'])
82 self.fake_github.emitEvent(B.getPullRequestOpenedEvent())
83 self.waitUntilSettled()
84 self.assertEqual(1, len(self.history))
85
Jan Hrubanc7ab1602015-10-14 15:29:33 +020086 @simple_layout('layouts/basic-github.yaml', driver='github')
87 def test_comment_event(self):
Jan Hruban37615e52015-11-19 14:30:49 +010088 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
89 self.fake_github.emitEvent(A.getCommentAddedEvent('test me'))
Jan Hrubanc7ab1602015-10-14 15:29:33 +020090 self.waitUntilSettled()
91 self.assertEqual(2, len(self.history))
92
93 # Test an unmatched comment, history should remain the same
Jan Hruban37615e52015-11-19 14:30:49 +010094 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
95 self.fake_github.emitEvent(B.getCommentAddedEvent('casual comment'))
Jan Hrubanc7ab1602015-10-14 15:29:33 +020096 self.waitUntilSettled()
97 self.assertEqual(2, len(self.history))
98
Wayne1a78c612015-06-11 17:14:13 -070099 @simple_layout('layouts/push-tag-github.yaml', driver='github')
100 def test_tag_event(self):
101 self.executor_server.hold_jobs_in_build = True
102
James E. Blair21037782017-07-19 11:56:55 -0700103 self.create_branch('org/project', 'tagbranch')
104 files = {'README.txt': 'test'}
105 self.addCommitToRepo('org/project', 'test tag',
106 files, branch='tagbranch', tag='newtag')
107 path = os.path.join(self.upstream_root, 'org/project')
108 repo = git.Repo(path)
109 tag = repo.tags['newtag']
110 sha = tag.commit.hexsha
111 del repo
112
Wayne1a78c612015-06-11 17:14:13 -0700113 self.fake_github.emitEvent(
114 self.fake_github.getPushEvent('org/project', 'refs/tags/newtag',
115 new_rev=sha))
116 self.waitUntilSettled()
117
118 build_params = self.builds[0].parameters
James E. Blaira438c172017-07-21 14:54:42 -0700119 self.assertEqual('refs/tags/newtag', build_params['zuul']['ref'])
James E. Blairb8203e42017-08-02 17:00:14 -0700120 self.assertFalse('oldrev' in build_params['zuul'])
James E. Blaired8b0012017-07-21 14:49:29 -0700121 self.assertEqual(sha, build_params['zuul']['newrev'])
Wayne1a78c612015-06-11 17:14:13 -0700122 self.executor_server.hold_jobs_in_build = False
123 self.executor_server.release()
124 self.waitUntilSettled()
125
126 self.assertEqual('SUCCESS',
127 self.getJobFromHistory('project-tag').result)
128
129 @simple_layout('layouts/push-tag-github.yaml', driver='github')
130 def test_push_event(self):
131 self.executor_server.hold_jobs_in_build = True
132
James E. Blair289f5932017-07-27 15:02:29 -0700133 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
134 old_sha = '0' * 40
135 new_sha = A.head_sha
136 A.setMerged("merging A")
137 pevent = self.fake_github.getPushEvent(project='org/project',
138 ref='refs/heads/master',
139 old_rev=old_sha,
140 new_rev=new_sha)
141 self.fake_github.emitEvent(pevent)
Wayne1a78c612015-06-11 17:14:13 -0700142 self.waitUntilSettled()
143
144 build_params = self.builds[0].parameters
James E. Blaira438c172017-07-21 14:54:42 -0700145 self.assertEqual('refs/heads/master', build_params['zuul']['ref'])
James E. Blair289f5932017-07-27 15:02:29 -0700146 self.assertFalse('oldrev' in build_params['zuul'])
James E. Blaired8b0012017-07-21 14:49:29 -0700147 self.assertEqual(new_sha, build_params['zuul']['newrev'])
Wayne1a78c612015-06-11 17:14:13 -0700148
149 self.executor_server.hold_jobs_in_build = False
150 self.executor_server.release()
151 self.waitUntilSettled()
152
153 self.assertEqual('SUCCESS',
154 self.getJobFromHistory('project-post').result)
Jan Hruband4edee82015-12-16 12:49:51 +0100155 self.assertEqual(1, len(self.history))
156
157 # test unmatched push event
158 old_sha = random_sha1()
159 new_sha = random_sha1()
160 self.fake_github.emitEvent(
161 self.fake_github.getPushEvent('org/project',
162 'refs/heads/unmatched_branch',
163 old_sha, new_sha))
164 self.waitUntilSettled()
165
166 self.assertEqual(1, len(self.history))
Jan Hruban6d53c5e2015-10-24 03:03:34 +0200167
Jan Hruban16ad31f2015-11-07 14:39:07 +0100168 @simple_layout('layouts/labeling-github.yaml', driver='github')
169 def test_labels(self):
Jan Hruban37615e52015-11-19 14:30:49 +0100170 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
Jan Hruban16ad31f2015-11-07 14:39:07 +0100171 self.fake_github.emitEvent(A.addLabel('test'))
172 self.waitUntilSettled()
173 self.assertEqual(1, len(self.history))
174 self.assertEqual('project-labels', self.history[0].name)
175 self.assertEqual(['tests passed'], A.labels)
176
177 # test label removed
Jan Hruban37615e52015-11-19 14:30:49 +0100178 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
Jan Hruban16ad31f2015-11-07 14:39:07 +0100179 B.addLabel('do not test')
180 self.fake_github.emitEvent(B.removeLabel('do not test'))
181 self.waitUntilSettled()
182 self.assertEqual(2, len(self.history))
183 self.assertEqual('project-labels', self.history[1].name)
184 self.assertEqual(['tests passed'], B.labels)
185
186 # test unmatched label
Jan Hruban37615e52015-11-19 14:30:49 +0100187 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
Jan Hruban16ad31f2015-11-07 14:39:07 +0100188 self.fake_github.emitEvent(C.addLabel('other label'))
189 self.waitUntilSettled()
190 self.assertEqual(2, len(self.history))
191 self.assertEqual(['other label'], C.labels)
192
Jesse Keating5c05a9f2017-01-12 14:44:58 -0800193 @simple_layout('layouts/reviews-github.yaml', driver='github')
194 def test_review_event(self):
195 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
196 self.fake_github.emitEvent(A.getReviewAddedEvent('approve'))
197 self.waitUntilSettled()
198 self.assertEqual(1, len(self.history))
199 self.assertEqual('project-reviews', self.history[0].name)
200 self.assertEqual(['tests passed'], A.labels)
201
202 # test_review_unmatched_event
203 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
204 self.fake_github.emitEvent(B.getReviewAddedEvent('comment'))
205 self.waitUntilSettled()
206 self.assertEqual(1, len(self.history))
207
Jan Hruban324ca5b2015-11-05 19:28:54 +0100208 @simple_layout('layouts/dequeue-github.yaml', driver='github')
209 def test_dequeue_pull_synchronized(self):
210 self.executor_server.hold_jobs_in_build = True
211
Jan Hruban37615e52015-11-19 14:30:49 +0100212 A = self.fake_github.openFakePullRequest(
213 'org/one-job-project', 'master', 'A')
214 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100215 self.waitUntilSettled()
216
217 # event update stamp has resolution one second, wait so the latter
218 # one has newer timestamp
219 time.sleep(1)
Jan Hruban37615e52015-11-19 14:30:49 +0100220 A.addCommit()
221 self.fake_github.emitEvent(A.getPullRequestSynchronizeEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100222 self.waitUntilSettled()
223
224 self.executor_server.hold_jobs_in_build = False
225 self.executor_server.release()
226 self.waitUntilSettled()
227
228 self.assertEqual(2, len(self.history))
229 self.assertEqual(1, self.countJobResults(self.history, 'ABORTED'))
230
231 @simple_layout('layouts/dequeue-github.yaml', driver='github')
232 def test_dequeue_pull_abandoned(self):
233 self.executor_server.hold_jobs_in_build = True
234
Jan Hruban37615e52015-11-19 14:30:49 +0100235 A = self.fake_github.openFakePullRequest(
236 'org/one-job-project', 'master', 'A')
237 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100238 self.waitUntilSettled()
Jan Hruban37615e52015-11-19 14:30:49 +0100239 self.fake_github.emitEvent(A.getPullRequestClosedEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100240 self.waitUntilSettled()
241
242 self.executor_server.hold_jobs_in_build = False
243 self.executor_server.release()
244 self.waitUntilSettled()
245
246 self.assertEqual(1, len(self.history))
247 self.assertEqual(1, self.countJobResults(self.history, 'ABORTED'))
248
Jan Hruban6d53c5e2015-10-24 03:03:34 +0200249 @simple_layout('layouts/basic-github.yaml', driver='github')
250 def test_git_https_url(self):
251 """Test that git_ssh option gives git url with ssh"""
252 url = self.fake_github.real_getGitUrl('org/project')
253 self.assertEqual('https://github.com/org/project', url)
254
255 @simple_layout('layouts/basic-github.yaml', driver='github')
256 def test_git_ssh_url(self):
257 """Test that git_ssh option gives git url with ssh"""
258 url = self.fake_github_ssh.real_getGitUrl('org/project')
259 self.assertEqual('ssh://git@github.com/org/project.git', url)
Jan Hrubane252a732017-01-03 15:03:09 +0100260
Jesse Keatingbe4ef8a2016-12-06 11:29:13 -0800261 @simple_layout('layouts/basic-github.yaml', driver='github')
262 def test_git_enterprise_url(self):
263 """Test that git_url option gives git url with proper host"""
264 url = self.fake_github_ent.real_getGitUrl('org/project')
265 self.assertEqual('ssh://git@github.enterprise.io/org/project.git', url)
266
Jan Hrubane252a732017-01-03 15:03:09 +0100267 @simple_layout('layouts/reporting-github.yaml', driver='github')
268 def test_reporting(self):
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700269 project = 'org/project'
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200270 github = self.fake_github.github_client
271
Jan Hrubane252a732017-01-03 15:03:09 +0100272 # pipeline reports pull status both on start and success
273 self.executor_server.hold_jobs_in_build = True
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700274 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
Jan Hruban37615e52015-11-19 14:30:49 +0100275 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Jan Hrubane252a732017-01-03 15:03:09 +0100276 self.waitUntilSettled()
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200277
Jesse Keatingd96e5882017-01-19 13:55:50 -0800278 # We should have a status container for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200279 self.assertIn(
280 A.head_sha, github.repo_from_project(project)._commits.keys())
281 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
282
Jesse Keatingd96e5882017-01-19 13:55:50 -0800283 # We should only have one status for the head sha
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700284 self.assertEqual(1, len(statuses))
285 check_status = statuses[0]
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100286 check_url = ('http://zuul.example.com/status/#%s,%s' %
287 (A.number, A.head_sha))
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000288 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700289 self.assertEqual('check status: pending',
290 check_status['description'])
Jan Hrubane252a732017-01-03 15:03:09 +0100291 self.assertEqual('pending', check_status['state'])
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100292 self.assertEqual(check_url, check_status['url'])
Jan Hruban37615e52015-11-19 14:30:49 +0100293 self.assertEqual(0, len(A.comments))
Jan Hrubane252a732017-01-03 15:03:09 +0100294
295 self.executor_server.hold_jobs_in_build = False
296 self.executor_server.release()
297 self.waitUntilSettled()
Jesse Keatingd96e5882017-01-19 13:55:50 -0800298 # We should only have two statuses for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200299 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700300 self.assertEqual(2, len(statuses))
301 check_status = statuses[0]
Jesse Keatingd96e5882017-01-19 13:55:50 -0800302 check_url = ('http://zuul.example.com/status/#%s,%s' %
303 (A.number, A.head_sha))
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000304 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700305 self.assertEqual('check status: success',
306 check_status['description'])
Jan Hrubane252a732017-01-03 15:03:09 +0100307 self.assertEqual('success', check_status['state'])
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100308 self.assertEqual(check_url, check_status['url'])
Jan Hruban37615e52015-11-19 14:30:49 +0100309 self.assertEqual(1, len(A.comments))
310 self.assertThat(A.comments[0],
Jan Hrubane252a732017-01-03 15:03:09 +0100311 MatchesRegex('.*Build succeeded.*', re.DOTALL))
312
313 # pipeline does not report any status but does comment
314 self.executor_server.hold_jobs_in_build = True
315 self.fake_github.emitEvent(
Jan Hruban37615e52015-11-19 14:30:49 +0100316 A.getCommentAddedEvent('reporting check'))
Jan Hrubane252a732017-01-03 15:03:09 +0100317 self.waitUntilSettled()
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200318 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700319 self.assertEqual(2, len(statuses))
Jan Hrubane252a732017-01-03 15:03:09 +0100320 # comments increased by one for the start message
Jan Hruban37615e52015-11-19 14:30:49 +0100321 self.assertEqual(2, len(A.comments))
322 self.assertThat(A.comments[1],
Jan Hrubane252a732017-01-03 15:03:09 +0100323 MatchesRegex('.*Starting reporting jobs.*', re.DOTALL))
324 self.executor_server.hold_jobs_in_build = False
325 self.executor_server.release()
326 self.waitUntilSettled()
Jesse Keatingd96e5882017-01-19 13:55:50 -0800327 # pipeline reports success status
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200328 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700329 self.assertEqual(3, len(statuses))
330 report_status = statuses[0]
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000331 self.assertEqual('tenant-one/reporting', report_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700332 self.assertEqual('reporting status: success',
333 report_status['description'])
Jesse Keatingd96e5882017-01-19 13:55:50 -0800334 self.assertEqual('success', report_status['state'])
Jan Hruban37615e52015-11-19 14:30:49 +0100335 self.assertEqual(2, len(A.comments))
Jamie Lennox3f16de52017-05-09 14:24:11 +1000336
337 base = 'http://logs.example.com/tenant-one/reporting/%s/%s/' % (
338 A.project, A.number)
339
340 # Deconstructing the URL because we don't save the BuildSet UUID
341 # anywhere to do a direct comparison and doing regexp matches on a full
342 # URL is painful.
343
344 # The first part of the URL matches the easy base string
345 self.assertThat(report_status['url'], StartsWith(base))
346
347 # The rest of the URL is a UUID and a trailing slash.
348 self.assertThat(report_status['url'][len(base):],
349 MatchesRegex('^[a-fA-F0-9]{32}\/$'))
Jan Hruban49bff072015-11-03 11:45:46 +0100350
Jesse Keating08dab8f2017-06-21 12:59:23 +0100351 @simple_layout('layouts/reporting-github.yaml', driver='github')
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700352 def test_truncated_status_description(self):
353 project = 'org/project'
354 # pipeline reports pull status both on start and success
355 self.executor_server.hold_jobs_in_build = True
356 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
357 self.fake_github.emitEvent(
358 A.getCommentAddedEvent('long pipeline'))
359 self.waitUntilSettled()
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200360 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700361 self.assertEqual(1, len(statuses))
362 check_status = statuses[0]
363 # Status is truncated due to long pipeline name
364 self.assertEqual('status: pending',
365 check_status['description'])
366
367 self.executor_server.hold_jobs_in_build = False
368 self.executor_server.release()
369 self.waitUntilSettled()
370 # We should only have two statuses for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200371 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700372 self.assertEqual(2, len(statuses))
373 check_status = statuses[0]
374 # Status is truncated due to long pipeline name
375 self.assertEqual('status: success',
376 check_status['description'])
377
378 @simple_layout('layouts/reporting-github.yaml', driver='github')
Jesse Keating08dab8f2017-06-21 12:59:23 +0100379 def test_push_reporting(self):
380 project = 'org/project2'
381 # pipeline reports pull status both on start and success
382 self.executor_server.hold_jobs_in_build = True
Jesse Keating08dab8f2017-06-21 12:59:23 +0100383
James E. Blair289f5932017-07-27 15:02:29 -0700384 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
385 old_sha = '0' * 40
386 new_sha = A.head_sha
387 A.setMerged("merging A")
388 pevent = self.fake_github.getPushEvent(project=project,
389 ref='refs/heads/master',
390 old_rev=old_sha,
391 new_rev=new_sha)
Jesse Keating08dab8f2017-06-21 12:59:23 +0100392 self.fake_github.emitEvent(pevent)
393 self.waitUntilSettled()
394
395 # there should only be one report, a status
396 self.assertEqual(1, len(self.fake_github.reports))
397 # Verify the user/context/state of the status
398 status = ('zuul', 'tenant-one/push-reporting', 'pending')
399 self.assertEqual(status, self.fake_github.reports[0][-1])
400
401 # free the executor, allow the build to finish
402 self.executor_server.hold_jobs_in_build = False
403 self.executor_server.release()
404 self.waitUntilSettled()
405
406 # Now there should be a second report, the success of the build
407 self.assertEqual(2, len(self.fake_github.reports))
408 # Verify the user/context/state of the status
409 status = ('zuul', 'tenant-one/push-reporting', 'success')
410 self.assertEqual(status, self.fake_github.reports[-1][-1])
411
412 # now make a PR which should also comment
413 self.executor_server.hold_jobs_in_build = True
414 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
415 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
416 self.waitUntilSettled()
417
418 # Now there should be a four reports, a new comment
419 # and status
420 self.assertEqual(4, len(self.fake_github.reports))
421 self.executor_server.release()
422 self.waitUntilSettled()
423
Jan Hruban49bff072015-11-03 11:45:46 +0100424 @simple_layout('layouts/merging-github.yaml', driver='github')
425 def test_report_pull_merge(self):
426 # pipeline merges the pull request on success
Jan Hruban3b415922016-02-03 13:10:22 +0100427 A = self.fake_github.openFakePullRequest('org/project', 'master',
428 'PR title')
Jan Hruban49bff072015-11-03 11:45:46 +0100429 self.fake_github.emitEvent(A.getCommentAddedEvent('merge me'))
430 self.waitUntilSettled()
431 self.assertTrue(A.is_merged)
Jan Hruban3b415922016-02-03 13:10:22 +0100432 self.assertThat(A.merge_message,
433 MatchesRegex('.*PR title.*Reviewed-by.*', re.DOTALL))
Jan Hruban49bff072015-11-03 11:45:46 +0100434
435 # pipeline merges the pull request on success after failure
436 self.fake_github.merge_failure = True
Jan Hruban37615e52015-11-19 14:30:49 +0100437 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
Jan Hruban49bff072015-11-03 11:45:46 +0100438 self.fake_github.emitEvent(B.getCommentAddedEvent('merge me'))
439 self.waitUntilSettled()
440 self.assertFalse(B.is_merged)
441 self.fake_github.merge_failure = False
442
443 # pipeline merges the pull request on second run of merge
444 # first merge failed on 405 Method Not Allowed error
445 self.fake_github.merge_not_allowed_count = 1
Jan Hruban37615e52015-11-19 14:30:49 +0100446 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
Jan Hruban49bff072015-11-03 11:45:46 +0100447 self.fake_github.emitEvent(C.getCommentAddedEvent('merge me'))
448 self.waitUntilSettled()
449 self.assertTrue(C.is_merged)
450
451 # pipeline does not merge the pull request
452 # merge failed on 405 Method Not Allowed error - twice
453 self.fake_github.merge_not_allowed_count = 2
Jan Hruban37615e52015-11-19 14:30:49 +0100454 D = self.fake_github.openFakePullRequest('org/project', 'master', 'D')
Jan Hruban49bff072015-11-03 11:45:46 +0100455 self.fake_github.emitEvent(D.getCommentAddedEvent('merge me'))
456 self.waitUntilSettled()
457 self.assertFalse(D.is_merged)
Adam Gandelman62198cb2017-02-14 16:11:02 -0800458 self.assertEqual(len(D.comments), 1)
459 self.assertEqual(D.comments[0], 'Merge failed')
Jan Hruban37615e52015-11-19 14:30:49 +0100460
Jesse Keating28434312017-07-31 11:32:48 -0700461 @simple_layout('layouts/reporting-multiple-github.yaml', driver='github')
462 def test_reporting_multiple_github(self):
463 project = 'org/project1'
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200464 github = self.fake_github.github_client
465
Jesse Keating28434312017-07-31 11:32:48 -0700466 # pipeline reports pull status both on start and success
467 self.executor_server.hold_jobs_in_build = True
468 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
469 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
470 # open one on B as well, which should not effect A reporting
471 B = self.fake_github.openFakePullRequest('org/project2', 'master',
472 'B')
473 self.fake_github.emitEvent(B.getPullRequestOpenedEvent())
474 self.waitUntilSettled()
475 # We should have a status container for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200476 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
477 self.assertIn(
478 A.head_sha, github.repo_from_project(project)._commits.keys())
Jesse Keating28434312017-07-31 11:32:48 -0700479 # We should only have one status for the head sha
480 self.assertEqual(1, len(statuses))
481 check_status = statuses[0]
482 check_url = ('http://zuul.example.com/status/#%s,%s' %
483 (A.number, A.head_sha))
484 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700485 self.assertEqual('check status: pending', check_status['description'])
Jesse Keating28434312017-07-31 11:32:48 -0700486 self.assertEqual('pending', check_status['state'])
487 self.assertEqual(check_url, check_status['url'])
488 self.assertEqual(0, len(A.comments))
489
490 self.executor_server.hold_jobs_in_build = False
491 self.executor_server.release()
492 self.waitUntilSettled()
493 # We should only have two statuses for the head sha
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200494 statuses = self.fake_github.getCommitStatuses(project, A.head_sha)
Jesse Keating28434312017-07-31 11:32:48 -0700495 self.assertEqual(2, len(statuses))
496 check_status = statuses[0]
497 check_url = ('http://zuul.example.com/status/#%s,%s' %
498 (A.number, A.head_sha))
499 self.assertEqual('tenant-one/check', check_status['context'])
500 self.assertEqual('success', check_status['state'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700501 self.assertEqual('check status: success', check_status['description'])
Jesse Keating28434312017-07-31 11:32:48 -0700502 self.assertEqual(check_url, check_status['url'])
503 self.assertEqual(1, len(A.comments))
504 self.assertThat(A.comments[0],
505 MatchesRegex('.*Build succeeded.*', re.DOTALL))
506
Jan Hruban37615e52015-11-19 14:30:49 +0100507 @simple_layout('layouts/dependent-github.yaml', driver='github')
508 def test_parallel_changes(self):
509 "Test that changes are tested in parallel and merged in series"
510
511 self.executor_server.hold_jobs_in_build = True
512 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
513 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
514 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
515
516 self.fake_github.emitEvent(A.addLabel('merge'))
517 self.fake_github.emitEvent(B.addLabel('merge'))
518 self.fake_github.emitEvent(C.addLabel('merge'))
519
520 self.waitUntilSettled()
521 self.assertEqual(len(self.builds), 1)
522 self.assertEqual(self.builds[0].name, 'project-merge')
523 self.assertTrue(self.builds[0].hasChanges(A))
524
525 self.executor_server.release('.*-merge')
526 self.waitUntilSettled()
527 self.assertEqual(len(self.builds), 3)
528 self.assertEqual(self.builds[0].name, 'project-test1')
529 self.assertTrue(self.builds[0].hasChanges(A))
530 self.assertEqual(self.builds[1].name, 'project-test2')
531 self.assertTrue(self.builds[1].hasChanges(A))
532 self.assertEqual(self.builds[2].name, 'project-merge')
533 self.assertTrue(self.builds[2].hasChanges(A, B))
534
535 self.executor_server.release('.*-merge')
536 self.waitUntilSettled()
537 self.assertEqual(len(self.builds), 5)
538 self.assertEqual(self.builds[0].name, 'project-test1')
539 self.assertTrue(self.builds[0].hasChanges(A))
540 self.assertEqual(self.builds[1].name, 'project-test2')
541 self.assertTrue(self.builds[1].hasChanges(A))
542
543 self.assertEqual(self.builds[2].name, 'project-test1')
544 self.assertTrue(self.builds[2].hasChanges(A))
545 self.assertEqual(self.builds[3].name, 'project-test2')
546 self.assertTrue(self.builds[3].hasChanges(A, B))
547
548 self.assertEqual(self.builds[4].name, 'project-merge')
549 self.assertTrue(self.builds[4].hasChanges(A, B, C))
550
551 self.executor_server.release('.*-merge')
552 self.waitUntilSettled()
553 self.assertEqual(len(self.builds), 6)
554 self.assertEqual(self.builds[0].name, 'project-test1')
555 self.assertTrue(self.builds[0].hasChanges(A))
556 self.assertEqual(self.builds[1].name, 'project-test2')
557 self.assertTrue(self.builds[1].hasChanges(A))
558
559 self.assertEqual(self.builds[2].name, 'project-test1')
560 self.assertTrue(self.builds[2].hasChanges(A, B))
561 self.assertEqual(self.builds[3].name, 'project-test2')
562 self.assertTrue(self.builds[3].hasChanges(A, B))
563
564 self.assertEqual(self.builds[4].name, 'project-test1')
565 self.assertTrue(self.builds[4].hasChanges(A, B, C))
566 self.assertEqual(self.builds[5].name, 'project-test2')
567 self.assertTrue(self.builds[5].hasChanges(A, B, C))
568
569 all_builds = self.builds[:]
570 self.release(all_builds[2])
571 self.release(all_builds[3])
572 self.waitUntilSettled()
573 self.assertFalse(A.is_merged)
574 self.assertFalse(B.is_merged)
575 self.assertFalse(C.is_merged)
576
577 self.release(all_builds[0])
578 self.release(all_builds[1])
579 self.waitUntilSettled()
580 self.assertTrue(A.is_merged)
581 self.assertTrue(B.is_merged)
582 self.assertFalse(C.is_merged)
583
584 self.executor_server.hold_jobs_in_build = False
585 self.executor_server.release()
586 self.waitUntilSettled()
587 self.assertEqual(len(self.builds), 0)
588 self.assertEqual(len(self.history), 9)
589 self.assertTrue(C.is_merged)
590
591 self.assertNotIn('merge', A.labels)
592 self.assertNotIn('merge', B.labels)
593 self.assertNotIn('merge', C.labels)
594
595 @simple_layout('layouts/dependent-github.yaml', driver='github')
596 def test_failed_changes(self):
597 "Test that a change behind a failed change is retested"
598 self.executor_server.hold_jobs_in_build = True
599
600 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
601 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
602
603 self.executor_server.failJob('project-test1', A)
604
605 self.fake_github.emitEvent(A.addLabel('merge'))
606 self.fake_github.emitEvent(B.addLabel('merge'))
607 self.waitUntilSettled()
608
609 self.executor_server.release('.*-merge')
610 self.waitUntilSettled()
611
612 self.executor_server.hold_jobs_in_build = False
613 self.executor_server.release()
614
615 self.waitUntilSettled()
616 # It's certain that the merge job for change 2 will run, but
617 # the test1 and test2 jobs may or may not run.
618 self.assertTrue(len(self.history) > 6)
619 self.assertFalse(A.is_merged)
620 self.assertTrue(B.is_merged)
621 self.assertNotIn('merge', A.labels)
622 self.assertNotIn('merge', B.labels)
623
624 @simple_layout('layouts/dependent-github.yaml', driver='github')
625 def test_failed_change_at_head(self):
626 "Test that if a change at the head fails, jobs behind it are canceled"
627
628 self.executor_server.hold_jobs_in_build = True
629 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
630 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
631 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
632
633 self.executor_server.failJob('project-test1', A)
634
635 self.fake_github.emitEvent(A.addLabel('merge'))
636 self.fake_github.emitEvent(B.addLabel('merge'))
637 self.fake_github.emitEvent(C.addLabel('merge'))
638
639 self.waitUntilSettled()
640
641 self.assertEqual(len(self.builds), 1)
642 self.assertEqual(self.builds[0].name, 'project-merge')
643 self.assertTrue(self.builds[0].hasChanges(A))
644
645 self.executor_server.release('.*-merge')
646 self.waitUntilSettled()
647 self.executor_server.release('.*-merge')
648 self.waitUntilSettled()
649 self.executor_server.release('.*-merge')
650 self.waitUntilSettled()
651
652 self.assertEqual(len(self.builds), 6)
653 self.assertEqual(self.builds[0].name, 'project-test1')
654 self.assertEqual(self.builds[1].name, 'project-test2')
655 self.assertEqual(self.builds[2].name, 'project-test1')
656 self.assertEqual(self.builds[3].name, 'project-test2')
657 self.assertEqual(self.builds[4].name, 'project-test1')
658 self.assertEqual(self.builds[5].name, 'project-test2')
659
660 self.release(self.builds[0])
661 self.waitUntilSettled()
662
663 # project-test2, project-merge for B
664 self.assertEqual(len(self.builds), 2)
665 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
666
667 self.executor_server.hold_jobs_in_build = False
668 self.executor_server.release()
669 self.waitUntilSettled()
670
671 self.assertEqual(len(self.builds), 0)
672 self.assertEqual(len(self.history), 15)
673 self.assertFalse(A.is_merged)
674 self.assertTrue(B.is_merged)
675 self.assertTrue(C.is_merged)
676 self.assertNotIn('merge', A.labels)
677 self.assertNotIn('merge', B.labels)
678 self.assertNotIn('merge', C.labels)
Jesse Keating71a47ff2017-06-06 11:36:43 -0700679
680 @simple_layout('layouts/basic-github.yaml', driver='github')
681 def test_push_event_reconfigure(self):
Tobias Henkel3c17d5f2017-08-03 11:46:54 +0200682 pevent = self.fake_github.getPushEvent(project='org/common-config',
Jesse Keating71a47ff2017-06-06 11:36:43 -0700683 ref='refs/heads/master',
684 modified_files=['zuul.yaml'])
685
686 # record previous tenant reconfiguration time, which may not be set
687 old = self.sched.tenant_last_reconfigured.get('tenant-one', 0)
688 time.sleep(1)
689 self.fake_github.emitEvent(pevent)
690 self.waitUntilSettled()
691 new = self.sched.tenant_last_reconfigured.get('tenant-one', 0)
692 # New timestamp should be greater than the old timestamp
693 self.assertLess(old, new)
Tristan Cacqueray2bafb1f2017-06-12 07:10:26 +0000694
Jesse Keating64d29012017-09-06 12:27:49 -0700695 # TODO(jlk): Make this a more generic test for unknown project
696 @skip("Skipped for rewrite of webhook handler")
Tristan Cacqueray2bafb1f2017-06-12 07:10:26 +0000697 @simple_layout('layouts/basic-github.yaml', driver='github')
698 def test_ping_event(self):
699 # Test valid ping
700 pevent = {'repository': {'full_name': 'org/project'}}
701 req = self.fake_github.emitEvent(('ping', pevent))
702 self.assertEqual(req.status, 200, "Ping event didn't succeed")
703
704 # Test invalid ping
705 pevent = {'repository': {'full_name': 'unknown-project'}}
706 self.assertRaises(
707 urllib.error.HTTPError,
708 self.fake_github.emitEvent,
709 ('ping', pevent),
710 )
Tobias Henkeleca46202017-08-02 20:27:10 +0200711
712
713class TestGithubUnprotectedBranches(ZuulTestCase):
714 config_file = 'zuul-github-driver.conf'
715 tenant_config_file = 'config/unprotected-branches/main.yaml'
716
717 def test_unprotected_branches(self):
718 tenant = self.sched.abide.tenants.get('tenant-one')
719
720 project1 = tenant.untrusted_projects[0]
721 project2 = tenant.untrusted_projects[1]
722
723 # project1 should have parsed master
724 self.assertIn('master', project1.unparsed_branch_config.keys())
725
726 # project2 should have no parsed branch
727 self.assertEqual(0, len(project2.unparsed_branch_config.keys()))