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