blob: 0e199dfa82a68f6e45aeb5dc138e29afe54246f7 [file] [log] [blame]
Gregory Haynes4fc12542015-04-22 20:38:06 -07001# Copyright 2015 GoodData
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
James E. Blair21037782017-07-19 11:56:55 -070015import os
Jan Hrubane252a732017-01-03 15:03:09 +010016import re
Jamie Lennox3f16de52017-05-09 14:24:11 +100017from testtools.matchers import MatchesRegex, StartsWith
Tristan Cacqueray2bafb1f2017-06-12 07:10:26 +000018import urllib
Jan Hruban324ca5b2015-11-05 19:28:54 +010019import time
Gregory Haynes4fc12542015-04-22 20:38:06 -070020
James E. Blair21037782017-07-19 11:56:55 -070021import git
22
Wayne1a78c612015-06-11 17:14:13 -070023from tests.base import ZuulTestCase, simple_layout, random_sha1
Gregory Haynes4fc12542015-04-22 20:38:06 -070024
Gregory Haynes4fc12542015-04-22 20:38:06 -070025
26class TestGithubDriver(ZuulTestCase):
27 config_file = 'zuul-github-driver.conf'
28
29 @simple_layout('layouts/basic-github.yaml', driver='github')
30 def test_pull_event(self):
31 self.executor_server.hold_jobs_in_build = True
32
Jan Hruban37615e52015-11-19 14:30:49 +010033 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
34 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Gregory Haynes4fc12542015-04-22 20:38:06 -070035 self.waitUntilSettled()
36
Gregory Haynes4fc12542015-04-22 20:38:06 -070037 self.executor_server.hold_jobs_in_build = False
38 self.executor_server.release()
39 self.waitUntilSettled()
40
41 self.assertEqual('SUCCESS',
42 self.getJobFromHistory('project-test1').result)
43 self.assertEqual('SUCCESS',
44 self.getJobFromHistory('project-test2').result)
45
46 job = self.getJobFromHistory('project-test2')
Monty Taylord13bc362017-06-30 13:11:37 -050047 zuulvars = job.parameters['zuul']
James E. Blaire3db2952017-07-21 15:03:36 -070048 self.assertEqual(str(A.number), zuulvars['change'])
49 self.assertEqual(str(A.head_sha), zuulvars['patchset'])
James E. Blair3b222492017-07-21 15:17:37 -070050 self.assertEqual('master', zuulvars['branch'])
Jan Hruban37615e52015-11-19 14:30:49 +010051 self.assertEqual(1, len(A.comments))
Jan Hruband4edee82015-12-16 12:49:51 +010052 self.assertEqual(2, len(self.history))
53
54 # test_pull_unmatched_branch_event(self):
55 self.create_branch('org/project', 'unmatched_branch')
56 B = self.fake_github.openFakePullRequest(
57 'org/project', 'unmatched_branch', 'B')
58 self.fake_github.emitEvent(B.getPullRequestOpenedEvent())
59 self.waitUntilSettled()
60
61 self.assertEqual(2, len(self.history))
Wayne1a78c612015-06-11 17:14:13 -070062
Jan Hruban570d01c2016-03-10 21:51:32 +010063 @simple_layout('layouts/files-github.yaml', driver='github')
64 def test_pull_matched_file_event(self):
65 A = self.fake_github.openFakePullRequest(
66 'org/project', 'master', 'A',
67 files=['random.txt', 'build-requires'])
68 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
69 self.waitUntilSettled()
70 self.assertEqual(1, len(self.history))
71
72 # test_pull_unmatched_file_event
73 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B',
74 files=['random.txt'])
75 self.fake_github.emitEvent(B.getPullRequestOpenedEvent())
76 self.waitUntilSettled()
77 self.assertEqual(1, len(self.history))
78
Jan Hrubanc7ab1602015-10-14 15:29:33 +020079 @simple_layout('layouts/basic-github.yaml', driver='github')
80 def test_comment_event(self):
Jan Hruban37615e52015-11-19 14:30:49 +010081 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
82 self.fake_github.emitEvent(A.getCommentAddedEvent('test me'))
Jan Hrubanc7ab1602015-10-14 15:29:33 +020083 self.waitUntilSettled()
84 self.assertEqual(2, len(self.history))
85
86 # Test an unmatched comment, history should remain the same
Jan Hruban37615e52015-11-19 14:30:49 +010087 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
88 self.fake_github.emitEvent(B.getCommentAddedEvent('casual comment'))
Jan Hrubanc7ab1602015-10-14 15:29:33 +020089 self.waitUntilSettled()
90 self.assertEqual(2, len(self.history))
91
Wayne1a78c612015-06-11 17:14:13 -070092 @simple_layout('layouts/push-tag-github.yaml', driver='github')
93 def test_tag_event(self):
94 self.executor_server.hold_jobs_in_build = True
95
James E. Blair21037782017-07-19 11:56:55 -070096 self.create_branch('org/project', 'tagbranch')
97 files = {'README.txt': 'test'}
98 self.addCommitToRepo('org/project', 'test tag',
99 files, branch='tagbranch', tag='newtag')
100 path = os.path.join(self.upstream_root, 'org/project')
101 repo = git.Repo(path)
102 tag = repo.tags['newtag']
103 sha = tag.commit.hexsha
104 del repo
105
Wayne1a78c612015-06-11 17:14:13 -0700106 self.fake_github.emitEvent(
107 self.fake_github.getPushEvent('org/project', 'refs/tags/newtag',
108 new_rev=sha))
109 self.waitUntilSettled()
110
111 build_params = self.builds[0].parameters
James E. Blaira438c172017-07-21 14:54:42 -0700112 self.assertEqual('refs/tags/newtag', build_params['zuul']['ref'])
Wayne1a78c612015-06-11 17:14:13 -0700113 self.assertEqual('00000000000000000000000000000000',
James E. Blaired8b0012017-07-21 14:49:29 -0700114 build_params['zuul']['oldrev'])
115 self.assertEqual(sha, build_params['zuul']['newrev'])
Wayne1a78c612015-06-11 17:14:13 -0700116
117 self.executor_server.hold_jobs_in_build = False
118 self.executor_server.release()
119 self.waitUntilSettled()
120
121 self.assertEqual('SUCCESS',
122 self.getJobFromHistory('project-tag').result)
123
124 @simple_layout('layouts/push-tag-github.yaml', driver='github')
125 def test_push_event(self):
126 self.executor_server.hold_jobs_in_build = True
127
128 old_sha = random_sha1()
129 new_sha = random_sha1()
130 self.fake_github.emitEvent(
131 self.fake_github.getPushEvent('org/project', 'refs/heads/master',
132 old_sha, new_sha))
133 self.waitUntilSettled()
134
135 build_params = self.builds[0].parameters
James E. Blaira438c172017-07-21 14:54:42 -0700136 self.assertEqual('refs/heads/master', build_params['zuul']['ref'])
James E. Blaired8b0012017-07-21 14:49:29 -0700137 self.assertEqual(old_sha, build_params['zuul']['oldrev'])
138 self.assertEqual(new_sha, build_params['zuul']['newrev'])
Wayne1a78c612015-06-11 17:14:13 -0700139
140 self.executor_server.hold_jobs_in_build = False
141 self.executor_server.release()
142 self.waitUntilSettled()
143
144 self.assertEqual('SUCCESS',
145 self.getJobFromHistory('project-post').result)
Jan Hruband4edee82015-12-16 12:49:51 +0100146 self.assertEqual(1, len(self.history))
147
148 # test unmatched push event
149 old_sha = random_sha1()
150 new_sha = random_sha1()
151 self.fake_github.emitEvent(
152 self.fake_github.getPushEvent('org/project',
153 'refs/heads/unmatched_branch',
154 old_sha, new_sha))
155 self.waitUntilSettled()
156
157 self.assertEqual(1, len(self.history))
Jan Hruban6d53c5e2015-10-24 03:03:34 +0200158
Jan Hruban16ad31f2015-11-07 14:39:07 +0100159 @simple_layout('layouts/labeling-github.yaml', driver='github')
160 def test_labels(self):
Jan Hruban37615e52015-11-19 14:30:49 +0100161 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
Jan Hruban16ad31f2015-11-07 14:39:07 +0100162 self.fake_github.emitEvent(A.addLabel('test'))
163 self.waitUntilSettled()
164 self.assertEqual(1, len(self.history))
165 self.assertEqual('project-labels', self.history[0].name)
166 self.assertEqual(['tests passed'], A.labels)
167
168 # test label removed
Jan Hruban37615e52015-11-19 14:30:49 +0100169 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
Jan Hruban16ad31f2015-11-07 14:39:07 +0100170 B.addLabel('do not test')
171 self.fake_github.emitEvent(B.removeLabel('do not test'))
172 self.waitUntilSettled()
173 self.assertEqual(2, len(self.history))
174 self.assertEqual('project-labels', self.history[1].name)
175 self.assertEqual(['tests passed'], B.labels)
176
177 # test unmatched label
Jan Hruban37615e52015-11-19 14:30:49 +0100178 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
Jan Hruban16ad31f2015-11-07 14:39:07 +0100179 self.fake_github.emitEvent(C.addLabel('other label'))
180 self.waitUntilSettled()
181 self.assertEqual(2, len(self.history))
182 self.assertEqual(['other label'], C.labels)
183
Jesse Keating5c05a9f2017-01-12 14:44:58 -0800184 @simple_layout('layouts/reviews-github.yaml', driver='github')
185 def test_review_event(self):
186 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
187 self.fake_github.emitEvent(A.getReviewAddedEvent('approve'))
188 self.waitUntilSettled()
189 self.assertEqual(1, len(self.history))
190 self.assertEqual('project-reviews', self.history[0].name)
191 self.assertEqual(['tests passed'], A.labels)
192
193 # test_review_unmatched_event
194 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
195 self.fake_github.emitEvent(B.getReviewAddedEvent('comment'))
196 self.waitUntilSettled()
197 self.assertEqual(1, len(self.history))
198
Jan Hruban324ca5b2015-11-05 19:28:54 +0100199 @simple_layout('layouts/dequeue-github.yaml', driver='github')
200 def test_dequeue_pull_synchronized(self):
201 self.executor_server.hold_jobs_in_build = True
202
Jan Hruban37615e52015-11-19 14:30:49 +0100203 A = self.fake_github.openFakePullRequest(
204 'org/one-job-project', 'master', 'A')
205 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100206 self.waitUntilSettled()
207
208 # event update stamp has resolution one second, wait so the latter
209 # one has newer timestamp
210 time.sleep(1)
Jan Hruban37615e52015-11-19 14:30:49 +0100211 A.addCommit()
212 self.fake_github.emitEvent(A.getPullRequestSynchronizeEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100213 self.waitUntilSettled()
214
215 self.executor_server.hold_jobs_in_build = False
216 self.executor_server.release()
217 self.waitUntilSettled()
218
219 self.assertEqual(2, len(self.history))
220 self.assertEqual(1, self.countJobResults(self.history, 'ABORTED'))
221
222 @simple_layout('layouts/dequeue-github.yaml', driver='github')
223 def test_dequeue_pull_abandoned(self):
224 self.executor_server.hold_jobs_in_build = True
225
Jan Hruban37615e52015-11-19 14:30:49 +0100226 A = self.fake_github.openFakePullRequest(
227 'org/one-job-project', 'master', 'A')
228 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100229 self.waitUntilSettled()
Jan Hruban37615e52015-11-19 14:30:49 +0100230 self.fake_github.emitEvent(A.getPullRequestClosedEvent())
Jan Hruban324ca5b2015-11-05 19:28:54 +0100231 self.waitUntilSettled()
232
233 self.executor_server.hold_jobs_in_build = False
234 self.executor_server.release()
235 self.waitUntilSettled()
236
237 self.assertEqual(1, len(self.history))
238 self.assertEqual(1, self.countJobResults(self.history, 'ABORTED'))
239
Jan Hruban6d53c5e2015-10-24 03:03:34 +0200240 @simple_layout('layouts/basic-github.yaml', driver='github')
241 def test_git_https_url(self):
242 """Test that git_ssh option gives git url with ssh"""
243 url = self.fake_github.real_getGitUrl('org/project')
244 self.assertEqual('https://github.com/org/project', url)
245
246 @simple_layout('layouts/basic-github.yaml', driver='github')
247 def test_git_ssh_url(self):
248 """Test that git_ssh option gives git url with ssh"""
249 url = self.fake_github_ssh.real_getGitUrl('org/project')
250 self.assertEqual('ssh://git@github.com/org/project.git', url)
Jan Hrubane252a732017-01-03 15:03:09 +0100251
Jesse Keatingbe4ef8a2016-12-06 11:29:13 -0800252 @simple_layout('layouts/basic-github.yaml', driver='github')
253 def test_git_enterprise_url(self):
254 """Test that git_url option gives git url with proper host"""
255 url = self.fake_github_ent.real_getGitUrl('org/project')
256 self.assertEqual('ssh://git@github.enterprise.io/org/project.git', url)
257
Jan Hrubane252a732017-01-03 15:03:09 +0100258 @simple_layout('layouts/reporting-github.yaml', driver='github')
259 def test_reporting(self):
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700260 project = 'org/project'
Jan Hrubane252a732017-01-03 15:03:09 +0100261 # pipeline reports pull status both on start and success
262 self.executor_server.hold_jobs_in_build = True
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700263 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
Jan Hruban37615e52015-11-19 14:30:49 +0100264 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
Jan Hrubane252a732017-01-03 15:03:09 +0100265 self.waitUntilSettled()
Jesse Keatingd96e5882017-01-19 13:55:50 -0800266 # We should have a status container for the head sha
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700267 statuses = self.fake_github.statuses[project][A.head_sha]
268 self.assertIn(A.head_sha, self.fake_github.statuses[project].keys())
Jesse Keatingd96e5882017-01-19 13:55:50 -0800269 # We should only have one status for the head sha
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700270 self.assertEqual(1, len(statuses))
271 check_status = statuses[0]
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100272 check_url = ('http://zuul.example.com/status/#%s,%s' %
273 (A.number, A.head_sha))
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000274 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700275 self.assertEqual('check status: pending',
276 check_status['description'])
Jan Hrubane252a732017-01-03 15:03:09 +0100277 self.assertEqual('pending', check_status['state'])
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100278 self.assertEqual(check_url, check_status['url'])
Jan Hruban37615e52015-11-19 14:30:49 +0100279 self.assertEqual(0, len(A.comments))
Jan Hrubane252a732017-01-03 15:03:09 +0100280
281 self.executor_server.hold_jobs_in_build = False
282 self.executor_server.release()
283 self.waitUntilSettled()
Jesse Keatingd96e5882017-01-19 13:55:50 -0800284 # We should only have two statuses for the head sha
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700285 statuses = self.fake_github.statuses[project][A.head_sha]
286 self.assertEqual(2, len(statuses))
287 check_status = statuses[0]
Jesse Keatingd96e5882017-01-19 13:55:50 -0800288 check_url = ('http://zuul.example.com/status/#%s,%s' %
289 (A.number, A.head_sha))
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000290 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700291 self.assertEqual('check status: success',
292 check_status['description'])
Jan Hrubane252a732017-01-03 15:03:09 +0100293 self.assertEqual('success', check_status['state'])
Jan Hrubanddeb95a2017-01-03 15:12:41 +0100294 self.assertEqual(check_url, check_status['url'])
Jan Hruban37615e52015-11-19 14:30:49 +0100295 self.assertEqual(1, len(A.comments))
296 self.assertThat(A.comments[0],
Jan Hrubane252a732017-01-03 15:03:09 +0100297 MatchesRegex('.*Build succeeded.*', re.DOTALL))
298
299 # pipeline does not report any status but does comment
300 self.executor_server.hold_jobs_in_build = True
301 self.fake_github.emitEvent(
Jan Hruban37615e52015-11-19 14:30:49 +0100302 A.getCommentAddedEvent('reporting check'))
Jan Hrubane252a732017-01-03 15:03:09 +0100303 self.waitUntilSettled()
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700304 statuses = self.fake_github.statuses[project][A.head_sha]
305 self.assertEqual(2, len(statuses))
Jan Hrubane252a732017-01-03 15:03:09 +0100306 # comments increased by one for the start message
Jan Hruban37615e52015-11-19 14:30:49 +0100307 self.assertEqual(2, len(A.comments))
308 self.assertThat(A.comments[1],
Jan Hrubane252a732017-01-03 15:03:09 +0100309 MatchesRegex('.*Starting reporting jobs.*', re.DOTALL))
310 self.executor_server.hold_jobs_in_build = False
311 self.executor_server.release()
312 self.waitUntilSettled()
Jesse Keatingd96e5882017-01-19 13:55:50 -0800313 # pipeline reports success status
Jesse Keating1f7ebe92017-06-12 17:21:00 -0700314 statuses = self.fake_github.statuses[project][A.head_sha]
315 self.assertEqual(3, len(statuses))
316 report_status = statuses[0]
Jamie Lennox18bc7ed2017-05-10 10:37:55 +1000317 self.assertEqual('tenant-one/reporting', report_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700318 self.assertEqual('reporting status: success',
319 report_status['description'])
Jesse Keatingd96e5882017-01-19 13:55:50 -0800320 self.assertEqual('success', report_status['state'])
Jan Hruban37615e52015-11-19 14:30:49 +0100321 self.assertEqual(2, len(A.comments))
Jamie Lennox3f16de52017-05-09 14:24:11 +1000322
323 base = 'http://logs.example.com/tenant-one/reporting/%s/%s/' % (
324 A.project, A.number)
325
326 # Deconstructing the URL because we don't save the BuildSet UUID
327 # anywhere to do a direct comparison and doing regexp matches on a full
328 # URL is painful.
329
330 # The first part of the URL matches the easy base string
331 self.assertThat(report_status['url'], StartsWith(base))
332
333 # The rest of the URL is a UUID and a trailing slash.
334 self.assertThat(report_status['url'][len(base):],
335 MatchesRegex('^[a-fA-F0-9]{32}\/$'))
Jan Hruban49bff072015-11-03 11:45:46 +0100336
Jesse Keating08dab8f2017-06-21 12:59:23 +0100337 @simple_layout('layouts/reporting-github.yaml', driver='github')
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700338 def test_truncated_status_description(self):
339 project = 'org/project'
340 # pipeline reports pull status both on start and success
341 self.executor_server.hold_jobs_in_build = True
342 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
343 self.fake_github.emitEvent(
344 A.getCommentAddedEvent('long pipeline'))
345 self.waitUntilSettled()
346 statuses = self.fake_github.statuses[project][A.head_sha]
347 self.assertEqual(1, len(statuses))
348 check_status = statuses[0]
349 # Status is truncated due to long pipeline name
350 self.assertEqual('status: pending',
351 check_status['description'])
352
353 self.executor_server.hold_jobs_in_build = False
354 self.executor_server.release()
355 self.waitUntilSettled()
356 # We should only have two statuses for the head sha
357 statuses = self.fake_github.statuses[project][A.head_sha]
358 self.assertEqual(2, len(statuses))
359 check_status = statuses[0]
360 # Status is truncated due to long pipeline name
361 self.assertEqual('status: success',
362 check_status['description'])
363
364 @simple_layout('layouts/reporting-github.yaml', driver='github')
Jesse Keating08dab8f2017-06-21 12:59:23 +0100365 def test_push_reporting(self):
366 project = 'org/project2'
367 # pipeline reports pull status both on start and success
368 self.executor_server.hold_jobs_in_build = True
369 pevent = self.fake_github.getPushEvent(project=project,
370 ref='refs/heads/master')
371
372 self.fake_github.emitEvent(pevent)
373 self.waitUntilSettled()
374
375 # there should only be one report, a status
376 self.assertEqual(1, len(self.fake_github.reports))
377 # Verify the user/context/state of the status
378 status = ('zuul', 'tenant-one/push-reporting', 'pending')
379 self.assertEqual(status, self.fake_github.reports[0][-1])
380
381 # free the executor, allow the build to finish
382 self.executor_server.hold_jobs_in_build = False
383 self.executor_server.release()
384 self.waitUntilSettled()
385
386 # Now there should be a second report, the success of the build
387 self.assertEqual(2, len(self.fake_github.reports))
388 # Verify the user/context/state of the status
389 status = ('zuul', 'tenant-one/push-reporting', 'success')
390 self.assertEqual(status, self.fake_github.reports[-1][-1])
391
392 # now make a PR which should also comment
393 self.executor_server.hold_jobs_in_build = True
394 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
395 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
396 self.waitUntilSettled()
397
398 # Now there should be a four reports, a new comment
399 # and status
400 self.assertEqual(4, len(self.fake_github.reports))
401 self.executor_server.release()
402 self.waitUntilSettled()
403
Jan Hruban49bff072015-11-03 11:45:46 +0100404 @simple_layout('layouts/merging-github.yaml', driver='github')
405 def test_report_pull_merge(self):
406 # pipeline merges the pull request on success
Jan Hruban3b415922016-02-03 13:10:22 +0100407 A = self.fake_github.openFakePullRequest('org/project', 'master',
408 'PR title')
Jan Hruban49bff072015-11-03 11:45:46 +0100409 self.fake_github.emitEvent(A.getCommentAddedEvent('merge me'))
410 self.waitUntilSettled()
411 self.assertTrue(A.is_merged)
Jan Hruban3b415922016-02-03 13:10:22 +0100412 self.assertThat(A.merge_message,
413 MatchesRegex('.*PR title.*Reviewed-by.*', re.DOTALL))
Jan Hruban49bff072015-11-03 11:45:46 +0100414
415 # pipeline merges the pull request on success after failure
416 self.fake_github.merge_failure = True
Jan Hruban37615e52015-11-19 14:30:49 +0100417 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
Jan Hruban49bff072015-11-03 11:45:46 +0100418 self.fake_github.emitEvent(B.getCommentAddedEvent('merge me'))
419 self.waitUntilSettled()
420 self.assertFalse(B.is_merged)
421 self.fake_github.merge_failure = False
422
423 # pipeline merges the pull request on second run of merge
424 # first merge failed on 405 Method Not Allowed error
425 self.fake_github.merge_not_allowed_count = 1
Jan Hruban37615e52015-11-19 14:30:49 +0100426 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
Jan Hruban49bff072015-11-03 11:45:46 +0100427 self.fake_github.emitEvent(C.getCommentAddedEvent('merge me'))
428 self.waitUntilSettled()
429 self.assertTrue(C.is_merged)
430
431 # pipeline does not merge the pull request
432 # merge failed on 405 Method Not Allowed error - twice
433 self.fake_github.merge_not_allowed_count = 2
Jan Hruban37615e52015-11-19 14:30:49 +0100434 D = self.fake_github.openFakePullRequest('org/project', 'master', 'D')
Jan Hruban49bff072015-11-03 11:45:46 +0100435 self.fake_github.emitEvent(D.getCommentAddedEvent('merge me'))
436 self.waitUntilSettled()
437 self.assertFalse(D.is_merged)
Adam Gandelman62198cb2017-02-14 16:11:02 -0800438 self.assertEqual(len(D.comments), 1)
439 self.assertEqual(D.comments[0], 'Merge failed')
Jan Hruban37615e52015-11-19 14:30:49 +0100440
Jesse Keating28434312017-07-31 11:32:48 -0700441 @simple_layout('layouts/reporting-multiple-github.yaml', driver='github')
442 def test_reporting_multiple_github(self):
443 project = 'org/project1'
444 # pipeline reports pull status both on start and success
445 self.executor_server.hold_jobs_in_build = True
446 A = self.fake_github.openFakePullRequest(project, 'master', 'A')
447 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
448 # open one on B as well, which should not effect A reporting
449 B = self.fake_github.openFakePullRequest('org/project2', 'master',
450 'B')
451 self.fake_github.emitEvent(B.getPullRequestOpenedEvent())
452 self.waitUntilSettled()
453 # We should have a status container for the head sha
454 statuses = self.fake_github.statuses[project][A.head_sha]
455 self.assertIn(A.head_sha, self.fake_github.statuses[project].keys())
456 # We should only have one status for the head sha
457 self.assertEqual(1, len(statuses))
458 check_status = statuses[0]
459 check_url = ('http://zuul.example.com/status/#%s,%s' %
460 (A.number, A.head_sha))
461 self.assertEqual('tenant-one/check', check_status['context'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700462 self.assertEqual('check status: pending', check_status['description'])
Jesse Keating28434312017-07-31 11:32:48 -0700463 self.assertEqual('pending', check_status['state'])
464 self.assertEqual(check_url, check_status['url'])
465 self.assertEqual(0, len(A.comments))
466
467 self.executor_server.hold_jobs_in_build = False
468 self.executor_server.release()
469 self.waitUntilSettled()
470 # We should only have two statuses for the head sha
471 statuses = self.fake_github.statuses[project][A.head_sha]
472 self.assertEqual(2, len(statuses))
473 check_status = statuses[0]
474 check_url = ('http://zuul.example.com/status/#%s,%s' %
475 (A.number, A.head_sha))
476 self.assertEqual('tenant-one/check', check_status['context'])
477 self.assertEqual('success', check_status['state'])
Jesse Keatingfb6cc992017-08-01 14:18:13 -0700478 self.assertEqual('check status: success', check_status['description'])
Jesse Keating28434312017-07-31 11:32:48 -0700479 self.assertEqual(check_url, check_status['url'])
480 self.assertEqual(1, len(A.comments))
481 self.assertThat(A.comments[0],
482 MatchesRegex('.*Build succeeded.*', re.DOTALL))
483
Jan Hruban37615e52015-11-19 14:30:49 +0100484 @simple_layout('layouts/dependent-github.yaml', driver='github')
485 def test_parallel_changes(self):
486 "Test that changes are tested in parallel and merged in series"
487
488 self.executor_server.hold_jobs_in_build = True
489 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
490 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
491 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
492
493 self.fake_github.emitEvent(A.addLabel('merge'))
494 self.fake_github.emitEvent(B.addLabel('merge'))
495 self.fake_github.emitEvent(C.addLabel('merge'))
496
497 self.waitUntilSettled()
498 self.assertEqual(len(self.builds), 1)
499 self.assertEqual(self.builds[0].name, 'project-merge')
500 self.assertTrue(self.builds[0].hasChanges(A))
501
502 self.executor_server.release('.*-merge')
503 self.waitUntilSettled()
504 self.assertEqual(len(self.builds), 3)
505 self.assertEqual(self.builds[0].name, 'project-test1')
506 self.assertTrue(self.builds[0].hasChanges(A))
507 self.assertEqual(self.builds[1].name, 'project-test2')
508 self.assertTrue(self.builds[1].hasChanges(A))
509 self.assertEqual(self.builds[2].name, 'project-merge')
510 self.assertTrue(self.builds[2].hasChanges(A, B))
511
512 self.executor_server.release('.*-merge')
513 self.waitUntilSettled()
514 self.assertEqual(len(self.builds), 5)
515 self.assertEqual(self.builds[0].name, 'project-test1')
516 self.assertTrue(self.builds[0].hasChanges(A))
517 self.assertEqual(self.builds[1].name, 'project-test2')
518 self.assertTrue(self.builds[1].hasChanges(A))
519
520 self.assertEqual(self.builds[2].name, 'project-test1')
521 self.assertTrue(self.builds[2].hasChanges(A))
522 self.assertEqual(self.builds[3].name, 'project-test2')
523 self.assertTrue(self.builds[3].hasChanges(A, B))
524
525 self.assertEqual(self.builds[4].name, 'project-merge')
526 self.assertTrue(self.builds[4].hasChanges(A, B, C))
527
528 self.executor_server.release('.*-merge')
529 self.waitUntilSettled()
530 self.assertEqual(len(self.builds), 6)
531 self.assertEqual(self.builds[0].name, 'project-test1')
532 self.assertTrue(self.builds[0].hasChanges(A))
533 self.assertEqual(self.builds[1].name, 'project-test2')
534 self.assertTrue(self.builds[1].hasChanges(A))
535
536 self.assertEqual(self.builds[2].name, 'project-test1')
537 self.assertTrue(self.builds[2].hasChanges(A, B))
538 self.assertEqual(self.builds[3].name, 'project-test2')
539 self.assertTrue(self.builds[3].hasChanges(A, B))
540
541 self.assertEqual(self.builds[4].name, 'project-test1')
542 self.assertTrue(self.builds[4].hasChanges(A, B, C))
543 self.assertEqual(self.builds[5].name, 'project-test2')
544 self.assertTrue(self.builds[5].hasChanges(A, B, C))
545
546 all_builds = self.builds[:]
547 self.release(all_builds[2])
548 self.release(all_builds[3])
549 self.waitUntilSettled()
550 self.assertFalse(A.is_merged)
551 self.assertFalse(B.is_merged)
552 self.assertFalse(C.is_merged)
553
554 self.release(all_builds[0])
555 self.release(all_builds[1])
556 self.waitUntilSettled()
557 self.assertTrue(A.is_merged)
558 self.assertTrue(B.is_merged)
559 self.assertFalse(C.is_merged)
560
561 self.executor_server.hold_jobs_in_build = False
562 self.executor_server.release()
563 self.waitUntilSettled()
564 self.assertEqual(len(self.builds), 0)
565 self.assertEqual(len(self.history), 9)
566 self.assertTrue(C.is_merged)
567
568 self.assertNotIn('merge', A.labels)
569 self.assertNotIn('merge', B.labels)
570 self.assertNotIn('merge', C.labels)
571
572 @simple_layout('layouts/dependent-github.yaml', driver='github')
573 def test_failed_changes(self):
574 "Test that a change behind a failed change is retested"
575 self.executor_server.hold_jobs_in_build = True
576
577 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
578 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
579
580 self.executor_server.failJob('project-test1', A)
581
582 self.fake_github.emitEvent(A.addLabel('merge'))
583 self.fake_github.emitEvent(B.addLabel('merge'))
584 self.waitUntilSettled()
585
586 self.executor_server.release('.*-merge')
587 self.waitUntilSettled()
588
589 self.executor_server.hold_jobs_in_build = False
590 self.executor_server.release()
591
592 self.waitUntilSettled()
593 # It's certain that the merge job for change 2 will run, but
594 # the test1 and test2 jobs may or may not run.
595 self.assertTrue(len(self.history) > 6)
596 self.assertFalse(A.is_merged)
597 self.assertTrue(B.is_merged)
598 self.assertNotIn('merge', A.labels)
599 self.assertNotIn('merge', B.labels)
600
601 @simple_layout('layouts/dependent-github.yaml', driver='github')
602 def test_failed_change_at_head(self):
603 "Test that if a change at the head fails, jobs behind it are canceled"
604
605 self.executor_server.hold_jobs_in_build = True
606 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
607 B = self.fake_github.openFakePullRequest('org/project', 'master', 'B')
608 C = self.fake_github.openFakePullRequest('org/project', 'master', 'C')
609
610 self.executor_server.failJob('project-test1', A)
611
612 self.fake_github.emitEvent(A.addLabel('merge'))
613 self.fake_github.emitEvent(B.addLabel('merge'))
614 self.fake_github.emitEvent(C.addLabel('merge'))
615
616 self.waitUntilSettled()
617
618 self.assertEqual(len(self.builds), 1)
619 self.assertEqual(self.builds[0].name, 'project-merge')
620 self.assertTrue(self.builds[0].hasChanges(A))
621
622 self.executor_server.release('.*-merge')
623 self.waitUntilSettled()
624 self.executor_server.release('.*-merge')
625 self.waitUntilSettled()
626 self.executor_server.release('.*-merge')
627 self.waitUntilSettled()
628
629 self.assertEqual(len(self.builds), 6)
630 self.assertEqual(self.builds[0].name, 'project-test1')
631 self.assertEqual(self.builds[1].name, 'project-test2')
632 self.assertEqual(self.builds[2].name, 'project-test1')
633 self.assertEqual(self.builds[3].name, 'project-test2')
634 self.assertEqual(self.builds[4].name, 'project-test1')
635 self.assertEqual(self.builds[5].name, 'project-test2')
636
637 self.release(self.builds[0])
638 self.waitUntilSettled()
639
640 # project-test2, project-merge for B
641 self.assertEqual(len(self.builds), 2)
642 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
643
644 self.executor_server.hold_jobs_in_build = False
645 self.executor_server.release()
646 self.waitUntilSettled()
647
648 self.assertEqual(len(self.builds), 0)
649 self.assertEqual(len(self.history), 15)
650 self.assertFalse(A.is_merged)
651 self.assertTrue(B.is_merged)
652 self.assertTrue(C.is_merged)
653 self.assertNotIn('merge', A.labels)
654 self.assertNotIn('merge', B.labels)
655 self.assertNotIn('merge', C.labels)
Jesse Keating71a47ff2017-06-06 11:36:43 -0700656
657 @simple_layout('layouts/basic-github.yaml', driver='github')
658 def test_push_event_reconfigure(self):
659 pevent = self.fake_github.getPushEvent(project='common-config',
660 ref='refs/heads/master',
661 modified_files=['zuul.yaml'])
662
663 # record previous tenant reconfiguration time, which may not be set
664 old = self.sched.tenant_last_reconfigured.get('tenant-one', 0)
665 time.sleep(1)
666 self.fake_github.emitEvent(pevent)
667 self.waitUntilSettled()
668 new = self.sched.tenant_last_reconfigured.get('tenant-one', 0)
669 # New timestamp should be greater than the old timestamp
670 self.assertLess(old, new)
Tristan Cacqueray2bafb1f2017-06-12 07:10:26 +0000671
672 @simple_layout('layouts/basic-github.yaml', driver='github')
673 def test_ping_event(self):
674 # Test valid ping
675 pevent = {'repository': {'full_name': 'org/project'}}
676 req = self.fake_github.emitEvent(('ping', pevent))
677 self.assertEqual(req.status, 200, "Ping event didn't succeed")
678
679 # Test invalid ping
680 pevent = {'repository': {'full_name': 'unknown-project'}}
681 self.assertRaises(
682 urllib.error.HTTPError,
683 self.fake_github.emitEvent,
684 ('ping', pevent),
685 )