blob: c9847b228f3b65ca9a9db8069b39277f2f55faa0 [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
16
Wayne1a78c612015-06-11 17:14:13 -070017from tests.base import ZuulTestCase, simple_layout, random_sha1
Gregory Haynes4fc12542015-04-22 20:38:06 -070018
19logging.basicConfig(level=logging.DEBUG,
20 format='%(asctime)s %(name)-32s '
21 '%(levelname)-8s %(message)s')
22
23
24class TestGithubDriver(ZuulTestCase):
25 config_file = 'zuul-github-driver.conf'
26
27 @simple_layout('layouts/basic-github.yaml', driver='github')
28 def test_pull_event(self):
29 self.executor_server.hold_jobs_in_build = True
30
31 pr = self.fake_github.openFakePullRequest('org/project', 'master')
32 self.fake_github.emitEvent(pr.getPullRequestOpenedEvent())
33 self.waitUntilSettled()
34
35 build_params = self.builds[0].parameters
36 self.assertEqual('master', build_params['ZUUL_BRANCH'])
37 self.assertEqual(str(pr.number), build_params['ZUUL_CHANGE'])
38 self.assertEqual(pr.head_sha, build_params['ZUUL_PATCHSET'])
39
40 self.executor_server.hold_jobs_in_build = False
41 self.executor_server.release()
42 self.waitUntilSettled()
43
44 self.assertEqual('SUCCESS',
45 self.getJobFromHistory('project-test1').result)
46 self.assertEqual('SUCCESS',
47 self.getJobFromHistory('project-test2').result)
48
49 job = self.getJobFromHistory('project-test2')
50 zuulvars = job.parameters['vars']['zuul']
51 self.assertEqual(pr.number, zuulvars['change'])
52 self.assertEqual(pr.head_sha, zuulvars['patchset'])
Wayne40f40042015-06-12 16:56:30 -070053 self.assertEqual(1, len(pr.comments))
Wayne1a78c612015-06-11 17:14:13 -070054
Jan Hrubanc7ab1602015-10-14 15:29:33 +020055 @simple_layout('layouts/basic-github.yaml', driver='github')
56 def test_comment_event(self):
57 pr = self.fake_github.openFakePullRequest('org/project', 'master')
58 self.fake_github.emitEvent(pr.getCommentAddedEvent('test me'))
59 self.waitUntilSettled()
60 self.assertEqual(2, len(self.history))
61
62 # Test an unmatched comment, history should remain the same
63 pr = self.fake_github.openFakePullRequest('org/project', 'master')
64 self.fake_github.emitEvent(pr.getCommentAddedEvent('casual comment'))
65 self.waitUntilSettled()
66 self.assertEqual(2, len(self.history))
67
Wayne1a78c612015-06-11 17:14:13 -070068 @simple_layout('layouts/push-tag-github.yaml', driver='github')
69 def test_tag_event(self):
70 self.executor_server.hold_jobs_in_build = True
71
72 sha = random_sha1()
73 self.fake_github.emitEvent(
74 self.fake_github.getPushEvent('org/project', 'refs/tags/newtag',
75 new_rev=sha))
76 self.waitUntilSettled()
77
78 build_params = self.builds[0].parameters
79 self.assertEqual('refs/tags/newtag', build_params['ZUUL_REF'])
80 self.assertEqual('00000000000000000000000000000000',
81 build_params['ZUUL_OLDREV'])
82 self.assertEqual(sha, build_params['ZUUL_NEWREV'])
83
84 self.executor_server.hold_jobs_in_build = False
85 self.executor_server.release()
86 self.waitUntilSettled()
87
88 self.assertEqual('SUCCESS',
89 self.getJobFromHistory('project-tag').result)
90
91 @simple_layout('layouts/push-tag-github.yaml', driver='github')
92 def test_push_event(self):
93 self.executor_server.hold_jobs_in_build = True
94
95 old_sha = random_sha1()
96 new_sha = random_sha1()
97 self.fake_github.emitEvent(
98 self.fake_github.getPushEvent('org/project', 'refs/heads/master',
99 old_sha, new_sha))
100 self.waitUntilSettled()
101
102 build_params = self.builds[0].parameters
103 self.assertEqual('refs/heads/master', build_params['ZUUL_REF'])
104 self.assertEqual(old_sha, build_params['ZUUL_OLDREV'])
105 self.assertEqual(new_sha, build_params['ZUUL_NEWREV'])
106
107 self.executor_server.hold_jobs_in_build = False
108 self.executor_server.release()
109 self.waitUntilSettled()
110
111 self.assertEqual('SUCCESS',
112 self.getJobFromHistory('project-post').result)