James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright 2012 Hewlett-Packard Development Company, L.P. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
James E. Blair | a92cbc8 | 2017-01-23 14:56:49 -0800 | [diff] [blame] | 17 | import os |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 18 | import textwrap |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 19 | |
James E. Blair | b9c0d77 | 2017-03-03 14:34:49 -0800 | [diff] [blame] | 20 | import testtools |
| 21 | |
| 22 | import zuul.configloader |
James E. Blair | bf1a4f2 | 2017-03-17 10:59:37 -0700 | [diff] [blame] | 23 | from zuul.lib import encryption |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 24 | from tests.base import AnsibleZuulTestCase, ZuulTestCase, FIXTURE_DIR |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 25 | |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 26 | |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 27 | class TestMultipleTenants(AnsibleZuulTestCase): |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 28 | # A temporary class to hold new tests while others are disabled |
| 29 | |
James E. Blair | 2a629ec | 2015-12-22 15:32:02 -0800 | [diff] [blame] | 30 | tenant_config_file = 'config/multi-tenant/main.yaml' |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 31 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 32 | def test_multiple_tenants(self): |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 33 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [diff] [blame] | 34 | A.addApproval('code-review', 2) |
| 35 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 36 | self.waitUntilSettled() |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 37 | self.assertEqual(self.getJobFromHistory('project1-test1').result, |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 38 | 'SUCCESS') |
James E. Blair | 96c6bf8 | 2016-01-15 16:20:40 -0800 | [diff] [blame] | 39 | self.assertEqual(self.getJobFromHistory('python27').result, |
| 40 | 'SUCCESS') |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 41 | self.assertEqual(A.data['status'], 'MERGED') |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 42 | self.assertEqual(A.reported, 2, |
| 43 | "A should report start and success") |
| 44 | self.assertIn('tenant-one-gate', A.messages[1], |
| 45 | "A should transit tenant-one gate") |
| 46 | self.assertNotIn('tenant-two-gate', A.messages[1], |
| 47 | "A should *not* transit tenant-two gate") |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 48 | |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 49 | B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B') |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [diff] [blame] | 50 | B.addApproval('code-review', 2) |
| 51 | self.fake_gerrit.addEvent(B.addApproval('approved', 1)) |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 52 | self.waitUntilSettled() |
James E. Blair | 96c6bf8 | 2016-01-15 16:20:40 -0800 | [diff] [blame] | 53 | self.assertEqual(self.getJobFromHistory('python27', |
| 54 | 'org/project2').result, |
| 55 | 'SUCCESS') |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 56 | self.assertEqual(self.getJobFromHistory('project2-test1').result, |
| 57 | 'SUCCESS') |
| 58 | self.assertEqual(B.data['status'], 'MERGED') |
| 59 | self.assertEqual(B.reported, 2, |
| 60 | "B should report start and success") |
| 61 | self.assertIn('tenant-two-gate', B.messages[1], |
| 62 | "B should transit tenant-two gate") |
| 63 | self.assertNotIn('tenant-one-gate', B.messages[1], |
| 64 | "B should *not* transit tenant-one gate") |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 65 | |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 66 | self.assertEqual(A.reported, 2, "Activity in tenant two should" |
| 67 | "not affect tenant one") |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 68 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 69 | |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 70 | class TestInRepoConfig(ZuulTestCase): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 71 | # A temporary class to hold new tests while others are disabled |
| 72 | |
James E. Blair | 2a629ec | 2015-12-22 15:32:02 -0800 | [diff] [blame] | 73 | tenant_config_file = 'config/in-repo/main.yaml' |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 74 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 75 | def test_in_repo_config(self): |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 76 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [diff] [blame] | 77 | A.addApproval('code-review', 2) |
| 78 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 79 | self.waitUntilSettled() |
| 80 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 81 | 'SUCCESS') |
| 82 | self.assertEqual(A.data['status'], 'MERGED') |
| 83 | self.assertEqual(A.reported, 2, |
| 84 | "A should report start and success") |
| 85 | self.assertIn('tenant-one-gate', A.messages[1], |
| 86 | "A should transit tenant-one gate") |
James E. Blair | b97ed80 | 2015-12-21 15:55:35 -0800 | [diff] [blame] | 87 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 88 | def test_dynamic_config(self): |
| 89 | in_repo_conf = textwrap.dedent( |
| 90 | """ |
| 91 | - job: |
| 92 | name: project-test2 |
| 93 | |
| 94 | - project: |
| 95 | name: org/project |
| 96 | tenant-one-gate: |
| 97 | jobs: |
| 98 | - project-test2 |
| 99 | """) |
| 100 | |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 101 | in_repo_playbook = textwrap.dedent( |
| 102 | """ |
| 103 | - hosts: all |
| 104 | tasks: [] |
| 105 | """) |
| 106 | |
| 107 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 108 | 'playbooks/project-test2.yaml': in_repo_playbook} |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 109 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 110 | files=file_dict) |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [diff] [blame] | 111 | A.addApproval('code-review', 2) |
| 112 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 113 | self.waitUntilSettled() |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 114 | self.assertEqual(A.data['status'], 'MERGED') |
| 115 | self.assertEqual(A.reported, 2, |
| 116 | "A should report start and success") |
| 117 | self.assertIn('tenant-one-gate', A.messages[1], |
| 118 | "A should transit tenant-one gate") |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 119 | self.assertHistory([ |
| 120 | dict(name='project-test2', result='SUCCESS', changes='1,1')]) |
| 121 | |
James E. Blair | c2a5ed7 | 2017-02-20 14:12:01 -0500 | [diff] [blame] | 122 | self.fake_gerrit.addEvent(A.getChangeMergedEvent()) |
James E. Blair | 7bbd7a3 | 2017-03-06 11:36:13 -0800 | [diff] [blame] | 123 | self.waitUntilSettled() |
James E. Blair | c2a5ed7 | 2017-02-20 14:12:01 -0500 | [diff] [blame] | 124 | |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 125 | # Now that the config change is landed, it should be live for |
| 126 | # subsequent changes. |
| 127 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 128 | B.addApproval('code-review', 2) |
| 129 | self.fake_gerrit.addEvent(B.addApproval('approved', 1)) |
| 130 | self.waitUntilSettled() |
| 131 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 132 | 'SUCCESS') |
| 133 | self.assertHistory([ |
| 134 | dict(name='project-test2', result='SUCCESS', changes='1,1'), |
| 135 | dict(name='project-test2', result='SUCCESS', changes='2,1')]) |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 136 | |
Tobias Henkel | 0f71400 | 2017-06-30 23:30:52 +0200 | [diff] [blame] | 137 | def test_dynamic_config_new_patchset(self): |
| 138 | self.executor_server.hold_jobs_in_build = True |
| 139 | |
| 140 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 141 | check_pipeline = tenant.layout.pipelines['check'] |
| 142 | |
| 143 | in_repo_conf = textwrap.dedent( |
| 144 | """ |
| 145 | - job: |
| 146 | name: project-test2 |
| 147 | |
| 148 | - project: |
| 149 | name: org/project |
| 150 | check: |
| 151 | jobs: |
| 152 | - project-test2 |
| 153 | """) |
| 154 | |
| 155 | in_repo_playbook = textwrap.dedent( |
| 156 | """ |
| 157 | - hosts: all |
| 158 | tasks: [] |
| 159 | """) |
| 160 | |
| 161 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 162 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 163 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 164 | files=file_dict) |
| 165 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 166 | self.waitUntilSettled() |
| 167 | |
| 168 | items = check_pipeline.getAllItems() |
| 169 | self.assertEqual(items[0].change.number, '1') |
| 170 | self.assertEqual(items[0].change.patchset, '1') |
| 171 | self.assertTrue(items[0].live) |
| 172 | |
| 173 | in_repo_conf = textwrap.dedent( |
| 174 | """ |
| 175 | - job: |
| 176 | name: project-test2 |
| 177 | |
| 178 | - project: |
| 179 | name: org/project |
| 180 | check: |
| 181 | jobs: |
| 182 | - project-test1 |
| 183 | - project-test2 |
| 184 | """) |
| 185 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 186 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 187 | |
| 188 | A.addPatchset(files=file_dict) |
| 189 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2)) |
| 190 | |
| 191 | self.waitUntilSettled() |
| 192 | |
| 193 | items = check_pipeline.getAllItems() |
| 194 | self.assertEqual(items[0].change.number, '1') |
| 195 | self.assertEqual(items[0].change.patchset, '2') |
| 196 | self.assertTrue(items[0].live) |
| 197 | |
| 198 | self.executor_server.hold_jobs_in_build = False |
| 199 | self.executor_server.release() |
| 200 | self.waitUntilSettled() |
| 201 | |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 202 | def test_in_repo_branch(self): |
| 203 | in_repo_conf = textwrap.dedent( |
| 204 | """ |
| 205 | - job: |
| 206 | name: project-test2 |
| 207 | |
| 208 | - project: |
| 209 | name: org/project |
| 210 | tenant-one-gate: |
| 211 | jobs: |
| 212 | - project-test2 |
| 213 | """) |
| 214 | |
| 215 | in_repo_playbook = textwrap.dedent( |
| 216 | """ |
| 217 | - hosts: all |
| 218 | tasks: [] |
| 219 | """) |
| 220 | |
| 221 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 222 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 223 | self.create_branch('org/project', 'stable') |
| 224 | A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A', |
| 225 | files=file_dict) |
| 226 | A.addApproval('code-review', 2) |
| 227 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
| 228 | self.waitUntilSettled() |
| 229 | self.assertEqual(A.data['status'], 'MERGED') |
| 230 | self.assertEqual(A.reported, 2, |
| 231 | "A should report start and success") |
| 232 | self.assertIn('tenant-one-gate', A.messages[1], |
| 233 | "A should transit tenant-one gate") |
| 234 | self.assertHistory([ |
| 235 | dict(name='project-test2', result='SUCCESS', changes='1,1')]) |
| 236 | self.fake_gerrit.addEvent(A.getChangeMergedEvent()) |
James E. Blair | 7bbd7a3 | 2017-03-06 11:36:13 -0800 | [diff] [blame] | 237 | self.waitUntilSettled() |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 238 | |
| 239 | # The config change should not affect master. |
| 240 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 241 | B.addApproval('code-review', 2) |
| 242 | self.fake_gerrit.addEvent(B.addApproval('approved', 1)) |
| 243 | self.waitUntilSettled() |
| 244 | self.assertHistory([ |
| 245 | dict(name='project-test2', result='SUCCESS', changes='1,1'), |
| 246 | dict(name='project-test1', result='SUCCESS', changes='2,1')]) |
| 247 | |
| 248 | # The config change should be live for further changes on |
| 249 | # stable. |
| 250 | C = self.fake_gerrit.addFakeChange('org/project', 'stable', 'C') |
| 251 | C.addApproval('code-review', 2) |
| 252 | self.fake_gerrit.addEvent(C.addApproval('approved', 1)) |
| 253 | self.waitUntilSettled() |
| 254 | self.assertHistory([ |
| 255 | dict(name='project-test2', result='SUCCESS', changes='1,1'), |
| 256 | dict(name='project-test1', result='SUCCESS', changes='2,1'), |
| 257 | dict(name='project-test2', result='SUCCESS', changes='3,1')]) |
| 258 | |
James E. Blair | a5a1249 | 2017-05-03 11:40:48 -0700 | [diff] [blame] | 259 | def test_crd_dynamic_config_branch(self): |
| 260 | # Test that we can create a job in one repo and be able to use |
| 261 | # it from a different branch on a different repo. |
| 262 | |
| 263 | self.create_branch('org/project1', 'stable') |
| 264 | |
| 265 | in_repo_conf = textwrap.dedent( |
| 266 | """ |
| 267 | - job: |
| 268 | name: project-test2 |
| 269 | |
| 270 | - project: |
| 271 | name: org/project |
| 272 | check: |
| 273 | jobs: |
| 274 | - project-test2 |
| 275 | """) |
| 276 | |
| 277 | in_repo_playbook = textwrap.dedent( |
| 278 | """ |
| 279 | - hosts: all |
| 280 | tasks: [] |
| 281 | """) |
| 282 | |
| 283 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 284 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 285 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 286 | files=file_dict) |
| 287 | |
| 288 | second_repo_conf = textwrap.dedent( |
| 289 | """ |
| 290 | - project: |
| 291 | name: org/project1 |
| 292 | check: |
| 293 | jobs: |
| 294 | - project-test2 |
| 295 | """) |
| 296 | |
| 297 | second_file_dict = {'.zuul.yaml': second_repo_conf} |
| 298 | B = self.fake_gerrit.addFakeChange('org/project1', 'stable', 'B', |
| 299 | files=second_file_dict) |
| 300 | B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % ( |
| 301 | B.subject, A.data['id']) |
| 302 | |
| 303 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 304 | self.waitUntilSettled() |
| 305 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
| 306 | self.waitUntilSettled() |
| 307 | |
| 308 | self.assertEqual(A.reported, 1, "A should report") |
| 309 | self.assertHistory([ |
| 310 | dict(name='project-test2', result='SUCCESS', changes='1,1'), |
| 311 | dict(name='project-test2', result='SUCCESS', changes='1,1 2,1'), |
| 312 | ]) |
| 313 | |
James E. Blair | 149b69c | 2017-03-02 10:48:16 -0800 | [diff] [blame] | 314 | def test_untrusted_syntax_error(self): |
James E. Blair | e53250c | 2017-03-01 14:34:36 -0800 | [diff] [blame] | 315 | in_repo_conf = textwrap.dedent( |
| 316 | """ |
| 317 | - job: |
| 318 | name: project-test2 |
| 319 | foo: error |
| 320 | """) |
| 321 | |
| 322 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 323 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 324 | files=file_dict) |
| 325 | A.addApproval('code-review', 2) |
| 326 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
| 327 | self.waitUntilSettled() |
| 328 | |
| 329 | self.assertEqual(A.data['status'], 'NEW') |
Tobias Henkel | 9842bd7 | 2017-05-16 13:40:03 +0200 | [diff] [blame] | 330 | self.assertEqual(A.reported, 1, |
| 331 | "A should report failure") |
| 332 | self.assertIn('syntax error', A.messages[0], |
James E. Blair | e53250c | 2017-03-01 14:34:36 -0800 | [diff] [blame] | 333 | "A should have a syntax error reported") |
| 334 | |
James E. Blair | 149b69c | 2017-03-02 10:48:16 -0800 | [diff] [blame] | 335 | def test_trusted_syntax_error(self): |
| 336 | in_repo_conf = textwrap.dedent( |
| 337 | """ |
| 338 | - job: |
| 339 | name: project-test2 |
| 340 | foo: error |
| 341 | """) |
| 342 | |
| 343 | file_dict = {'zuul.yaml': in_repo_conf} |
| 344 | A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A', |
| 345 | files=file_dict) |
| 346 | A.addApproval('code-review', 2) |
| 347 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
| 348 | self.waitUntilSettled() |
| 349 | |
| 350 | self.assertEqual(A.data['status'], 'NEW') |
Tobias Henkel | 9842bd7 | 2017-05-16 13:40:03 +0200 | [diff] [blame] | 351 | self.assertEqual(A.reported, 1, |
| 352 | "A should report failure") |
| 353 | self.assertIn('syntax error', A.messages[0], |
James E. Blair | 149b69c | 2017-03-02 10:48:16 -0800 | [diff] [blame] | 354 | "A should have a syntax error reported") |
| 355 | |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 356 | def test_untrusted_yaml_error(self): |
| 357 | in_repo_conf = textwrap.dedent( |
| 358 | """ |
| 359 | - job: |
| 360 | foo: error |
| 361 | """) |
| 362 | |
| 363 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 364 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 365 | files=file_dict) |
| 366 | A.addApproval('code-review', 2) |
| 367 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
| 368 | self.waitUntilSettled() |
| 369 | |
| 370 | self.assertEqual(A.data['status'], 'NEW') |
Tobias Henkel | 9842bd7 | 2017-05-16 13:40:03 +0200 | [diff] [blame] | 371 | self.assertEqual(A.reported, 1, |
| 372 | "A should report failure") |
| 373 | self.assertIn('syntax error', A.messages[0], |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 374 | "A should have a syntax error reported") |
| 375 | |
James E. Blair | db04e6a | 2017-05-03 14:49:36 -0700 | [diff] [blame] | 376 | def test_untrusted_shadow_error(self): |
| 377 | in_repo_conf = textwrap.dedent( |
| 378 | """ |
| 379 | - job: |
| 380 | name: common-config-test |
| 381 | """) |
| 382 | |
| 383 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 384 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 385 | files=file_dict) |
| 386 | A.addApproval('code-review', 2) |
| 387 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
| 388 | self.waitUntilSettled() |
| 389 | |
| 390 | self.assertEqual(A.data['status'], 'NEW') |
Tobias Henkel | 9842bd7 | 2017-05-16 13:40:03 +0200 | [diff] [blame] | 391 | self.assertEqual(A.reported, 1, |
| 392 | "A should report failure") |
| 393 | self.assertIn('not permitted to shadow', A.messages[0], |
James E. Blair | db04e6a | 2017-05-03 14:49:36 -0700 | [diff] [blame] | 394 | "A should have a syntax error reported") |
| 395 | |
James E. Blair | d5656ad | 2017-06-02 14:29:41 -0700 | [diff] [blame] | 396 | def test_untrusted_pipeline_error(self): |
| 397 | in_repo_conf = textwrap.dedent( |
| 398 | """ |
| 399 | - pipeline: |
| 400 | name: test |
| 401 | """) |
| 402 | |
| 403 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 404 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 405 | files=file_dict) |
| 406 | A.addApproval('code-review', 2) |
| 407 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
| 408 | self.waitUntilSettled() |
| 409 | |
| 410 | self.assertEqual(A.data['status'], 'NEW') |
| 411 | self.assertEqual(A.reported, 1, |
| 412 | "A should report failure") |
| 413 | self.assertIn('Pipelines may not be defined', A.messages[0], |
| 414 | "A should have a syntax error reported") |
| 415 | |
| 416 | def test_untrusted_project_error(self): |
| 417 | in_repo_conf = textwrap.dedent( |
| 418 | """ |
| 419 | - project: |
| 420 | name: org/project1 |
| 421 | """) |
| 422 | |
| 423 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 424 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 425 | files=file_dict) |
| 426 | A.addApproval('code-review', 2) |
| 427 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
| 428 | self.waitUntilSettled() |
| 429 | |
| 430 | self.assertEqual(A.data['status'], 'NEW') |
| 431 | self.assertEqual(A.reported, 1, |
| 432 | "A should report failure") |
| 433 | self.assertIn('the only project definition permitted', A.messages[0], |
| 434 | "A should have a syntax error reported") |
| 435 | |
James E. Blair | e64b0e4 | 2017-06-08 11:23:34 -0700 | [diff] [blame] | 436 | def test_duplicate_node_error(self): |
| 437 | in_repo_conf = textwrap.dedent( |
| 438 | """ |
| 439 | - nodeset: |
| 440 | name: duplicate |
| 441 | nodes: |
| 442 | - name: compute |
James E. Blair | 16d96a0 | 2017-06-08 11:32:56 -0700 | [diff] [blame] | 443 | label: foo |
James E. Blair | e64b0e4 | 2017-06-08 11:23:34 -0700 | [diff] [blame] | 444 | - name: compute |
James E. Blair | 16d96a0 | 2017-06-08 11:32:56 -0700 | [diff] [blame] | 445 | label: foo |
James E. Blair | e64b0e4 | 2017-06-08 11:23:34 -0700 | [diff] [blame] | 446 | """) |
| 447 | |
| 448 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 449 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 450 | files=file_dict) |
| 451 | A.addApproval('code-review', 2) |
| 452 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
| 453 | self.waitUntilSettled() |
| 454 | |
| 455 | self.assertEqual(A.data['status'], 'NEW') |
| 456 | self.assertEqual(A.reported, 1, |
| 457 | "A should report failure") |
| 458 | self.assertIn('appears multiple times', A.messages[0], |
| 459 | "A should have a syntax error reported") |
| 460 | |
| 461 | def test_duplicate_group_error(self): |
| 462 | in_repo_conf = textwrap.dedent( |
| 463 | """ |
| 464 | - nodeset: |
| 465 | name: duplicate |
| 466 | nodes: |
| 467 | - name: compute |
James E. Blair | 16d96a0 | 2017-06-08 11:32:56 -0700 | [diff] [blame] | 468 | label: foo |
James E. Blair | e64b0e4 | 2017-06-08 11:23:34 -0700 | [diff] [blame] | 469 | groups: |
| 470 | - name: group |
| 471 | nodes: compute |
| 472 | - name: group |
| 473 | nodes: compute |
| 474 | """) |
| 475 | |
| 476 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 477 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 478 | files=file_dict) |
| 479 | A.addApproval('code-review', 2) |
| 480 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
| 481 | self.waitUntilSettled() |
| 482 | |
| 483 | self.assertEqual(A.data['status'], 'NEW') |
| 484 | self.assertEqual(A.reported, 1, |
| 485 | "A should report failure") |
| 486 | self.assertIn('appears multiple times', A.messages[0], |
| 487 | "A should have a syntax error reported") |
| 488 | |
James E. Blair | 09f9ffe | 2017-07-11 15:30:25 -0700 | [diff] [blame] | 489 | def test_multi_repo(self): |
| 490 | downstream_repo_conf = textwrap.dedent( |
| 491 | """ |
| 492 | - project: |
| 493 | name: org/project1 |
| 494 | tenant-one-gate: |
| 495 | jobs: |
| 496 | - project-test1 |
| 497 | |
| 498 | - job: |
| 499 | name: project1-test1 |
| 500 | parent: project-test1 |
| 501 | """) |
| 502 | |
| 503 | file_dict = {'.zuul.yaml': downstream_repo_conf} |
| 504 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A', |
| 505 | files=file_dict) |
| 506 | A.addApproval('code-review', 2) |
| 507 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
| 508 | self.waitUntilSettled() |
| 509 | |
| 510 | self.assertEqual(A.data['status'], 'MERGED') |
| 511 | self.fake_gerrit.addEvent(A.getChangeMergedEvent()) |
| 512 | self.waitUntilSettled() |
| 513 | |
| 514 | upstream_repo_conf = textwrap.dedent( |
| 515 | """ |
| 516 | - job: |
| 517 | name: project-test1 |
| 518 | |
| 519 | - job: |
| 520 | name: project-test2 |
| 521 | |
| 522 | - project: |
| 523 | name: org/project |
| 524 | tenant-one-gate: |
| 525 | jobs: |
| 526 | - project-test1 |
| 527 | """) |
| 528 | |
| 529 | file_dict = {'.zuul.yaml': upstream_repo_conf} |
| 530 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B', |
| 531 | files=file_dict) |
| 532 | B.addApproval('code-review', 2) |
| 533 | self.fake_gerrit.addEvent(B.addApproval('approved', 1)) |
| 534 | self.waitUntilSettled() |
| 535 | |
| 536 | self.assertEqual(B.data['status'], 'MERGED') |
| 537 | self.fake_gerrit.addEvent(B.getChangeMergedEvent()) |
| 538 | self.waitUntilSettled() |
| 539 | |
| 540 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 541 | # Ensure the latest change is reflected in the config; if it |
| 542 | # isn't this will raise an exception. |
| 543 | tenant.layout.getJob('project-test2') |
| 544 | |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 545 | |
| 546 | class TestAnsible(AnsibleZuulTestCase): |
| 547 | # A temporary class to hold new tests while others are disabled |
| 548 | |
| 549 | tenant_config_file = 'config/ansible/main.yaml' |
| 550 | |
| 551 | def test_playbook(self): |
| 552 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 553 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 554 | self.waitUntilSettled() |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 555 | build_timeout = self.getJobFromHistory('timeout') |
| 556 | self.assertEqual(build_timeout.result, 'TIMED_OUT') |
| 557 | build_faillocal = self.getJobFromHistory('faillocal') |
| 558 | self.assertEqual(build_faillocal.result, 'FAILURE') |
| 559 | build_failpost = self.getJobFromHistory('failpost') |
| 560 | self.assertEqual(build_failpost.result, 'POST_FAILURE') |
| 561 | build_check_vars = self.getJobFromHistory('check-vars') |
| 562 | self.assertEqual(build_check_vars.result, 'SUCCESS') |
| 563 | build_hello = self.getJobFromHistory('hello-world') |
| 564 | self.assertEqual(build_hello.result, 'SUCCESS') |
| 565 | build_python27 = self.getJobFromHistory('python27') |
| 566 | self.assertEqual(build_python27.result, 'SUCCESS') |
| 567 | flag_path = os.path.join(self.test_root, build_python27.uuid + '.flag') |
James E. Blair | a92cbc8 | 2017-01-23 14:56:49 -0800 | [diff] [blame] | 568 | self.assertTrue(os.path.exists(flag_path)) |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 569 | copied_path = os.path.join(self.test_root, build_python27.uuid + |
Monty Taylor | c231d93 | 2017-02-03 09:57:15 -0600 | [diff] [blame] | 570 | '.copied') |
| 571 | self.assertTrue(os.path.exists(copied_path)) |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 572 | failed_path = os.path.join(self.test_root, build_python27.uuid + |
Monty Taylor | c231d93 | 2017-02-03 09:57:15 -0600 | [diff] [blame] | 573 | '.failed') |
| 574 | self.assertFalse(os.path.exists(failed_path)) |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 575 | pre_flag_path = os.path.join(self.test_root, build_python27.uuid + |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 576 | '.pre.flag') |
| 577 | self.assertTrue(os.path.exists(pre_flag_path)) |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 578 | post_flag_path = os.path.join(self.test_root, build_python27.uuid + |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 579 | '.post.flag') |
| 580 | self.assertTrue(os.path.exists(post_flag_path)) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 581 | bare_role_flag_path = os.path.join(self.test_root, |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 582 | build_python27.uuid + |
| 583 | '.bare-role.flag') |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 584 | self.assertTrue(os.path.exists(bare_role_flag_path)) |
James E. Blair | b9c0d77 | 2017-03-03 14:34:49 -0800 | [diff] [blame] | 585 | |
James E. Blair | 18f86a3 | 2017-03-15 14:43:26 -0700 | [diff] [blame] | 586 | secrets_path = os.path.join(self.test_root, |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 587 | build_python27.uuid + '.secrets') |
James E. Blair | 18f86a3 | 2017-03-15 14:43:26 -0700 | [diff] [blame] | 588 | with open(secrets_path) as f: |
| 589 | self.assertEqual(f.read(), "test-username test-password") |
| 590 | |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 591 | msg = A.messages[0] |
| 592 | success = "{} https://success.example.com/zuul-logs/{}" |
| 593 | fail = "{} https://failure.example.com/zuul-logs/{}" |
| 594 | self.assertIn(success.format("python27", build_python27.uuid), msg) |
| 595 | self.assertIn(fail.format("faillocal", build_faillocal.uuid), msg) |
| 596 | self.assertIn(success.format("check-vars", build_check_vars.uuid), msg) |
| 597 | self.assertIn(success.format("hello-world", build_hello.uuid), msg) |
| 598 | self.assertIn(fail.format("timeout", build_timeout.uuid), msg) |
| 599 | self.assertIn(fail.format("failpost", build_failpost.uuid), msg) |
| 600 | |
James E. Blair | b9c0d77 | 2017-03-03 14:34:49 -0800 | [diff] [blame] | 601 | |
James E. Blair | a4d4eef | 2017-06-30 14:49:17 -0700 | [diff] [blame] | 602 | class TestPrePlaybooks(AnsibleZuulTestCase): |
| 603 | # A temporary class to hold new tests while others are disabled |
| 604 | |
| 605 | tenant_config_file = 'config/pre-playbook/main.yaml' |
| 606 | |
| 607 | def test_pre_playbook_fail(self): |
| 608 | # Test that we run the post playbooks (but not the actual |
| 609 | # playbook) when a pre-playbook fails. |
| 610 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 611 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 612 | self.waitUntilSettled() |
| 613 | build = self.getJobFromHistory('python27') |
| 614 | self.assertIsNone(build.result) |
| 615 | self.assertIn('RETRY_LIMIT', A.messages[0]) |
| 616 | flag_path = os.path.join(self.test_root, build.uuid + |
| 617 | '.main.flag') |
| 618 | self.assertFalse(os.path.exists(flag_path)) |
| 619 | pre_flag_path = os.path.join(self.test_root, build.uuid + |
| 620 | '.pre.flag') |
| 621 | self.assertFalse(os.path.exists(pre_flag_path)) |
| 622 | post_flag_path = os.path.join(self.test_root, build.uuid + |
| 623 | '.post.flag') |
| 624 | self.assertTrue(os.path.exists(post_flag_path)) |
| 625 | |
| 626 | |
James E. Blair | b9c0d77 | 2017-03-03 14:34:49 -0800 | [diff] [blame] | 627 | class TestBrokenConfig(ZuulTestCase): |
| 628 | # Test that we get an appropriate syntax error if we start with a |
| 629 | # broken config. |
| 630 | |
| 631 | tenant_config_file = 'config/broken/main.yaml' |
| 632 | |
| 633 | def setUp(self): |
| 634 | with testtools.ExpectedException( |
| 635 | zuul.configloader.ConfigurationSyntaxError, |
| 636 | "\nZuul encountered a syntax error"): |
| 637 | super(TestBrokenConfig, self).setUp() |
| 638 | |
| 639 | def test_broken_config_on_startup(self): |
| 640 | pass |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 641 | |
| 642 | |
| 643 | class TestProjectKeys(ZuulTestCase): |
| 644 | # Test that we can generate project keys |
| 645 | |
| 646 | # Normally the test infrastructure copies a static key in place |
| 647 | # for each project before starting tests. This saves time because |
| 648 | # Zuul's automatic key-generation on startup can be slow. To make |
| 649 | # sure we exercise that code, in this test we allow Zuul to create |
| 650 | # keys for the project on startup. |
| 651 | create_project_keys = True |
| 652 | tenant_config_file = 'config/in-repo/main.yaml' |
| 653 | |
| 654 | def test_key_generation(self): |
| 655 | key_root = os.path.join(self.state_root, 'keys') |
| 656 | private_key_file = os.path.join(key_root, 'gerrit/org/project.pem') |
| 657 | # Make sure that a proper key was created on startup |
| 658 | with open(private_key_file, "rb") as f: |
James E. Blair | bf1a4f2 | 2017-03-17 10:59:37 -0700 | [diff] [blame] | 659 | private_key, public_key = \ |
| 660 | encryption.deserialize_rsa_keypair(f.read()) |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 661 | |
| 662 | with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i: |
| 663 | fixture_private_key = i.read() |
| 664 | |
| 665 | # Make sure that we didn't just end up with the static fixture |
| 666 | # key |
| 667 | self.assertNotEqual(fixture_private_key, private_key) |
| 668 | |
| 669 | # Make sure it's the right length |
| 670 | self.assertEqual(4096, private_key.key_size) |
James E. Blair | bce7693 | 2017-05-04 10:03:15 -0700 | [diff] [blame] | 671 | |
| 672 | |
| 673 | class TestRoles(ZuulTestCase): |
| 674 | tenant_config_file = 'config/roles/main.yaml' |
| 675 | |
| 676 | def test_role(self): |
| 677 | # This exercises a proposed change to a role being checked out |
| 678 | # and used. |
| 679 | A = self.fake_gerrit.addFakeChange('bare-role', 'master', 'A') |
| 680 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 681 | B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % ( |
| 682 | B.subject, A.data['id']) |
| 683 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 684 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
| 685 | self.waitUntilSettled() |
| 686 | self.assertHistory([ |
| 687 | dict(name='project-test', result='SUCCESS', changes='1,1 2,1'), |
| 688 | ]) |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 689 | |
| 690 | |
| 691 | class TestShadow(ZuulTestCase): |
| 692 | tenant_config_file = 'config/shadow/main.yaml' |
| 693 | |
| 694 | def test_shadow(self): |
| 695 | # Test that a repo is allowed to shadow another's job definitions. |
| 696 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 697 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 698 | self.waitUntilSettled() |
| 699 | self.assertHistory([ |
| 700 | dict(name='test1', result='SUCCESS', changes='1,1'), |
| 701 | dict(name='test2', result='SUCCESS', changes='1,1'), |
James E. Blair | adafa6c | 2017-07-12 08:50:56 -0700 | [diff] [blame] | 702 | ], ordered=False) |
James E. Blair | 196f61a | 2017-06-30 15:42:29 -0700 | [diff] [blame] | 703 | |
| 704 | |
| 705 | class TestDataReturn(AnsibleZuulTestCase): |
| 706 | tenant_config_file = 'config/data-return/main.yaml' |
| 707 | |
| 708 | def test_data_return(self): |
| 709 | # This exercises a proposed change to a role being checked out |
| 710 | # and used. |
| 711 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 712 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 713 | self.waitUntilSettled() |
| 714 | self.assertHistory([ |
| 715 | dict(name='data-return', result='SUCCESS', changes='1,1'), |
James E. Blair | 88e79c0 | 2017-07-07 13:36:54 -0700 | [diff] [blame^] | 716 | dict(name='data-return-relative', result='SUCCESS', changes='1,1'), |
| 717 | ], ordered=False) |
| 718 | self.assertIn('- data-return http://example.com/test/log/url/', |
| 719 | A.messages[-1]) |
| 720 | self.assertIn('- data-return-relative ' |
| 721 | 'http://example.com/test/log/url/docs/index.html', |
James E. Blair | 196f61a | 2017-06-30 15:42:29 -0700 | [diff] [blame] | 722 | A.messages[-1]) |