James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 1 | # 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 Cacqueray | 8688306 | 2017-07-11 06:00:07 +0000 | [diff] [blame] | 15 | import fixtures |
| 16 | import logging |
Tristan Cacqueray | 829e617 | 2017-06-13 06:49:36 +0000 | [diff] [blame] | 17 | import textwrap |
James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 18 | |
| 19 | from tests.base import ZuulTestCase |
| 20 | |
| 21 | |
| 22 | class 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 | |
| 34 | class 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. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 43 | |
| 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. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 53 | 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 | |
| 70 | class 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. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 79 | 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. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 84 | self.assertEqual(self.UNTRUSTED_SET - set(['project']), |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 85 | 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. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 89 | 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 | |
| 106 | class 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. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 115 | 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. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 120 | self.assertEqual(self.UNTRUSTED_SET - set(['project']), |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 121 | tpc.load_classes) |
| 122 | project = tenant.untrusted_projects[1] |
| 123 | tpc = tenant.project_configs[project.canonical_name] |
James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 124 | self.assertEqual(self.UNTRUSTED_SET - set(['project']), |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 125 | tpc.load_classes) |
James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 126 | 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 | |
| 143 | class 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. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 152 | 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. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 157 | self.assertEqual(self.UNTRUSTED_SET - set(['project']), |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 158 | tpc.load_classes) |
| 159 | project = tenant.untrusted_projects[1] |
| 160 | tpc = tenant.project_configs[project.canonical_name] |
James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 161 | self.assertEqual(self.UNTRUSTED_SET - set(['project', 'job']), |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 162 | tpc.load_classes) |
James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 163 | 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 | |
| 180 | class 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 Henkel | eca4620 | 2017-08-02 20:27:10 +0200 | [diff] [blame] | 185 | self.assertEqual(False, tenant.exclude_unprotected_branches) |
James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 186 | 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. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 190 | 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. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 199 | 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 Cacqueray | 829e617 | 2017-06-13 06:49:36 +0000 | [diff] [blame] | 214 | |
| 215 | |
Tobias Henkel | eca4620 | 2017-08-02 20:27:10 +0200 | [diff] [blame] | 216 | class 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 Cacqueray | 829e617 | 2017-06-13 06:49:36 +0000 | [diff] [blame] | 239 | class 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 Cacqueray | 8688306 | 2017-07-11 06:00:07 +0000 | [diff] [blame] | 274 | |
| 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 | |
| 301 | class 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( |
| 308 | ['noop', 'trusted-zuul.yaml-job', 'untrusted-zuul.yaml-job'], |
| 309 | jobs) |