blob: c7c5f3a373921290cbe40662739adf410fa6e969 [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
Gregory Haynes4fc12542015-04-22 20:38:06 -070018
Wayne1a78c612015-06-11 17:14:13 -070019from tests.base import ZuulTestCase, simple_layout, random_sha1
Gregory Haynes4fc12542015-04-22 20:38:06 -070020
21logging.basicConfig(level=logging.DEBUG,
22 format='%(asctime)s %(name)-32s '
23 '%(levelname)-8s %(message)s')
24
25
26class 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
33 pr = self.fake_github.openFakePullRequest('org/project', 'master')
34 self.fake_github.emitEvent(pr.getPullRequestOpenedEvent())
35 self.waitUntilSettled()
36
37 build_params = self.builds[0].parameters
38 self.assertEqual('master', build_params['ZUUL_BRANCH'])
39 self.assertEqual(str(pr.number), build_params['ZUUL_CHANGE'])
40 self.assertEqual(pr.head_sha, build_params['ZUUL_PATCHSET'])
41
42 self.executor_server.hold_jobs_in_build = False
43 self.executor_server.release()
44 self.waitUntilSettled()
45
46 self.assertEqual('SUCCESS',
47 self.getJobFromHistory('project-test1').result)
48 self.assertEqual('SUCCESS',
49 self.getJobFromHistory('project-test2').result)
50
51 job = self.getJobFromHistory('project-test2')
52 zuulvars = job.parameters['vars']['zuul']
53 self.assertEqual(pr.number, zuulvars['change'])
54 self.assertEqual(pr.head_sha, zuulvars['patchset'])
Wayne40f40042015-06-12 16:56:30 -070055 self.assertEqual(1, len(pr.comments))
Wayne1a78c612015-06-11 17:14:13 -070056
Jan Hrubanc7ab1602015-10-14 15:29:33 +020057 @simple_layout('layouts/basic-github.yaml', driver='github')
58 def test_comment_event(self):
59 pr = self.fake_github.openFakePullRequest('org/project', 'master')
60 self.fake_github.emitEvent(pr.getCommentAddedEvent('test me'))
61 self.waitUntilSettled()
62 self.assertEqual(2, len(self.history))
63
64 # Test an unmatched comment, history should remain the same
65 pr = self.fake_github.openFakePullRequest('org/project', 'master')
66 self.fake_github.emitEvent(pr.getCommentAddedEvent('casual comment'))
67 self.waitUntilSettled()
68 self.assertEqual(2, len(self.history))
69
Wayne1a78c612015-06-11 17:14:13 -070070 @simple_layout('layouts/push-tag-github.yaml', driver='github')
71 def test_tag_event(self):
72 self.executor_server.hold_jobs_in_build = True
73
74 sha = random_sha1()
75 self.fake_github.emitEvent(
76 self.fake_github.getPushEvent('org/project', 'refs/tags/newtag',
77 new_rev=sha))
78 self.waitUntilSettled()
79
80 build_params = self.builds[0].parameters
81 self.assertEqual('refs/tags/newtag', build_params['ZUUL_REF'])
82 self.assertEqual('00000000000000000000000000000000',
83 build_params['ZUUL_OLDREV'])
84 self.assertEqual(sha, build_params['ZUUL_NEWREV'])
85
86 self.executor_server.hold_jobs_in_build = False
87 self.executor_server.release()
88 self.waitUntilSettled()
89
90 self.assertEqual('SUCCESS',
91 self.getJobFromHistory('project-tag').result)
92
93 @simple_layout('layouts/push-tag-github.yaml', driver='github')
94 def test_push_event(self):
95 self.executor_server.hold_jobs_in_build = True
96
97 old_sha = random_sha1()
98 new_sha = random_sha1()
99 self.fake_github.emitEvent(
100 self.fake_github.getPushEvent('org/project', 'refs/heads/master',
101 old_sha, new_sha))
102 self.waitUntilSettled()
103
104 build_params = self.builds[0].parameters
105 self.assertEqual('refs/heads/master', build_params['ZUUL_REF'])
106 self.assertEqual(old_sha, build_params['ZUUL_OLDREV'])
107 self.assertEqual(new_sha, build_params['ZUUL_NEWREV'])
108
109 self.executor_server.hold_jobs_in_build = False
110 self.executor_server.release()
111 self.waitUntilSettled()
112
113 self.assertEqual('SUCCESS',
114 self.getJobFromHistory('project-post').result)
Jan Hruban6d53c5e2015-10-24 03:03:34 +0200115
Jan Hruban16ad31f2015-11-07 14:39:07 +0100116 @simple_layout('layouts/labeling-github.yaml', driver='github')
117 def test_labels(self):
118 A = self.fake_github.openFakePullRequest('org/project', 'master')
119 self.fake_github.emitEvent(A.addLabel('test'))
120 self.waitUntilSettled()
121 self.assertEqual(1, len(self.history))
122 self.assertEqual('project-labels', self.history[0].name)
123 self.assertEqual(['tests passed'], A.labels)
124
125 # test label removed
126 B = self.fake_github.openFakePullRequest('org/project', 'master')
127 B.addLabel('do not test')
128 self.fake_github.emitEvent(B.removeLabel('do not test'))
129 self.waitUntilSettled()
130 self.assertEqual(2, len(self.history))
131 self.assertEqual('project-labels', self.history[1].name)
132 self.assertEqual(['tests passed'], B.labels)
133
134 # test unmatched label
135 C = self.fake_github.openFakePullRequest('org/project', 'master')
136 self.fake_github.emitEvent(C.addLabel('other label'))
137 self.waitUntilSettled()
138 self.assertEqual(2, len(self.history))
139 self.assertEqual(['other label'], C.labels)
140
Jan Hruban6d53c5e2015-10-24 03:03:34 +0200141 @simple_layout('layouts/basic-github.yaml', driver='github')
142 def test_git_https_url(self):
143 """Test that git_ssh option gives git url with ssh"""
144 url = self.fake_github.real_getGitUrl('org/project')
145 self.assertEqual('https://github.com/org/project', url)
146
147 @simple_layout('layouts/basic-github.yaml', driver='github')
148 def test_git_ssh_url(self):
149 """Test that git_ssh option gives git url with ssh"""
150 url = self.fake_github_ssh.real_getGitUrl('org/project')
151 self.assertEqual('ssh://git@github.com/org/project.git', url)
Jan Hrubane252a732017-01-03 15:03:09 +0100152
153 @simple_layout('layouts/reporting-github.yaml', driver='github')
154 def test_reporting(self):
155 # pipeline reports pull status both on start and success
156 self.executor_server.hold_jobs_in_build = True
157 pr = self.fake_github.openFakePullRequest('org/project', 'master')
158 self.fake_github.emitEvent(pr.getPullRequestOpenedEvent())
159 self.waitUntilSettled()
160 self.assertIn('check', pr.statuses)
161 check_status = pr.statuses['check']
162 self.assertEqual('Standard check', check_status['description'])
163 self.assertEqual('pending', check_status['state'])
164 self.assertEqual('http://zuul.example.com/status', check_status['url'])
165 self.assertEqual(0, len(pr.comments))
166
167 self.executor_server.hold_jobs_in_build = False
168 self.executor_server.release()
169 self.waitUntilSettled()
170 check_status = pr.statuses['check']
171 self.assertEqual('Standard check', check_status['description'])
172 self.assertEqual('success', check_status['state'])
173 self.assertEqual('http://zuul.example.com/status', check_status['url'])
174 self.assertEqual(1, len(pr.comments))
175 self.assertThat(pr.comments[0],
176 MatchesRegex('.*Build succeeded.*', re.DOTALL))
177
178 # pipeline does not report any status but does comment
179 self.executor_server.hold_jobs_in_build = True
180 self.fake_github.emitEvent(
181 pr.getCommentAddedEvent('reporting check'))
182 self.waitUntilSettled()
183 self.assertNotIn('reporting', pr.statuses)
184 # comments increased by one for the start message
185 self.assertEqual(2, len(pr.comments))
186 self.assertThat(pr.comments[1],
187 MatchesRegex('.*Starting reporting jobs.*', re.DOTALL))
188 self.executor_server.hold_jobs_in_build = False
189 self.executor_server.release()
190 self.waitUntilSettled()
191 self.assertNotIn('reporting', pr.statuses)
192 self.assertEqual(2, len(pr.comments))
Jan Hruban49bff072015-11-03 11:45:46 +0100193
194 @simple_layout('layouts/merging-github.yaml', driver='github')
195 def test_report_pull_merge(self):
196 # pipeline merges the pull request on success
197 A = self.fake_github.openFakePullRequest('org/project', 'master')
198 self.fake_github.emitEvent(A.getCommentAddedEvent('merge me'))
199 self.waitUntilSettled()
200 self.assertTrue(A.is_merged)
201
202 # pipeline merges the pull request on success after failure
203 self.fake_github.merge_failure = True
204 B = self.fake_github.openFakePullRequest('org/project', 'master')
205 self.fake_github.emitEvent(B.getCommentAddedEvent('merge me'))
206 self.waitUntilSettled()
207 self.assertFalse(B.is_merged)
208 self.fake_github.merge_failure = False
209
210 # pipeline merges the pull request on second run of merge
211 # first merge failed on 405 Method Not Allowed error
212 self.fake_github.merge_not_allowed_count = 1
213 C = self.fake_github.openFakePullRequest('org/project', 'master')
214 self.fake_github.emitEvent(C.getCommentAddedEvent('merge me'))
215 self.waitUntilSettled()
216 self.assertTrue(C.is_merged)
217
218 # pipeline does not merge the pull request
219 # merge failed on 405 Method Not Allowed error - twice
220 self.fake_github.merge_not_allowed_count = 2
221 D = self.fake_github.openFakePullRequest('org/project', 'master')
222 self.fake_github.emitEvent(D.getCommentAddedEvent('merge me'))
223 self.waitUntilSettled()
224 self.assertFalse(D.is_merged)