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') |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [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') |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [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 | |
Tobias Henkel | 8316762 | 2017-06-30 19:45:03 +0200 | [diff] [blame] | 70 | class TestFinal(ZuulTestCase): |
| 71 | |
| 72 | tenant_config_file = 'config/final/main.yaml' |
| 73 | |
| 74 | def test_final_variant_ok(self): |
| 75 | # test clean usage of final parent job |
| 76 | in_repo_conf = textwrap.dedent( |
| 77 | """ |
| 78 | - project: |
| 79 | name: org/project |
| 80 | check: |
| 81 | jobs: |
| 82 | - job-final |
| 83 | """) |
| 84 | |
| 85 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 86 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 87 | files=file_dict) |
| 88 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 89 | self.waitUntilSettled() |
| 90 | |
| 91 | self.assertEqual(A.reported, 1) |
| 92 | self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '1') |
| 93 | |
| 94 | def test_final_variant_error(self): |
| 95 | # test misuse of final parent job |
| 96 | in_repo_conf = textwrap.dedent( |
| 97 | """ |
| 98 | - project: |
| 99 | name: org/project |
| 100 | check: |
| 101 | jobs: |
| 102 | - job-final: |
| 103 | vars: |
| 104 | dont_override_this: bar |
| 105 | """) |
| 106 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 107 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 108 | files=file_dict) |
| 109 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 110 | self.waitUntilSettled() |
| 111 | |
| 112 | # The second patch tried to override some variables. |
| 113 | # Thus it should fail. |
| 114 | self.assertEqual(A.reported, 1) |
| 115 | self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '-1') |
| 116 | self.assertIn('Unable to modify final job', A.messages[0]) |
| 117 | |
| 118 | def test_final_inheritance(self): |
| 119 | # test misuse of final parent job |
| 120 | in_repo_conf = textwrap.dedent( |
| 121 | """ |
| 122 | - job: |
| 123 | name: project-test |
| 124 | parent: job-final |
| 125 | |
| 126 | - project: |
| 127 | name: org/project |
| 128 | check: |
| 129 | jobs: |
| 130 | - project-test |
| 131 | """) |
| 132 | |
| 133 | in_repo_playbook = textwrap.dedent( |
| 134 | """ |
| 135 | - hosts: all |
| 136 | tasks: [] |
| 137 | """) |
| 138 | |
| 139 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 140 | 'playbooks/project-test.yaml': in_repo_playbook} |
| 141 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 142 | files=file_dict) |
| 143 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 144 | self.waitUntilSettled() |
| 145 | |
| 146 | # The second patch tried to override some variables. |
| 147 | # Thus it should fail. |
| 148 | self.assertEqual(A.reported, 1) |
| 149 | self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '-1') |
| 150 | self.assertIn('Unable to inherit from final job', A.messages[0]) |
| 151 | |
| 152 | |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 153 | class TestInRepoConfig(ZuulTestCase): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 154 | # A temporary class to hold new tests while others are disabled |
| 155 | |
Tobias Henkel | abf973e | 2017-07-28 10:07:34 +0200 | [diff] [blame] | 156 | config_file = 'zuul-connections-gerrit-and-github.conf' |
James E. Blair | 2a629ec | 2015-12-22 15:32:02 -0800 | [diff] [blame] | 157 | tenant_config_file = 'config/in-repo/main.yaml' |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 158 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 159 | def test_in_repo_config(self): |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 160 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 161 | A.addApproval('Code-Review', 2) |
| 162 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 163 | self.waitUntilSettled() |
| 164 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 165 | 'SUCCESS') |
| 166 | self.assertEqual(A.data['status'], 'MERGED') |
| 167 | self.assertEqual(A.reported, 2, |
| 168 | "A should report start and success") |
| 169 | self.assertIn('tenant-one-gate', A.messages[1], |
| 170 | "A should transit tenant-one gate") |
James E. Blair | b97ed80 | 2015-12-21 15:55:35 -0800 | [diff] [blame] | 171 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 172 | def test_dynamic_config(self): |
| 173 | in_repo_conf = textwrap.dedent( |
| 174 | """ |
| 175 | - job: |
Tobias Henkel | f02cf51 | 2017-07-21 22:55:34 +0200 | [diff] [blame] | 176 | name: project-test1 |
| 177 | |
| 178 | - job: |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 179 | name: project-test2 |
| 180 | |
| 181 | - project: |
| 182 | name: org/project |
| 183 | tenant-one-gate: |
| 184 | jobs: |
| 185 | - project-test2 |
| 186 | """) |
| 187 | |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 188 | in_repo_playbook = textwrap.dedent( |
| 189 | """ |
| 190 | - hosts: all |
| 191 | tasks: [] |
| 192 | """) |
| 193 | |
| 194 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 195 | 'playbooks/project-test2.yaml': in_repo_playbook} |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 196 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 197 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 198 | A.addApproval('Code-Review', 2) |
| 199 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 200 | self.waitUntilSettled() |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 201 | self.assertEqual(A.data['status'], 'MERGED') |
| 202 | self.assertEqual(A.reported, 2, |
| 203 | "A should report start and success") |
| 204 | self.assertIn('tenant-one-gate', A.messages[1], |
| 205 | "A should transit tenant-one gate") |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 206 | self.assertHistory([ |
| 207 | dict(name='project-test2', result='SUCCESS', changes='1,1')]) |
| 208 | |
James E. Blair | c2a5ed7 | 2017-02-20 14:12:01 -0500 | [diff] [blame] | 209 | self.fake_gerrit.addEvent(A.getChangeMergedEvent()) |
James E. Blair | 7bbd7a3 | 2017-03-06 11:36:13 -0800 | [diff] [blame] | 210 | self.waitUntilSettled() |
James E. Blair | c2a5ed7 | 2017-02-20 14:12:01 -0500 | [diff] [blame] | 211 | |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 212 | # Now that the config change is landed, it should be live for |
| 213 | # subsequent changes. |
| 214 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 215 | B.addApproval('Code-Review', 2) |
| 216 | self.fake_gerrit.addEvent(B.addApproval('Approved', 1)) |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 217 | self.waitUntilSettled() |
| 218 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 219 | 'SUCCESS') |
| 220 | self.assertHistory([ |
| 221 | dict(name='project-test2', result='SUCCESS', changes='1,1'), |
| 222 | dict(name='project-test2', result='SUCCESS', changes='2,1')]) |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 223 | |
Tobias Henkel | f02cf51 | 2017-07-21 22:55:34 +0200 | [diff] [blame] | 224 | def test_dynamic_config_non_existing_job(self): |
| 225 | """Test that requesting a non existent job fails""" |
| 226 | in_repo_conf = textwrap.dedent( |
| 227 | """ |
| 228 | - job: |
| 229 | name: project-test1 |
| 230 | |
| 231 | - project: |
| 232 | name: org/project |
| 233 | check: |
| 234 | jobs: |
| 235 | - non-existent-job |
| 236 | """) |
| 237 | |
| 238 | in_repo_playbook = textwrap.dedent( |
| 239 | """ |
| 240 | - hosts: all |
| 241 | tasks: [] |
| 242 | """) |
| 243 | |
| 244 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 245 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 246 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 247 | files=file_dict) |
| 248 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 249 | self.waitUntilSettled() |
| 250 | self.assertEqual(A.reported, 1, |
| 251 | "A should report failure") |
| 252 | self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1") |
| 253 | self.assertIn('Job non-existent-job not defined', A.messages[0], |
| 254 | "A should have failed the check pipeline") |
| 255 | self.assertHistory([]) |
| 256 | |
| 257 | def test_dynamic_config_non_existing_job_in_template(self): |
| 258 | """Test that requesting a non existent job fails""" |
| 259 | in_repo_conf = textwrap.dedent( |
| 260 | """ |
| 261 | - job: |
| 262 | name: project-test1 |
| 263 | |
| 264 | - project-template: |
| 265 | name: test-template |
| 266 | check: |
| 267 | jobs: |
| 268 | - non-existent-job |
| 269 | |
| 270 | - project: |
| 271 | name: org/project |
| 272 | templates: |
| 273 | - test-template |
| 274 | """) |
| 275 | |
| 276 | in_repo_playbook = textwrap.dedent( |
| 277 | """ |
| 278 | - hosts: all |
| 279 | tasks: [] |
| 280 | """) |
| 281 | |
| 282 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 283 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 284 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 285 | files=file_dict) |
| 286 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 287 | self.waitUntilSettled() |
| 288 | self.assertEqual(A.reported, 1, |
| 289 | "A should report failure") |
| 290 | self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1") |
| 291 | self.assertIn('Job non-existent-job not defined', A.messages[0], |
| 292 | "A should have failed the check pipeline") |
| 293 | self.assertHistory([]) |
| 294 | |
Tobias Henkel | 0f71400 | 2017-06-30 23:30:52 +0200 | [diff] [blame] | 295 | def test_dynamic_config_new_patchset(self): |
| 296 | self.executor_server.hold_jobs_in_build = True |
| 297 | |
| 298 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 299 | check_pipeline = tenant.layout.pipelines['check'] |
| 300 | |
| 301 | in_repo_conf = textwrap.dedent( |
| 302 | """ |
| 303 | - job: |
Tobias Henkel | f02cf51 | 2017-07-21 22:55:34 +0200 | [diff] [blame] | 304 | name: project-test1 |
| 305 | |
| 306 | - job: |
Tobias Henkel | 0f71400 | 2017-06-30 23:30:52 +0200 | [diff] [blame] | 307 | name: project-test2 |
| 308 | |
| 309 | - project: |
| 310 | name: org/project |
| 311 | check: |
| 312 | jobs: |
| 313 | - project-test2 |
| 314 | """) |
| 315 | |
| 316 | in_repo_playbook = textwrap.dedent( |
| 317 | """ |
| 318 | - hosts: all |
| 319 | tasks: [] |
| 320 | """) |
| 321 | |
| 322 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 323 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 324 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 325 | files=file_dict) |
| 326 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 327 | self.waitUntilSettled() |
| 328 | |
| 329 | items = check_pipeline.getAllItems() |
| 330 | self.assertEqual(items[0].change.number, '1') |
| 331 | self.assertEqual(items[0].change.patchset, '1') |
| 332 | self.assertTrue(items[0].live) |
| 333 | |
| 334 | in_repo_conf = textwrap.dedent( |
| 335 | """ |
| 336 | - job: |
Tobias Henkel | 0ce7ec6 | 2017-07-21 22:50:17 +0200 | [diff] [blame] | 337 | name: project-test1 |
| 338 | |
| 339 | - job: |
Tobias Henkel | 0f71400 | 2017-06-30 23:30:52 +0200 | [diff] [blame] | 340 | name: project-test2 |
| 341 | |
| 342 | - project: |
| 343 | name: org/project |
| 344 | check: |
| 345 | jobs: |
| 346 | - project-test1 |
| 347 | - project-test2 |
| 348 | """) |
| 349 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 350 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 351 | |
| 352 | A.addPatchset(files=file_dict) |
| 353 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2)) |
| 354 | |
| 355 | self.waitUntilSettled() |
| 356 | |
| 357 | items = check_pipeline.getAllItems() |
| 358 | self.assertEqual(items[0].change.number, '1') |
| 359 | self.assertEqual(items[0].change.patchset, '2') |
| 360 | self.assertTrue(items[0].live) |
| 361 | |
| 362 | self.executor_server.hold_jobs_in_build = False |
Tobias Henkel | 0ce7ec6 | 2017-07-21 22:50:17 +0200 | [diff] [blame] | 363 | self.executor_server.release('project-test1') |
| 364 | self.waitUntilSettled() |
Tobias Henkel | 0f71400 | 2017-06-30 23:30:52 +0200 | [diff] [blame] | 365 | self.executor_server.release() |
| 366 | self.waitUntilSettled() |
| 367 | |
Tobias Henkel | 0ce7ec6 | 2017-07-21 22:50:17 +0200 | [diff] [blame] | 368 | self.assertHistory([ |
| 369 | dict(name='project-test2', result='ABORTED', changes='1,1'), |
| 370 | dict(name='project-test1', result='SUCCESS', changes='1,2'), |
| 371 | dict(name='project-test2', result='SUCCESS', changes='1,2')]) |
| 372 | |
Jesse Keating | 78f544a | 2017-07-13 14:27:40 -0700 | [diff] [blame] | 373 | def test_dynamic_dependent_pipeline(self): |
| 374 | # Test dynamically adding a project to a |
| 375 | # dependent pipeline for the first time |
| 376 | self.executor_server.hold_jobs_in_build = True |
| 377 | |
| 378 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 379 | gate_pipeline = tenant.layout.pipelines['gate'] |
| 380 | |
| 381 | in_repo_conf = textwrap.dedent( |
| 382 | """ |
| 383 | - job: |
Tobias Henkel | f02cf51 | 2017-07-21 22:55:34 +0200 | [diff] [blame] | 384 | name: project-test1 |
| 385 | |
| 386 | - job: |
Jesse Keating | 78f544a | 2017-07-13 14:27:40 -0700 | [diff] [blame] | 387 | name: project-test2 |
| 388 | |
| 389 | - project: |
| 390 | name: org/project |
| 391 | gate: |
| 392 | jobs: |
| 393 | - project-test2 |
| 394 | """) |
| 395 | |
| 396 | in_repo_playbook = textwrap.dedent( |
| 397 | """ |
| 398 | - hosts: all |
| 399 | tasks: [] |
| 400 | """) |
| 401 | |
| 402 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 403 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 404 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 405 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 406 | A.addApproval('Approved', 1) |
| 407 | self.fake_gerrit.addEvent(A.addApproval('Code-Review', 2)) |
Jesse Keating | 78f544a | 2017-07-13 14:27:40 -0700 | [diff] [blame] | 408 | self.waitUntilSettled() |
| 409 | |
| 410 | items = gate_pipeline.getAllItems() |
| 411 | self.assertEqual(items[0].change.number, '1') |
| 412 | self.assertEqual(items[0].change.patchset, '1') |
| 413 | self.assertTrue(items[0].live) |
| 414 | |
| 415 | self.executor_server.hold_jobs_in_build = False |
| 416 | self.executor_server.release() |
| 417 | self.waitUntilSettled() |
| 418 | |
| 419 | # Make sure the dynamic queue got cleaned up |
| 420 | self.assertEqual(gate_pipeline.queues, []) |
| 421 | |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 422 | def test_in_repo_branch(self): |
| 423 | in_repo_conf = textwrap.dedent( |
| 424 | """ |
| 425 | - job: |
Tobias Henkel | f02cf51 | 2017-07-21 22:55:34 +0200 | [diff] [blame] | 426 | name: project-test1 |
| 427 | |
| 428 | - job: |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 429 | name: project-test2 |
| 430 | |
| 431 | - project: |
| 432 | name: org/project |
| 433 | tenant-one-gate: |
| 434 | jobs: |
| 435 | - project-test2 |
| 436 | """) |
| 437 | |
| 438 | in_repo_playbook = textwrap.dedent( |
| 439 | """ |
| 440 | - hosts: all |
| 441 | tasks: [] |
| 442 | """) |
| 443 | |
| 444 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 445 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 446 | self.create_branch('org/project', 'stable') |
James E. Blair | 72facdc | 2017-08-17 10:29:12 -0700 | [diff] [blame^] | 447 | self.fake_gerrit.addEvent( |
| 448 | self.fake_gerrit.getFakeBranchCreatedEvent( |
| 449 | 'org/project', 'stable')) |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 450 | A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A', |
| 451 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 452 | A.addApproval('Code-Review', 2) |
| 453 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 454 | self.waitUntilSettled() |
| 455 | self.assertEqual(A.data['status'], 'MERGED') |
| 456 | self.assertEqual(A.reported, 2, |
| 457 | "A should report start and success") |
| 458 | self.assertIn('tenant-one-gate', A.messages[1], |
| 459 | "A should transit tenant-one gate") |
| 460 | self.assertHistory([ |
| 461 | dict(name='project-test2', result='SUCCESS', changes='1,1')]) |
| 462 | self.fake_gerrit.addEvent(A.getChangeMergedEvent()) |
James E. Blair | 7bbd7a3 | 2017-03-06 11:36:13 -0800 | [diff] [blame] | 463 | self.waitUntilSettled() |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 464 | |
| 465 | # The config change should not affect master. |
| 466 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 467 | B.addApproval('Code-Review', 2) |
| 468 | self.fake_gerrit.addEvent(B.addApproval('Approved', 1)) |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 469 | self.waitUntilSettled() |
| 470 | self.assertHistory([ |
| 471 | dict(name='project-test2', result='SUCCESS', changes='1,1'), |
| 472 | dict(name='project-test1', result='SUCCESS', changes='2,1')]) |
| 473 | |
| 474 | # The config change should be live for further changes on |
| 475 | # stable. |
| 476 | C = self.fake_gerrit.addFakeChange('org/project', 'stable', 'C') |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 477 | C.addApproval('Code-Review', 2) |
| 478 | self.fake_gerrit.addEvent(C.addApproval('Approved', 1)) |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 479 | self.waitUntilSettled() |
| 480 | self.assertHistory([ |
| 481 | dict(name='project-test2', result='SUCCESS', changes='1,1'), |
| 482 | dict(name='project-test1', result='SUCCESS', changes='2,1'), |
| 483 | dict(name='project-test2', result='SUCCESS', changes='3,1')]) |
| 484 | |
James E. Blair | a5a1249 | 2017-05-03 11:40:48 -0700 | [diff] [blame] | 485 | def test_crd_dynamic_config_branch(self): |
| 486 | # Test that we can create a job in one repo and be able to use |
| 487 | # it from a different branch on a different repo. |
| 488 | |
| 489 | self.create_branch('org/project1', 'stable') |
James E. Blair | 72facdc | 2017-08-17 10:29:12 -0700 | [diff] [blame^] | 490 | self.fake_gerrit.addEvent( |
| 491 | self.fake_gerrit.getFakeBranchCreatedEvent( |
| 492 | 'org/project1', 'stable')) |
James E. Blair | a5a1249 | 2017-05-03 11:40:48 -0700 | [diff] [blame] | 493 | |
| 494 | in_repo_conf = textwrap.dedent( |
| 495 | """ |
| 496 | - job: |
Tobias Henkel | f02cf51 | 2017-07-21 22:55:34 +0200 | [diff] [blame] | 497 | name: project-test1 |
| 498 | |
| 499 | - job: |
James E. Blair | a5a1249 | 2017-05-03 11:40:48 -0700 | [diff] [blame] | 500 | name: project-test2 |
| 501 | |
| 502 | - project: |
| 503 | name: org/project |
| 504 | check: |
| 505 | jobs: |
| 506 | - project-test2 |
| 507 | """) |
| 508 | |
| 509 | in_repo_playbook = textwrap.dedent( |
| 510 | """ |
| 511 | - hosts: all |
| 512 | tasks: [] |
| 513 | """) |
| 514 | |
| 515 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 516 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 517 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 518 | files=file_dict) |
| 519 | |
| 520 | second_repo_conf = textwrap.dedent( |
| 521 | """ |
| 522 | - project: |
| 523 | name: org/project1 |
| 524 | check: |
| 525 | jobs: |
| 526 | - project-test2 |
| 527 | """) |
| 528 | |
| 529 | second_file_dict = {'.zuul.yaml': second_repo_conf} |
| 530 | B = self.fake_gerrit.addFakeChange('org/project1', 'stable', 'B', |
| 531 | files=second_file_dict) |
| 532 | B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % ( |
| 533 | B.subject, A.data['id']) |
| 534 | |
| 535 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 536 | self.waitUntilSettled() |
| 537 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
| 538 | self.waitUntilSettled() |
| 539 | |
| 540 | self.assertEqual(A.reported, 1, "A should report") |
| 541 | self.assertHistory([ |
| 542 | dict(name='project-test2', result='SUCCESS', changes='1,1'), |
| 543 | dict(name='project-test2', result='SUCCESS', changes='1,1 2,1'), |
| 544 | ]) |
| 545 | |
James E. Blair | 149b69c | 2017-03-02 10:48:16 -0800 | [diff] [blame] | 546 | def test_untrusted_syntax_error(self): |
James E. Blair | e53250c | 2017-03-01 14:34:36 -0800 | [diff] [blame] | 547 | in_repo_conf = textwrap.dedent( |
| 548 | """ |
| 549 | - job: |
| 550 | name: project-test2 |
| 551 | foo: error |
| 552 | """) |
| 553 | |
| 554 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 555 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 556 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 557 | A.addApproval('Code-Review', 2) |
| 558 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | e53250c | 2017-03-01 14:34:36 -0800 | [diff] [blame] | 559 | self.waitUntilSettled() |
| 560 | |
| 561 | self.assertEqual(A.data['status'], 'NEW') |
Tobias Henkel | 9842bd7 | 2017-05-16 13:40:03 +0200 | [diff] [blame] | 562 | self.assertEqual(A.reported, 1, |
| 563 | "A should report failure") |
| 564 | self.assertIn('syntax error', A.messages[0], |
James E. Blair | e53250c | 2017-03-01 14:34:36 -0800 | [diff] [blame] | 565 | "A should have a syntax error reported") |
| 566 | |
James E. Blair | 149b69c | 2017-03-02 10:48:16 -0800 | [diff] [blame] | 567 | def test_trusted_syntax_error(self): |
| 568 | in_repo_conf = textwrap.dedent( |
| 569 | """ |
| 570 | - job: |
| 571 | name: project-test2 |
| 572 | foo: error |
| 573 | """) |
| 574 | |
| 575 | file_dict = {'zuul.yaml': in_repo_conf} |
| 576 | A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A', |
| 577 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 578 | A.addApproval('Code-Review', 2) |
| 579 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | 149b69c | 2017-03-02 10:48:16 -0800 | [diff] [blame] | 580 | self.waitUntilSettled() |
| 581 | |
| 582 | self.assertEqual(A.data['status'], 'NEW') |
Tobias Henkel | 9842bd7 | 2017-05-16 13:40:03 +0200 | [diff] [blame] | 583 | self.assertEqual(A.reported, 1, |
| 584 | "A should report failure") |
| 585 | self.assertIn('syntax error', A.messages[0], |
James E. Blair | 149b69c | 2017-03-02 10:48:16 -0800 | [diff] [blame] | 586 | "A should have a syntax error reported") |
| 587 | |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 588 | def test_untrusted_yaml_error(self): |
| 589 | in_repo_conf = textwrap.dedent( |
| 590 | """ |
| 591 | - job: |
| 592 | foo: error |
| 593 | """) |
| 594 | |
| 595 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 596 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 597 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 598 | A.addApproval('Code-Review', 2) |
| 599 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 600 | self.waitUntilSettled() |
| 601 | |
| 602 | self.assertEqual(A.data['status'], 'NEW') |
Tobias Henkel | 9842bd7 | 2017-05-16 13:40:03 +0200 | [diff] [blame] | 603 | self.assertEqual(A.reported, 1, |
| 604 | "A should report failure") |
| 605 | self.assertIn('syntax error', A.messages[0], |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 606 | "A should have a syntax error reported") |
| 607 | |
James E. Blair | db04e6a | 2017-05-03 14:49:36 -0700 | [diff] [blame] | 608 | def test_untrusted_shadow_error(self): |
| 609 | in_repo_conf = textwrap.dedent( |
| 610 | """ |
| 611 | - job: |
| 612 | name: common-config-test |
| 613 | """) |
| 614 | |
| 615 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 616 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 617 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 618 | A.addApproval('Code-Review', 2) |
| 619 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | db04e6a | 2017-05-03 14:49:36 -0700 | [diff] [blame] | 620 | self.waitUntilSettled() |
| 621 | |
| 622 | self.assertEqual(A.data['status'], 'NEW') |
Tobias Henkel | 9842bd7 | 2017-05-16 13:40:03 +0200 | [diff] [blame] | 623 | self.assertEqual(A.reported, 1, |
| 624 | "A should report failure") |
| 625 | self.assertIn('not permitted to shadow', A.messages[0], |
James E. Blair | db04e6a | 2017-05-03 14:49:36 -0700 | [diff] [blame] | 626 | "A should have a syntax error reported") |
| 627 | |
James E. Blair | d5656ad | 2017-06-02 14:29:41 -0700 | [diff] [blame] | 628 | def test_untrusted_pipeline_error(self): |
| 629 | in_repo_conf = textwrap.dedent( |
| 630 | """ |
| 631 | - pipeline: |
| 632 | name: test |
| 633 | """) |
| 634 | |
| 635 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 636 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 637 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 638 | A.addApproval('Code-Review', 2) |
| 639 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | d5656ad | 2017-06-02 14:29:41 -0700 | [diff] [blame] | 640 | self.waitUntilSettled() |
| 641 | |
| 642 | self.assertEqual(A.data['status'], 'NEW') |
| 643 | self.assertEqual(A.reported, 1, |
| 644 | "A should report failure") |
| 645 | self.assertIn('Pipelines may not be defined', A.messages[0], |
| 646 | "A should have a syntax error reported") |
| 647 | |
| 648 | def test_untrusted_project_error(self): |
| 649 | in_repo_conf = textwrap.dedent( |
| 650 | """ |
| 651 | - project: |
| 652 | name: org/project1 |
| 653 | """) |
| 654 | |
| 655 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 656 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 657 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 658 | A.addApproval('Code-Review', 2) |
| 659 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | d5656ad | 2017-06-02 14:29:41 -0700 | [diff] [blame] | 660 | self.waitUntilSettled() |
| 661 | |
| 662 | self.assertEqual(A.data['status'], 'NEW') |
| 663 | self.assertEqual(A.reported, 1, |
| 664 | "A should report failure") |
| 665 | self.assertIn('the only project definition permitted', A.messages[0], |
| 666 | "A should have a syntax error reported") |
| 667 | |
James E. Blair | e64b0e4 | 2017-06-08 11:23:34 -0700 | [diff] [blame] | 668 | def test_duplicate_node_error(self): |
| 669 | in_repo_conf = textwrap.dedent( |
| 670 | """ |
| 671 | - nodeset: |
| 672 | name: duplicate |
| 673 | nodes: |
| 674 | - name: compute |
James E. Blair | 16d96a0 | 2017-06-08 11:32:56 -0700 | [diff] [blame] | 675 | label: foo |
James E. Blair | e64b0e4 | 2017-06-08 11:23:34 -0700 | [diff] [blame] | 676 | - name: compute |
James E. Blair | 16d96a0 | 2017-06-08 11:32:56 -0700 | [diff] [blame] | 677 | label: foo |
James E. Blair | e64b0e4 | 2017-06-08 11:23:34 -0700 | [diff] [blame] | 678 | """) |
| 679 | |
| 680 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 681 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 682 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 683 | A.addApproval('Code-Review', 2) |
| 684 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | e64b0e4 | 2017-06-08 11:23:34 -0700 | [diff] [blame] | 685 | self.waitUntilSettled() |
| 686 | |
| 687 | self.assertEqual(A.data['status'], 'NEW') |
| 688 | self.assertEqual(A.reported, 1, |
| 689 | "A should report failure") |
| 690 | self.assertIn('appears multiple times', A.messages[0], |
| 691 | "A should have a syntax error reported") |
| 692 | |
| 693 | def test_duplicate_group_error(self): |
| 694 | in_repo_conf = textwrap.dedent( |
| 695 | """ |
| 696 | - nodeset: |
| 697 | name: duplicate |
| 698 | nodes: |
| 699 | - name: compute |
James E. Blair | 16d96a0 | 2017-06-08 11:32:56 -0700 | [diff] [blame] | 700 | label: foo |
James E. Blair | e64b0e4 | 2017-06-08 11:23:34 -0700 | [diff] [blame] | 701 | groups: |
| 702 | - name: group |
| 703 | nodes: compute |
| 704 | - name: group |
| 705 | nodes: compute |
| 706 | """) |
| 707 | |
| 708 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 709 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 710 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 711 | A.addApproval('Code-Review', 2) |
| 712 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | e64b0e4 | 2017-06-08 11:23:34 -0700 | [diff] [blame] | 713 | self.waitUntilSettled() |
| 714 | |
| 715 | self.assertEqual(A.data['status'], 'NEW') |
| 716 | self.assertEqual(A.reported, 1, |
| 717 | "A should report failure") |
| 718 | self.assertIn('appears multiple times', A.messages[0], |
| 719 | "A should have a syntax error reported") |
| 720 | |
James E. Blair | 09f9ffe | 2017-07-11 15:30:25 -0700 | [diff] [blame] | 721 | def test_multi_repo(self): |
| 722 | downstream_repo_conf = textwrap.dedent( |
| 723 | """ |
| 724 | - project: |
| 725 | name: org/project1 |
| 726 | tenant-one-gate: |
| 727 | jobs: |
| 728 | - project-test1 |
| 729 | |
| 730 | - job: |
| 731 | name: project1-test1 |
| 732 | parent: project-test1 |
| 733 | """) |
| 734 | |
| 735 | file_dict = {'.zuul.yaml': downstream_repo_conf} |
| 736 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A', |
| 737 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 738 | A.addApproval('Code-Review', 2) |
| 739 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | 09f9ffe | 2017-07-11 15:30:25 -0700 | [diff] [blame] | 740 | self.waitUntilSettled() |
| 741 | |
| 742 | self.assertEqual(A.data['status'], 'MERGED') |
| 743 | self.fake_gerrit.addEvent(A.getChangeMergedEvent()) |
| 744 | self.waitUntilSettled() |
| 745 | |
| 746 | upstream_repo_conf = textwrap.dedent( |
| 747 | """ |
| 748 | - job: |
| 749 | name: project-test1 |
| 750 | |
| 751 | - job: |
| 752 | name: project-test2 |
| 753 | |
| 754 | - project: |
| 755 | name: org/project |
| 756 | tenant-one-gate: |
| 757 | jobs: |
| 758 | - project-test1 |
| 759 | """) |
| 760 | |
| 761 | file_dict = {'.zuul.yaml': upstream_repo_conf} |
| 762 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B', |
| 763 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 764 | B.addApproval('Code-Review', 2) |
| 765 | self.fake_gerrit.addEvent(B.addApproval('Approved', 1)) |
James E. Blair | 09f9ffe | 2017-07-11 15:30:25 -0700 | [diff] [blame] | 766 | self.waitUntilSettled() |
| 767 | |
| 768 | self.assertEqual(B.data['status'], 'MERGED') |
| 769 | self.fake_gerrit.addEvent(B.getChangeMergedEvent()) |
| 770 | self.waitUntilSettled() |
| 771 | |
| 772 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 773 | # Ensure the latest change is reflected in the config; if it |
| 774 | # isn't this will raise an exception. |
| 775 | tenant.layout.getJob('project-test2') |
| 776 | |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 777 | |
| 778 | class TestAnsible(AnsibleZuulTestCase): |
| 779 | # A temporary class to hold new tests while others are disabled |
| 780 | |
| 781 | tenant_config_file = 'config/ansible/main.yaml' |
| 782 | |
| 783 | def test_playbook(self): |
Jamie Lennox | 7655b55 | 2017-03-17 12:33:38 +1100 | [diff] [blame] | 784 | # Keep the jobdir around so we can inspect contents if an |
| 785 | # assert fails. |
| 786 | self.executor_server.keep_jobdir = True |
| 787 | # Output extra ansible info so we might see errors. |
| 788 | self.executor_server.verbose = True |
| 789 | # Add a site variables file, used by check-vars |
| 790 | path = os.path.join(FIXTURE_DIR, 'config', 'ansible', |
| 791 | 'variables.yaml') |
| 792 | self.config.set('executor', 'variables', path) |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 793 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 794 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 795 | self.waitUntilSettled() |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 796 | build_timeout = self.getJobFromHistory('timeout') |
Jamie Lennox | 7655b55 | 2017-03-17 12:33:38 +1100 | [diff] [blame] | 797 | with self.jobLog(build_timeout): |
| 798 | self.assertEqual(build_timeout.result, 'TIMED_OUT') |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 799 | build_faillocal = self.getJobFromHistory('faillocal') |
Jamie Lennox | 7655b55 | 2017-03-17 12:33:38 +1100 | [diff] [blame] | 800 | with self.jobLog(build_faillocal): |
| 801 | self.assertEqual(build_faillocal.result, 'FAILURE') |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 802 | build_failpost = self.getJobFromHistory('failpost') |
Jamie Lennox | 7655b55 | 2017-03-17 12:33:38 +1100 | [diff] [blame] | 803 | with self.jobLog(build_failpost): |
| 804 | self.assertEqual(build_failpost.result, 'POST_FAILURE') |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 805 | build_check_vars = self.getJobFromHistory('check-vars') |
Jamie Lennox | 7655b55 | 2017-03-17 12:33:38 +1100 | [diff] [blame] | 806 | with self.jobLog(build_check_vars): |
| 807 | self.assertEqual(build_check_vars.result, 'SUCCESS') |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 808 | build_hello = self.getJobFromHistory('hello-world') |
Jamie Lennox | 7655b55 | 2017-03-17 12:33:38 +1100 | [diff] [blame] | 809 | with self.jobLog(build_hello): |
| 810 | self.assertEqual(build_hello.result, 'SUCCESS') |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 811 | build_python27 = self.getJobFromHistory('python27') |
Jamie Lennox | 7655b55 | 2017-03-17 12:33:38 +1100 | [diff] [blame] | 812 | with self.jobLog(build_python27): |
| 813 | self.assertEqual(build_python27.result, 'SUCCESS') |
| 814 | flag_path = os.path.join(self.test_root, |
| 815 | build_python27.uuid + '.flag') |
| 816 | self.assertTrue(os.path.exists(flag_path)) |
| 817 | copied_path = os.path.join(self.test_root, build_python27.uuid + |
| 818 | '.copied') |
| 819 | self.assertTrue(os.path.exists(copied_path)) |
| 820 | failed_path = os.path.join(self.test_root, build_python27.uuid + |
| 821 | '.failed') |
| 822 | self.assertFalse(os.path.exists(failed_path)) |
| 823 | pre_flag_path = os.path.join(self.test_root, build_python27.uuid + |
| 824 | '.pre.flag') |
| 825 | self.assertTrue(os.path.exists(pre_flag_path)) |
| 826 | post_flag_path = os.path.join(self.test_root, build_python27.uuid + |
| 827 | '.post.flag') |
| 828 | self.assertTrue(os.path.exists(post_flag_path)) |
| 829 | bare_role_flag_path = os.path.join(self.test_root, |
| 830 | build_python27.uuid + |
| 831 | '.bare-role.flag') |
| 832 | self.assertTrue(os.path.exists(bare_role_flag_path)) |
| 833 | secrets_path = os.path.join(self.test_root, |
| 834 | build_python27.uuid + '.secrets') |
| 835 | with open(secrets_path) as f: |
| 836 | self.assertEqual(f.read(), "test-username test-password") |
James E. Blair | b9c0d77 | 2017-03-03 14:34:49 -0800 | [diff] [blame] | 837 | |
Jamie Lennox | 7655b55 | 2017-03-17 12:33:38 +1100 | [diff] [blame] | 838 | msg = A.messages[0] |
| 839 | success = "{} https://success.example.com/zuul-logs/{}" |
| 840 | fail = "{} https://failure.example.com/zuul-logs/{}" |
| 841 | self.assertIn(success.format("python27", build_python27.uuid), msg) |
| 842 | self.assertIn(fail.format("faillocal", build_faillocal.uuid), msg) |
| 843 | self.assertIn(success.format("check-vars", |
| 844 | build_check_vars.uuid), msg) |
| 845 | self.assertIn(success.format("hello-world", build_hello.uuid), msg) |
| 846 | self.assertIn(fail.format("timeout", build_timeout.uuid), msg) |
| 847 | self.assertIn(fail.format("failpost", build_failpost.uuid), msg) |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 848 | |
James E. Blair | abbaa6f | 2017-04-06 16:11:44 -0700 | [diff] [blame] | 849 | def _add_job(self, job_name): |
| 850 | conf = textwrap.dedent( |
| 851 | """ |
| 852 | - job: |
| 853 | name: %s |
| 854 | |
| 855 | - project: |
| 856 | name: org/plugin-project |
| 857 | check: |
| 858 | jobs: |
| 859 | - %s |
| 860 | """ % (job_name, job_name)) |
| 861 | |
| 862 | file_dict = {'.zuul.yaml': conf} |
| 863 | A = self.fake_gerrit.addFakeChange('org/plugin-project', 'master', 'A', |
| 864 | files=file_dict) |
| 865 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 866 | self.waitUntilSettled() |
| 867 | |
| 868 | def test_plugins(self): |
| 869 | # Keep the jobdir around so we can inspect contents if an |
| 870 | # assert fails. |
| 871 | self.executor_server.keep_jobdir = True |
| 872 | # Output extra ansible info so we might see errors. |
| 873 | self.executor_server.verbose = True |
| 874 | |
| 875 | count = 0 |
| 876 | plugin_tests = [ |
| 877 | ('passwd', 'FAILURE'), |
| 878 | ('cartesian', 'SUCCESS'), |
| 879 | ('consul_kv', 'FAILURE'), |
| 880 | ('credstash', 'FAILURE'), |
| 881 | ('csvfile_good', 'SUCCESS'), |
| 882 | ('csvfile_bad', 'FAILURE'), |
Monty Taylor | 93ad221 | 2017-08-02 14:59:50 -0500 | [diff] [blame] | 883 | ('uri_bad_path', 'FAILURE'), |
| 884 | ('uri_bad_scheme', 'FAILURE'), |
James E. Blair | abbaa6f | 2017-04-06 16:11:44 -0700 | [diff] [blame] | 885 | ] |
| 886 | for job_name, result in plugin_tests: |
| 887 | count += 1 |
| 888 | self._add_job(job_name) |
| 889 | |
| 890 | job = self.getJobFromHistory(job_name) |
| 891 | with self.jobLog(job): |
| 892 | self.assertEqual(count, len(self.history)) |
| 893 | build = self.history[-1] |
| 894 | self.assertEqual(build.result, result) |
| 895 | |
| 896 | # TODOv3(jeblair): parse the ansible output and verify we're |
| 897 | # getting the exception we expect. |
| 898 | |
James E. Blair | b9c0d77 | 2017-03-03 14:34:49 -0800 | [diff] [blame] | 899 | |
James E. Blair | a4d4eef | 2017-06-30 14:49:17 -0700 | [diff] [blame] | 900 | class TestPrePlaybooks(AnsibleZuulTestCase): |
| 901 | # A temporary class to hold new tests while others are disabled |
| 902 | |
| 903 | tenant_config_file = 'config/pre-playbook/main.yaml' |
| 904 | |
| 905 | def test_pre_playbook_fail(self): |
| 906 | # Test that we run the post playbooks (but not the actual |
| 907 | # playbook) when a pre-playbook fails. |
| 908 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 909 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 910 | self.waitUntilSettled() |
| 911 | build = self.getJobFromHistory('python27') |
| 912 | self.assertIsNone(build.result) |
| 913 | self.assertIn('RETRY_LIMIT', A.messages[0]) |
| 914 | flag_path = os.path.join(self.test_root, build.uuid + |
| 915 | '.main.flag') |
| 916 | self.assertFalse(os.path.exists(flag_path)) |
| 917 | pre_flag_path = os.path.join(self.test_root, build.uuid + |
| 918 | '.pre.flag') |
| 919 | self.assertFalse(os.path.exists(pre_flag_path)) |
| 920 | post_flag_path = os.path.join(self.test_root, build.uuid + |
| 921 | '.post.flag') |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 922 | self.assertTrue(os.path.exists(post_flag_path), |
| 923 | "The file %s should exist" % post_flag_path) |
James E. Blair | a4d4eef | 2017-06-30 14:49:17 -0700 | [diff] [blame] | 924 | |
| 925 | |
James E. Blair | b9c0d77 | 2017-03-03 14:34:49 -0800 | [diff] [blame] | 926 | class TestBrokenConfig(ZuulTestCase): |
| 927 | # Test that we get an appropriate syntax error if we start with a |
| 928 | # broken config. |
| 929 | |
| 930 | tenant_config_file = 'config/broken/main.yaml' |
| 931 | |
| 932 | def setUp(self): |
| 933 | with testtools.ExpectedException( |
| 934 | zuul.configloader.ConfigurationSyntaxError, |
| 935 | "\nZuul encountered a syntax error"): |
| 936 | super(TestBrokenConfig, self).setUp() |
| 937 | |
| 938 | def test_broken_config_on_startup(self): |
| 939 | pass |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 940 | |
| 941 | |
| 942 | class TestProjectKeys(ZuulTestCase): |
| 943 | # Test that we can generate project keys |
| 944 | |
| 945 | # Normally the test infrastructure copies a static key in place |
| 946 | # for each project before starting tests. This saves time because |
| 947 | # Zuul's automatic key-generation on startup can be slow. To make |
| 948 | # sure we exercise that code, in this test we allow Zuul to create |
| 949 | # keys for the project on startup. |
| 950 | create_project_keys = True |
Tobias Henkel | abf973e | 2017-07-28 10:07:34 +0200 | [diff] [blame] | 951 | config_file = 'zuul-connections-gerrit-and-github.conf' |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 952 | tenant_config_file = 'config/in-repo/main.yaml' |
| 953 | |
| 954 | def test_key_generation(self): |
| 955 | key_root = os.path.join(self.state_root, 'keys') |
| 956 | private_key_file = os.path.join(key_root, 'gerrit/org/project.pem') |
| 957 | # Make sure that a proper key was created on startup |
| 958 | with open(private_key_file, "rb") as f: |
James E. Blair | bf1a4f2 | 2017-03-17 10:59:37 -0700 | [diff] [blame] | 959 | private_key, public_key = \ |
| 960 | encryption.deserialize_rsa_keypair(f.read()) |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 961 | |
| 962 | with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i: |
| 963 | fixture_private_key = i.read() |
| 964 | |
| 965 | # Make sure that we didn't just end up with the static fixture |
| 966 | # key |
| 967 | self.assertNotEqual(fixture_private_key, private_key) |
| 968 | |
| 969 | # Make sure it's the right length |
| 970 | self.assertEqual(4096, private_key.key_size) |
James E. Blair | bce7693 | 2017-05-04 10:03:15 -0700 | [diff] [blame] | 971 | |
| 972 | |
James E. Blair | bb94dfa | 2017-07-11 07:45:19 -0700 | [diff] [blame] | 973 | class RoleTestCase(ZuulTestCase): |
James E. Blair | 1b27f6a | 2017-07-14 14:09:07 -0700 | [diff] [blame] | 974 | def _assertRolePath(self, build, playbook, content): |
| 975 | path = os.path.join(self.test_root, build.uuid, |
| 976 | 'ansible', playbook, 'ansible.cfg') |
| 977 | roles_paths = [] |
| 978 | with open(path) as f: |
| 979 | for line in f: |
| 980 | if line.startswith('roles_path'): |
| 981 | roles_paths.append(line) |
| 982 | print(roles_paths) |
| 983 | if content: |
| 984 | self.assertEqual(len(roles_paths), 1, |
| 985 | "Should have one roles_path line in %s" % |
| 986 | (playbook,)) |
| 987 | self.assertIn(content, roles_paths[0]) |
| 988 | else: |
| 989 | self.assertEqual(len(roles_paths), 0, |
| 990 | "Should have no roles_path line in %s" % |
| 991 | (playbook,)) |
| 992 | |
James E. Blair | bb94dfa | 2017-07-11 07:45:19 -0700 | [diff] [blame] | 993 | |
| 994 | class TestRoles(RoleTestCase): |
| 995 | tenant_config_file = 'config/roles/main.yaml' |
| 996 | |
James E. Blair | bce7693 | 2017-05-04 10:03:15 -0700 | [diff] [blame] | 997 | def test_role(self): |
| 998 | # This exercises a proposed change to a role being checked out |
| 999 | # and used. |
| 1000 | A = self.fake_gerrit.addFakeChange('bare-role', 'master', 'A') |
| 1001 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1002 | B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % ( |
| 1003 | B.subject, A.data['id']) |
| 1004 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1005 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
| 1006 | self.waitUntilSettled() |
| 1007 | self.assertHistory([ |
| 1008 | dict(name='project-test', result='SUCCESS', changes='1,1 2,1'), |
| 1009 | ]) |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 1010 | |
James E. Blair | 1b27f6a | 2017-07-14 14:09:07 -0700 | [diff] [blame] | 1011 | def test_role_inheritance(self): |
| 1012 | self.executor_server.hold_jobs_in_build = True |
| 1013 | conf = textwrap.dedent( |
| 1014 | """ |
| 1015 | - job: |
| 1016 | name: parent |
| 1017 | roles: |
| 1018 | - zuul: bare-role |
| 1019 | pre-run: playbooks/parent-pre |
| 1020 | post-run: playbooks/parent-post |
| 1021 | |
| 1022 | - job: |
| 1023 | name: project-test |
| 1024 | parent: parent |
| 1025 | roles: |
| 1026 | - zuul: org/project |
| 1027 | |
| 1028 | - project: |
| 1029 | name: org/project |
| 1030 | check: |
| 1031 | jobs: |
| 1032 | - project-test |
| 1033 | """) |
| 1034 | |
| 1035 | file_dict = {'.zuul.yaml': conf} |
| 1036 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 1037 | files=file_dict) |
| 1038 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1039 | self.waitUntilSettled() |
| 1040 | |
| 1041 | self.assertEqual(len(self.builds), 1) |
| 1042 | build = self.getBuildByName('project-test') |
| 1043 | self._assertRolePath(build, 'pre_playbook_0', 'role_0') |
| 1044 | self._assertRolePath(build, 'playbook_0', 'role_0') |
| 1045 | self._assertRolePath(build, 'playbook_0', 'role_1') |
| 1046 | self._assertRolePath(build, 'post_playbook_0', 'role_0') |
| 1047 | |
| 1048 | self.executor_server.hold_jobs_in_build = False |
| 1049 | self.executor_server.release() |
| 1050 | self.waitUntilSettled() |
| 1051 | |
| 1052 | self.assertHistory([ |
| 1053 | dict(name='project-test', result='SUCCESS', changes='1,1'), |
| 1054 | ]) |
| 1055 | |
James E. Blair | 6f69973 | 2017-07-18 14:19:11 -0700 | [diff] [blame] | 1056 | def test_role_error(self): |
| 1057 | conf = textwrap.dedent( |
| 1058 | """ |
| 1059 | - job: |
| 1060 | name: project-test |
| 1061 | roles: |
| 1062 | - zuul: common-config |
| 1063 | |
| 1064 | - project: |
| 1065 | name: org/project |
| 1066 | check: |
| 1067 | jobs: |
| 1068 | - project-test |
| 1069 | """) |
| 1070 | |
| 1071 | file_dict = {'.zuul.yaml': conf} |
| 1072 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 1073 | files=file_dict) |
| 1074 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1075 | self.waitUntilSettled() |
| 1076 | self.assertIn( |
| 1077 | '- project-test project-test : ERROR Unable to find role', |
| 1078 | A.messages[-1]) |
| 1079 | |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 1080 | |
James E. Blair | bb94dfa | 2017-07-11 07:45:19 -0700 | [diff] [blame] | 1081 | class TestImplicitRoles(RoleTestCase): |
| 1082 | tenant_config_file = 'config/implicit-roles/main.yaml' |
| 1083 | |
| 1084 | def test_missing_roles(self): |
| 1085 | # Test implicit and explicit roles for a project which does |
| 1086 | # not have roles. The implicit role should be silently |
| 1087 | # ignored since the project doesn't supply roles, but if a |
| 1088 | # user declares an explicit role, it should error. |
| 1089 | self.executor_server.hold_jobs_in_build = True |
| 1090 | A = self.fake_gerrit.addFakeChange('org/norole-project', 'master', 'A') |
| 1091 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1092 | self.waitUntilSettled() |
| 1093 | |
| 1094 | self.assertEqual(len(self.builds), 2) |
| 1095 | build = self.getBuildByName('implicit-role-fail') |
| 1096 | self._assertRolePath(build, 'playbook_0', None) |
| 1097 | |
| 1098 | self.executor_server.hold_jobs_in_build = False |
| 1099 | self.executor_server.release() |
| 1100 | self.waitUntilSettled() |
| 1101 | # The retry_limit doesn't get recorded |
| 1102 | self.assertHistory([ |
| 1103 | dict(name='implicit-role-fail', result='SUCCESS', changes='1,1'), |
| 1104 | ]) |
| 1105 | |
| 1106 | def test_roles(self): |
| 1107 | # Test implicit and explicit roles for a project which does |
| 1108 | # have roles. In both cases, we should end up with the role |
| 1109 | # in the path. In the explicit case, ensure we end up with |
| 1110 | # the name we specified. |
| 1111 | self.executor_server.hold_jobs_in_build = True |
| 1112 | A = self.fake_gerrit.addFakeChange('org/role-project', 'master', 'A') |
| 1113 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1114 | self.waitUntilSettled() |
| 1115 | |
| 1116 | self.assertEqual(len(self.builds), 2) |
| 1117 | build = self.getBuildByName('implicit-role-ok') |
| 1118 | self._assertRolePath(build, 'playbook_0', 'role_0') |
| 1119 | |
| 1120 | build = self.getBuildByName('explicit-role-ok') |
| 1121 | self._assertRolePath(build, 'playbook_0', 'role_0') |
| 1122 | |
| 1123 | self.executor_server.hold_jobs_in_build = False |
| 1124 | self.executor_server.release() |
| 1125 | self.waitUntilSettled() |
| 1126 | self.assertHistory([ |
| 1127 | dict(name='implicit-role-ok', result='SUCCESS', changes='1,1'), |
| 1128 | dict(name='explicit-role-ok', result='SUCCESS', changes='1,1'), |
| 1129 | ], ordered=False) |
| 1130 | |
| 1131 | |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 1132 | class TestShadow(ZuulTestCase): |
| 1133 | tenant_config_file = 'config/shadow/main.yaml' |
| 1134 | |
| 1135 | def test_shadow(self): |
| 1136 | # Test that a repo is allowed to shadow another's job definitions. |
| 1137 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1138 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1139 | self.waitUntilSettled() |
| 1140 | self.assertHistory([ |
| 1141 | dict(name='test1', result='SUCCESS', changes='1,1'), |
| 1142 | dict(name='test2', result='SUCCESS', changes='1,1'), |
James E. Blair | adafa6c | 2017-07-12 08:50:56 -0700 | [diff] [blame] | 1143 | ], ordered=False) |
James E. Blair | 196f61a | 2017-06-30 15:42:29 -0700 | [diff] [blame] | 1144 | |
| 1145 | |
| 1146 | class TestDataReturn(AnsibleZuulTestCase): |
| 1147 | tenant_config_file = 'config/data-return/main.yaml' |
| 1148 | |
| 1149 | def test_data_return(self): |
| 1150 | # This exercises a proposed change to a role being checked out |
| 1151 | # and used. |
| 1152 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1153 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1154 | self.waitUntilSettled() |
| 1155 | self.assertHistory([ |
| 1156 | dict(name='data-return', result='SUCCESS', changes='1,1'), |
James E. Blair | 88e79c0 | 2017-07-07 13:36:54 -0700 | [diff] [blame] | 1157 | dict(name='data-return-relative', result='SUCCESS', changes='1,1'), |
| 1158 | ], ordered=False) |
| 1159 | self.assertIn('- data-return http://example.com/test/log/url/', |
| 1160 | A.messages[-1]) |
| 1161 | self.assertIn('- data-return-relative ' |
| 1162 | 'http://example.com/test/log/url/docs/index.html', |
James E. Blair | 196f61a | 2017-06-30 15:42:29 -0700 | [diff] [blame] | 1163 | A.messages[-1]) |
Clint Byrum | dc8a090 | 2017-07-20 16:36:27 -0700 | [diff] [blame] | 1164 | |
| 1165 | |
| 1166 | class TestDiskAccounting(AnsibleZuulTestCase): |
| 1167 | config_file = 'zuul-disk-accounting.conf' |
| 1168 | tenant_config_file = 'config/disk-accountant/main.yaml' |
| 1169 | |
| 1170 | def test_disk_accountant_kills_job(self): |
| 1171 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1172 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1173 | self.waitUntilSettled() |
| 1174 | self.assertHistory([ |
| 1175 | dict(name='dd-big-empty-file', result='ABORTED', changes='1,1')]) |
Tristan Cacqueray | 82f864b | 2017-08-01 05:54:42 +0000 | [diff] [blame] | 1176 | |
| 1177 | |
| 1178 | class TestMaxNodesPerJob(AnsibleZuulTestCase): |
| 1179 | tenant_config_file = 'config/multi-tenant/main.yaml' |
| 1180 | |
| 1181 | def test_max_nodes_reached(self): |
| 1182 | in_repo_conf = textwrap.dedent( |
| 1183 | """ |
| 1184 | - job: |
| 1185 | name: test-job |
| 1186 | nodes: |
| 1187 | - name: node01 |
| 1188 | label: fake |
| 1189 | - name: node02 |
| 1190 | label: fake |
| 1191 | - name: node03 |
| 1192 | label: fake |
| 1193 | - name: node04 |
| 1194 | label: fake |
| 1195 | - name: node05 |
| 1196 | label: fake |
| 1197 | - name: node06 |
| 1198 | label: fake |
| 1199 | """) |
| 1200 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 1201 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A', |
| 1202 | files=file_dict) |
| 1203 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1204 | self.waitUntilSettled() |
| 1205 | self.assertIn('The job "test-job" exceeds tenant max-nodes-per-job 5.', |
| 1206 | A.messages[0], "A should fail because of nodes limit") |
| 1207 | |
| 1208 | B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A', |
| 1209 | files=file_dict) |
| 1210 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
| 1211 | self.waitUntilSettled() |
| 1212 | self.assertNotIn("exceeds tenant max-nodes", B.messages[0], |
| 1213 | "B should not fail because of nodes limit") |
James E. Blair | 2bab6e7 | 2017-08-07 09:52:45 -0700 | [diff] [blame] | 1214 | |
| 1215 | |
| 1216 | class TestBaseJobs(ZuulTestCase): |
| 1217 | tenant_config_file = 'config/base-jobs/main.yaml' |
| 1218 | |
| 1219 | def test_multiple_base_jobs(self): |
| 1220 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1221 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1222 | self.waitUntilSettled() |
| 1223 | self.assertHistory([ |
| 1224 | dict(name='my-job', result='SUCCESS', changes='1,1'), |
| 1225 | dict(name='other-job', result='SUCCESS', changes='1,1'), |
| 1226 | ], ordered=False) |
| 1227 | self.assertEqual(self.getJobFromHistory('my-job'). |
| 1228 | parameters['zuul']['jobtags'], |
| 1229 | ['mybase']) |
| 1230 | self.assertEqual(self.getJobFromHistory('other-job'). |
| 1231 | parameters['zuul']['jobtags'], |
| 1232 | ['otherbase']) |
| 1233 | |
| 1234 | def test_untrusted_base_job(self): |
| 1235 | """Test that a base job may not be defined in an untrusted repo""" |
| 1236 | in_repo_conf = textwrap.dedent( |
| 1237 | """ |
| 1238 | - job: |
| 1239 | name: fail-base |
| 1240 | parent: null |
| 1241 | """) |
| 1242 | |
| 1243 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 1244 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 1245 | files=file_dict) |
| 1246 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1247 | self.waitUntilSettled() |
| 1248 | self.assertEqual(A.reported, 1, |
| 1249 | "A should report failure") |
| 1250 | self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1") |
| 1251 | self.assertIn('Base jobs must be defined in config projects', |
| 1252 | A.messages[0]) |
| 1253 | self.assertHistory([]) |
James E. Blair | db08903 | 2017-08-15 13:42:12 -0700 | [diff] [blame] | 1254 | |
| 1255 | |
| 1256 | class TestSecretLeaks(AnsibleZuulTestCase): |
| 1257 | tenant_config_file = 'config/secret-leaks/main.yaml' |
| 1258 | |
| 1259 | def searchForContent(self, path, content): |
| 1260 | matches = [] |
| 1261 | for (dirpath, dirnames, filenames) in os.walk(path): |
| 1262 | for filename in filenames: |
| 1263 | filepath = os.path.join(dirpath, filename) |
| 1264 | with open(filepath, 'rb') as f: |
| 1265 | if content in f.read(): |
| 1266 | matches.append(filepath[len(path):]) |
| 1267 | return matches |
| 1268 | |
| 1269 | def _test_secret_file(self): |
| 1270 | # Or rather -- test that they *don't* leak. |
| 1271 | # Keep the jobdir around so we can inspect contents. |
| 1272 | self.executor_server.keep_jobdir = True |
| 1273 | conf = textwrap.dedent( |
| 1274 | """ |
| 1275 | - project: |
| 1276 | name: org/project |
| 1277 | check: |
| 1278 | jobs: |
| 1279 | - secret-file |
| 1280 | """) |
| 1281 | |
| 1282 | file_dict = {'.zuul.yaml': conf} |
| 1283 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 1284 | files=file_dict) |
| 1285 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1286 | self.waitUntilSettled() |
| 1287 | self.assertHistory([ |
| 1288 | dict(name='secret-file', result='SUCCESS', changes='1,1'), |
| 1289 | ], ordered=False) |
| 1290 | matches = self.searchForContent(self.history[0].jobdir.root, |
| 1291 | b'test-password') |
| 1292 | self.assertEqual(set(['/ansible/playbook_0/secrets.yaml', |
| 1293 | '/work/secret-file.txt']), |
| 1294 | set(matches)) |
| 1295 | |
| 1296 | def test_secret_file(self): |
| 1297 | self._test_secret_file() |
| 1298 | |
| 1299 | def test_secret_file_verbose(self): |
| 1300 | # Output extra ansible info to exercise alternate logging code |
| 1301 | # paths. |
| 1302 | self.executor_server.verbose = True |
| 1303 | self._test_secret_file() |
| 1304 | |
| 1305 | def _test_secret_file_fail(self): |
| 1306 | # Or rather -- test that they *don't* leak. |
| 1307 | # Keep the jobdir around so we can inspect contents. |
| 1308 | self.executor_server.keep_jobdir = True |
| 1309 | conf = textwrap.dedent( |
| 1310 | """ |
| 1311 | - project: |
| 1312 | name: org/project |
| 1313 | check: |
| 1314 | jobs: |
| 1315 | - secret-file-fail |
| 1316 | """) |
| 1317 | |
| 1318 | file_dict = {'.zuul.yaml': conf} |
| 1319 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 1320 | files=file_dict) |
| 1321 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1322 | self.waitUntilSettled() |
| 1323 | self.assertHistory([ |
| 1324 | dict(name='secret-file-fail', result='FAILURE', changes='1,1'), |
| 1325 | ], ordered=False) |
| 1326 | matches = self.searchForContent(self.history[0].jobdir.root, |
| 1327 | b'test-password') |
| 1328 | self.assertEqual(set(['/ansible/playbook_0/secrets.yaml', |
| 1329 | '/work/failure-file.txt']), |
| 1330 | set(matches)) |
| 1331 | |
| 1332 | def test_secret_file_fail(self): |
| 1333 | self._test_secret_file_fail() |
| 1334 | |
| 1335 | def test_secret_file_fail_verbose(self): |
| 1336 | # Output extra ansible info to exercise alternate logging code |
| 1337 | # paths. |
| 1338 | self.executor_server.verbose = True |
| 1339 | self._test_secret_file_fail() |