blob: b9e6c6e921a01e01478d01cab17d9264e3ba4c2f [file] [log] [blame]
James E. Blair45de61c2017-02-14 13:23:14 -08001# Copyright 2016 Red Hat, Inc.
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
Fabien Boucher194a2bf2017-12-02 18:17:58 +010015
16import os
17import time
18import yaml
19
20from tests.base import ZuulTestCase, simple_layout
James E. Blair45de61c2017-02-14 13:23:14 -080021
22
23class TestGitDriver(ZuulTestCase):
24 config_file = 'zuul-git-driver.conf'
25 tenant_config_file = 'config/git-driver/main.yaml'
26
27 def setup_config(self):
28 super(TestGitDriver, self).setup_config()
29 self.config.set('connection git', 'baseurl', self.upstream_root)
30
Fabien Boucher194a2bf2017-12-02 18:17:58 +010031 def test_basic(self):
James E. Blair45de61c2017-02-14 13:23:14 -080032 tenant = self.sched.abide.tenants.get('tenant-one')
33 # Check that we have the git source for common-config and the
34 # gerrit source for the project.
James E. Blair109da3f2017-04-04 14:39:43 -070035 self.assertEqual('git', tenant.config_projects[0].source.name)
36 self.assertEqual('common-config', tenant.config_projects[0].name)
37 self.assertEqual('gerrit', tenant.untrusted_projects[0].source.name)
38 self.assertEqual('org/project', tenant.untrusted_projects[0].name)
James E. Blair45de61c2017-02-14 13:23:14 -080039
40 # The configuration for this test is accessed via the git
41 # driver (in common-config), rather than the gerrit driver, so
42 # if the job runs, it worked.
43 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
44 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
45 self.waitUntilSettled()
46 self.assertEqual(len(self.history), 1)
47 self.assertEqual(A.reported, 1)
Fabien Boucher194a2bf2017-12-02 18:17:58 +010048
49 def test_config_refreshed(self):
50 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
51 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
52 self.waitUntilSettled()
53 self.assertEqual(len(self.history), 1)
54 self.assertEqual(A.reported, 1)
55 self.assertEqual(self.history[0].name, 'project-test1')
56
57 # Update zuul.yaml to force a tenant reconfiguration
58 path = os.path.join(self.upstream_root, 'common-config', 'zuul.yaml')
59 config = yaml.load(open(path, 'r').read())
60 change = {
61 'name': 'org/project',
62 'check': {
63 'jobs': [
64 'project-test2'
65 ]
66 }
67 }
68 config[4]['project'] = change
69 files = {'zuul.yaml': yaml.dump(config)}
70 self.addCommitToRepo(
71 'common-config', 'Change zuul.yaml configuration', files)
72
73 # Let some time for the tenant reconfiguration to happen
74 time.sleep(2)
75 self.waitUntilSettled()
76
77 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
78 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
79 self.waitUntilSettled()
80 self.assertEqual(len(self.history), 2)
81 self.assertEqual(A.reported, 1)
82 # We make sure the new job has run
83 self.assertEqual(self.history[1].name, 'project-test2')
84
85 # Let's stop the git Watcher to let us merge some changes commits
86 # We want to verify that config changes are detected for commits
87 # on the range oldrev..newrev
88 self.sched.connections.getSource('git').connection.w_pause = True
89 # Add a config change
90 change = {
91 'name': 'org/project',
92 'check': {
93 'jobs': [
94 'project-test1'
95 ]
96 }
97 }
98 config[4]['project'] = change
99 files = {'zuul.yaml': yaml.dump(config)}
100 self.addCommitToRepo(
101 'common-config', 'Change zuul.yaml configuration', files)
102 # Add two other changes
103 self.addCommitToRepo(
104 'common-config', 'Adding f1',
105 {'f1': "Content"})
106 self.addCommitToRepo(
107 'common-config', 'Adding f2',
108 {'f2': "Content"})
109 # Restart the git watcher
110 self.sched.connections.getSource('git').connection.w_pause = False
111
112 # Let some time for the tenant reconfiguration to happen
113 time.sleep(2)
114 self.waitUntilSettled()
115
116 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
117 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
118 self.waitUntilSettled()
119 self.assertEqual(len(self.history), 3)
120 self.assertEqual(A.reported, 1)
121 # We make sure the new job has run
122 self.assertEqual(self.history[2].name, 'project-test1')
123
124 def ensure_watcher_has_context(self):
125 # Make sure watcher have read initial refs shas
126 cnx = self.sched.connections.getSource('git').connection
127 delay = 0.1
128 max_delay = 1
129 while not cnx.projects_refs:
130 time.sleep(delay)
131 max_delay -= delay
132 if max_delay <= 0:
133 raise Exception("Timeout waiting for initial read")
134
135 @simple_layout('layouts/basic-git.yaml', driver='git')
136 def test_ref_updated_event(self):
137 self.ensure_watcher_has_context()
138 # Add a commit to trigger a ref-updated event
139 self.addCommitToRepo(
140 'org/project', 'A change for ref-updated', {'f1': 'Content'})
141 # Let some time for the git watcher to detect the ref-update event
142 time.sleep(0.2)
143 self.waitUntilSettled()
144 self.assertEqual(len(self.history), 1)
145 self.assertEqual('SUCCESS',
146 self.getJobFromHistory('post-job').result)
147
148 @simple_layout('layouts/basic-git.yaml', driver='git')
149 def test_ref_created(self):
150 self.ensure_watcher_has_context()
151 # Tag HEAD to trigger a ref-updated event
152 self.addTagToRepo(
153 'org/project', 'atag', 'HEAD')
154 # Let some time for the git watcher to detect the ref-update event
155 time.sleep(0.2)
156 self.waitUntilSettled()
157 self.assertEqual(len(self.history), 1)
158 self.assertEqual('SUCCESS',
159 self.getJobFromHistory('tag-job').result)
160
161 @simple_layout('layouts/basic-git.yaml', driver='git')
162 def test_ref_deleted(self):
163 self.ensure_watcher_has_context()
164 # Delete default tag init to trigger a ref-updated event
165 self.delTagFromRepo(
166 'org/project', 'init')
167 # Let some time for the git watcher to detect the ref-update event
168 time.sleep(0.2)
169 self.waitUntilSettled()
170 # Make sure no job as run as ignore-delete is True by default
171 self.assertEqual(len(self.history), 0)