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 | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 489 | |
| 490 | class TestAnsible(AnsibleZuulTestCase): |
| 491 | # A temporary class to hold new tests while others are disabled |
| 492 | |
| 493 | tenant_config_file = 'config/ansible/main.yaml' |
| 494 | |
| 495 | def test_playbook(self): |
| 496 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 497 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 498 | self.waitUntilSettled() |
Paul Belanger | 96618ed | 2017-03-01 09:42:33 -0500 | [diff] [blame] | 499 | build = self.getJobFromHistory('timeout') |
Clark Boylan | 53fb126 | 2017-04-25 08:37:56 -0700 | [diff] [blame] | 500 | self.assertEqual(build.result, 'TIMED_OUT') |
Monty Taylor | c231d93 | 2017-02-03 09:57:15 -0600 | [diff] [blame] | 501 | build = self.getJobFromHistory('faillocal') |
| 502 | self.assertEqual(build.result, 'FAILURE') |
Paul Belanger | e2b8d49 | 2017-03-22 18:57:40 -0400 | [diff] [blame] | 503 | build = self.getJobFromHistory('check-vars') |
Paul Belanger | 30ba93a | 2017-03-16 16:28:10 -0400 | [diff] [blame] | 504 | self.assertEqual(build.result, 'SUCCESS') |
Paul Belanger | 9d9023f | 2017-05-02 14:51:07 -0400 | [diff] [blame] | 505 | build = self.getJobFromHistory('hello-world') |
| 506 | self.assertEqual(build.result, 'SUCCESS') |
James E. Blair | a92cbc8 | 2017-01-23 14:56:49 -0800 | [diff] [blame] | 507 | build = self.getJobFromHistory('python27') |
| 508 | self.assertEqual(build.result, 'SUCCESS') |
| 509 | flag_path = os.path.join(self.test_root, build.uuid + '.flag') |
| 510 | self.assertTrue(os.path.exists(flag_path)) |
Monty Taylor | c231d93 | 2017-02-03 09:57:15 -0600 | [diff] [blame] | 511 | copied_path = os.path.join(self.test_root, build.uuid + |
| 512 | '.copied') |
| 513 | self.assertTrue(os.path.exists(copied_path)) |
| 514 | failed_path = os.path.join(self.test_root, build.uuid + |
| 515 | '.failed') |
| 516 | self.assertFalse(os.path.exists(failed_path)) |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 517 | pre_flag_path = os.path.join(self.test_root, build.uuid + |
| 518 | '.pre.flag') |
| 519 | self.assertTrue(os.path.exists(pre_flag_path)) |
| 520 | post_flag_path = os.path.join(self.test_root, build.uuid + |
| 521 | '.post.flag') |
| 522 | self.assertTrue(os.path.exists(post_flag_path)) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 523 | bare_role_flag_path = os.path.join(self.test_root, |
| 524 | build.uuid + '.bare-role.flag') |
| 525 | self.assertTrue(os.path.exists(bare_role_flag_path)) |
James E. Blair | b9c0d77 | 2017-03-03 14:34:49 -0800 | [diff] [blame] | 526 | |
James E. Blair | 18f86a3 | 2017-03-15 14:43:26 -0700 | [diff] [blame] | 527 | secrets_path = os.path.join(self.test_root, |
| 528 | build.uuid + '.secrets') |
| 529 | with open(secrets_path) as f: |
| 530 | self.assertEqual(f.read(), "test-username test-password") |
| 531 | |
James E. Blair | b9c0d77 | 2017-03-03 14:34:49 -0800 | [diff] [blame] | 532 | |
James E. Blair | a4d4eef | 2017-06-30 14:49:17 -0700 | [diff] [blame] | 533 | class TestPrePlaybooks(AnsibleZuulTestCase): |
| 534 | # A temporary class to hold new tests while others are disabled |
| 535 | |
| 536 | tenant_config_file = 'config/pre-playbook/main.yaml' |
| 537 | |
| 538 | def test_pre_playbook_fail(self): |
| 539 | # Test that we run the post playbooks (but not the actual |
| 540 | # playbook) when a pre-playbook fails. |
| 541 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 542 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 543 | self.waitUntilSettled() |
| 544 | build = self.getJobFromHistory('python27') |
| 545 | self.assertIsNone(build.result) |
| 546 | self.assertIn('RETRY_LIMIT', A.messages[0]) |
| 547 | flag_path = os.path.join(self.test_root, build.uuid + |
| 548 | '.main.flag') |
| 549 | self.assertFalse(os.path.exists(flag_path)) |
| 550 | pre_flag_path = os.path.join(self.test_root, build.uuid + |
| 551 | '.pre.flag') |
| 552 | self.assertFalse(os.path.exists(pre_flag_path)) |
| 553 | post_flag_path = os.path.join(self.test_root, build.uuid + |
| 554 | '.post.flag') |
| 555 | self.assertTrue(os.path.exists(post_flag_path)) |
| 556 | |
| 557 | |
James E. Blair | b9c0d77 | 2017-03-03 14:34:49 -0800 | [diff] [blame] | 558 | class TestBrokenConfig(ZuulTestCase): |
| 559 | # Test that we get an appropriate syntax error if we start with a |
| 560 | # broken config. |
| 561 | |
| 562 | tenant_config_file = 'config/broken/main.yaml' |
| 563 | |
| 564 | def setUp(self): |
| 565 | with testtools.ExpectedException( |
| 566 | zuul.configloader.ConfigurationSyntaxError, |
| 567 | "\nZuul encountered a syntax error"): |
| 568 | super(TestBrokenConfig, self).setUp() |
| 569 | |
| 570 | def test_broken_config_on_startup(self): |
| 571 | pass |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 572 | |
| 573 | |
| 574 | class TestProjectKeys(ZuulTestCase): |
| 575 | # Test that we can generate project keys |
| 576 | |
| 577 | # Normally the test infrastructure copies a static key in place |
| 578 | # for each project before starting tests. This saves time because |
| 579 | # Zuul's automatic key-generation on startup can be slow. To make |
| 580 | # sure we exercise that code, in this test we allow Zuul to create |
| 581 | # keys for the project on startup. |
| 582 | create_project_keys = True |
| 583 | tenant_config_file = 'config/in-repo/main.yaml' |
| 584 | |
| 585 | def test_key_generation(self): |
| 586 | key_root = os.path.join(self.state_root, 'keys') |
| 587 | private_key_file = os.path.join(key_root, 'gerrit/org/project.pem') |
| 588 | # Make sure that a proper key was created on startup |
| 589 | with open(private_key_file, "rb") as f: |
James E. Blair | bf1a4f2 | 2017-03-17 10:59:37 -0700 | [diff] [blame] | 590 | private_key, public_key = \ |
| 591 | encryption.deserialize_rsa_keypair(f.read()) |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 592 | |
| 593 | with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i: |
| 594 | fixture_private_key = i.read() |
| 595 | |
| 596 | # Make sure that we didn't just end up with the static fixture |
| 597 | # key |
| 598 | self.assertNotEqual(fixture_private_key, private_key) |
| 599 | |
| 600 | # Make sure it's the right length |
| 601 | self.assertEqual(4096, private_key.key_size) |
James E. Blair | bce7693 | 2017-05-04 10:03:15 -0700 | [diff] [blame] | 602 | |
| 603 | |
| 604 | class TestRoles(ZuulTestCase): |
| 605 | tenant_config_file = 'config/roles/main.yaml' |
| 606 | |
| 607 | def test_role(self): |
| 608 | # This exercises a proposed change to a role being checked out |
| 609 | # and used. |
| 610 | A = self.fake_gerrit.addFakeChange('bare-role', 'master', 'A') |
| 611 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 612 | B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % ( |
| 613 | B.subject, A.data['id']) |
| 614 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 615 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
| 616 | self.waitUntilSettled() |
| 617 | self.assertHistory([ |
| 618 | dict(name='project-test', result='SUCCESS', changes='1,1 2,1'), |
| 619 | ]) |