blob: 49220f205bf9fa8ff49b6d87a03f9dd7557e7958 [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
Fabien Boucher52252312018-01-18 19:54:34 +0100216class TestTenantGroups4(TenantParserTestCase):
217 tenant_config_file = 'config/tenant-parser/groups4.yaml'
218
219 def test_tenant_groups(self):
220 tenant = self.sched.abide.tenants.get('tenant-one')
221 self.assertEqual(['common-config'],
222 [x.name for x in tenant.config_projects])
223 self.assertEqual(['org/project1', 'org/project2'],
224 [x.name for x in tenant.untrusted_projects])
225 project = tenant.config_projects[0]
226 tpc = tenant.project_configs[project.canonical_name]
227 self.assertEqual(self.CONFIG_SET, tpc.load_classes)
228 project = tenant.untrusted_projects[0]
229 tpc = tenant.project_configs[project.canonical_name]
230 self.assertEqual(set([]),
231 tpc.load_classes)
232 project = tenant.untrusted_projects[1]
233 tpc = tenant.project_configs[project.canonical_name]
234 self.assertEqual(set([]),
235 tpc.load_classes)
236 # Check that only one merger:cat job was requested
237 # org/project1 and org/project2 have an empty load_classes
238 cat_jobs = [job for job in self.gearman_server.jobs_history
239 if job.name == b'merger:cat']
240 self.assertEqual(1, len(cat_jobs))
241
242
Tobias Henkeleca46202017-08-02 20:27:10 +0200243class TestTenantUnprotectedBranches(TenantParserTestCase):
244 tenant_config_file = 'config/tenant-parser/unprotected-branches.yaml'
245
246 def test_tenant_unprotected_branches(self):
247 tenant = self.sched.abide.tenants.get('tenant-one')
248 self.assertEqual(True, tenant.exclude_unprotected_branches)
249
250 self.assertEqual(['common-config'],
251 [x.name for x in tenant.config_projects])
252 self.assertEqual(['org/project1', 'org/project2'],
253 [x.name for x in tenant.untrusted_projects])
254
255 tpc = tenant.project_configs
256 project_name = tenant.config_projects[0].canonical_name
257 self.assertEqual(False, tpc[project_name].exclude_unprotected_branches)
258
259 project_name = tenant.untrusted_projects[0].canonical_name
260 self.assertIsNone(tpc[project_name].exclude_unprotected_branches)
261
262 project_name = tenant.untrusted_projects[1].canonical_name
263 self.assertIsNone(tpc[project_name].exclude_unprotected_branches)
264
265
Tristan Cacqueray829e6172017-06-13 06:49:36 +0000266class TestSplitConfig(ZuulTestCase):
267 tenant_config_file = 'config/split-config/main.yaml'
268
269 def setup_config(self):
270 super(TestSplitConfig, self).setup_config()
271
272 def test_split_config(self):
273 tenant = self.sched.abide.tenants.get('tenant-one')
274 self.assertIn('project-test1', tenant.layout.jobs)
275 project_config = tenant.layout.project_configs.get(
276 'review.example.com/org/project')
277 self.assertIn('project-test1',
278 project_config.pipelines['check'].job_list.jobs)
279 project1_config = tenant.layout.project_configs.get(
280 'review.example.com/org/project1')
281 self.assertIn('project1-project2-integration',
282 project1_config.pipelines['check'].job_list.jobs)
283
284 def test_dynamic_split_config(self):
285 in_repo_conf = textwrap.dedent(
286 """
287 - project:
288 name: org/project1
289 check:
290 jobs:
291 - project-test1
292 """)
293 file_dict = {'.zuul.d/gate.yaml': in_repo_conf}
294 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
295 files=file_dict)
296 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
297 self.waitUntilSettled()
298 # project1-project2-integration test removed, only want project-test1
299 self.assertHistory([
300 dict(name='project-test1', result='SUCCESS', changes='1,1')])
Tristan Cacqueray86883062017-07-11 06:00:07 +0000301
302 def test_config_path_conflict(self):
303 def add_file(project, path):
304 new_file = textwrap.dedent(
305 """
306 - job:
307 name: test-job
308 """
309 )
310 file_dict = {path: new_file}
311 A = self.fake_gerrit.addFakeChange(project, 'master', 'A',
312 files=file_dict)
313 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
314 self.waitUntilSettled()
315
316 log_fixture = self.useFixture(
317 fixtures.FakeLogger(level=logging.WARNING))
318
319 log_fixture._output.truncate(0)
320 add_file("common-config", "zuul.yaml")
321 self.assertIn("Multiple configuration", log_fixture.output)
322
323 log_fixture._output.truncate(0)
324 add_file("org/project1", ".zuul.yaml")
325 self.assertIn("Multiple configuration", log_fixture.output)
326
327
328class TestConfigConflict(ZuulTestCase):
329 tenant_config_file = 'config/conflict-config/main.yaml'
330
331 def test_conflict_config(self):
332 tenant = self.sched.abide.tenants.get('tenant-one')
333 jobs = sorted(tenant.layout.jobs.keys())
zhangyangyangc3e786f2017-09-13 10:47:52 +0800334 self.assertEqual(
James E. Blair2bab6e72017-08-07 09:52:45 -0700335 ['base', 'noop', 'trusted-zuul.yaml-job',
336 'untrusted-zuul.yaml-job'],
Tristan Cacqueray86883062017-07-11 06:00:07 +0000337 jobs)