blob: 409d9660cfd38d0b685b05f60d201fdbdf27da1d [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
116 @simple_layout('layouts/basic-github.yaml', driver='github')
117 def test_git_https_url(self):
118 """Test that git_ssh option gives git url with ssh"""
119 url = self.fake_github.real_getGitUrl('org/project')
120 self.assertEqual('https://github.com/org/project', url)
121
122 @simple_layout('layouts/basic-github.yaml', driver='github')
123 def test_git_ssh_url(self):
124 """Test that git_ssh option gives git url with ssh"""
125 url = self.fake_github_ssh.real_getGitUrl('org/project')
126 self.assertEqual('ssh://git@github.com/org/project.git', url)
Jan Hrubane252a732017-01-03 15:03:09 +0100127
128 @simple_layout('layouts/reporting-github.yaml', driver='github')
129 def test_reporting(self):
130 # pipeline reports pull status both on start and success
131 self.executor_server.hold_jobs_in_build = True
132 pr = self.fake_github.openFakePullRequest('org/project', 'master')
133 self.fake_github.emitEvent(pr.getPullRequestOpenedEvent())
134 self.waitUntilSettled()
135 self.assertIn('check', pr.statuses)
136 check_status = pr.statuses['check']
137 self.assertEqual('Standard check', check_status['description'])
138 self.assertEqual('pending', check_status['state'])
139 self.assertEqual('http://zuul.example.com/status', check_status['url'])
140 self.assertEqual(0, len(pr.comments))
141
142 self.executor_server.hold_jobs_in_build = False
143 self.executor_server.release()
144 self.waitUntilSettled()
145 check_status = pr.statuses['check']
146 self.assertEqual('Standard check', check_status['description'])
147 self.assertEqual('success', check_status['state'])
148 self.assertEqual('http://zuul.example.com/status', check_status['url'])
149 self.assertEqual(1, len(pr.comments))
150 self.assertThat(pr.comments[0],
151 MatchesRegex('.*Build succeeded.*', re.DOTALL))
152
153 # pipeline does not report any status but does comment
154 self.executor_server.hold_jobs_in_build = True
155 self.fake_github.emitEvent(
156 pr.getCommentAddedEvent('reporting check'))
157 self.waitUntilSettled()
158 self.assertNotIn('reporting', pr.statuses)
159 # comments increased by one for the start message
160 self.assertEqual(2, len(pr.comments))
161 self.assertThat(pr.comments[1],
162 MatchesRegex('.*Starting reporting jobs.*', re.DOTALL))
163 self.executor_server.hold_jobs_in_build = False
164 self.executor_server.release()
165 self.waitUntilSettled()
166 self.assertNotIn('reporting', pr.statuses)
167 self.assertEqual(2, len(pr.comments))
Jan Hruban49bff072015-11-03 11:45:46 +0100168
169 @simple_layout('layouts/merging-github.yaml', driver='github')
170 def test_report_pull_merge(self):
171 # pipeline merges the pull request on success
172 A = self.fake_github.openFakePullRequest('org/project', 'master')
173 self.fake_github.emitEvent(A.getCommentAddedEvent('merge me'))
174 self.waitUntilSettled()
175 self.assertTrue(A.is_merged)
176
177 # pipeline merges the pull request on success after failure
178 self.fake_github.merge_failure = True
179 B = self.fake_github.openFakePullRequest('org/project', 'master')
180 self.fake_github.emitEvent(B.getCommentAddedEvent('merge me'))
181 self.waitUntilSettled()
182 self.assertFalse(B.is_merged)
183 self.fake_github.merge_failure = False
184
185 # pipeline merges the pull request on second run of merge
186 # first merge failed on 405 Method Not Allowed error
187 self.fake_github.merge_not_allowed_count = 1
188 C = self.fake_github.openFakePullRequest('org/project', 'master')
189 self.fake_github.emitEvent(C.getCommentAddedEvent('merge me'))
190 self.waitUntilSettled()
191 self.assertTrue(C.is_merged)
192
193 # pipeline does not merge the pull request
194 # merge failed on 405 Method Not Allowed error - twice
195 self.fake_github.merge_not_allowed_count = 2
196 D = self.fake_github.openFakePullRequest('org/project', 'master')
197 self.fake_github.emitEvent(D.getCommentAddedEvent('merge me'))
198 self.waitUntilSettled()
199 self.assertFalse(D.is_merged)