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 | 829e617 | 2017-06-13 06:49:36 +0000 | [diff] [blame] | 15 | import textwrap |
James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 16 | |
| 17 | from tests.base import ZuulTestCase |
| 18 | |
| 19 | |
| 20 | class TenantParserTestCase(ZuulTestCase): |
| 21 | create_project_keys = True |
| 22 | |
| 23 | CONFIG_SET = set(['pipeline', 'job', 'semaphore', 'project', |
| 24 | 'project-template', 'nodeset', 'secret']) |
| 25 | UNTRUSTED_SET = CONFIG_SET - set(['pipeline']) |
| 26 | |
| 27 | def setupAllProjectKeys(self): |
| 28 | for project in ['common-config', 'org/project1', 'org/project2']: |
| 29 | self.setupProjectKeys('gerrit', project) |
| 30 | |
| 31 | |
| 32 | class TestTenantSimple(TenantParserTestCase): |
| 33 | tenant_config_file = 'config/tenant-parser/simple.yaml' |
| 34 | |
| 35 | def test_tenant_simple(self): |
| 36 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 37 | self.assertEqual(['common-config'], |
| 38 | [x.name for x in tenant.config_projects]) |
| 39 | self.assertEqual(['org/project1', 'org/project2'], |
| 40 | [x.name for x in tenant.untrusted_projects]) |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 41 | |
| 42 | project = tenant.config_projects[0] |
| 43 | tpc = tenant.project_configs[project.canonical_name] |
| 44 | self.assertEqual(self.CONFIG_SET, tpc.load_classes) |
| 45 | project = tenant.untrusted_projects[0] |
| 46 | tpc = tenant.project_configs[project.canonical_name] |
| 47 | self.assertEqual(self.UNTRUSTED_SET, tpc.load_classes) |
| 48 | project = tenant.untrusted_projects[1] |
| 49 | tpc = tenant.project_configs[project.canonical_name] |
| 50 | self.assertEqual(self.UNTRUSTED_SET, tpc.load_classes) |
James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 51 | self.assertTrue('common-config-job' in tenant.layout.jobs) |
| 52 | self.assertTrue('project1-job' in tenant.layout.jobs) |
| 53 | self.assertTrue('project2-job' in tenant.layout.jobs) |
| 54 | project1_config = tenant.layout.project_configs.get( |
| 55 | 'review.example.com/org/project1') |
| 56 | self.assertTrue('common-config-job' in |
| 57 | project1_config.pipelines['check'].job_list.jobs) |
| 58 | self.assertTrue('project1-job' in |
| 59 | project1_config.pipelines['check'].job_list.jobs) |
| 60 | project2_config = tenant.layout.project_configs.get( |
| 61 | 'review.example.com/org/project2') |
| 62 | self.assertTrue('common-config-job' in |
| 63 | project2_config.pipelines['check'].job_list.jobs) |
| 64 | self.assertTrue('project2-job' in |
| 65 | project2_config.pipelines['check'].job_list.jobs) |
| 66 | |
| 67 | |
| 68 | class TestTenantOverride(TenantParserTestCase): |
| 69 | tenant_config_file = 'config/tenant-parser/override.yaml' |
| 70 | |
| 71 | def test_tenant_override(self): |
| 72 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 73 | self.assertEqual(['common-config'], |
| 74 | [x.name for x in tenant.config_projects]) |
| 75 | self.assertEqual(['org/project1', 'org/project2'], |
| 76 | [x.name for x in tenant.untrusted_projects]) |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 77 | project = tenant.config_projects[0] |
| 78 | tpc = tenant.project_configs[project.canonical_name] |
| 79 | self.assertEqual(self.CONFIG_SET, tpc.load_classes) |
| 80 | project = tenant.untrusted_projects[0] |
| 81 | tpc = tenant.project_configs[project.canonical_name] |
James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 82 | self.assertEqual(self.UNTRUSTED_SET - set(['project']), |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 83 | tpc.load_classes) |
| 84 | project = tenant.untrusted_projects[1] |
| 85 | tpc = tenant.project_configs[project.canonical_name] |
| 86 | self.assertEqual(set(['job']), tpc.load_classes) |
James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 87 | self.assertTrue('common-config-job' in tenant.layout.jobs) |
| 88 | self.assertTrue('project1-job' in tenant.layout.jobs) |
| 89 | self.assertTrue('project2-job' in tenant.layout.jobs) |
| 90 | project1_config = tenant.layout.project_configs.get( |
| 91 | 'review.example.com/org/project1') |
| 92 | self.assertTrue('common-config-job' in |
| 93 | project1_config.pipelines['check'].job_list.jobs) |
| 94 | self.assertFalse('project1-job' in |
| 95 | project1_config.pipelines['check'].job_list.jobs) |
| 96 | project2_config = tenant.layout.project_configs.get( |
| 97 | 'review.example.com/org/project2') |
| 98 | self.assertTrue('common-config-job' in |
| 99 | project2_config.pipelines['check'].job_list.jobs) |
| 100 | self.assertFalse('project2-job' in |
| 101 | project2_config.pipelines['check'].job_list.jobs) |
| 102 | |
| 103 | |
| 104 | class TestTenantGroups(TenantParserTestCase): |
| 105 | tenant_config_file = 'config/tenant-parser/groups.yaml' |
| 106 | |
| 107 | def test_tenant_groups(self): |
| 108 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 109 | self.assertEqual(['common-config'], |
| 110 | [x.name for x in tenant.config_projects]) |
| 111 | self.assertEqual(['org/project1', 'org/project2'], |
| 112 | [x.name for x in tenant.untrusted_projects]) |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 113 | project = tenant.config_projects[0] |
| 114 | tpc = tenant.project_configs[project.canonical_name] |
| 115 | self.assertEqual(self.CONFIG_SET, tpc.load_classes) |
| 116 | project = tenant.untrusted_projects[0] |
| 117 | tpc = tenant.project_configs[project.canonical_name] |
James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 118 | self.assertEqual(self.UNTRUSTED_SET - set(['project']), |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 119 | tpc.load_classes) |
| 120 | project = tenant.untrusted_projects[1] |
| 121 | tpc = tenant.project_configs[project.canonical_name] |
James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 122 | self.assertEqual(self.UNTRUSTED_SET - set(['project']), |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 123 | tpc.load_classes) |
James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 124 | self.assertTrue('common-config-job' in tenant.layout.jobs) |
| 125 | self.assertTrue('project1-job' in tenant.layout.jobs) |
| 126 | self.assertTrue('project2-job' in tenant.layout.jobs) |
| 127 | project1_config = tenant.layout.project_configs.get( |
| 128 | 'review.example.com/org/project1') |
| 129 | self.assertTrue('common-config-job' in |
| 130 | project1_config.pipelines['check'].job_list.jobs) |
| 131 | self.assertFalse('project1-job' in |
| 132 | project1_config.pipelines['check'].job_list.jobs) |
| 133 | project2_config = tenant.layout.project_configs.get( |
| 134 | 'review.example.com/org/project2') |
| 135 | self.assertTrue('common-config-job' in |
| 136 | project2_config.pipelines['check'].job_list.jobs) |
| 137 | self.assertFalse('project2-job' in |
| 138 | project2_config.pipelines['check'].job_list.jobs) |
| 139 | |
| 140 | |
| 141 | class TestTenantGroups2(TenantParserTestCase): |
| 142 | tenant_config_file = 'config/tenant-parser/groups2.yaml' |
| 143 | |
| 144 | def test_tenant_groups2(self): |
| 145 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 146 | self.assertEqual(['common-config'], |
| 147 | [x.name for x in tenant.config_projects]) |
| 148 | self.assertEqual(['org/project1', 'org/project2'], |
| 149 | [x.name for x in tenant.untrusted_projects]) |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 150 | project = tenant.config_projects[0] |
| 151 | tpc = tenant.project_configs[project.canonical_name] |
| 152 | self.assertEqual(self.CONFIG_SET, tpc.load_classes) |
| 153 | project = tenant.untrusted_projects[0] |
| 154 | tpc = tenant.project_configs[project.canonical_name] |
James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 155 | self.assertEqual(self.UNTRUSTED_SET - set(['project']), |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 156 | tpc.load_classes) |
| 157 | project = tenant.untrusted_projects[1] |
| 158 | tpc = tenant.project_configs[project.canonical_name] |
James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 159 | self.assertEqual(self.UNTRUSTED_SET - set(['project', 'job']), |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 160 | tpc.load_classes) |
James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 161 | self.assertTrue('common-config-job' in tenant.layout.jobs) |
| 162 | self.assertTrue('project1-job' in tenant.layout.jobs) |
| 163 | self.assertFalse('project2-job' in tenant.layout.jobs) |
| 164 | project1_config = tenant.layout.project_configs.get( |
| 165 | 'review.example.com/org/project1') |
| 166 | self.assertTrue('common-config-job' in |
| 167 | project1_config.pipelines['check'].job_list.jobs) |
| 168 | self.assertFalse('project1-job' in |
| 169 | project1_config.pipelines['check'].job_list.jobs) |
| 170 | project2_config = tenant.layout.project_configs.get( |
| 171 | 'review.example.com/org/project2') |
| 172 | self.assertTrue('common-config-job' in |
| 173 | project2_config.pipelines['check'].job_list.jobs) |
| 174 | self.assertFalse('project2-job' in |
| 175 | project2_config.pipelines['check'].job_list.jobs) |
| 176 | |
| 177 | |
| 178 | class TestTenantGroups3(TenantParserTestCase): |
| 179 | tenant_config_file = 'config/tenant-parser/groups3.yaml' |
| 180 | |
| 181 | def test_tenant_groups3(self): |
| 182 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 183 | self.assertEqual(['common-config'], |
| 184 | [x.name for x in tenant.config_projects]) |
| 185 | self.assertEqual(['org/project1', 'org/project2'], |
| 186 | [x.name for x in tenant.untrusted_projects]) |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 187 | project = tenant.config_projects[0] |
| 188 | tpc = tenant.project_configs[project.canonical_name] |
| 189 | self.assertEqual(self.CONFIG_SET, tpc.load_classes) |
| 190 | project = tenant.untrusted_projects[0] |
| 191 | tpc = tenant.project_configs[project.canonical_name] |
| 192 | self.assertEqual(set(['job']), tpc.load_classes) |
| 193 | project = tenant.untrusted_projects[1] |
| 194 | tpc = tenant.project_configs[project.canonical_name] |
| 195 | self.assertEqual(set(['project', 'job']), tpc.load_classes) |
James E. Blair | ac180ce | 2017-06-07 19:31:02 -0700 | [diff] [blame] | 196 | self.assertTrue('common-config-job' in tenant.layout.jobs) |
| 197 | self.assertTrue('project1-job' in tenant.layout.jobs) |
| 198 | self.assertTrue('project2-job' in tenant.layout.jobs) |
| 199 | project1_config = tenant.layout.project_configs.get( |
| 200 | 'review.example.com/org/project1') |
| 201 | self.assertTrue('common-config-job' in |
| 202 | project1_config.pipelines['check'].job_list.jobs) |
| 203 | self.assertFalse('project1-job' in |
| 204 | project1_config.pipelines['check'].job_list.jobs) |
| 205 | project2_config = tenant.layout.project_configs.get( |
| 206 | 'review.example.com/org/project2') |
| 207 | self.assertTrue('common-config-job' in |
| 208 | project2_config.pipelines['check'].job_list.jobs) |
| 209 | self.assertTrue('project2-job' in |
| 210 | project2_config.pipelines['check'].job_list.jobs) |
Tristan Cacqueray | 829e617 | 2017-06-13 06:49:36 +0000 | [diff] [blame] | 211 | |
| 212 | |
| 213 | class TestSplitConfig(ZuulTestCase): |
| 214 | tenant_config_file = 'config/split-config/main.yaml' |
| 215 | |
| 216 | def setup_config(self): |
| 217 | super(TestSplitConfig, self).setup_config() |
| 218 | |
| 219 | def test_split_config(self): |
| 220 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 221 | self.assertIn('project-test1', tenant.layout.jobs) |
| 222 | project_config = tenant.layout.project_configs.get( |
| 223 | 'review.example.com/org/project') |
| 224 | self.assertIn('project-test1', |
| 225 | project_config.pipelines['check'].job_list.jobs) |
| 226 | project1_config = tenant.layout.project_configs.get( |
| 227 | 'review.example.com/org/project1') |
| 228 | self.assertIn('project1-project2-integration', |
| 229 | project1_config.pipelines['check'].job_list.jobs) |
| 230 | |
| 231 | def test_dynamic_split_config(self): |
| 232 | in_repo_conf = textwrap.dedent( |
| 233 | """ |
| 234 | - project: |
| 235 | name: org/project1 |
| 236 | check: |
| 237 | jobs: |
| 238 | - project-test1 |
| 239 | """) |
| 240 | file_dict = {'.zuul.d/gate.yaml': in_repo_conf} |
| 241 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A', |
| 242 | files=file_dict) |
| 243 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 244 | self.waitUntilSettled() |
| 245 | # project1-project2-integration test removed, only want project-test1 |
| 246 | self.assertHistory([ |
| 247 | dict(name='project-test1', result='SUCCESS', changes='1,1')]) |