blob: 97a006650aa4df59879cc810eaaa508f85a5ca79 [file] [log] [blame]
Gregory Haynes4fc12542015-04-22 20:38:06 -07001# Copyright 2015 GoodData
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import logging
Jan Hrubane252a732017-01-03 15:03:09 +010016import re
17from testtools.matchers import MatchesRegex
Jan Hruban324ca5b2015-11-05 19:28:54 +010018import time
Gregory Haynes4fc12542015-04-22 20:38:06 -070019
Wayne1a78c612015-06-11 17:14:13 -070020from tests.base import ZuulTestCase, simple_layout, random_sha1
Gregory Haynes4fc12542015-04-22 20:38:06 -070021
22logging.basicConfig(level=logging.DEBUG,
23 format='%(asctime)s %(name)-32s '
24 '%(levelname)-8s %(message)s')
25
26
27class TestGithubDriver(ZuulTestCase):
28 config_file = 'zuul-github-driver.conf'
29
30 @simple_layout('layouts/basic-github.yaml', driver='github')
31 def test_pull_event(self):
32 self.executor_server.hold_jobs_in_build = True
33
34 pr = self.fake_github.openFakePullRequest('org/project', 'master')
35 self.fake_github.emitEvent(pr.getPullRequestOpenedEvent())
36 self.waitUntilSettled()
37
38 build_params = self.builds[0].parameters
39 self.assertEqual('master', build_params['ZUUL_BRANCH'])
40 self.assertEqual(str(pr.number), build_params['ZUUL_CHANGE'])
41 self.assertEqual(pr.head_sha, build_params['ZUUL_PATCHSET'])
42
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')
53 zuulvars = job.parameters['vars']['zuul']
54 self.assertEqual(pr.number, zuulvars['change'])
55 self.assertEqual(pr.head_sha, zuulvars['patchset'])
Wayne40f40042015-06-12 16:56:30 -070056 self.assertEqual(1, len(pr.comments))
Wayne1a78c612015-06-11 17:14:13 -070057
Jan Hrubanc7ab1602015-10-14 15:29:33 +020058 @simple_layout('layouts/basic-github.yaml', driver='github')
59 def test_comment_event(self):
60 pr = self.fake_github.openFakePullRequest('org/project', 'master')
61 self.fake_github.emitEvent(pr.getCommentAddedEvent('test me'))
62 self.waitUntilSettled()
63 self.assertEqual(2, len(self.history))
64
65 # Test an unmatched comment, history should remain the same
66 pr = self.fake_github.openFakePullRequest('org/project', 'master')
67 self.fake_github.emitEvent(pr.getCommentAddedEvent('casual comment'))
68 self.waitUntilSettled()
69 self.assertEqual(2, len(self.history))
70
Wayne1a78c612015-06-11 17:14:13 -070071 @simple_layout('layouts/push-tag-github.yaml', driver='github')
72 def test_tag_event(self):
73 self.executor_server.hold_jobs_in_build = True
74
75 sha = random_sha1()
76 self.fake_github.emitEvent(
77 self.fake_github.getPushEvent('org/project', 'refs/tags/newtag',
78 new_rev=sha))
79 self.waitUntilSettled()
80
81 build_params = self.builds[0].parameters
82 self.assertEqual('refs/tags/newtag', build_params['ZUUL_REF'])
83 self.assertEqual('00000000000000000000000000000000',
84 build_params['ZUUL_OLDREV'])
85 self.assertEqual(sha, build_params['ZUUL_NEWREV'])
86
87 self.executor_server.hold_jobs_in_build = False
88 self.executor_server.release()
89 self.waitUntilSettled()
90
91 self.assertEqual('SUCCESS',
92 self.getJobFromHistory('project-tag').result)
93
94 @simple_layout('layouts/push-tag-github.yaml', driver='github')
95 def test_push_event(self):
96 self.executor_server.hold_jobs_in_build = True
97
98 old_sha = random_sha1()
99 new_sha = random_sha1()
100 self.fake_github.emitEvent(
101 self.fake_github.getPushEvent('org/project', 'refs/heads/master',
102 old_sha, new_sha))
103 self.waitUntilSettled()
104
105 build_params = self.builds[0].parameters
106 self.assertEqual('refs/heads/master', build_params['ZUUL_REF'])
107 self.assertEqual(old_sha, build_params['ZUUL_OLDREV'])
108 self.assertEqual(new_sha, build_params['ZUUL_NEWREV'])
109
110 self.executor_server.hold_jobs_in_build = False
111 self.executor_server.release()
112 self.waitUntilSettled()
113
114 self.assertEqual('SUCCESS',
115 self.getJobFromHistory('project-post').result)
Jan Hruban6d53c5e2015-10-24 03:03:34 +0200116
Jan Hruban16ad31f2015-11-07 14:39:07 +0100117 @simple_layout('layouts/labeling-github.yaml', driver='github')
118 def test_labels(self):
119 A = self.fake_github.openFakePullRequest('org/project', 'master')
120 self.fake_github.emitEvent(A.addLabel('test'))
121 self.waitUntilSettled()
122 self.assertEqual(1, len(self.history))
123 self.assertEqual('project-labels', self.history[0].name)
124 self.assertEqual(['tests passed'], A.labels)
125
126 # test label removed
127 B = self.fake_github.openFakePullRequest('org/project', 'master')
128 B.addLabel('do not test')
129 self.fake_github.emitEvent(B.removeLabel('do not test'))
130 self.waitUntilSettled()
131 self.assertEqual(2, len(self.history))
132 self.assertEqual('project-labels', self.history[1].name)
133 self.assertEqual(['tests passed'], B.labels)
134
135 # test unmatched label
136 C = self.fake_github.openFakePullRequest('org/project', 'master')
137 self.fake_github.emitEvent(C.addLabel('other label'))
138 self.waitUntilSettled()
139 self.assertEqual(2, len(self.history))
140 self.assertEqual(['other label'], C.labels)
141
Jan Hruban324ca5b2015-11-05 19:28:54 +0100142 @simple_layout('layouts/dequeue-github.yaml', driver='github')
143 def test_dequeue_pull_synchronized(self):
144 self.executor_server.hold_jobs_in_build = True
145
146 pr = self.fake_github.openFakePullRequest(
147 'org/one-job-project', 'master')
148 self.fake_github.emitEvent(pr.getPullRequestOpenedEvent())
149 self.waitUntilSettled()
150
151 # event update stamp has resolution one second, wait so the latter
152 # one has newer timestamp
153 time.sleep(1)
154 pr.addCommit()
155 self.fake_github.emitEvent(pr.getPullRequestSynchronizeEvent())
156 self.waitUntilSettled()
157
158 self.executor_server.hold_jobs_in_build = False
159 self.executor_server.release()
160 self.waitUntilSettled()
161
162 self.assertEqual(2, len(self.history))
163 self.assertEqual(1, self.countJobResults(self.history, 'ABORTED'))
164
165 @simple_layout('layouts/dequeue-github.yaml', driver='github')
166 def test_dequeue_pull_abandoned(self):
167 self.executor_server.hold_jobs_in_build = True
168
169 pr = self.fake_github.openFakePullRequest(
170 'org/one-job-project', 'master')
171 self.fake_github.emitEvent(pr.getPullRequestOpenedEvent())
172 self.waitUntilSettled()
173 self.fake_github.emitEvent(pr.getPullRequestClosedEvent())
174 self.waitUntilSettled()
175
176 self.executor_server.hold_jobs_in_build = False
177 self.executor_server.release()
178 self.waitUntilSettled()
179
180 self.assertEqual(1, len(self.history))
181 self.assertEqual(1, self.countJobResults(self.history, 'ABORTED'))
182
Jan Hruban6d53c5e2015-10-24 03:03:34 +0200183 @simple_layout('layouts/basic-github.yaml', driver='github')
184 def test_git_https_url(self):
185 """Test that git_ssh option gives git url with ssh"""
186 url = self.fake_github.real_getGitUrl('org/project')
187 self.assertEqual('https://github.com/org/project', url)
188
189 @simple_layout('layouts/basic-github.yaml', driver='github')
190 def test_git_ssh_url(self):
191 """Test that git_ssh option gives git url with ssh"""
192 url = self.fake_github_ssh.real_getGitUrl('org/project')
193 self.assertEqual('ssh://git@github.com/org/project.git', url)
Jan Hrubane252a732017-01-03 15:03:09 +0100194
195 @simple_layout('layouts/reporting-github.yaml', driver='github')
196 def test_reporting(self):
197 # pipeline reports pull status both on start and success
198 self.executor_server.hold_jobs_in_build = True
199 pr = self.fake_github.openFakePullRequest('org/project', 'master')
200 self.fake_github.emitEvent(pr.getPullRequestOpenedEvent())
201 self.waitUntilSettled()
202 self.assertIn('check', pr.statuses)
203 check_status = pr.statuses['check']
204 self.assertEqual('Standard check', check_status['description'])
205 self.assertEqual('pending', check_status['state'])
206 self.assertEqual('http://zuul.example.com/status', check_status['url'])
207 self.assertEqual(0, len(pr.comments))
208
209 self.executor_server.hold_jobs_in_build = False
210 self.executor_server.release()
211 self.waitUntilSettled()
212 check_status = pr.statuses['check']
213 self.assertEqual('Standard check', check_status['description'])
214 self.assertEqual('success', check_status['state'])
215 self.assertEqual('http://zuul.example.com/status', check_status['url'])
216 self.assertEqual(1, len(pr.comments))
217 self.assertThat(pr.comments[0],
218 MatchesRegex('.*Build succeeded.*', re.DOTALL))
219
220 # pipeline does not report any status but does comment
221 self.executor_server.hold_jobs_in_build = True
222 self.fake_github.emitEvent(
223 pr.getCommentAddedEvent('reporting check'))
224 self.waitUntilSettled()
225 self.assertNotIn('reporting', pr.statuses)
226 # comments increased by one for the start message
227 self.assertEqual(2, len(pr.comments))
228 self.assertThat(pr.comments[1],
229 MatchesRegex('.*Starting reporting jobs.*', re.DOTALL))
230 self.executor_server.hold_jobs_in_build = False
231 self.executor_server.release()
232 self.waitUntilSettled()
233 self.assertNotIn('reporting', pr.statuses)
234 self.assertEqual(2, len(pr.comments))
Jan Hruban49bff072015-11-03 11:45:46 +0100235
236 @simple_layout('layouts/merging-github.yaml', driver='github')
237 def test_report_pull_merge(self):
238 # pipeline merges the pull request on success
239 A = self.fake_github.openFakePullRequest('org/project', 'master')
240 self.fake_github.emitEvent(A.getCommentAddedEvent('merge me'))
241 self.waitUntilSettled()
242 self.assertTrue(A.is_merged)
243
244 # pipeline merges the pull request on success after failure
245 self.fake_github.merge_failure = True
246 B = self.fake_github.openFakePullRequest('org/project', 'master')
247 self.fake_github.emitEvent(B.getCommentAddedEvent('merge me'))
248 self.waitUntilSettled()
249 self.assertFalse(B.is_merged)
250 self.fake_github.merge_failure = False
251
252 # pipeline merges the pull request on second run of merge
253 # first merge failed on 405 Method Not Allowed error
254 self.fake_github.merge_not_allowed_count = 1
255 C = self.fake_github.openFakePullRequest('org/project', 'master')
256 self.fake_github.emitEvent(C.getCommentAddedEvent('merge me'))
257 self.waitUntilSettled()
258 self.assertTrue(C.is_merged)
259
260 # pipeline does not merge the pull request
261 # merge failed on 405 Method Not Allowed error - twice
262 self.fake_github.merge_not_allowed_count = 2
263 D = self.fake_github.openFakePullRequest('org/project', 'master')
264 self.fake_github.emitEvent(D.getCommentAddedEvent('merge me'))
265 self.waitUntilSettled()
266 self.assertFalse(D.is_merged)