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 | |
Jesse Keating | 80730e6 | 2017-09-14 15:35:11 -0600 | [diff] [blame] | 15 | import asyncio |
| 16 | import threading |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 17 | import os |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 18 | import re |
Jamie Lennox | 3f16de5 | 2017-05-09 14:24:11 +1000 | [diff] [blame] | 19 | from testtools.matchers import MatchesRegex, StartsWith |
Tristan Cacqueray | 2bafb1f | 2017-06-12 07:10:26 +0000 | [diff] [blame] | 20 | import urllib |
Jesse Keating | 80730e6 | 2017-09-14 15:35:11 -0600 | [diff] [blame] | 21 | import socket |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 22 | import time |
Jesse Keating | 64d2901 | 2017-09-06 12:27:49 -0700 | [diff] [blame] | 23 | from unittest import skip |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 24 | |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 25 | import git |
| 26 | |
Jesse Keating | 80730e6 | 2017-09-14 15:35:11 -0600 | [diff] [blame] | 27 | import zuul.web |
| 28 | |
Wayne | 1a78c61 | 2015-06-11 17:14:13 -0700 | [diff] [blame] | 29 | from tests.base import ZuulTestCase, simple_layout, random_sha1 |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 30 | |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 31 | |
| 32 | class TestGithubDriver(ZuulTestCase): |
| 33 | config_file = 'zuul-github-driver.conf' |
| 34 | |
| 35 | @simple_layout('layouts/basic-github.yaml', driver='github') |
| 36 | def test_pull_event(self): |
| 37 | self.executor_server.hold_jobs_in_build = True |
| 38 | |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 39 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
| 40 | self.fake_github.emitEvent(A.getPullRequestOpenedEvent()) |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 41 | self.waitUntilSettled() |
| 42 | |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 43 | self.executor_server.hold_jobs_in_build = False |
| 44 | self.executor_server.release() |
| 45 | self.waitUntilSettled() |
| 46 | |
| 47 | self.assertEqual('SUCCESS', |
| 48 | self.getJobFromHistory('project-test1').result) |
| 49 | self.assertEqual('SUCCESS', |
| 50 | self.getJobFromHistory('project-test2').result) |
| 51 | |
| 52 | job = self.getJobFromHistory('project-test2') |
Monty Taylor | d13bc36 | 2017-06-30 13:11:37 -0500 | [diff] [blame] | 53 | zuulvars = job.parameters['zuul'] |
James E. Blair | e3db295 | 2017-07-21 15:03:36 -0700 | [diff] [blame] | 54 | self.assertEqual(str(A.number), zuulvars['change']) |
| 55 | self.assertEqual(str(A.head_sha), zuulvars['patchset']) |
James E. Blair | 3b22249 | 2017-07-21 15:17:37 -0700 | [diff] [blame] | 56 | self.assertEqual('master', zuulvars['branch']) |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 57 | self.assertEqual(1, len(A.comments)) |
liusheng | d3419e8 | 2018-01-06 19:52:03 +0800 | [diff] [blame] | 58 | self.assertThat( |
| 59 | A.comments[0], |
| 60 | MatchesRegex('.*\[project-test1 \]\(.*\).*', re.DOTALL)) |
| 61 | self.assertThat( |
| 62 | A.comments[0], |
| 63 | MatchesRegex('.*\[project-test2 \]\(.*\).*', re.DOTALL)) |
Jan Hruban | d4edee8 | 2015-12-16 12:49:51 +0100 | [diff] [blame] | 64 | self.assertEqual(2, len(self.history)) |
| 65 | |
| 66 | # test_pull_unmatched_branch_event(self): |
| 67 | self.create_branch('org/project', 'unmatched_branch') |
| 68 | B = self.fake_github.openFakePullRequest( |
| 69 | 'org/project', 'unmatched_branch', 'B') |
| 70 | self.fake_github.emitEvent(B.getPullRequestOpenedEvent()) |
| 71 | self.waitUntilSettled() |
| 72 | |
| 73 | self.assertEqual(2, len(self.history)) |
Wayne | 1a78c61 | 2015-06-11 17:14:13 -0700 | [diff] [blame] | 74 | |
Jan Hruban | 570d01c | 2016-03-10 21:51:32 +0100 | [diff] [blame] | 75 | @simple_layout('layouts/files-github.yaml', driver='github') |
| 76 | def test_pull_matched_file_event(self): |
| 77 | A = self.fake_github.openFakePullRequest( |
| 78 | 'org/project', 'master', 'A', |
| 79 | files=['random.txt', 'build-requires']) |
| 80 | self.fake_github.emitEvent(A.getPullRequestOpenedEvent()) |
| 81 | self.waitUntilSettled() |
| 82 | self.assertEqual(1, len(self.history)) |
| 83 | |
| 84 | # test_pull_unmatched_file_event |
| 85 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B', |
| 86 | files=['random.txt']) |
| 87 | self.fake_github.emitEvent(B.getPullRequestOpenedEvent()) |
| 88 | self.waitUntilSettled() |
| 89 | self.assertEqual(1, len(self.history)) |
| 90 | |
Jan Hruban | c7ab160 | 2015-10-14 15:29:33 +0200 | [diff] [blame] | 91 | @simple_layout('layouts/basic-github.yaml', driver='github') |
| 92 | def test_comment_event(self): |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 93 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
| 94 | self.fake_github.emitEvent(A.getCommentAddedEvent('test me')) |
Jan Hruban | c7ab160 | 2015-10-14 15:29:33 +0200 | [diff] [blame] | 95 | self.waitUntilSettled() |
| 96 | self.assertEqual(2, len(self.history)) |
| 97 | |
| 98 | # Test an unmatched comment, history should remain the same |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 99 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
| 100 | self.fake_github.emitEvent(B.getCommentAddedEvent('casual comment')) |
Jan Hruban | c7ab160 | 2015-10-14 15:29:33 +0200 | [diff] [blame] | 101 | self.waitUntilSettled() |
| 102 | self.assertEqual(2, len(self.history)) |
| 103 | |
Wayne | 1a78c61 | 2015-06-11 17:14:13 -0700 | [diff] [blame] | 104 | @simple_layout('layouts/push-tag-github.yaml', driver='github') |
| 105 | def test_tag_event(self): |
| 106 | self.executor_server.hold_jobs_in_build = True |
| 107 | |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 108 | self.create_branch('org/project', 'tagbranch') |
| 109 | files = {'README.txt': 'test'} |
| 110 | self.addCommitToRepo('org/project', 'test tag', |
| 111 | files, branch='tagbranch', tag='newtag') |
| 112 | path = os.path.join(self.upstream_root, 'org/project') |
| 113 | repo = git.Repo(path) |
| 114 | tag = repo.tags['newtag'] |
| 115 | sha = tag.commit.hexsha |
| 116 | del repo |
| 117 | |
Wayne | 1a78c61 | 2015-06-11 17:14:13 -0700 | [diff] [blame] | 118 | self.fake_github.emitEvent( |
| 119 | self.fake_github.getPushEvent('org/project', 'refs/tags/newtag', |
| 120 | new_rev=sha)) |
| 121 | self.waitUntilSettled() |
| 122 | |
| 123 | build_params = self.builds[0].parameters |
James E. Blair | a438c17 | 2017-07-21 14:54:42 -0700 | [diff] [blame] | 124 | self.assertEqual('refs/tags/newtag', build_params['zuul']['ref']) |
James E. Blair | b8203e4 | 2017-08-02 17:00:14 -0700 | [diff] [blame] | 125 | self.assertFalse('oldrev' in build_params['zuul']) |
James E. Blair | ed8b001 | 2017-07-21 14:49:29 -0700 | [diff] [blame] | 126 | self.assertEqual(sha, build_params['zuul']['newrev']) |
Wayne | 1a78c61 | 2015-06-11 17:14:13 -0700 | [diff] [blame] | 127 | self.executor_server.hold_jobs_in_build = False |
| 128 | self.executor_server.release() |
| 129 | self.waitUntilSettled() |
| 130 | |
| 131 | self.assertEqual('SUCCESS', |
| 132 | self.getJobFromHistory('project-tag').result) |
| 133 | |
| 134 | @simple_layout('layouts/push-tag-github.yaml', driver='github') |
| 135 | def test_push_event(self): |
| 136 | self.executor_server.hold_jobs_in_build = True |
| 137 | |
James E. Blair | 289f593 | 2017-07-27 15:02:29 -0700 | [diff] [blame] | 138 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
| 139 | old_sha = '0' * 40 |
| 140 | new_sha = A.head_sha |
| 141 | A.setMerged("merging A") |
| 142 | pevent = self.fake_github.getPushEvent(project='org/project', |
| 143 | ref='refs/heads/master', |
| 144 | old_rev=old_sha, |
| 145 | new_rev=new_sha) |
| 146 | self.fake_github.emitEvent(pevent) |
Wayne | 1a78c61 | 2015-06-11 17:14:13 -0700 | [diff] [blame] | 147 | self.waitUntilSettled() |
| 148 | |
| 149 | build_params = self.builds[0].parameters |
James E. Blair | a438c17 | 2017-07-21 14:54:42 -0700 | [diff] [blame] | 150 | self.assertEqual('refs/heads/master', build_params['zuul']['ref']) |
James E. Blair | 289f593 | 2017-07-27 15:02:29 -0700 | [diff] [blame] | 151 | self.assertFalse('oldrev' in build_params['zuul']) |
James E. Blair | ed8b001 | 2017-07-21 14:49:29 -0700 | [diff] [blame] | 152 | self.assertEqual(new_sha, build_params['zuul']['newrev']) |
Wayne | 1a78c61 | 2015-06-11 17:14:13 -0700 | [diff] [blame] | 153 | |
| 154 | self.executor_server.hold_jobs_in_build = False |
| 155 | self.executor_server.release() |
| 156 | self.waitUntilSettled() |
| 157 | |
| 158 | self.assertEqual('SUCCESS', |
| 159 | self.getJobFromHistory('project-post').result) |
Jan Hruban | d4edee8 | 2015-12-16 12:49:51 +0100 | [diff] [blame] | 160 | self.assertEqual(1, len(self.history)) |
| 161 | |
| 162 | # test unmatched push event |
| 163 | old_sha = random_sha1() |
| 164 | new_sha = random_sha1() |
| 165 | self.fake_github.emitEvent( |
| 166 | self.fake_github.getPushEvent('org/project', |
| 167 | 'refs/heads/unmatched_branch', |
| 168 | old_sha, new_sha)) |
| 169 | self.waitUntilSettled() |
| 170 | |
| 171 | self.assertEqual(1, len(self.history)) |
Jan Hruban | 6d53c5e | 2015-10-24 03:03:34 +0200 | [diff] [blame] | 172 | |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 173 | @simple_layout('layouts/labeling-github.yaml', driver='github') |
| 174 | def test_labels(self): |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 175 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 176 | self.fake_github.emitEvent(A.addLabel('test')) |
| 177 | self.waitUntilSettled() |
| 178 | self.assertEqual(1, len(self.history)) |
| 179 | self.assertEqual('project-labels', self.history[0].name) |
| 180 | self.assertEqual(['tests passed'], A.labels) |
| 181 | |
| 182 | # test label removed |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 183 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 184 | B.addLabel('do not test') |
| 185 | self.fake_github.emitEvent(B.removeLabel('do not test')) |
| 186 | self.waitUntilSettled() |
| 187 | self.assertEqual(2, len(self.history)) |
| 188 | self.assertEqual('project-labels', self.history[1].name) |
| 189 | self.assertEqual(['tests passed'], B.labels) |
| 190 | |
| 191 | # test unmatched label |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 192 | C = self.fake_github.openFakePullRequest('org/project', 'master', 'C') |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 193 | self.fake_github.emitEvent(C.addLabel('other label')) |
| 194 | self.waitUntilSettled() |
| 195 | self.assertEqual(2, len(self.history)) |
| 196 | self.assertEqual(['other label'], C.labels) |
| 197 | |
Jesse Keating | 5c05a9f | 2017-01-12 14:44:58 -0800 | [diff] [blame] | 198 | @simple_layout('layouts/reviews-github.yaml', driver='github') |
| 199 | def test_review_event(self): |
| 200 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
| 201 | self.fake_github.emitEvent(A.getReviewAddedEvent('approve')) |
| 202 | self.waitUntilSettled() |
| 203 | self.assertEqual(1, len(self.history)) |
| 204 | self.assertEqual('project-reviews', self.history[0].name) |
| 205 | self.assertEqual(['tests passed'], A.labels) |
| 206 | |
| 207 | # test_review_unmatched_event |
| 208 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
| 209 | self.fake_github.emitEvent(B.getReviewAddedEvent('comment')) |
| 210 | self.waitUntilSettled() |
| 211 | self.assertEqual(1, len(self.history)) |
| 212 | |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 213 | @simple_layout('layouts/dequeue-github.yaml', driver='github') |
| 214 | def test_dequeue_pull_synchronized(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() |
| 221 | |
| 222 | # event update stamp has resolution one second, wait so the latter |
| 223 | # one has newer timestamp |
| 224 | time.sleep(1) |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 225 | A.addCommit() |
| 226 | self.fake_github.emitEvent(A.getPullRequestSynchronizeEvent()) |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 227 | self.waitUntilSettled() |
| 228 | |
| 229 | self.executor_server.hold_jobs_in_build = False |
| 230 | self.executor_server.release() |
| 231 | self.waitUntilSettled() |
| 232 | |
| 233 | self.assertEqual(2, len(self.history)) |
| 234 | self.assertEqual(1, self.countJobResults(self.history, 'ABORTED')) |
| 235 | |
| 236 | @simple_layout('layouts/dequeue-github.yaml', driver='github') |
| 237 | def test_dequeue_pull_abandoned(self): |
| 238 | self.executor_server.hold_jobs_in_build = True |
| 239 | |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 240 | A = self.fake_github.openFakePullRequest( |
| 241 | 'org/one-job-project', 'master', 'A') |
| 242 | self.fake_github.emitEvent(A.getPullRequestOpenedEvent()) |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 243 | self.waitUntilSettled() |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 244 | self.fake_github.emitEvent(A.getPullRequestClosedEvent()) |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 245 | self.waitUntilSettled() |
| 246 | |
| 247 | self.executor_server.hold_jobs_in_build = False |
| 248 | self.executor_server.release() |
| 249 | self.waitUntilSettled() |
| 250 | |
| 251 | self.assertEqual(1, len(self.history)) |
| 252 | self.assertEqual(1, self.countJobResults(self.history, 'ABORTED')) |
| 253 | |
Jan Hruban | 6d53c5e | 2015-10-24 03:03:34 +0200 | [diff] [blame] | 254 | @simple_layout('layouts/basic-github.yaml', driver='github') |
| 255 | def test_git_https_url(self): |
| 256 | """Test that git_ssh option gives git url with ssh""" |
Tobias Henkel | 42f7d6f | 2017-12-15 16:09:43 +0100 | [diff] [blame] | 257 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 258 | _, project = tenant.getProject('org/project') |
| 259 | |
| 260 | url = self.fake_github.real_getGitUrl(project) |
Jan Hruban | 6d53c5e | 2015-10-24 03:03:34 +0200 | [diff] [blame] | 261 | self.assertEqual('https://github.com/org/project', url) |
| 262 | |
| 263 | @simple_layout('layouts/basic-github.yaml', driver='github') |
| 264 | def test_git_ssh_url(self): |
| 265 | """Test that git_ssh option gives git url with ssh""" |
Tobias Henkel | 42f7d6f | 2017-12-15 16:09:43 +0100 | [diff] [blame] | 266 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 267 | _, project = tenant.getProject('org/project') |
| 268 | |
| 269 | url = self.fake_github_ssh.real_getGitUrl(project) |
Jan Hruban | 6d53c5e | 2015-10-24 03:03:34 +0200 | [diff] [blame] | 270 | self.assertEqual('ssh://git@github.com/org/project.git', url) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 271 | |
Jesse Keating | be4ef8a | 2016-12-06 11:29:13 -0800 | [diff] [blame] | 272 | @simple_layout('layouts/basic-github.yaml', driver='github') |
| 273 | def test_git_enterprise_url(self): |
| 274 | """Test that git_url option gives git url with proper host""" |
Tobias Henkel | 42f7d6f | 2017-12-15 16:09:43 +0100 | [diff] [blame] | 275 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 276 | _, project = tenant.getProject('org/project') |
| 277 | |
| 278 | url = self.fake_github_ent.real_getGitUrl(project) |
Jesse Keating | be4ef8a | 2016-12-06 11:29:13 -0800 | [diff] [blame] | 279 | self.assertEqual('ssh://git@github.enterprise.io/org/project.git', url) |
| 280 | |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 281 | @simple_layout('layouts/reporting-github.yaml', driver='github') |
| 282 | def test_reporting(self): |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 283 | project = 'org/project' |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 284 | github = self.fake_github.github_client |
| 285 | |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 286 | # pipeline reports pull status both on start and success |
| 287 | self.executor_server.hold_jobs_in_build = True |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 288 | A = self.fake_github.openFakePullRequest(project, 'master', 'A') |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 289 | self.fake_github.emitEvent(A.getPullRequestOpenedEvent()) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 290 | self.waitUntilSettled() |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 291 | |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 292 | # We should have a status container for the head sha |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 293 | self.assertIn( |
| 294 | A.head_sha, github.repo_from_project(project)._commits.keys()) |
| 295 | statuses = self.fake_github.getCommitStatuses(project, A.head_sha) |
| 296 | |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 297 | # We should only have one status for the head sha |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 298 | self.assertEqual(1, len(statuses)) |
| 299 | check_status = statuses[0] |
Jan Hruban | ddeb95a | 2017-01-03 15:12:41 +0100 | [diff] [blame] | 300 | check_url = ('http://zuul.example.com/status/#%s,%s' % |
| 301 | (A.number, A.head_sha)) |
Jamie Lennox | 18bc7ed | 2017-05-10 10:37:55 +1000 | [diff] [blame] | 302 | self.assertEqual('tenant-one/check', check_status['context']) |
Jesse Keating | fb6cc99 | 2017-08-01 14:18:13 -0700 | [diff] [blame] | 303 | self.assertEqual('check status: pending', |
| 304 | check_status['description']) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 305 | self.assertEqual('pending', check_status['state']) |
Jan Hruban | ddeb95a | 2017-01-03 15:12:41 +0100 | [diff] [blame] | 306 | self.assertEqual(check_url, check_status['url']) |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 307 | self.assertEqual(0, len(A.comments)) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 308 | |
| 309 | self.executor_server.hold_jobs_in_build = False |
| 310 | self.executor_server.release() |
| 311 | self.waitUntilSettled() |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 312 | # We should only have two statuses for the head sha |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 313 | statuses = self.fake_github.getCommitStatuses(project, A.head_sha) |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 314 | self.assertEqual(2, len(statuses)) |
| 315 | check_status = statuses[0] |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 316 | check_url = ('http://zuul.example.com/status/#%s,%s' % |
| 317 | (A.number, A.head_sha)) |
Jamie Lennox | 18bc7ed | 2017-05-10 10:37:55 +1000 | [diff] [blame] | 318 | self.assertEqual('tenant-one/check', check_status['context']) |
Jesse Keating | fb6cc99 | 2017-08-01 14:18:13 -0700 | [diff] [blame] | 319 | self.assertEqual('check status: success', |
| 320 | check_status['description']) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 321 | self.assertEqual('success', check_status['state']) |
Jan Hruban | ddeb95a | 2017-01-03 15:12:41 +0100 | [diff] [blame] | 322 | self.assertEqual(check_url, check_status['url']) |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 323 | self.assertEqual(1, len(A.comments)) |
| 324 | self.assertThat(A.comments[0], |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 325 | MatchesRegex('.*Build succeeded.*', re.DOTALL)) |
| 326 | |
| 327 | # pipeline does not report any status but does comment |
| 328 | self.executor_server.hold_jobs_in_build = True |
| 329 | self.fake_github.emitEvent( |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 330 | A.getCommentAddedEvent('reporting check')) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 331 | self.waitUntilSettled() |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 332 | statuses = self.fake_github.getCommitStatuses(project, A.head_sha) |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 333 | self.assertEqual(2, len(statuses)) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 334 | # comments increased by one for the start message |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 335 | self.assertEqual(2, len(A.comments)) |
| 336 | self.assertThat(A.comments[1], |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 337 | MatchesRegex('.*Starting reporting jobs.*', re.DOTALL)) |
| 338 | self.executor_server.hold_jobs_in_build = False |
| 339 | self.executor_server.release() |
| 340 | self.waitUntilSettled() |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 341 | # pipeline reports success status |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 342 | statuses = self.fake_github.getCommitStatuses(project, A.head_sha) |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 343 | self.assertEqual(3, len(statuses)) |
| 344 | report_status = statuses[0] |
Jamie Lennox | 18bc7ed | 2017-05-10 10:37:55 +1000 | [diff] [blame] | 345 | self.assertEqual('tenant-one/reporting', report_status['context']) |
Jesse Keating | fb6cc99 | 2017-08-01 14:18:13 -0700 | [diff] [blame] | 346 | self.assertEqual('reporting status: success', |
| 347 | report_status['description']) |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 348 | self.assertEqual('success', report_status['state']) |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 349 | self.assertEqual(2, len(A.comments)) |
Jamie Lennox | 3f16de5 | 2017-05-09 14:24:11 +1000 | [diff] [blame] | 350 | |
| 351 | base = 'http://logs.example.com/tenant-one/reporting/%s/%s/' % ( |
| 352 | A.project, A.number) |
| 353 | |
| 354 | # Deconstructing the URL because we don't save the BuildSet UUID |
| 355 | # anywhere to do a direct comparison and doing regexp matches on a full |
| 356 | # URL is painful. |
| 357 | |
| 358 | # The first part of the URL matches the easy base string |
| 359 | self.assertThat(report_status['url'], StartsWith(base)) |
| 360 | |
| 361 | # The rest of the URL is a UUID and a trailing slash. |
| 362 | self.assertThat(report_status['url'][len(base):], |
| 363 | MatchesRegex('^[a-fA-F0-9]{32}\/$')) |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 364 | |
Jesse Keating | 08dab8f | 2017-06-21 12:59:23 +0100 | [diff] [blame] | 365 | @simple_layout('layouts/reporting-github.yaml', driver='github') |
Jesse Keating | fb6cc99 | 2017-08-01 14:18:13 -0700 | [diff] [blame] | 366 | def test_truncated_status_description(self): |
| 367 | project = 'org/project' |
| 368 | # pipeline reports pull status both on start and success |
| 369 | self.executor_server.hold_jobs_in_build = True |
| 370 | A = self.fake_github.openFakePullRequest(project, 'master', 'A') |
| 371 | self.fake_github.emitEvent( |
| 372 | A.getCommentAddedEvent('long pipeline')) |
| 373 | self.waitUntilSettled() |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 374 | statuses = self.fake_github.getCommitStatuses(project, A.head_sha) |
Jesse Keating | fb6cc99 | 2017-08-01 14:18:13 -0700 | [diff] [blame] | 375 | self.assertEqual(1, len(statuses)) |
| 376 | check_status = statuses[0] |
| 377 | # Status is truncated due to long pipeline name |
| 378 | self.assertEqual('status: pending', |
| 379 | check_status['description']) |
| 380 | |
| 381 | self.executor_server.hold_jobs_in_build = False |
| 382 | self.executor_server.release() |
| 383 | self.waitUntilSettled() |
| 384 | # We should only have two statuses for the head sha |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 385 | statuses = self.fake_github.getCommitStatuses(project, A.head_sha) |
Jesse Keating | fb6cc99 | 2017-08-01 14:18:13 -0700 | [diff] [blame] | 386 | self.assertEqual(2, len(statuses)) |
| 387 | check_status = statuses[0] |
| 388 | # Status is truncated due to long pipeline name |
| 389 | self.assertEqual('status: success', |
| 390 | check_status['description']) |
| 391 | |
| 392 | @simple_layout('layouts/reporting-github.yaml', driver='github') |
Jesse Keating | 08dab8f | 2017-06-21 12:59:23 +0100 | [diff] [blame] | 393 | def test_push_reporting(self): |
| 394 | project = 'org/project2' |
| 395 | # pipeline reports pull status both on start and success |
| 396 | self.executor_server.hold_jobs_in_build = True |
Jesse Keating | 08dab8f | 2017-06-21 12:59:23 +0100 | [diff] [blame] | 397 | |
James E. Blair | 289f593 | 2017-07-27 15:02:29 -0700 | [diff] [blame] | 398 | A = self.fake_github.openFakePullRequest(project, 'master', 'A') |
| 399 | old_sha = '0' * 40 |
| 400 | new_sha = A.head_sha |
| 401 | A.setMerged("merging A") |
| 402 | pevent = self.fake_github.getPushEvent(project=project, |
| 403 | ref='refs/heads/master', |
| 404 | old_rev=old_sha, |
| 405 | new_rev=new_sha) |
Jesse Keating | 08dab8f | 2017-06-21 12:59:23 +0100 | [diff] [blame] | 406 | self.fake_github.emitEvent(pevent) |
| 407 | self.waitUntilSettled() |
| 408 | |
| 409 | # there should only be one report, a status |
| 410 | self.assertEqual(1, len(self.fake_github.reports)) |
| 411 | # Verify the user/context/state of the status |
| 412 | status = ('zuul', 'tenant-one/push-reporting', 'pending') |
| 413 | self.assertEqual(status, self.fake_github.reports[0][-1]) |
| 414 | |
| 415 | # free the executor, allow the build to finish |
| 416 | self.executor_server.hold_jobs_in_build = False |
| 417 | self.executor_server.release() |
| 418 | self.waitUntilSettled() |
| 419 | |
| 420 | # Now there should be a second report, the success of the build |
| 421 | self.assertEqual(2, len(self.fake_github.reports)) |
| 422 | # Verify the user/context/state of the status |
| 423 | status = ('zuul', 'tenant-one/push-reporting', 'success') |
| 424 | self.assertEqual(status, self.fake_github.reports[-1][-1]) |
| 425 | |
| 426 | # now make a PR which should also comment |
| 427 | self.executor_server.hold_jobs_in_build = True |
| 428 | A = self.fake_github.openFakePullRequest(project, 'master', 'A') |
| 429 | self.fake_github.emitEvent(A.getPullRequestOpenedEvent()) |
| 430 | self.waitUntilSettled() |
| 431 | |
| 432 | # Now there should be a four reports, a new comment |
| 433 | # and status |
| 434 | self.assertEqual(4, len(self.fake_github.reports)) |
| 435 | self.executor_server.release() |
| 436 | self.waitUntilSettled() |
| 437 | |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 438 | @simple_layout('layouts/merging-github.yaml', driver='github') |
| 439 | def test_report_pull_merge(self): |
| 440 | # pipeline merges the pull request on success |
Jan Hruban | 3b41592 | 2016-02-03 13:10:22 +0100 | [diff] [blame] | 441 | A = self.fake_github.openFakePullRequest('org/project', 'master', |
| 442 | 'PR title') |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 443 | self.fake_github.emitEvent(A.getCommentAddedEvent('merge me')) |
| 444 | self.waitUntilSettled() |
| 445 | self.assertTrue(A.is_merged) |
Jan Hruban | 3b41592 | 2016-02-03 13:10:22 +0100 | [diff] [blame] | 446 | self.assertThat(A.merge_message, |
| 447 | MatchesRegex('.*PR title.*Reviewed-by.*', re.DOTALL)) |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 448 | |
| 449 | # pipeline merges the pull request on success after failure |
| 450 | self.fake_github.merge_failure = True |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 451 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 452 | self.fake_github.emitEvent(B.getCommentAddedEvent('merge me')) |
| 453 | self.waitUntilSettled() |
| 454 | self.assertFalse(B.is_merged) |
| 455 | self.fake_github.merge_failure = False |
| 456 | |
| 457 | # pipeline merges the pull request on second run of merge |
| 458 | # first merge failed on 405 Method Not Allowed error |
| 459 | self.fake_github.merge_not_allowed_count = 1 |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 460 | C = self.fake_github.openFakePullRequest('org/project', 'master', 'C') |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 461 | self.fake_github.emitEvent(C.getCommentAddedEvent('merge me')) |
| 462 | self.waitUntilSettled() |
| 463 | self.assertTrue(C.is_merged) |
| 464 | |
| 465 | # pipeline does not merge the pull request |
| 466 | # merge failed on 405 Method Not Allowed error - twice |
| 467 | self.fake_github.merge_not_allowed_count = 2 |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 468 | D = self.fake_github.openFakePullRequest('org/project', 'master', 'D') |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 469 | self.fake_github.emitEvent(D.getCommentAddedEvent('merge me')) |
| 470 | self.waitUntilSettled() |
| 471 | self.assertFalse(D.is_merged) |
Adam Gandelman | 62198cb | 2017-02-14 16:11:02 -0800 | [diff] [blame] | 472 | self.assertEqual(len(D.comments), 1) |
| 473 | self.assertEqual(D.comments[0], 'Merge failed') |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 474 | |
Jesse Keating | 2843431 | 2017-07-31 11:32:48 -0700 | [diff] [blame] | 475 | @simple_layout('layouts/reporting-multiple-github.yaml', driver='github') |
| 476 | def test_reporting_multiple_github(self): |
| 477 | project = 'org/project1' |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 478 | github = self.fake_github.github_client |
| 479 | |
Jesse Keating | 2843431 | 2017-07-31 11:32:48 -0700 | [diff] [blame] | 480 | # pipeline reports pull status both on start and success |
| 481 | self.executor_server.hold_jobs_in_build = True |
| 482 | A = self.fake_github.openFakePullRequest(project, 'master', 'A') |
| 483 | self.fake_github.emitEvent(A.getPullRequestOpenedEvent()) |
| 484 | # open one on B as well, which should not effect A reporting |
| 485 | B = self.fake_github.openFakePullRequest('org/project2', 'master', |
| 486 | 'B') |
| 487 | self.fake_github.emitEvent(B.getPullRequestOpenedEvent()) |
| 488 | self.waitUntilSettled() |
| 489 | # We should have a status container for the head sha |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 490 | statuses = self.fake_github.getCommitStatuses(project, A.head_sha) |
| 491 | self.assertIn( |
| 492 | A.head_sha, github.repo_from_project(project)._commits.keys()) |
Jesse Keating | 2843431 | 2017-07-31 11:32:48 -0700 | [diff] [blame] | 493 | # We should only have one status for the head sha |
| 494 | self.assertEqual(1, len(statuses)) |
| 495 | check_status = statuses[0] |
| 496 | check_url = ('http://zuul.example.com/status/#%s,%s' % |
| 497 | (A.number, A.head_sha)) |
| 498 | self.assertEqual('tenant-one/check', check_status['context']) |
Jesse Keating | fb6cc99 | 2017-08-01 14:18:13 -0700 | [diff] [blame] | 499 | self.assertEqual('check status: pending', check_status['description']) |
Jesse Keating | 2843431 | 2017-07-31 11:32:48 -0700 | [diff] [blame] | 500 | self.assertEqual('pending', check_status['state']) |
| 501 | self.assertEqual(check_url, check_status['url']) |
| 502 | self.assertEqual(0, len(A.comments)) |
| 503 | |
| 504 | self.executor_server.hold_jobs_in_build = False |
| 505 | self.executor_server.release() |
| 506 | self.waitUntilSettled() |
| 507 | # We should only have two statuses for the head sha |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 508 | statuses = self.fake_github.getCommitStatuses(project, A.head_sha) |
Jesse Keating | 2843431 | 2017-07-31 11:32:48 -0700 | [diff] [blame] | 509 | self.assertEqual(2, len(statuses)) |
| 510 | check_status = statuses[0] |
| 511 | check_url = ('http://zuul.example.com/status/#%s,%s' % |
| 512 | (A.number, A.head_sha)) |
| 513 | self.assertEqual('tenant-one/check', check_status['context']) |
| 514 | self.assertEqual('success', check_status['state']) |
Jesse Keating | fb6cc99 | 2017-08-01 14:18:13 -0700 | [diff] [blame] | 515 | self.assertEqual('check status: success', check_status['description']) |
Jesse Keating | 2843431 | 2017-07-31 11:32:48 -0700 | [diff] [blame] | 516 | self.assertEqual(check_url, check_status['url']) |
| 517 | self.assertEqual(1, len(A.comments)) |
| 518 | self.assertThat(A.comments[0], |
| 519 | MatchesRegex('.*Build succeeded.*', re.DOTALL)) |
| 520 | |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 521 | @simple_layout('layouts/dependent-github.yaml', driver='github') |
| 522 | def test_parallel_changes(self): |
| 523 | "Test that changes are tested in parallel and merged in series" |
| 524 | |
| 525 | self.executor_server.hold_jobs_in_build = True |
| 526 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
| 527 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
| 528 | C = self.fake_github.openFakePullRequest('org/project', 'master', 'C') |
| 529 | |
| 530 | self.fake_github.emitEvent(A.addLabel('merge')) |
| 531 | self.fake_github.emitEvent(B.addLabel('merge')) |
| 532 | self.fake_github.emitEvent(C.addLabel('merge')) |
| 533 | |
| 534 | self.waitUntilSettled() |
| 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.assertEqual(len(self.builds), 3) |
| 542 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 543 | self.assertTrue(self.builds[0].hasChanges(A)) |
| 544 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 545 | self.assertTrue(self.builds[1].hasChanges(A)) |
| 546 | self.assertEqual(self.builds[2].name, 'project-merge') |
| 547 | self.assertTrue(self.builds[2].hasChanges(A, B)) |
| 548 | |
| 549 | self.executor_server.release('.*-merge') |
| 550 | self.waitUntilSettled() |
| 551 | self.assertEqual(len(self.builds), 5) |
| 552 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 553 | self.assertTrue(self.builds[0].hasChanges(A)) |
| 554 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 555 | self.assertTrue(self.builds[1].hasChanges(A)) |
| 556 | |
| 557 | self.assertEqual(self.builds[2].name, 'project-test1') |
| 558 | self.assertTrue(self.builds[2].hasChanges(A)) |
| 559 | self.assertEqual(self.builds[3].name, 'project-test2') |
| 560 | self.assertTrue(self.builds[3].hasChanges(A, B)) |
| 561 | |
| 562 | self.assertEqual(self.builds[4].name, 'project-merge') |
| 563 | self.assertTrue(self.builds[4].hasChanges(A, B, C)) |
| 564 | |
| 565 | self.executor_server.release('.*-merge') |
| 566 | self.waitUntilSettled() |
| 567 | self.assertEqual(len(self.builds), 6) |
| 568 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 569 | self.assertTrue(self.builds[0].hasChanges(A)) |
| 570 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 571 | self.assertTrue(self.builds[1].hasChanges(A)) |
| 572 | |
| 573 | self.assertEqual(self.builds[2].name, 'project-test1') |
| 574 | self.assertTrue(self.builds[2].hasChanges(A, B)) |
| 575 | self.assertEqual(self.builds[3].name, 'project-test2') |
| 576 | self.assertTrue(self.builds[3].hasChanges(A, B)) |
| 577 | |
| 578 | self.assertEqual(self.builds[4].name, 'project-test1') |
| 579 | self.assertTrue(self.builds[4].hasChanges(A, B, C)) |
| 580 | self.assertEqual(self.builds[5].name, 'project-test2') |
| 581 | self.assertTrue(self.builds[5].hasChanges(A, B, C)) |
| 582 | |
| 583 | all_builds = self.builds[:] |
| 584 | self.release(all_builds[2]) |
| 585 | self.release(all_builds[3]) |
| 586 | self.waitUntilSettled() |
| 587 | self.assertFalse(A.is_merged) |
| 588 | self.assertFalse(B.is_merged) |
| 589 | self.assertFalse(C.is_merged) |
| 590 | |
| 591 | self.release(all_builds[0]) |
| 592 | self.release(all_builds[1]) |
| 593 | self.waitUntilSettled() |
| 594 | self.assertTrue(A.is_merged) |
| 595 | self.assertTrue(B.is_merged) |
| 596 | self.assertFalse(C.is_merged) |
| 597 | |
| 598 | self.executor_server.hold_jobs_in_build = False |
| 599 | self.executor_server.release() |
| 600 | self.waitUntilSettled() |
| 601 | self.assertEqual(len(self.builds), 0) |
| 602 | self.assertEqual(len(self.history), 9) |
| 603 | self.assertTrue(C.is_merged) |
| 604 | |
| 605 | self.assertNotIn('merge', A.labels) |
| 606 | self.assertNotIn('merge', B.labels) |
| 607 | self.assertNotIn('merge', C.labels) |
| 608 | |
| 609 | @simple_layout('layouts/dependent-github.yaml', driver='github') |
| 610 | def test_failed_changes(self): |
| 611 | "Test that a change behind a failed change is retested" |
| 612 | self.executor_server.hold_jobs_in_build = True |
| 613 | |
| 614 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
| 615 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
| 616 | |
| 617 | self.executor_server.failJob('project-test1', A) |
| 618 | |
| 619 | self.fake_github.emitEvent(A.addLabel('merge')) |
| 620 | self.fake_github.emitEvent(B.addLabel('merge')) |
| 621 | self.waitUntilSettled() |
| 622 | |
| 623 | self.executor_server.release('.*-merge') |
| 624 | self.waitUntilSettled() |
| 625 | |
| 626 | self.executor_server.hold_jobs_in_build = False |
| 627 | self.executor_server.release() |
| 628 | |
| 629 | self.waitUntilSettled() |
| 630 | # It's certain that the merge job for change 2 will run, but |
| 631 | # the test1 and test2 jobs may or may not run. |
| 632 | self.assertTrue(len(self.history) > 6) |
| 633 | self.assertFalse(A.is_merged) |
| 634 | self.assertTrue(B.is_merged) |
| 635 | self.assertNotIn('merge', A.labels) |
| 636 | self.assertNotIn('merge', B.labels) |
| 637 | |
| 638 | @simple_layout('layouts/dependent-github.yaml', driver='github') |
| 639 | def test_failed_change_at_head(self): |
| 640 | "Test that if a change at the head fails, jobs behind it are canceled" |
| 641 | |
| 642 | self.executor_server.hold_jobs_in_build = True |
| 643 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
| 644 | B = self.fake_github.openFakePullRequest('org/project', 'master', 'B') |
| 645 | C = self.fake_github.openFakePullRequest('org/project', 'master', 'C') |
| 646 | |
| 647 | self.executor_server.failJob('project-test1', A) |
| 648 | |
| 649 | self.fake_github.emitEvent(A.addLabel('merge')) |
| 650 | self.fake_github.emitEvent(B.addLabel('merge')) |
| 651 | self.fake_github.emitEvent(C.addLabel('merge')) |
| 652 | |
| 653 | self.waitUntilSettled() |
| 654 | |
| 655 | self.assertEqual(len(self.builds), 1) |
| 656 | self.assertEqual(self.builds[0].name, 'project-merge') |
| 657 | self.assertTrue(self.builds[0].hasChanges(A)) |
| 658 | |
| 659 | self.executor_server.release('.*-merge') |
| 660 | self.waitUntilSettled() |
| 661 | self.executor_server.release('.*-merge') |
| 662 | self.waitUntilSettled() |
| 663 | self.executor_server.release('.*-merge') |
| 664 | self.waitUntilSettled() |
| 665 | |
| 666 | self.assertEqual(len(self.builds), 6) |
| 667 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 668 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 669 | self.assertEqual(self.builds[2].name, 'project-test1') |
| 670 | self.assertEqual(self.builds[3].name, 'project-test2') |
| 671 | self.assertEqual(self.builds[4].name, 'project-test1') |
| 672 | self.assertEqual(self.builds[5].name, 'project-test2') |
| 673 | |
| 674 | self.release(self.builds[0]) |
| 675 | self.waitUntilSettled() |
| 676 | |
| 677 | # project-test2, project-merge for B |
| 678 | self.assertEqual(len(self.builds), 2) |
| 679 | self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4) |
| 680 | |
| 681 | self.executor_server.hold_jobs_in_build = False |
| 682 | self.executor_server.release() |
| 683 | self.waitUntilSettled() |
| 684 | |
| 685 | self.assertEqual(len(self.builds), 0) |
| 686 | self.assertEqual(len(self.history), 15) |
| 687 | self.assertFalse(A.is_merged) |
| 688 | self.assertTrue(B.is_merged) |
| 689 | self.assertTrue(C.is_merged) |
| 690 | self.assertNotIn('merge', A.labels) |
| 691 | self.assertNotIn('merge', B.labels) |
| 692 | self.assertNotIn('merge', C.labels) |
Jesse Keating | 71a47ff | 2017-06-06 11:36:43 -0700 | [diff] [blame] | 693 | |
| 694 | @simple_layout('layouts/basic-github.yaml', driver='github') |
| 695 | def test_push_event_reconfigure(self): |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 696 | pevent = self.fake_github.getPushEvent(project='org/common-config', |
Jesse Keating | 71a47ff | 2017-06-06 11:36:43 -0700 | [diff] [blame] | 697 | ref='refs/heads/master', |
| 698 | modified_files=['zuul.yaml']) |
| 699 | |
| 700 | # record previous tenant reconfiguration time, which may not be set |
| 701 | old = self.sched.tenant_last_reconfigured.get('tenant-one', 0) |
| 702 | time.sleep(1) |
| 703 | self.fake_github.emitEvent(pevent) |
| 704 | self.waitUntilSettled() |
| 705 | new = self.sched.tenant_last_reconfigured.get('tenant-one', 0) |
| 706 | # New timestamp should be greater than the old timestamp |
| 707 | self.assertLess(old, new) |
Tristan Cacqueray | 2bafb1f | 2017-06-12 07:10:26 +0000 | [diff] [blame] | 708 | |
Jesse Keating | 64d2901 | 2017-09-06 12:27:49 -0700 | [diff] [blame] | 709 | # TODO(jlk): Make this a more generic test for unknown project |
| 710 | @skip("Skipped for rewrite of webhook handler") |
Tristan Cacqueray | 2bafb1f | 2017-06-12 07:10:26 +0000 | [diff] [blame] | 711 | @simple_layout('layouts/basic-github.yaml', driver='github') |
| 712 | def test_ping_event(self): |
| 713 | # Test valid ping |
| 714 | pevent = {'repository': {'full_name': 'org/project'}} |
| 715 | req = self.fake_github.emitEvent(('ping', pevent)) |
| 716 | self.assertEqual(req.status, 200, "Ping event didn't succeed") |
| 717 | |
| 718 | # Test invalid ping |
| 719 | pevent = {'repository': {'full_name': 'unknown-project'}} |
| 720 | self.assertRaises( |
| 721 | urllib.error.HTTPError, |
| 722 | self.fake_github.emitEvent, |
| 723 | ('ping', pevent), |
| 724 | ) |
Tobias Henkel | eca4620 | 2017-08-02 20:27:10 +0200 | [diff] [blame] | 725 | |
| 726 | |
| 727 | class TestGithubUnprotectedBranches(ZuulTestCase): |
| 728 | config_file = 'zuul-github-driver.conf' |
| 729 | tenant_config_file = 'config/unprotected-branches/main.yaml' |
| 730 | |
| 731 | def test_unprotected_branches(self): |
| 732 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 733 | |
| 734 | project1 = tenant.untrusted_projects[0] |
| 735 | project2 = tenant.untrusted_projects[1] |
| 736 | |
| 737 | # project1 should have parsed master |
| 738 | self.assertIn('master', project1.unparsed_branch_config.keys()) |
| 739 | |
| 740 | # project2 should have no parsed branch |
| 741 | self.assertEqual(0, len(project2.unparsed_branch_config.keys())) |
Jesse Keating | 80730e6 | 2017-09-14 15:35:11 -0600 | [diff] [blame] | 742 | |
| 743 | |
| 744 | class TestGithubWebhook(ZuulTestCase): |
| 745 | config_file = 'zuul-github-driver.conf' |
| 746 | |
| 747 | def setUp(self): |
| 748 | super(TestGithubWebhook, self).setUp() |
| 749 | |
| 750 | # Start the web server |
| 751 | self.web = zuul.web.ZuulWeb( |
| 752 | listen_address='127.0.0.1', listen_port=0, |
| 753 | gear_server='127.0.0.1', gear_port=self.gearman_server.port, |
Monty Taylor | 64bf8e0 | 2018-01-23 16:39:30 -0600 | [diff] [blame] | 754 | connections=[self.fake_github]) |
Jesse Keating | 80730e6 | 2017-09-14 15:35:11 -0600 | [diff] [blame] | 755 | loop = asyncio.new_event_loop() |
| 756 | loop.set_debug(True) |
| 757 | ws_thread = threading.Thread(target=self.web.run, args=(loop,)) |
| 758 | ws_thread.start() |
| 759 | self.addCleanup(loop.close) |
| 760 | self.addCleanup(ws_thread.join) |
| 761 | self.addCleanup(self.web.stop) |
| 762 | |
| 763 | host = '127.0.0.1' |
| 764 | # Wait until web server is started |
| 765 | while True: |
| 766 | time.sleep(0.1) |
| 767 | if self.web.server is None: |
| 768 | continue |
| 769 | port = self.web.server.sockets[0].getsockname()[1] |
| 770 | try: |
| 771 | with socket.create_connection((host, port)): |
| 772 | break |
| 773 | except ConnectionRefusedError: |
| 774 | pass |
| 775 | |
| 776 | self.fake_github.setZuulWebPort(port) |
| 777 | |
| 778 | def tearDown(self): |
| 779 | super(TestGithubWebhook, self).tearDown() |
| 780 | |
| 781 | @simple_layout('layouts/basic-github.yaml', driver='github') |
| 782 | def test_webhook(self): |
| 783 | """Test that we can get github events via zuul-web.""" |
| 784 | |
| 785 | self.executor_server.hold_jobs_in_build = True |
| 786 | |
| 787 | A = self.fake_github.openFakePullRequest('org/project', 'master', 'A') |
| 788 | self.fake_github.emitEvent(A.getPullRequestOpenedEvent(), |
| 789 | use_zuulweb=True) |
| 790 | self.waitUntilSettled() |
| 791 | |
| 792 | self.executor_server.hold_jobs_in_build = False |
| 793 | self.executor_server.release() |
| 794 | self.waitUntilSettled() |
| 795 | |
| 796 | self.assertEqual('SUCCESS', |
| 797 | self.getJobFromHistory('project-test1').result) |
| 798 | self.assertEqual('SUCCESS', |
| 799 | self.getJobFromHistory('project-test2').result) |
| 800 | |
| 801 | job = self.getJobFromHistory('project-test2') |
| 802 | zuulvars = job.parameters['zuul'] |
| 803 | self.assertEqual(str(A.number), zuulvars['change']) |
| 804 | self.assertEqual(str(A.head_sha), zuulvars['patchset']) |
| 805 | self.assertEqual('master', zuulvars['branch']) |
| 806 | self.assertEqual(1, len(A.comments)) |
| 807 | self.assertThat( |
| 808 | A.comments[0], |
| 809 | MatchesRegex('.*\[project-test1 \]\(.*\).*', re.DOTALL)) |
| 810 | self.assertThat( |
| 811 | A.comments[0], |
| 812 | MatchesRegex('.*\[project-test2 \]\(.*\).*', re.DOTALL)) |
| 813 | self.assertEqual(2, len(self.history)) |
| 814 | |
| 815 | # test_pull_unmatched_branch_event(self): |
| 816 | self.create_branch('org/project', 'unmatched_branch') |
| 817 | B = self.fake_github.openFakePullRequest( |
| 818 | 'org/project', 'unmatched_branch', 'B') |
| 819 | self.fake_github.emitEvent(B.getPullRequestOpenedEvent(), |
| 820 | use_zuulweb=True) |
| 821 | self.waitUntilSettled() |
| 822 | |
| 823 | self.assertEqual(2, len(self.history)) |