Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 1 | # 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 | |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 15 | import re |
Jamie Lennox | 3f16de5 | 2017-05-09 14:24:11 +1000 | [diff] [blame] | 16 | from testtools.matchers import MatchesRegex, StartsWith |
Tristan Cacqueray | 2bafb1f | 2017-06-12 07:10:26 +0000 | [diff] [blame] | 17 | import urllib |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 18 | import time |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 19 | |
Wayne | 1a78c61 | 2015-06-11 17:14:13 -0700 | [diff] [blame] | 20 | from tests.base import ZuulTestCase, simple_layout, random_sha1 |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 21 | |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 22 | |
| 23 | class TestGithubDriver(ZuulTestCase): |
| 24 | config_file = 'zuul-github-driver.conf' |
| 25 | |
| 26 | @simple_layout('layouts/basic-github.yaml', driver='github') |
| 27 | def test_pull_event(self): |
| 28 | self.executor_server.hold_jobs_in_build = True |
| 29 | |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 30 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
| 31 | self.fake_github.emitEvent(A.getPullRequestOpenedEvent()) |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 32 | self.waitUntilSettled() |
| 33 | |
| 34 | build_params = self.builds[0].parameters |
| 35 | self.assertEqual('master', build_params['ZUUL_BRANCH']) |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 36 | self.assertEqual(str(A.number), build_params['ZUUL_CHANGE']) |
| 37 | self.assertEqual(A.head_sha, build_params['ZUUL_PATCHSET']) |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 38 | |
| 39 | self.executor_server.hold_jobs_in_build = False |
| 40 | self.executor_server.release() |
| 41 | self.waitUntilSettled() |
| 42 | |
| 43 | self.assertEqual('SUCCESS', |
| 44 | self.getJobFromHistory('project-test1').result) |
| 45 | self.assertEqual('SUCCESS', |
| 46 | self.getJobFromHistory('project-test2').result) |
| 47 | |
| 48 | job = self.getJobFromHistory('project-test2') |
| 49 | zuulvars = job.parameters['vars']['zuul'] |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 50 | self.assertEqual(A.number, zuulvars['change']) |
| 51 | self.assertEqual(A.head_sha, zuulvars['patchset']) |
| 52 | self.assertEqual(1, len(A.comments)) |
Jan Hruban | d4edee8 | 2015-12-16 12:49:51 +0100 | [diff] [blame] | 53 | self.assertEqual(2, len(self.history)) |
| 54 | |
| 55 | # test_pull_unmatched_branch_event(self): |
| 56 | self.create_branch('org/project', 'unmatched_branch') |
| 57 | B = self.fake_github.openFakePullRequest( |
| 58 | 'org/project', 'unmatched_branch', 'B') |
| 59 | self.fake_github.emitEvent(B.getPullRequestOpenedEvent()) |
| 60 | self.waitUntilSettled() |
| 61 | |
| 62 | self.assertEqual(2, len(self.history)) |
Wayne | 1a78c61 | 2015-06-11 17:14:13 -0700 | [diff] [blame] | 63 | |
Jan Hruban | 570d01c | 2016-03-10 21:51:32 +0100 | [diff] [blame] | 64 | @simple_layout('layouts/files-github.yaml', driver='github') |
| 65 | def test_pull_matched_file_event(self): |
| 66 | A = self.fake_github.openFakePullRequest( |
| 67 | 'org/project', 'master', 'A', |
| 68 | files=['random.txt', 'build-requires']) |
| 69 | self.fake_github.emitEvent(A.getPullRequestOpenedEvent()) |
| 70 | self.waitUntilSettled() |
| 71 | self.assertEqual(1, len(self.history)) |
| 72 | |
| 73 | # test_pull_unmatched_file_event |
| 74 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B', |
| 75 | files=['random.txt']) |
| 76 | self.fake_github.emitEvent(B.getPullRequestOpenedEvent()) |
| 77 | self.waitUntilSettled() |
| 78 | self.assertEqual(1, len(self.history)) |
| 79 | |
Jan Hruban | c7ab160 | 2015-10-14 15:29:33 +0200 | [diff] [blame] | 80 | @simple_layout('layouts/basic-github.yaml', driver='github') |
| 81 | def test_comment_event(self): |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 82 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
| 83 | self.fake_github.emitEvent(A.getCommentAddedEvent('test me')) |
Jan Hruban | c7ab160 | 2015-10-14 15:29:33 +0200 | [diff] [blame] | 84 | self.waitUntilSettled() |
| 85 | self.assertEqual(2, len(self.history)) |
| 86 | |
| 87 | # Test an unmatched comment, history should remain the same |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 88 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
| 89 | self.fake_github.emitEvent(B.getCommentAddedEvent('casual comment')) |
Jan Hruban | c7ab160 | 2015-10-14 15:29:33 +0200 | [diff] [blame] | 90 | self.waitUntilSettled() |
| 91 | self.assertEqual(2, len(self.history)) |
| 92 | |
Wayne | 1a78c61 | 2015-06-11 17:14:13 -0700 | [diff] [blame] | 93 | @simple_layout('layouts/push-tag-github.yaml', driver='github') |
| 94 | def test_tag_event(self): |
| 95 | self.executor_server.hold_jobs_in_build = True |
| 96 | |
| 97 | sha = random_sha1() |
| 98 | self.fake_github.emitEvent( |
| 99 | self.fake_github.getPushEvent('org/project', 'refs/tags/newtag', |
| 100 | new_rev=sha)) |
| 101 | self.waitUntilSettled() |
| 102 | |
| 103 | build_params = self.builds[0].parameters |
| 104 | self.assertEqual('refs/tags/newtag', build_params['ZUUL_REF']) |
| 105 | self.assertEqual('00000000000000000000000000000000', |
| 106 | build_params['ZUUL_OLDREV']) |
| 107 | self.assertEqual(sha, build_params['ZUUL_NEWREV']) |
| 108 | |
| 109 | self.executor_server.hold_jobs_in_build = False |
| 110 | self.executor_server.release() |
| 111 | self.waitUntilSettled() |
| 112 | |
| 113 | self.assertEqual('SUCCESS', |
| 114 | self.getJobFromHistory('project-tag').result) |
| 115 | |
| 116 | @simple_layout('layouts/push-tag-github.yaml', driver='github') |
| 117 | def test_push_event(self): |
| 118 | self.executor_server.hold_jobs_in_build = True |
| 119 | |
| 120 | old_sha = random_sha1() |
| 121 | new_sha = random_sha1() |
| 122 | self.fake_github.emitEvent( |
| 123 | self.fake_github.getPushEvent('org/project', 'refs/heads/master', |
| 124 | old_sha, new_sha)) |
| 125 | self.waitUntilSettled() |
| 126 | |
| 127 | build_params = self.builds[0].parameters |
| 128 | self.assertEqual('refs/heads/master', build_params['ZUUL_REF']) |
| 129 | self.assertEqual(old_sha, build_params['ZUUL_OLDREV']) |
| 130 | self.assertEqual(new_sha, build_params['ZUUL_NEWREV']) |
| 131 | |
| 132 | self.executor_server.hold_jobs_in_build = False |
| 133 | self.executor_server.release() |
| 134 | self.waitUntilSettled() |
| 135 | |
| 136 | self.assertEqual('SUCCESS', |
| 137 | self.getJobFromHistory('project-post').result) |
Jan Hruban | d4edee8 | 2015-12-16 12:49:51 +0100 | [diff] [blame] | 138 | self.assertEqual(1, len(self.history)) |
| 139 | |
| 140 | # test unmatched push event |
| 141 | old_sha = random_sha1() |
| 142 | new_sha = random_sha1() |
| 143 | self.fake_github.emitEvent( |
| 144 | self.fake_github.getPushEvent('org/project', |
| 145 | 'refs/heads/unmatched_branch', |
| 146 | old_sha, new_sha)) |
| 147 | self.waitUntilSettled() |
| 148 | |
| 149 | self.assertEqual(1, len(self.history)) |
Jan Hruban | 6d53c5e | 2015-10-24 03:03:34 +0200 | [diff] [blame] | 150 | |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 151 | @simple_layout('layouts/labeling-github.yaml', driver='github') |
| 152 | def test_labels(self): |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 153 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 154 | self.fake_github.emitEvent(A.addLabel('test')) |
| 155 | self.waitUntilSettled() |
| 156 | self.assertEqual(1, len(self.history)) |
| 157 | self.assertEqual('project-labels', self.history[0].name) |
| 158 | self.assertEqual(['tests passed'], A.labels) |
| 159 | |
| 160 | # test label removed |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 161 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 162 | B.addLabel('do not test') |
| 163 | self.fake_github.emitEvent(B.removeLabel('do not test')) |
| 164 | self.waitUntilSettled() |
| 165 | self.assertEqual(2, len(self.history)) |
| 166 | self.assertEqual('project-labels', self.history[1].name) |
| 167 | self.assertEqual(['tests passed'], B.labels) |
| 168 | |
| 169 | # test unmatched label |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 170 | C = self.fake_github.openFakePullRequest('org/project', 'master', 'C') |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 171 | self.fake_github.emitEvent(C.addLabel('other label')) |
| 172 | self.waitUntilSettled() |
| 173 | self.assertEqual(2, len(self.history)) |
| 174 | self.assertEqual(['other label'], C.labels) |
| 175 | |
Jesse Keating | 5c05a9f | 2017-01-12 14:44:58 -0800 | [diff] [blame] | 176 | @simple_layout('layouts/reviews-github.yaml', driver='github') |
| 177 | def test_review_event(self): |
| 178 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
| 179 | self.fake_github.emitEvent(A.getReviewAddedEvent('approve')) |
| 180 | self.waitUntilSettled() |
| 181 | self.assertEqual(1, len(self.history)) |
| 182 | self.assertEqual('project-reviews', self.history[0].name) |
| 183 | self.assertEqual(['tests passed'], A.labels) |
| 184 | |
| 185 | # test_review_unmatched_event |
| 186 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
| 187 | self.fake_github.emitEvent(B.getReviewAddedEvent('comment')) |
| 188 | self.waitUntilSettled() |
| 189 | self.assertEqual(1, len(self.history)) |
| 190 | |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 191 | @simple_layout('layouts/dequeue-github.yaml', driver='github') |
| 192 | def test_dequeue_pull_synchronized(self): |
| 193 | self.executor_server.hold_jobs_in_build = True |
| 194 | |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 195 | A = self.fake_github.openFakePullRequest( |
| 196 | 'org/one-job-project', 'master', 'A') |
| 197 | self.fake_github.emitEvent(A.getPullRequestOpenedEvent()) |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 198 | self.waitUntilSettled() |
| 199 | |
| 200 | # event update stamp has resolution one second, wait so the latter |
| 201 | # one has newer timestamp |
| 202 | time.sleep(1) |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 203 | A.addCommit() |
| 204 | self.fake_github.emitEvent(A.getPullRequestSynchronizeEvent()) |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 205 | self.waitUntilSettled() |
| 206 | |
| 207 | self.executor_server.hold_jobs_in_build = False |
| 208 | self.executor_server.release() |
| 209 | self.waitUntilSettled() |
| 210 | |
| 211 | self.assertEqual(2, len(self.history)) |
| 212 | self.assertEqual(1, self.countJobResults(self.history, 'ABORTED')) |
| 213 | |
| 214 | @simple_layout('layouts/dequeue-github.yaml', driver='github') |
| 215 | def test_dequeue_pull_abandoned(self): |
| 216 | self.executor_server.hold_jobs_in_build = True |
| 217 | |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 218 | A = self.fake_github.openFakePullRequest( |
| 219 | 'org/one-job-project', 'master', 'A') |
| 220 | self.fake_github.emitEvent(A.getPullRequestOpenedEvent()) |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 221 | self.waitUntilSettled() |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 222 | self.fake_github.emitEvent(A.getPullRequestClosedEvent()) |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 223 | self.waitUntilSettled() |
| 224 | |
| 225 | self.executor_server.hold_jobs_in_build = False |
| 226 | self.executor_server.release() |
| 227 | self.waitUntilSettled() |
| 228 | |
| 229 | self.assertEqual(1, len(self.history)) |
| 230 | self.assertEqual(1, self.countJobResults(self.history, 'ABORTED')) |
| 231 | |
Jan Hruban | 6d53c5e | 2015-10-24 03:03:34 +0200 | [diff] [blame] | 232 | @simple_layout('layouts/basic-github.yaml', driver='github') |
| 233 | def test_git_https_url(self): |
| 234 | """Test that git_ssh option gives git url with ssh""" |
| 235 | url = self.fake_github.real_getGitUrl('org/project') |
| 236 | self.assertEqual('https://github.com/org/project', url) |
| 237 | |
| 238 | @simple_layout('layouts/basic-github.yaml', driver='github') |
| 239 | def test_git_ssh_url(self): |
| 240 | """Test that git_ssh option gives git url with ssh""" |
| 241 | url = self.fake_github_ssh.real_getGitUrl('org/project') |
| 242 | self.assertEqual('ssh://git@github.com/org/project.git', url) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 243 | |
Jesse Keating | be4ef8a | 2016-12-06 11:29:13 -0800 | [diff] [blame] | 244 | @simple_layout('layouts/basic-github.yaml', driver='github') |
| 245 | def test_git_enterprise_url(self): |
| 246 | """Test that git_url option gives git url with proper host""" |
| 247 | url = self.fake_github_ent.real_getGitUrl('org/project') |
| 248 | self.assertEqual('ssh://git@github.enterprise.io/org/project.git', url) |
| 249 | |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 250 | @simple_layout('layouts/reporting-github.yaml', driver='github') |
| 251 | def test_reporting(self): |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 252 | project = 'org/project' |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 253 | # pipeline reports pull status both on start and success |
| 254 | self.executor_server.hold_jobs_in_build = True |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 255 | A = self.fake_github.openFakePullRequest(project, 'master', 'A') |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 256 | self.fake_github.emitEvent(A.getPullRequestOpenedEvent()) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 257 | self.waitUntilSettled() |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 258 | # We should have a status container for the head sha |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 259 | statuses = self.fake_github.statuses[project][A.head_sha] |
| 260 | self.assertIn(A.head_sha, self.fake_github.statuses[project].keys()) |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 261 | # We should only have one status for the head sha |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 262 | self.assertEqual(1, len(statuses)) |
| 263 | check_status = statuses[0] |
Jan Hruban | ddeb95a | 2017-01-03 15:12:41 +0100 | [diff] [blame] | 264 | check_url = ('http://zuul.example.com/status/#%s,%s' % |
| 265 | (A.number, A.head_sha)) |
Jamie Lennox | 18bc7ed | 2017-05-10 10:37:55 +1000 | [diff] [blame] | 266 | self.assertEqual('tenant-one/check', check_status['context']) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 267 | self.assertEqual('Standard check', check_status['description']) |
| 268 | self.assertEqual('pending', check_status['state']) |
Jan Hruban | ddeb95a | 2017-01-03 15:12:41 +0100 | [diff] [blame] | 269 | self.assertEqual(check_url, check_status['url']) |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 270 | self.assertEqual(0, len(A.comments)) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 271 | |
| 272 | self.executor_server.hold_jobs_in_build = False |
| 273 | self.executor_server.release() |
| 274 | self.waitUntilSettled() |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 275 | # We should only have two statuses for the head sha |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 276 | statuses = self.fake_github.statuses[project][A.head_sha] |
| 277 | self.assertEqual(2, len(statuses)) |
| 278 | check_status = statuses[0] |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 279 | check_url = ('http://zuul.example.com/status/#%s,%s' % |
| 280 | (A.number, A.head_sha)) |
Jamie Lennox | 18bc7ed | 2017-05-10 10:37:55 +1000 | [diff] [blame] | 281 | self.assertEqual('tenant-one/check', check_status['context']) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 282 | self.assertEqual('success', check_status['state']) |
Jan Hruban | ddeb95a | 2017-01-03 15:12:41 +0100 | [diff] [blame] | 283 | self.assertEqual(check_url, check_status['url']) |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 284 | self.assertEqual(1, len(A.comments)) |
| 285 | self.assertThat(A.comments[0], |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 286 | MatchesRegex('.*Build succeeded.*', re.DOTALL)) |
| 287 | |
| 288 | # pipeline does not report any status but does comment |
| 289 | self.executor_server.hold_jobs_in_build = True |
| 290 | self.fake_github.emitEvent( |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 291 | A.getCommentAddedEvent('reporting check')) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 292 | self.waitUntilSettled() |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 293 | statuses = self.fake_github.statuses[project][A.head_sha] |
| 294 | self.assertEqual(2, len(statuses)) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 295 | # comments increased by one for the start message |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 296 | self.assertEqual(2, len(A.comments)) |
| 297 | self.assertThat(A.comments[1], |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 298 | MatchesRegex('.*Starting reporting jobs.*', re.DOTALL)) |
| 299 | self.executor_server.hold_jobs_in_build = False |
| 300 | self.executor_server.release() |
| 301 | self.waitUntilSettled() |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 302 | # pipeline reports success status |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 303 | statuses = self.fake_github.statuses[project][A.head_sha] |
| 304 | self.assertEqual(3, len(statuses)) |
| 305 | report_status = statuses[0] |
Jamie Lennox | 18bc7ed | 2017-05-10 10:37:55 +1000 | [diff] [blame] | 306 | self.assertEqual('tenant-one/reporting', report_status['context']) |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 307 | self.assertEqual('success', report_status['state']) |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 308 | self.assertEqual(2, len(A.comments)) |
Jamie Lennox | 3f16de5 | 2017-05-09 14:24:11 +1000 | [diff] [blame] | 309 | |
| 310 | base = 'http://logs.example.com/tenant-one/reporting/%s/%s/' % ( |
| 311 | A.project, A.number) |
| 312 | |
| 313 | # Deconstructing the URL because we don't save the BuildSet UUID |
| 314 | # anywhere to do a direct comparison and doing regexp matches on a full |
| 315 | # URL is painful. |
| 316 | |
| 317 | # The first part of the URL matches the easy base string |
| 318 | self.assertThat(report_status['url'], StartsWith(base)) |
| 319 | |
| 320 | # The rest of the URL is a UUID and a trailing slash. |
| 321 | self.assertThat(report_status['url'][len(base):], |
| 322 | MatchesRegex('^[a-fA-F0-9]{32}\/$')) |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 323 | |
Jesse Keating | 08dab8f | 2017-06-21 12:59:23 +0100 | [diff] [blame] | 324 | @simple_layout('layouts/reporting-github.yaml', driver='github') |
| 325 | def test_push_reporting(self): |
| 326 | project = 'org/project2' |
| 327 | # pipeline reports pull status both on start and success |
| 328 | self.executor_server.hold_jobs_in_build = True |
| 329 | pevent = self.fake_github.getPushEvent(project=project, |
| 330 | ref='refs/heads/master') |
| 331 | |
| 332 | self.fake_github.emitEvent(pevent) |
| 333 | self.waitUntilSettled() |
| 334 | |
| 335 | # there should only be one report, a status |
| 336 | self.assertEqual(1, len(self.fake_github.reports)) |
| 337 | # Verify the user/context/state of the status |
| 338 | status = ('zuul', 'tenant-one/push-reporting', 'pending') |
| 339 | self.assertEqual(status, self.fake_github.reports[0][-1]) |
| 340 | |
| 341 | # free the executor, allow the build to finish |
| 342 | self.executor_server.hold_jobs_in_build = False |
| 343 | self.executor_server.release() |
| 344 | self.waitUntilSettled() |
| 345 | |
| 346 | # Now there should be a second report, the success of the build |
| 347 | self.assertEqual(2, len(self.fake_github.reports)) |
| 348 | # Verify the user/context/state of the status |
| 349 | status = ('zuul', 'tenant-one/push-reporting', 'success') |
| 350 | self.assertEqual(status, self.fake_github.reports[-1][-1]) |
| 351 | |
| 352 | # now make a PR which should also comment |
| 353 | self.executor_server.hold_jobs_in_build = True |
| 354 | A = self.fake_github.openFakePullRequest(project, 'master', 'A') |
| 355 | self.fake_github.emitEvent(A.getPullRequestOpenedEvent()) |
| 356 | self.waitUntilSettled() |
| 357 | |
| 358 | # Now there should be a four reports, a new comment |
| 359 | # and status |
| 360 | self.assertEqual(4, len(self.fake_github.reports)) |
| 361 | self.executor_server.release() |
| 362 | self.waitUntilSettled() |
| 363 | |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 364 | @simple_layout('layouts/merging-github.yaml', driver='github') |
| 365 | def test_report_pull_merge(self): |
| 366 | # pipeline merges the pull request on success |
Jan Hruban | 3b41592 | 2016-02-03 13:10:22 +0100 | [diff] [blame] | 367 | A = self.fake_github.openFakePullRequest('org/project', 'master', |
| 368 | 'PR title') |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 369 | self.fake_github.emitEvent(A.getCommentAddedEvent('merge me')) |
| 370 | self.waitUntilSettled() |
| 371 | self.assertTrue(A.is_merged) |
Jan Hruban | 3b41592 | 2016-02-03 13:10:22 +0100 | [diff] [blame] | 372 | self.assertThat(A.merge_message, |
| 373 | MatchesRegex('.*PR title.*Reviewed-by.*', re.DOTALL)) |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 374 | |
| 375 | # pipeline merges the pull request on success after failure |
| 376 | self.fake_github.merge_failure = True |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 377 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 378 | self.fake_github.emitEvent(B.getCommentAddedEvent('merge me')) |
| 379 | self.waitUntilSettled() |
| 380 | self.assertFalse(B.is_merged) |
| 381 | self.fake_github.merge_failure = False |
| 382 | |
| 383 | # pipeline merges the pull request on second run of merge |
| 384 | # first merge failed on 405 Method Not Allowed error |
| 385 | self.fake_github.merge_not_allowed_count = 1 |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 386 | C = self.fake_github.openFakePullRequest('org/project', 'master', 'C') |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 387 | self.fake_github.emitEvent(C.getCommentAddedEvent('merge me')) |
| 388 | self.waitUntilSettled() |
| 389 | self.assertTrue(C.is_merged) |
| 390 | |
| 391 | # pipeline does not merge the pull request |
| 392 | # merge failed on 405 Method Not Allowed error - twice |
| 393 | self.fake_github.merge_not_allowed_count = 2 |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 394 | D = self.fake_github.openFakePullRequest('org/project', 'master', 'D') |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 395 | self.fake_github.emitEvent(D.getCommentAddedEvent('merge me')) |
| 396 | self.waitUntilSettled() |
| 397 | self.assertFalse(D.is_merged) |
Adam Gandelman | 62198cb | 2017-02-14 16:11:02 -0800 | [diff] [blame] | 398 | self.assertEqual(len(D.comments), 1) |
| 399 | self.assertEqual(D.comments[0], 'Merge failed') |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 400 | |
| 401 | @simple_layout('layouts/dependent-github.yaml', driver='github') |
| 402 | def test_parallel_changes(self): |
| 403 | "Test that changes are tested in parallel and merged in series" |
| 404 | |
| 405 | self.executor_server.hold_jobs_in_build = True |
| 406 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
| 407 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
| 408 | C = self.fake_github.openFakePullRequest('org/project', 'master', 'C') |
| 409 | |
| 410 | self.fake_github.emitEvent(A.addLabel('merge')) |
| 411 | self.fake_github.emitEvent(B.addLabel('merge')) |
| 412 | self.fake_github.emitEvent(C.addLabel('merge')) |
| 413 | |
| 414 | self.waitUntilSettled() |
| 415 | self.assertEqual(len(self.builds), 1) |
| 416 | self.assertEqual(self.builds[0].name, 'project-merge') |
| 417 | self.assertTrue(self.builds[0].hasChanges(A)) |
| 418 | |
| 419 | self.executor_server.release('.*-merge') |
| 420 | self.waitUntilSettled() |
| 421 | self.assertEqual(len(self.builds), 3) |
| 422 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 423 | self.assertTrue(self.builds[0].hasChanges(A)) |
| 424 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 425 | self.assertTrue(self.builds[1].hasChanges(A)) |
| 426 | self.assertEqual(self.builds[2].name, 'project-merge') |
| 427 | self.assertTrue(self.builds[2].hasChanges(A, B)) |
| 428 | |
| 429 | self.executor_server.release('.*-merge') |
| 430 | self.waitUntilSettled() |
| 431 | self.assertEqual(len(self.builds), 5) |
| 432 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 433 | self.assertTrue(self.builds[0].hasChanges(A)) |
| 434 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 435 | self.assertTrue(self.builds[1].hasChanges(A)) |
| 436 | |
| 437 | self.assertEqual(self.builds[2].name, 'project-test1') |
| 438 | self.assertTrue(self.builds[2].hasChanges(A)) |
| 439 | self.assertEqual(self.builds[3].name, 'project-test2') |
| 440 | self.assertTrue(self.builds[3].hasChanges(A, B)) |
| 441 | |
| 442 | self.assertEqual(self.builds[4].name, 'project-merge') |
| 443 | self.assertTrue(self.builds[4].hasChanges(A, B, C)) |
| 444 | |
| 445 | self.executor_server.release('.*-merge') |
| 446 | self.waitUntilSettled() |
| 447 | self.assertEqual(len(self.builds), 6) |
| 448 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 449 | self.assertTrue(self.builds[0].hasChanges(A)) |
| 450 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 451 | self.assertTrue(self.builds[1].hasChanges(A)) |
| 452 | |
| 453 | self.assertEqual(self.builds[2].name, 'project-test1') |
| 454 | self.assertTrue(self.builds[2].hasChanges(A, B)) |
| 455 | self.assertEqual(self.builds[3].name, 'project-test2') |
| 456 | self.assertTrue(self.builds[3].hasChanges(A, B)) |
| 457 | |
| 458 | self.assertEqual(self.builds[4].name, 'project-test1') |
| 459 | self.assertTrue(self.builds[4].hasChanges(A, B, C)) |
| 460 | self.assertEqual(self.builds[5].name, 'project-test2') |
| 461 | self.assertTrue(self.builds[5].hasChanges(A, B, C)) |
| 462 | |
| 463 | all_builds = self.builds[:] |
| 464 | self.release(all_builds[2]) |
| 465 | self.release(all_builds[3]) |
| 466 | self.waitUntilSettled() |
| 467 | self.assertFalse(A.is_merged) |
| 468 | self.assertFalse(B.is_merged) |
| 469 | self.assertFalse(C.is_merged) |
| 470 | |
| 471 | self.release(all_builds[0]) |
| 472 | self.release(all_builds[1]) |
| 473 | self.waitUntilSettled() |
| 474 | self.assertTrue(A.is_merged) |
| 475 | self.assertTrue(B.is_merged) |
| 476 | self.assertFalse(C.is_merged) |
| 477 | |
| 478 | self.executor_server.hold_jobs_in_build = False |
| 479 | self.executor_server.release() |
| 480 | self.waitUntilSettled() |
| 481 | self.assertEqual(len(self.builds), 0) |
| 482 | self.assertEqual(len(self.history), 9) |
| 483 | self.assertTrue(C.is_merged) |
| 484 | |
| 485 | self.assertNotIn('merge', A.labels) |
| 486 | self.assertNotIn('merge', B.labels) |
| 487 | self.assertNotIn('merge', C.labels) |
| 488 | |
| 489 | @simple_layout('layouts/dependent-github.yaml', driver='github') |
| 490 | def test_failed_changes(self): |
| 491 | "Test that a change behind a failed change is retested" |
| 492 | self.executor_server.hold_jobs_in_build = True |
| 493 | |
| 494 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
| 495 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
| 496 | |
| 497 | self.executor_server.failJob('project-test1', A) |
| 498 | |
| 499 | self.fake_github.emitEvent(A.addLabel('merge')) |
| 500 | self.fake_github.emitEvent(B.addLabel('merge')) |
| 501 | self.waitUntilSettled() |
| 502 | |
| 503 | self.executor_server.release('.*-merge') |
| 504 | self.waitUntilSettled() |
| 505 | |
| 506 | self.executor_server.hold_jobs_in_build = False |
| 507 | self.executor_server.release() |
| 508 | |
| 509 | self.waitUntilSettled() |
| 510 | # It's certain that the merge job for change 2 will run, but |
| 511 | # the test1 and test2 jobs may or may not run. |
| 512 | self.assertTrue(len(self.history) > 6) |
| 513 | self.assertFalse(A.is_merged) |
| 514 | self.assertTrue(B.is_merged) |
| 515 | self.assertNotIn('merge', A.labels) |
| 516 | self.assertNotIn('merge', B.labels) |
| 517 | |
| 518 | @simple_layout('layouts/dependent-github.yaml', driver='github') |
| 519 | def test_failed_change_at_head(self): |
| 520 | "Test that if a change at the head fails, jobs behind it are canceled" |
| 521 | |
| 522 | self.executor_server.hold_jobs_in_build = True |
| 523 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
| 524 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
| 525 | C = self.fake_github.openFakePullRequest('org/project', 'master', 'C') |
| 526 | |
| 527 | self.executor_server.failJob('project-test1', A) |
| 528 | |
| 529 | self.fake_github.emitEvent(A.addLabel('merge')) |
| 530 | self.fake_github.emitEvent(B.addLabel('merge')) |
| 531 | self.fake_github.emitEvent(C.addLabel('merge')) |
| 532 | |
| 533 | self.waitUntilSettled() |
| 534 | |
| 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.executor_server.release('.*-merge') |
| 542 | self.waitUntilSettled() |
| 543 | self.executor_server.release('.*-merge') |
| 544 | self.waitUntilSettled() |
| 545 | |
| 546 | self.assertEqual(len(self.builds), 6) |
| 547 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 548 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 549 | self.assertEqual(self.builds[2].name, 'project-test1') |
| 550 | self.assertEqual(self.builds[3].name, 'project-test2') |
| 551 | self.assertEqual(self.builds[4].name, 'project-test1') |
| 552 | self.assertEqual(self.builds[5].name, 'project-test2') |
| 553 | |
| 554 | self.release(self.builds[0]) |
| 555 | self.waitUntilSettled() |
| 556 | |
| 557 | # project-test2, project-merge for B |
| 558 | self.assertEqual(len(self.builds), 2) |
| 559 | self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4) |
| 560 | |
| 561 | self.executor_server.hold_jobs_in_build = False |
| 562 | self.executor_server.release() |
| 563 | self.waitUntilSettled() |
| 564 | |
| 565 | self.assertEqual(len(self.builds), 0) |
| 566 | self.assertEqual(len(self.history), 15) |
| 567 | self.assertFalse(A.is_merged) |
| 568 | self.assertTrue(B.is_merged) |
| 569 | self.assertTrue(C.is_merged) |
| 570 | self.assertNotIn('merge', A.labels) |
| 571 | self.assertNotIn('merge', B.labels) |
| 572 | self.assertNotIn('merge', C.labels) |
Jesse Keating | 71a47ff | 2017-06-06 11:36:43 -0700 | [diff] [blame] | 573 | |
| 574 | @simple_layout('layouts/basic-github.yaml', driver='github') |
| 575 | def test_push_event_reconfigure(self): |
| 576 | pevent = self.fake_github.getPushEvent(project='common-config', |
| 577 | ref='refs/heads/master', |
| 578 | modified_files=['zuul.yaml']) |
| 579 | |
| 580 | # record previous tenant reconfiguration time, which may not be set |
| 581 | old = self.sched.tenant_last_reconfigured.get('tenant-one', 0) |
| 582 | time.sleep(1) |
| 583 | self.fake_github.emitEvent(pevent) |
| 584 | self.waitUntilSettled() |
| 585 | new = self.sched.tenant_last_reconfigured.get('tenant-one', 0) |
| 586 | # New timestamp should be greater than the old timestamp |
| 587 | self.assertLess(old, new) |
Tristan Cacqueray | 2bafb1f | 2017-06-12 07:10:26 +0000 | [diff] [blame] | 588 | |
| 589 | @simple_layout('layouts/basic-github.yaml', driver='github') |
| 590 | def test_ping_event(self): |
| 591 | # Test valid ping |
| 592 | pevent = {'repository': {'full_name': 'org/project'}} |
| 593 | req = self.fake_github.emitEvent(('ping', pevent)) |
| 594 | self.assertEqual(req.status, 200, "Ping event didn't succeed") |
| 595 | |
| 596 | # Test invalid ping |
| 597 | pevent = {'repository': {'full_name': 'unknown-project'}} |
| 598 | self.assertRaises( |
| 599 | urllib.error.HTTPError, |
| 600 | self.fake_github.emitEvent, |
| 601 | ('ping', pevent), |
| 602 | ) |