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 | |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 15 | import os |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 16 | import re |
Jamie Lennox | 3f16de5 | 2017-05-09 14:24:11 +1000 | [diff] [blame] | 17 | from testtools.matchers import MatchesRegex, StartsWith |
Tristan Cacqueray | 2bafb1f | 2017-06-12 07:10:26 +0000 | [diff] [blame] | 18 | import urllib |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 19 | import time |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 20 | |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 21 | import git |
| 22 | |
Wayne | 1a78c61 | 2015-06-11 17:14:13 -0700 | [diff] [blame] | 23 | from tests.base import ZuulTestCase, simple_layout, random_sha1 |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 24 | |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 25 | |
| 26 | class 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 Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 33 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
| 34 | self.fake_github.emitEvent(A.getPullRequestOpenedEvent()) |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 35 | self.waitUntilSettled() |
| 36 | |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 37 | 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 Taylor | d13bc36 | 2017-06-30 13:11:37 -0500 | [diff] [blame] | 47 | zuulvars = job.parameters['zuul'] |
James E. Blair | e3db295 | 2017-07-21 15:03:36 -0700 | [diff] [blame] | 48 | self.assertEqual(str(A.number), zuulvars['change']) |
| 49 | self.assertEqual(str(A.head_sha), zuulvars['patchset']) |
James E. Blair | 3b22249 | 2017-07-21 15:17:37 -0700 | [diff] [blame] | 50 | self.assertEqual('master', zuulvars['branch']) |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 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 | |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 96 | 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 | |
Wayne | 1a78c61 | 2015-06-11 17:14:13 -0700 | [diff] [blame] | 106 | 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. Blair | a438c17 | 2017-07-21 14:54:42 -0700 | [diff] [blame] | 112 | self.assertEqual('refs/tags/newtag', build_params['zuul']['ref']) |
Wayne | 1a78c61 | 2015-06-11 17:14:13 -0700 | [diff] [blame] | 113 | self.assertEqual('00000000000000000000000000000000', |
James E. Blair | ed8b001 | 2017-07-21 14:49:29 -0700 | [diff] [blame] | 114 | build_params['zuul']['oldrev']) |
| 115 | self.assertEqual(sha, build_params['zuul']['newrev']) |
Wayne | 1a78c61 | 2015-06-11 17:14:13 -0700 | [diff] [blame] | 116 | |
| 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. Blair | a438c17 | 2017-07-21 14:54:42 -0700 | [diff] [blame] | 136 | self.assertEqual('refs/heads/master', build_params['zuul']['ref']) |
James E. Blair | ed8b001 | 2017-07-21 14:49:29 -0700 | [diff] [blame] | 137 | self.assertEqual(old_sha, build_params['zuul']['oldrev']) |
| 138 | self.assertEqual(new_sha, build_params['zuul']['newrev']) |
Wayne | 1a78c61 | 2015-06-11 17:14:13 -0700 | [diff] [blame] | 139 | |
| 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 Hruban | d4edee8 | 2015-12-16 12:49:51 +0100 | [diff] [blame] | 146 | 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 Hruban | 6d53c5e | 2015-10-24 03:03:34 +0200 | [diff] [blame] | 158 | |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 159 | @simple_layout('layouts/labeling-github.yaml', driver='github') |
| 160 | def test_labels(self): |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 161 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 162 | 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 Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 169 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 170 | 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 Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 178 | C = self.fake_github.openFakePullRequest('org/project', 'master', 'C') |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 179 | 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 Keating | 5c05a9f | 2017-01-12 14:44:58 -0800 | [diff] [blame] | 184 | @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 Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 199 | @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 Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 203 | A = self.fake_github.openFakePullRequest( |
| 204 | 'org/one-job-project', 'master', 'A') |
| 205 | self.fake_github.emitEvent(A.getPullRequestOpenedEvent()) |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 206 | 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 Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 211 | A.addCommit() |
| 212 | self.fake_github.emitEvent(A.getPullRequestSynchronizeEvent()) |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 213 | 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 Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 226 | A = self.fake_github.openFakePullRequest( |
| 227 | 'org/one-job-project', 'master', 'A') |
| 228 | self.fake_github.emitEvent(A.getPullRequestOpenedEvent()) |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 229 | self.waitUntilSettled() |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 230 | self.fake_github.emitEvent(A.getPullRequestClosedEvent()) |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 231 | 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 Hruban | 6d53c5e | 2015-10-24 03:03:34 +0200 | [diff] [blame] | 240 | @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 Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 251 | |
Jesse Keating | be4ef8a | 2016-12-06 11:29:13 -0800 | [diff] [blame] | 252 | @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 Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 258 | @simple_layout('layouts/reporting-github.yaml', driver='github') |
| 259 | def test_reporting(self): |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 260 | project = 'org/project' |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 261 | # pipeline reports pull status both on start and success |
| 262 | self.executor_server.hold_jobs_in_build = True |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 263 | A = self.fake_github.openFakePullRequest(project, 'master', 'A') |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 264 | self.fake_github.emitEvent(A.getPullRequestOpenedEvent()) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 265 | self.waitUntilSettled() |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 266 | # We should have a status container for the head sha |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 267 | statuses = self.fake_github.statuses[project][A.head_sha] |
| 268 | self.assertIn(A.head_sha, self.fake_github.statuses[project].keys()) |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 269 | # We should only have one status for the head sha |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 270 | self.assertEqual(1, len(statuses)) |
| 271 | check_status = statuses[0] |
Jan Hruban | ddeb95a | 2017-01-03 15:12:41 +0100 | [diff] [blame] | 272 | check_url = ('http://zuul.example.com/status/#%s,%s' % |
| 273 | (A.number, A.head_sha)) |
Jamie Lennox | 18bc7ed | 2017-05-10 10:37:55 +1000 | [diff] [blame] | 274 | self.assertEqual('tenant-one/check', check_status['context']) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 275 | self.assertEqual('Standard check', check_status['description']) |
| 276 | self.assertEqual('pending', check_status['state']) |
Jan Hruban | ddeb95a | 2017-01-03 15:12:41 +0100 | [diff] [blame] | 277 | self.assertEqual(check_url, check_status['url']) |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 278 | self.assertEqual(0, len(A.comments)) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 279 | |
| 280 | self.executor_server.hold_jobs_in_build = False |
| 281 | self.executor_server.release() |
| 282 | self.waitUntilSettled() |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 283 | # We should only have two statuses for the head sha |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 284 | statuses = self.fake_github.statuses[project][A.head_sha] |
| 285 | self.assertEqual(2, len(statuses)) |
| 286 | check_status = statuses[0] |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 287 | check_url = ('http://zuul.example.com/status/#%s,%s' % |
| 288 | (A.number, A.head_sha)) |
Jamie Lennox | 18bc7ed | 2017-05-10 10:37:55 +1000 | [diff] [blame] | 289 | self.assertEqual('tenant-one/check', check_status['context']) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 290 | self.assertEqual('success', check_status['state']) |
Jan Hruban | ddeb95a | 2017-01-03 15:12:41 +0100 | [diff] [blame] | 291 | self.assertEqual(check_url, check_status['url']) |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 292 | self.assertEqual(1, len(A.comments)) |
| 293 | self.assertThat(A.comments[0], |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 294 | MatchesRegex('.*Build succeeded.*', re.DOTALL)) |
| 295 | |
| 296 | # pipeline does not report any status but does comment |
| 297 | self.executor_server.hold_jobs_in_build = True |
| 298 | self.fake_github.emitEvent( |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 299 | A.getCommentAddedEvent('reporting check')) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 300 | self.waitUntilSettled() |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 301 | statuses = self.fake_github.statuses[project][A.head_sha] |
| 302 | self.assertEqual(2, len(statuses)) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 303 | # comments increased by one for the start message |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 304 | self.assertEqual(2, len(A.comments)) |
| 305 | self.assertThat(A.comments[1], |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 306 | MatchesRegex('.*Starting reporting jobs.*', re.DOTALL)) |
| 307 | self.executor_server.hold_jobs_in_build = False |
| 308 | self.executor_server.release() |
| 309 | self.waitUntilSettled() |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 310 | # pipeline reports success status |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 311 | statuses = self.fake_github.statuses[project][A.head_sha] |
| 312 | self.assertEqual(3, len(statuses)) |
| 313 | report_status = statuses[0] |
Jamie Lennox | 18bc7ed | 2017-05-10 10:37:55 +1000 | [diff] [blame] | 314 | self.assertEqual('tenant-one/reporting', report_status['context']) |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 315 | self.assertEqual('success', report_status['state']) |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 316 | self.assertEqual(2, len(A.comments)) |
Jamie Lennox | 3f16de5 | 2017-05-09 14:24:11 +1000 | [diff] [blame] | 317 | |
| 318 | base = 'http://logs.example.com/tenant-one/reporting/%s/%s/' % ( |
| 319 | A.project, A.number) |
| 320 | |
| 321 | # Deconstructing the URL because we don't save the BuildSet UUID |
| 322 | # anywhere to do a direct comparison and doing regexp matches on a full |
| 323 | # URL is painful. |
| 324 | |
| 325 | # The first part of the URL matches the easy base string |
| 326 | self.assertThat(report_status['url'], StartsWith(base)) |
| 327 | |
| 328 | # The rest of the URL is a UUID and a trailing slash. |
| 329 | self.assertThat(report_status['url'][len(base):], |
| 330 | MatchesRegex('^[a-fA-F0-9]{32}\/$')) |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 331 | |
Jesse Keating | 08dab8f | 2017-06-21 12:59:23 +0100 | [diff] [blame] | 332 | @simple_layout('layouts/reporting-github.yaml', driver='github') |
| 333 | def test_push_reporting(self): |
| 334 | project = 'org/project2' |
| 335 | # pipeline reports pull status both on start and success |
| 336 | self.executor_server.hold_jobs_in_build = True |
| 337 | pevent = self.fake_github.getPushEvent(project=project, |
| 338 | ref='refs/heads/master') |
| 339 | |
| 340 | self.fake_github.emitEvent(pevent) |
| 341 | self.waitUntilSettled() |
| 342 | |
| 343 | # there should only be one report, a status |
| 344 | self.assertEqual(1, len(self.fake_github.reports)) |
| 345 | # Verify the user/context/state of the status |
| 346 | status = ('zuul', 'tenant-one/push-reporting', 'pending') |
| 347 | self.assertEqual(status, self.fake_github.reports[0][-1]) |
| 348 | |
| 349 | # free the executor, allow the build to finish |
| 350 | self.executor_server.hold_jobs_in_build = False |
| 351 | self.executor_server.release() |
| 352 | self.waitUntilSettled() |
| 353 | |
| 354 | # Now there should be a second report, the success of the build |
| 355 | self.assertEqual(2, len(self.fake_github.reports)) |
| 356 | # Verify the user/context/state of the status |
| 357 | status = ('zuul', 'tenant-one/push-reporting', 'success') |
| 358 | self.assertEqual(status, self.fake_github.reports[-1][-1]) |
| 359 | |
| 360 | # now make a PR which should also comment |
| 361 | self.executor_server.hold_jobs_in_build = True |
| 362 | A = self.fake_github.openFakePullRequest(project, 'master', 'A') |
| 363 | self.fake_github.emitEvent(A.getPullRequestOpenedEvent()) |
| 364 | self.waitUntilSettled() |
| 365 | |
| 366 | # Now there should be a four reports, a new comment |
| 367 | # and status |
| 368 | self.assertEqual(4, len(self.fake_github.reports)) |
| 369 | self.executor_server.release() |
| 370 | self.waitUntilSettled() |
| 371 | |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 372 | @simple_layout('layouts/merging-github.yaml', driver='github') |
| 373 | def test_report_pull_merge(self): |
| 374 | # pipeline merges the pull request on success |
Jan Hruban | 3b41592 | 2016-02-03 13:10:22 +0100 | [diff] [blame] | 375 | A = self.fake_github.openFakePullRequest('org/project', 'master', |
| 376 | 'PR title') |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 377 | self.fake_github.emitEvent(A.getCommentAddedEvent('merge me')) |
| 378 | self.waitUntilSettled() |
| 379 | self.assertTrue(A.is_merged) |
Jan Hruban | 3b41592 | 2016-02-03 13:10:22 +0100 | [diff] [blame] | 380 | self.assertThat(A.merge_message, |
| 381 | MatchesRegex('.*PR title.*Reviewed-by.*', re.DOTALL)) |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 382 | |
| 383 | # pipeline merges the pull request on success after failure |
| 384 | self.fake_github.merge_failure = True |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 385 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 386 | self.fake_github.emitEvent(B.getCommentAddedEvent('merge me')) |
| 387 | self.waitUntilSettled() |
| 388 | self.assertFalse(B.is_merged) |
| 389 | self.fake_github.merge_failure = False |
| 390 | |
| 391 | # pipeline merges the pull request on second run of merge |
| 392 | # first merge failed on 405 Method Not Allowed error |
| 393 | self.fake_github.merge_not_allowed_count = 1 |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 394 | C = self.fake_github.openFakePullRequest('org/project', 'master', 'C') |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 395 | self.fake_github.emitEvent(C.getCommentAddedEvent('merge me')) |
| 396 | self.waitUntilSettled() |
| 397 | self.assertTrue(C.is_merged) |
| 398 | |
| 399 | # pipeline does not merge the pull request |
| 400 | # merge failed on 405 Method Not Allowed error - twice |
| 401 | self.fake_github.merge_not_allowed_count = 2 |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 402 | D = self.fake_github.openFakePullRequest('org/project', 'master', 'D') |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 403 | self.fake_github.emitEvent(D.getCommentAddedEvent('merge me')) |
| 404 | self.waitUntilSettled() |
| 405 | self.assertFalse(D.is_merged) |
Adam Gandelman | 62198cb | 2017-02-14 16:11:02 -0800 | [diff] [blame] | 406 | self.assertEqual(len(D.comments), 1) |
| 407 | self.assertEqual(D.comments[0], 'Merge failed') |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 408 | |
| 409 | @simple_layout('layouts/dependent-github.yaml', driver='github') |
| 410 | def test_parallel_changes(self): |
| 411 | "Test that changes are tested in parallel and merged in series" |
| 412 | |
| 413 | self.executor_server.hold_jobs_in_build = True |
| 414 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
| 415 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
| 416 | C = self.fake_github.openFakePullRequest('org/project', 'master', 'C') |
| 417 | |
| 418 | self.fake_github.emitEvent(A.addLabel('merge')) |
| 419 | self.fake_github.emitEvent(B.addLabel('merge')) |
| 420 | self.fake_github.emitEvent(C.addLabel('merge')) |
| 421 | |
| 422 | self.waitUntilSettled() |
| 423 | self.assertEqual(len(self.builds), 1) |
| 424 | self.assertEqual(self.builds[0].name, 'project-merge') |
| 425 | self.assertTrue(self.builds[0].hasChanges(A)) |
| 426 | |
| 427 | self.executor_server.release('.*-merge') |
| 428 | self.waitUntilSettled() |
| 429 | self.assertEqual(len(self.builds), 3) |
| 430 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 431 | self.assertTrue(self.builds[0].hasChanges(A)) |
| 432 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 433 | self.assertTrue(self.builds[1].hasChanges(A)) |
| 434 | self.assertEqual(self.builds[2].name, 'project-merge') |
| 435 | self.assertTrue(self.builds[2].hasChanges(A, B)) |
| 436 | |
| 437 | self.executor_server.release('.*-merge') |
| 438 | self.waitUntilSettled() |
| 439 | self.assertEqual(len(self.builds), 5) |
| 440 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 441 | self.assertTrue(self.builds[0].hasChanges(A)) |
| 442 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 443 | self.assertTrue(self.builds[1].hasChanges(A)) |
| 444 | |
| 445 | self.assertEqual(self.builds[2].name, 'project-test1') |
| 446 | self.assertTrue(self.builds[2].hasChanges(A)) |
| 447 | self.assertEqual(self.builds[3].name, 'project-test2') |
| 448 | self.assertTrue(self.builds[3].hasChanges(A, B)) |
| 449 | |
| 450 | self.assertEqual(self.builds[4].name, 'project-merge') |
| 451 | self.assertTrue(self.builds[4].hasChanges(A, B, C)) |
| 452 | |
| 453 | self.executor_server.release('.*-merge') |
| 454 | self.waitUntilSettled() |
| 455 | self.assertEqual(len(self.builds), 6) |
| 456 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 457 | self.assertTrue(self.builds[0].hasChanges(A)) |
| 458 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 459 | self.assertTrue(self.builds[1].hasChanges(A)) |
| 460 | |
| 461 | self.assertEqual(self.builds[2].name, 'project-test1') |
| 462 | self.assertTrue(self.builds[2].hasChanges(A, B)) |
| 463 | self.assertEqual(self.builds[3].name, 'project-test2') |
| 464 | self.assertTrue(self.builds[3].hasChanges(A, B)) |
| 465 | |
| 466 | self.assertEqual(self.builds[4].name, 'project-test1') |
| 467 | self.assertTrue(self.builds[4].hasChanges(A, B, C)) |
| 468 | self.assertEqual(self.builds[5].name, 'project-test2') |
| 469 | self.assertTrue(self.builds[5].hasChanges(A, B, C)) |
| 470 | |
| 471 | all_builds = self.builds[:] |
| 472 | self.release(all_builds[2]) |
| 473 | self.release(all_builds[3]) |
| 474 | self.waitUntilSettled() |
| 475 | self.assertFalse(A.is_merged) |
| 476 | self.assertFalse(B.is_merged) |
| 477 | self.assertFalse(C.is_merged) |
| 478 | |
| 479 | self.release(all_builds[0]) |
| 480 | self.release(all_builds[1]) |
| 481 | self.waitUntilSettled() |
| 482 | self.assertTrue(A.is_merged) |
| 483 | self.assertTrue(B.is_merged) |
| 484 | self.assertFalse(C.is_merged) |
| 485 | |
| 486 | self.executor_server.hold_jobs_in_build = False |
| 487 | self.executor_server.release() |
| 488 | self.waitUntilSettled() |
| 489 | self.assertEqual(len(self.builds), 0) |
| 490 | self.assertEqual(len(self.history), 9) |
| 491 | self.assertTrue(C.is_merged) |
| 492 | |
| 493 | self.assertNotIn('merge', A.labels) |
| 494 | self.assertNotIn('merge', B.labels) |
| 495 | self.assertNotIn('merge', C.labels) |
| 496 | |
| 497 | @simple_layout('layouts/dependent-github.yaml', driver='github') |
| 498 | def test_failed_changes(self): |
| 499 | "Test that a change behind a failed change is retested" |
| 500 | self.executor_server.hold_jobs_in_build = True |
| 501 | |
| 502 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
| 503 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
| 504 | |
| 505 | self.executor_server.failJob('project-test1', A) |
| 506 | |
| 507 | self.fake_github.emitEvent(A.addLabel('merge')) |
| 508 | self.fake_github.emitEvent(B.addLabel('merge')) |
| 509 | self.waitUntilSettled() |
| 510 | |
| 511 | self.executor_server.release('.*-merge') |
| 512 | self.waitUntilSettled() |
| 513 | |
| 514 | self.executor_server.hold_jobs_in_build = False |
| 515 | self.executor_server.release() |
| 516 | |
| 517 | self.waitUntilSettled() |
| 518 | # It's certain that the merge job for change 2 will run, but |
| 519 | # the test1 and test2 jobs may or may not run. |
| 520 | self.assertTrue(len(self.history) > 6) |
| 521 | self.assertFalse(A.is_merged) |
| 522 | self.assertTrue(B.is_merged) |
| 523 | self.assertNotIn('merge', A.labels) |
| 524 | self.assertNotIn('merge', B.labels) |
| 525 | |
| 526 | @simple_layout('layouts/dependent-github.yaml', driver='github') |
| 527 | def test_failed_change_at_head(self): |
| 528 | "Test that if a change at the head fails, jobs behind it are canceled" |
| 529 | |
| 530 | self.executor_server.hold_jobs_in_build = True |
| 531 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
| 532 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
| 533 | C = self.fake_github.openFakePullRequest('org/project', 'master', 'C') |
| 534 | |
| 535 | self.executor_server.failJob('project-test1', A) |
| 536 | |
| 537 | self.fake_github.emitEvent(A.addLabel('merge')) |
| 538 | self.fake_github.emitEvent(B.addLabel('merge')) |
| 539 | self.fake_github.emitEvent(C.addLabel('merge')) |
| 540 | |
| 541 | self.waitUntilSettled() |
| 542 | |
| 543 | self.assertEqual(len(self.builds), 1) |
| 544 | self.assertEqual(self.builds[0].name, 'project-merge') |
| 545 | self.assertTrue(self.builds[0].hasChanges(A)) |
| 546 | |
| 547 | self.executor_server.release('.*-merge') |
| 548 | self.waitUntilSettled() |
| 549 | self.executor_server.release('.*-merge') |
| 550 | self.waitUntilSettled() |
| 551 | self.executor_server.release('.*-merge') |
| 552 | self.waitUntilSettled() |
| 553 | |
| 554 | self.assertEqual(len(self.builds), 6) |
| 555 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 556 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 557 | self.assertEqual(self.builds[2].name, 'project-test1') |
| 558 | self.assertEqual(self.builds[3].name, 'project-test2') |
| 559 | self.assertEqual(self.builds[4].name, 'project-test1') |
| 560 | self.assertEqual(self.builds[5].name, 'project-test2') |
| 561 | |
| 562 | self.release(self.builds[0]) |
| 563 | self.waitUntilSettled() |
| 564 | |
| 565 | # project-test2, project-merge for B |
| 566 | self.assertEqual(len(self.builds), 2) |
| 567 | self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4) |
| 568 | |
| 569 | self.executor_server.hold_jobs_in_build = False |
| 570 | self.executor_server.release() |
| 571 | self.waitUntilSettled() |
| 572 | |
| 573 | self.assertEqual(len(self.builds), 0) |
| 574 | self.assertEqual(len(self.history), 15) |
| 575 | self.assertFalse(A.is_merged) |
| 576 | self.assertTrue(B.is_merged) |
| 577 | self.assertTrue(C.is_merged) |
| 578 | self.assertNotIn('merge', A.labels) |
| 579 | self.assertNotIn('merge', B.labels) |
| 580 | self.assertNotIn('merge', C.labels) |
Jesse Keating | 71a47ff | 2017-06-06 11:36:43 -0700 | [diff] [blame] | 581 | |
| 582 | @simple_layout('layouts/basic-github.yaml', driver='github') |
| 583 | def test_push_event_reconfigure(self): |
| 584 | pevent = self.fake_github.getPushEvent(project='common-config', |
| 585 | ref='refs/heads/master', |
| 586 | modified_files=['zuul.yaml']) |
| 587 | |
| 588 | # record previous tenant reconfiguration time, which may not be set |
| 589 | old = self.sched.tenant_last_reconfigured.get('tenant-one', 0) |
| 590 | time.sleep(1) |
| 591 | self.fake_github.emitEvent(pevent) |
| 592 | self.waitUntilSettled() |
| 593 | new = self.sched.tenant_last_reconfigured.get('tenant-one', 0) |
| 594 | # New timestamp should be greater than the old timestamp |
| 595 | self.assertLess(old, new) |
Tristan Cacqueray | 2bafb1f | 2017-06-12 07:10:26 +0000 | [diff] [blame] | 596 | |
| 597 | @simple_layout('layouts/basic-github.yaml', driver='github') |
| 598 | def test_ping_event(self): |
| 599 | # Test valid ping |
| 600 | pevent = {'repository': {'full_name': 'org/project'}} |
| 601 | req = self.fake_github.emitEvent(('ping', pevent)) |
| 602 | self.assertEqual(req.status, 200, "Ping event didn't succeed") |
| 603 | |
| 604 | # Test invalid ping |
| 605 | pevent = {'repository': {'full_name': 'unknown-project'}} |
| 606 | self.assertRaises( |
| 607 | urllib.error.HTTPError, |
| 608 | self.fake_github.emitEvent, |
| 609 | ('ping', pevent), |
| 610 | ) |