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