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') |
| 185 | self.assertEqual(['common-config'], |
| 186 | [x.name for x in tenant.config_projects]) |
| 187 | self.assertEqual(['org/project1', 'org/project2'], |
| 188 | [x.name for x in tenant.untrusted_projects]) |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 189 | project = tenant.config_projects[0] |
| 190 | tpc = tenant.project_configs[project.canonical_name] |
| 191 | self.assertEqual(self.CONFIG_SET, tpc.load_classes) |
| 192 | project = tenant.untrusted_projects[0] |
| 193 | tpc = tenant.project_configs[project.canonical_name] |
| 194 | self.assertEqual(set(['job']), tpc.load_classes) |
| 195 | project = tenant.untrusted_projects[1] |
| 196 | tpc = tenant.project_configs[project.canonical_name] |
| 197 | self.assertEqual(set(['project', 'job']), tpc.load_classes) |
James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 198 | self.assertTrue('common-config-job' in tenant.layout.jobs) |
| 199 | self.assertTrue('project1-job' in tenant.layout.jobs) |
| 200 | self.assertTrue('project2-job' in tenant.layout.jobs) |
| 201 | project1_config = tenant.layout.project_configs.get( |
| 202 | 'review.example.com/org/project1') |
| 203 | self.assertTrue('common-config-job' in |
| 204 | project1_config.pipelines['check'].job_list.jobs) |
| 205 | self.assertFalse('project1-job' in |
| 206 | project1_config.pipelines['check'].job_list.jobs) |
| 207 | project2_config = tenant.layout.project_configs.get( |
| 208 | 'review.example.com/org/project2') |
| 209 | self.assertTrue('common-config-job' in |
| 210 | project2_config.pipelines['check'].job_list.jobs) |
| 211 | self.assertTrue('project2-job' in |
| 212 | project2_config.pipelines['check'].job_list.jobs) |
Tristan Cacqueray | 829e617 | 2017-06-13 06:49:36 +0000 | [diff] [blame] | 213 | |
| 214 | |
| 215 | class TestSplitConfig(ZuulTestCase): |
| 216 | tenant_config_file = 'config/split-config/main.yaml' |
| 217 | |
| 218 | def setup_config(self): |
| 219 | super(TestSplitConfig, self).setup_config() |
| 220 | |
| 221 | def test_split_config(self): |
| 222 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 223 | self.assertIn('project-test1', tenant.layout.jobs) |
| 224 | project_config = tenant.layout.project_configs.get( |
| 225 | 'review.example.com/org/project') |
| 226 | self.assertIn('project-test1', |
| 227 | project_config.pipelines['check'].job_list.jobs) |
| 228 | project1_config = tenant.layout.project_configs.get( |
| 229 | 'review.example.com/org/project1') |
| 230 | self.assertIn('project1-project2-integration', |
| 231 | project1_config.pipelines['check'].job_list.jobs) |
| 232 | |
| 233 | def test_dynamic_split_config(self): |
| 234 | in_repo_conf = textwrap.dedent( |
| 235 | """ |
| 236 | - project: |
| 237 | name: org/project1 |
| 238 | check: |
| 239 | jobs: |
| 240 | - project-test1 |
| 241 | """) |
| 242 | file_dict = {'.zuul.d/gate.yaml': in_repo_conf} |
| 243 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A', |
| 244 | files=file_dict) |
| 245 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 246 | self.waitUntilSettled() |
| 247 | # project1-project2-integration test removed, only want project-test1 |
| 248 | self.assertHistory([ |
| 249 | dict(name='project-test1', result='SUCCESS', changes='1,1')]) |
Tristan Cacqueray | 8688306 | 2017-07-11 06:00:07 +0000 | [diff] [blame^] | 250 | |
| 251 | def test_config_path_conflict(self): |
| 252 | def add_file(project, path): |
| 253 | new_file = textwrap.dedent( |
| 254 | """ |
| 255 | - job: |
| 256 | name: test-job |
| 257 | """ |
| 258 | ) |
| 259 | file_dict = {path: new_file} |
| 260 | A = self.fake_gerrit.addFakeChange(project, 'master', 'A', |
| 261 | files=file_dict) |
| 262 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 263 | self.waitUntilSettled() |
| 264 | |
| 265 | log_fixture = self.useFixture( |
| 266 | fixtures.FakeLogger(level=logging.WARNING)) |
| 267 | |
| 268 | log_fixture._output.truncate(0) |
| 269 | add_file("common-config", "zuul.yaml") |
| 270 | self.assertIn("Multiple configuration", log_fixture.output) |
| 271 | |
| 272 | log_fixture._output.truncate(0) |
| 273 | add_file("org/project1", ".zuul.yaml") |
| 274 | self.assertIn("Multiple configuration", log_fixture.output) |
| 275 | |
| 276 | |
| 277 | class TestConfigConflict(ZuulTestCase): |
| 278 | tenant_config_file = 'config/conflict-config/main.yaml' |
| 279 | |
| 280 | def test_conflict_config(self): |
| 281 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 282 | jobs = sorted(tenant.layout.jobs.keys()) |
| 283 | self.assertEquals( |
| 284 | ['noop', 'trusted-zuul.yaml-job', 'untrusted-zuul.yaml-job'], |
| 285 | jobs) |