blob: 3b5c206b723547047ab7e2cf15d34b021007d46e [file] [log] [blame]
James E. Blairac180ce2017-06-07 19:31:02 -07001# Copyright 2017 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
Tristan Cacqueray86883062017-07-11 06:00:07 +000015import fixtures
16import logging
Tristan Cacqueray829e6172017-06-13 06:49:36 +000017import textwrap
James E. Blairac180ce2017-06-07 19:31:02 -070018
19from tests.base import ZuulTestCase
20
21
22class TenantParserTestCase(ZuulTestCase):
23 create_project_keys = True
24
25 CONFIG_SET = set(['pipeline', 'job', 'semaphore', 'project',
26 'project-template', 'nodeset', 'secret'])
27 UNTRUSTED_SET = CONFIG_SET - set(['pipeline'])
28
29 def setupAllProjectKeys(self):
30 for project in ['common-config', 'org/project1', 'org/project2']:
31 self.setupProjectKeys('gerrit', project)
32
33
34class TestTenantSimple(TenantParserTestCase):
35 tenant_config_file = 'config/tenant-parser/simple.yaml'
36
37 def test_tenant_simple(self):
38 tenant = self.sched.abide.tenants.get('tenant-one')
39 self.assertEqual(['common-config'],
40 [x.name for x in tenant.config_projects])
41 self.assertEqual(['org/project1', 'org/project2'],
42 [x.name for x in tenant.untrusted_projects])
James E. Blair08d9b782017-06-29 14:22:48 -070043
44 project = tenant.config_projects[0]
45 tpc = tenant.project_configs[project.canonical_name]
46 self.assertEqual(self.CONFIG_SET, tpc.load_classes)
47 project = tenant.untrusted_projects[0]
48 tpc = tenant.project_configs[project.canonical_name]
49 self.assertEqual(self.UNTRUSTED_SET, tpc.load_classes)
50 project = tenant.untrusted_projects[1]
51 tpc = tenant.project_configs[project.canonical_name]
52 self.assertEqual(self.UNTRUSTED_SET, tpc.load_classes)
James E. Blairac180ce2017-06-07 19:31:02 -070053 self.assertTrue('common-config-job' in tenant.layout.jobs)
54 self.assertTrue('project1-job' in tenant.layout.jobs)
55 self.assertTrue('project2-job' in tenant.layout.jobs)
56 project1_config = tenant.layout.project_configs.get(
57 'review.example.com/org/project1')
58 self.assertTrue('common-config-job' in
59 project1_config.pipelines['check'].job_list.jobs)
60 self.assertTrue('project1-job' in
61 project1_config.pipelines['check'].job_list.jobs)
62 project2_config = tenant.layout.project_configs.get(
63 'review.example.com/org/project2')
64 self.assertTrue('common-config-job' in
65 project2_config.pipelines['check'].job_list.jobs)
66 self.assertTrue('project2-job' in
67 project2_config.pipelines['check'].job_list.jobs)
68
69
70class TestTenantOverride(TenantParserTestCase):
71 tenant_config_file = 'config/tenant-parser/override.yaml'
72
73 def test_tenant_override(self):
74 tenant = self.sched.abide.tenants.get('tenant-one')
75 self.assertEqual(['common-config'],
76 [x.name for x in tenant.config_projects])
77 self.assertEqual(['org/project1', 'org/project2'],
78 [x.name for x in tenant.untrusted_projects])
James E. Blair08d9b782017-06-29 14:22:48 -070079 project = tenant.config_projects[0]
80 tpc = tenant.project_configs[project.canonical_name]
81 self.assertEqual(self.CONFIG_SET, tpc.load_classes)
82 project = tenant.untrusted_projects[0]
83 tpc = tenant.project_configs[project.canonical_name]
James E. Blairac180ce2017-06-07 19:31:02 -070084 self.assertEqual(self.UNTRUSTED_SET - set(['project']),
James E. Blair08d9b782017-06-29 14:22:48 -070085 tpc.load_classes)
86 project = tenant.untrusted_projects[1]
87 tpc = tenant.project_configs[project.canonical_name]
88 self.assertEqual(set(['job']), tpc.load_classes)
James E. Blairac180ce2017-06-07 19:31:02 -070089 self.assertTrue('common-config-job' in tenant.layout.jobs)
90 self.assertTrue('project1-job' in tenant.layout.jobs)
91 self.assertTrue('project2-job' in tenant.layout.jobs)
92 project1_config = tenant.layout.project_configs.get(
93 'review.example.com/org/project1')
94 self.assertTrue('common-config-job' in
95 project1_config.pipelines['check'].job_list.jobs)
96 self.assertFalse('project1-job' in
97 project1_config.pipelines['check'].job_list.jobs)
98 project2_config = tenant.layout.project_configs.get(
99 'review.example.com/org/project2')
100 self.assertTrue('common-config-job' in
101 project2_config.pipelines['check'].job_list.jobs)
102 self.assertFalse('project2-job' in
103 project2_config.pipelines['check'].job_list.jobs)
104
105
106class TestTenantGroups(TenantParserTestCase):
107 tenant_config_file = 'config/tenant-parser/groups.yaml'
108
109 def test_tenant_groups(self):
110 tenant = self.sched.abide.tenants.get('tenant-one')
111 self.assertEqual(['common-config'],
112 [x.name for x in tenant.config_projects])
113 self.assertEqual(['org/project1', 'org/project2'],
114 [x.name for x in tenant.untrusted_projects])
James E. Blair08d9b782017-06-29 14:22:48 -0700115 project = tenant.config_projects[0]
116 tpc = tenant.project_configs[project.canonical_name]
117 self.assertEqual(self.CONFIG_SET, tpc.load_classes)
118 project = tenant.untrusted_projects[0]
119 tpc = tenant.project_configs[project.canonical_name]
James E. Blairac180ce2017-06-07 19:31:02 -0700120 self.assertEqual(self.UNTRUSTED_SET - set(['project']),
James E. Blair08d9b782017-06-29 14:22:48 -0700121 tpc.load_classes)
122 project = tenant.untrusted_projects[1]
123 tpc = tenant.project_configs[project.canonical_name]
James E. Blairac180ce2017-06-07 19:31:02 -0700124 self.assertEqual(self.UNTRUSTED_SET - set(['project']),
James E. Blair08d9b782017-06-29 14:22:48 -0700125 tpc.load_classes)
James E. Blairac180ce2017-06-07 19:31:02 -0700126 self.assertTrue('common-config-job' in tenant.layout.jobs)
127 self.assertTrue('project1-job' in tenant.layout.jobs)
128 self.assertTrue('project2-job' in tenant.layout.jobs)
129 project1_config = tenant.layout.project_configs.get(
130 'review.example.com/org/project1')
131 self.assertTrue('common-config-job' in
132 project1_config.pipelines['check'].job_list.jobs)
133 self.assertFalse('project1-job' in
134 project1_config.pipelines['check'].job_list.jobs)
135 project2_config = tenant.layout.project_configs.get(
136 'review.example.com/org/project2')
137 self.assertTrue('common-config-job' in
138 project2_config.pipelines['check'].job_list.jobs)
139 self.assertFalse('project2-job' in
140 project2_config.pipelines['check'].job_list.jobs)
141
142
143class TestTenantGroups2(TenantParserTestCase):
144 tenant_config_file = 'config/tenant-parser/groups2.yaml'
145
146 def test_tenant_groups2(self):
147 tenant = self.sched.abide.tenants.get('tenant-one')
148 self.assertEqual(['common-config'],
149 [x.name for x in tenant.config_projects])
150 self.assertEqual(['org/project1', 'org/project2'],
151 [x.name for x in tenant.untrusted_projects])
James E. Blair08d9b782017-06-29 14:22:48 -0700152 project = tenant.config_projects[0]
153 tpc = tenant.project_configs[project.canonical_name]
154 self.assertEqual(self.CONFIG_SET, tpc.load_classes)
155 project = tenant.untrusted_projects[0]
156 tpc = tenant.project_configs[project.canonical_name]
James E. Blairac180ce2017-06-07 19:31:02 -0700157 self.assertEqual(self.UNTRUSTED_SET - set(['project']),
James E. Blair08d9b782017-06-29 14:22:48 -0700158 tpc.load_classes)
159 project = tenant.untrusted_projects[1]
160 tpc = tenant.project_configs[project.canonical_name]
James E. Blairac180ce2017-06-07 19:31:02 -0700161 self.assertEqual(self.UNTRUSTED_SET - set(['project', 'job']),
James E. Blair08d9b782017-06-29 14:22:48 -0700162 tpc.load_classes)
James E. Blairac180ce2017-06-07 19:31:02 -0700163 self.assertTrue('common-config-job' in tenant.layout.jobs)
164 self.assertTrue('project1-job' in tenant.layout.jobs)
165 self.assertFalse('project2-job' in tenant.layout.jobs)
166 project1_config = tenant.layout.project_configs.get(
167 'review.example.com/org/project1')
168 self.assertTrue('common-config-job' in
169 project1_config.pipelines['check'].job_list.jobs)
170 self.assertFalse('project1-job' in
171 project1_config.pipelines['check'].job_list.jobs)
172 project2_config = tenant.layout.project_configs.get(
173 'review.example.com/org/project2')
174 self.assertTrue('common-config-job' in
175 project2_config.pipelines['check'].job_list.jobs)
176 self.assertFalse('project2-job' in
177 project2_config.pipelines['check'].job_list.jobs)
178
179
180class TestTenantGroups3(TenantParserTestCase):
181 tenant_config_file = 'config/tenant-parser/groups3.yaml'
182
183 def test_tenant_groups3(self):
184 tenant = self.sched.abide.tenants.get('tenant-one')
Tobias Henkeleca46202017-08-02 20:27:10 +0200185 self.assertEqual(False, tenant.exclude_unprotected_branches)
James E. Blairac180ce2017-06-07 19:31:02 -0700186 self.assertEqual(['common-config'],
187 [x.name for x in tenant.config_projects])
188 self.assertEqual(['org/project1', 'org/project2'],
189 [x.name for x in tenant.untrusted_projects])
James E. Blair08d9b782017-06-29 14:22:48 -0700190 project = tenant.config_projects[0]
191 tpc = tenant.project_configs[project.canonical_name]
192 self.assertEqual(self.CONFIG_SET, tpc.load_classes)
193 project = tenant.untrusted_projects[0]
194 tpc = tenant.project_configs[project.canonical_name]
195 self.assertEqual(set(['job']), tpc.load_classes)
196 project = tenant.untrusted_projects[1]
197 tpc = tenant.project_configs[project.canonical_name]
198 self.assertEqual(set(['project', 'job']), tpc.load_classes)
James E. Blairac180ce2017-06-07 19:31:02 -0700199 self.assertTrue('common-config-job' in tenant.layout.jobs)
200 self.assertTrue('project1-job' in tenant.layout.jobs)
201 self.assertTrue('project2-job' in tenant.layout.jobs)
202 project1_config = tenant.layout.project_configs.get(
203 'review.example.com/org/project1')
204 self.assertTrue('common-config-job' in
205 project1_config.pipelines['check'].job_list.jobs)
206 self.assertFalse('project1-job' in
207 project1_config.pipelines['check'].job_list.jobs)
208 project2_config = tenant.layout.project_configs.get(
209 'review.example.com/org/project2')
210 self.assertTrue('common-config-job' in
211 project2_config.pipelines['check'].job_list.jobs)
212 self.assertTrue('project2-job' in
213 project2_config.pipelines['check'].job_list.jobs)
Tristan Cacqueray829e6172017-06-13 06:49:36 +0000214
215
Tobias Henkeleca46202017-08-02 20:27:10 +0200216class TestTenantUnprotectedBranches(TenantParserTestCase):
217 tenant_config_file = 'config/tenant-parser/unprotected-branches.yaml'
218
219 def test_tenant_unprotected_branches(self):
220 tenant = self.sched.abide.tenants.get('tenant-one')
221 self.assertEqual(True, tenant.exclude_unprotected_branches)
222
223 self.assertEqual(['common-config'],
224 [x.name for x in tenant.config_projects])
225 self.assertEqual(['org/project1', 'org/project2'],
226 [x.name for x in tenant.untrusted_projects])
227
228 tpc = tenant.project_configs
229 project_name = tenant.config_projects[0].canonical_name
230 self.assertEqual(False, tpc[project_name].exclude_unprotected_branches)
231
232 project_name = tenant.untrusted_projects[0].canonical_name
233 self.assertIsNone(tpc[project_name].exclude_unprotected_branches)
234
235 project_name = tenant.untrusted_projects[1].canonical_name
236 self.assertIsNone(tpc[project_name].exclude_unprotected_branches)
237
238
Tristan Cacqueray829e6172017-06-13 06:49:36 +0000239class TestSplitConfig(ZuulTestCase):
240 tenant_config_file = 'config/split-config/main.yaml'
241
242 def setup_config(self):
243 super(TestSplitConfig, self).setup_config()
244
245 def test_split_config(self):
246 tenant = self.sched.abide.tenants.get('tenant-one')
247 self.assertIn('project-test1', tenant.layout.jobs)
248 project_config = tenant.layout.project_configs.get(
249 'review.example.com/org/project')
250 self.assertIn('project-test1',
251 project_config.pipelines['check'].job_list.jobs)
252 project1_config = tenant.layout.project_configs.get(
253 'review.example.com/org/project1')
254 self.assertIn('project1-project2-integration',
255 project1_config.pipelines['check'].job_list.jobs)
256
257 def test_dynamic_split_config(self):
258 in_repo_conf = textwrap.dedent(
259 """
260 - project:
261 name: org/project1
262 check:
263 jobs:
264 - project-test1
265 """)
266 file_dict = {'.zuul.d/gate.yaml': in_repo_conf}
267 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
268 files=file_dict)
269 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
270 self.waitUntilSettled()
271 # project1-project2-integration test removed, only want project-test1
272 self.assertHistory([
273 dict(name='project-test1', result='SUCCESS', changes='1,1')])
Tristan Cacqueray86883062017-07-11 06:00:07 +0000274
275 def test_config_path_conflict(self):
276 def add_file(project, path):
277 new_file = textwrap.dedent(
278 """
279 - job:
280 name: test-job
281 """
282 )
283 file_dict = {path: new_file}
284 A = self.fake_gerrit.addFakeChange(project, 'master', 'A',
285 files=file_dict)
286 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
287 self.waitUntilSettled()
288
289 log_fixture = self.useFixture(
290 fixtures.FakeLogger(level=logging.WARNING))
291
292 log_fixture._output.truncate(0)
293 add_file("common-config", "zuul.yaml")
294 self.assertIn("Multiple configuration", log_fixture.output)
295
296 log_fixture._output.truncate(0)
297 add_file("org/project1", ".zuul.yaml")
298 self.assertIn("Multiple configuration", log_fixture.output)
299
300
301class TestConfigConflict(ZuulTestCase):
302 tenant_config_file = 'config/conflict-config/main.yaml'
303
304 def test_conflict_config(self):
305 tenant = self.sched.abide.tenants.get('tenant-one')
306 jobs = sorted(tenant.layout.jobs.keys())
307 self.assertEquals(
James E. Blair2bab6e72017-08-07 09:52:45 -0700308 ['base', 'noop', 'trusted-zuul.yaml-job',
309 'untrusted-zuul.yaml-job'],
Tristan Cacqueray86883062017-07-11 06:00:07 +0000310 jobs)