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 | |
Monty Taylor | 0e2489a | 2017-10-10 11:57:29 -0500 | [diff] [blame] | 17 | import io |
James E. Blair | a00910c | 2017-08-23 09:15:04 -0700 | [diff] [blame] | 18 | import json |
Monty Taylor | 0e2489a | 2017-10-10 11:57:29 -0500 | [diff] [blame] | 19 | import logging |
James E. Blair | a92cbc8 | 2017-01-23 14:56:49 -0800 | [diff] [blame] | 20 | import os |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 21 | import textwrap |
James E. Blair | 3a098dd | 2017-10-04 14:37:29 -0700 | [diff] [blame] | 22 | import gc |
James E. Blair | bacbb88 | 2017-10-17 09:48:23 -0700 | [diff] [blame] | 23 | import time |
James E. Blair | 3a098dd | 2017-10-04 14:37:29 -0700 | [diff] [blame] | 24 | from unittest import skip |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 25 | |
James E. Blair | b9c0d77 | 2017-03-03 14:34:49 -0800 | [diff] [blame] | 26 | import testtools |
| 27 | |
| 28 | import zuul.configloader |
James E. Blair | bf1a4f2 | 2017-03-17 10:59:37 -0700 | [diff] [blame] | 29 | from zuul.lib import encryption |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 30 | from tests.base import AnsibleZuulTestCase, ZuulTestCase, FIXTURE_DIR |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 31 | |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 32 | |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 33 | class TestMultipleTenants(AnsibleZuulTestCase): |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 34 | # A temporary class to hold new tests while others are disabled |
| 35 | |
James E. Blair | 2a629ec | 2015-12-22 15:32:02 -0800 | [diff] [blame] | 36 | tenant_config_file = 'config/multi-tenant/main.yaml' |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 37 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 38 | def test_multiple_tenants(self): |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 39 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 40 | A.addApproval('Code-Review', 2) |
| 41 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 42 | self.waitUntilSettled() |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 43 | self.assertEqual(self.getJobFromHistory('project1-test1').result, |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 44 | 'SUCCESS') |
James E. Blair | 96c6bf8 | 2016-01-15 16:20:40 -0800 | [diff] [blame] | 45 | self.assertEqual(self.getJobFromHistory('python27').result, |
| 46 | 'SUCCESS') |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 47 | self.assertEqual(A.data['status'], 'MERGED') |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 48 | self.assertEqual(A.reported, 2, |
| 49 | "A should report start and success") |
| 50 | self.assertIn('tenant-one-gate', A.messages[1], |
| 51 | "A should transit tenant-one gate") |
| 52 | self.assertNotIn('tenant-two-gate', A.messages[1], |
| 53 | "A should *not* transit tenant-two gate") |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 54 | |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 55 | B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B') |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 56 | B.addApproval('Code-Review', 2) |
| 57 | self.fake_gerrit.addEvent(B.addApproval('Approved', 1)) |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 58 | self.waitUntilSettled() |
James E. Blair | 96c6bf8 | 2016-01-15 16:20:40 -0800 | [diff] [blame] | 59 | self.assertEqual(self.getJobFromHistory('python27', |
| 60 | 'org/project2').result, |
| 61 | 'SUCCESS') |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 62 | self.assertEqual(self.getJobFromHistory('project2-test1').result, |
| 63 | 'SUCCESS') |
| 64 | self.assertEqual(B.data['status'], 'MERGED') |
| 65 | self.assertEqual(B.reported, 2, |
| 66 | "B should report start and success") |
| 67 | self.assertIn('tenant-two-gate', B.messages[1], |
| 68 | "B should transit tenant-two gate") |
| 69 | self.assertNotIn('tenant-one-gate', B.messages[1], |
| 70 | "B should *not* transit tenant-one gate") |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 71 | |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 72 | self.assertEqual(A.reported, 2, "Activity in tenant two should" |
| 73 | "not affect tenant one") |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 74 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 75 | |
Tobias Henkel | 8316762 | 2017-06-30 19:45:03 +0200 | [diff] [blame] | 76 | class TestFinal(ZuulTestCase): |
| 77 | |
| 78 | tenant_config_file = 'config/final/main.yaml' |
| 79 | |
| 80 | def test_final_variant_ok(self): |
| 81 | # test clean usage of final parent job |
| 82 | in_repo_conf = textwrap.dedent( |
| 83 | """ |
| 84 | - project: |
| 85 | name: org/project |
| 86 | check: |
| 87 | jobs: |
| 88 | - job-final |
| 89 | """) |
| 90 | |
| 91 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 92 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 93 | files=file_dict) |
| 94 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 95 | self.waitUntilSettled() |
| 96 | |
| 97 | self.assertEqual(A.reported, 1) |
| 98 | self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '1') |
| 99 | |
| 100 | def test_final_variant_error(self): |
| 101 | # test misuse of final parent job |
| 102 | in_repo_conf = textwrap.dedent( |
| 103 | """ |
| 104 | - project: |
| 105 | name: org/project |
| 106 | check: |
| 107 | jobs: |
| 108 | - job-final: |
| 109 | vars: |
| 110 | dont_override_this: bar |
| 111 | """) |
| 112 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 113 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 114 | files=file_dict) |
| 115 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 116 | self.waitUntilSettled() |
| 117 | |
| 118 | # The second patch tried to override some variables. |
| 119 | # Thus it should fail. |
| 120 | self.assertEqual(A.reported, 1) |
| 121 | self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '-1') |
| 122 | self.assertIn('Unable to modify final job', A.messages[0]) |
| 123 | |
| 124 | def test_final_inheritance(self): |
| 125 | # test misuse of final parent job |
| 126 | in_repo_conf = textwrap.dedent( |
| 127 | """ |
| 128 | - job: |
| 129 | name: project-test |
| 130 | parent: job-final |
| 131 | |
| 132 | - project: |
| 133 | name: org/project |
| 134 | check: |
| 135 | jobs: |
| 136 | - project-test |
| 137 | """) |
| 138 | |
| 139 | in_repo_playbook = textwrap.dedent( |
| 140 | """ |
| 141 | - hosts: all |
| 142 | tasks: [] |
| 143 | """) |
| 144 | |
| 145 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 146 | 'playbooks/project-test.yaml': in_repo_playbook} |
| 147 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 148 | files=file_dict) |
| 149 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 150 | self.waitUntilSettled() |
| 151 | |
| 152 | # The second patch tried to override some variables. |
| 153 | # Thus it should fail. |
| 154 | self.assertEqual(A.reported, 1) |
| 155 | self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '-1') |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 156 | self.assertIn('Unable to modify final job', A.messages[0]) |
Tobias Henkel | 8316762 | 2017-06-30 19:45:03 +0200 | [diff] [blame] | 157 | |
| 158 | |
James E. Blair | 0999879 | 2017-10-15 18:02:18 -0700 | [diff] [blame] | 159 | class TestBranchVariants(ZuulTestCase): |
| 160 | tenant_config_file = 'config/branch-variants/main.yaml' |
| 161 | |
| 162 | def test_branch_variants(self): |
| 163 | # Test branch variants of jobs with inheritance |
| 164 | self.executor_server.hold_jobs_in_build = True |
| 165 | # This creates a new branch with a copy of the config in master |
| 166 | self.create_branch('puppet-integration', 'stable') |
| 167 | self.fake_gerrit.addEvent( |
| 168 | self.fake_gerrit.getFakeBranchCreatedEvent( |
| 169 | 'puppet-integration', 'stable')) |
| 170 | self.waitUntilSettled() |
| 171 | |
| 172 | A = self.fake_gerrit.addFakeChange('puppet-integration', 'stable', 'A') |
| 173 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 174 | self.waitUntilSettled() |
| 175 | |
| 176 | self.assertEqual(len(self.builds[0].parameters['pre_playbooks']), 3) |
| 177 | self.executor_server.hold_jobs_in_build = False |
| 178 | self.executor_server.release() |
| 179 | self.waitUntilSettled() |
| 180 | |
James E. Blair | c9e7759 | 2017-10-24 09:25:23 -0700 | [diff] [blame] | 181 | def test_branch_variants_reconfigure(self): |
| 182 | # Test branch variants of jobs with inheritance |
| 183 | self.executor_server.hold_jobs_in_build = True |
| 184 | # This creates a new branch with a copy of the config in master |
| 185 | self.create_branch('puppet-integration', 'stable') |
| 186 | self.fake_gerrit.addEvent( |
| 187 | self.fake_gerrit.getFakeBranchCreatedEvent( |
| 188 | 'puppet-integration', 'stable')) |
| 189 | self.waitUntilSettled() |
| 190 | |
| 191 | with open(os.path.join(FIXTURE_DIR, |
| 192 | 'config/branch-variants/git/', |
| 193 | 'puppet-integration/.zuul.yaml')) as f: |
| 194 | config = f.read() |
| 195 | |
| 196 | # Push a change that triggers a dynamic reconfiguration |
| 197 | file_dict = {'.zuul.yaml': config} |
| 198 | A = self.fake_gerrit.addFakeChange('puppet-integration', 'master', 'A', |
| 199 | files=file_dict) |
| 200 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 201 | self.waitUntilSettled() |
| 202 | |
| 203 | ipath = self.builds[0].parameters['zuul']['_inheritance_path'] |
| 204 | for i in ipath: |
| 205 | self.log.debug("inheritance path %s", i) |
| 206 | self.assertEqual(len(ipath), 5) |
| 207 | self.executor_server.hold_jobs_in_build = False |
| 208 | self.executor_server.release() |
| 209 | self.waitUntilSettled() |
| 210 | |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 211 | def test_branch_variants_divergent(self): |
| 212 | # Test branches can diverge and become independent |
| 213 | self.executor_server.hold_jobs_in_build = True |
| 214 | # This creates a new branch with a copy of the config in master |
| 215 | self.create_branch('puppet-integration', 'stable') |
| 216 | self.fake_gerrit.addEvent( |
| 217 | self.fake_gerrit.getFakeBranchCreatedEvent( |
| 218 | 'puppet-integration', 'stable')) |
| 219 | self.waitUntilSettled() |
| 220 | |
| 221 | with open(os.path.join(FIXTURE_DIR, |
| 222 | 'config/branch-variants/git/', |
| 223 | 'puppet-integration/stable.zuul.yaml')) as f: |
| 224 | config = f.read() |
| 225 | |
| 226 | file_dict = {'.zuul.yaml': config} |
| 227 | C = self.fake_gerrit.addFakeChange('puppet-integration', 'stable', 'C', |
| 228 | files=file_dict) |
| 229 | C.addApproval('Code-Review', 2) |
| 230 | self.fake_gerrit.addEvent(C.addApproval('Approved', 1)) |
| 231 | self.waitUntilSettled() |
| 232 | self.fake_gerrit.addEvent(C.getChangeMergedEvent()) |
| 233 | self.waitUntilSettled() |
| 234 | |
| 235 | A = self.fake_gerrit.addFakeChange('puppet-integration', 'master', 'A') |
| 236 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 237 | self.waitUntilSettled() |
| 238 | B = self.fake_gerrit.addFakeChange('puppet-integration', 'stable', 'B') |
| 239 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
| 240 | self.waitUntilSettled() |
| 241 | |
| 242 | self.assertEqual(self.builds[0].parameters['zuul']['jobtags'], |
| 243 | ['master']) |
| 244 | |
| 245 | self.assertEqual(self.builds[1].parameters['zuul']['jobtags'], |
| 246 | ['stable']) |
| 247 | |
| 248 | self.executor_server.hold_jobs_in_build = False |
| 249 | self.executor_server.release() |
| 250 | self.waitUntilSettled() |
| 251 | |
James E. Blair | 0999879 | 2017-10-15 18:02:18 -0700 | [diff] [blame] | 252 | |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 253 | class TestInRepoConfig(ZuulTestCase): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 254 | # A temporary class to hold new tests while others are disabled |
| 255 | |
Tobias Henkel | abf973e | 2017-07-28 10:07:34 +0200 | [diff] [blame] | 256 | config_file = 'zuul-connections-gerrit-and-github.conf' |
James E. Blair | 2a629ec | 2015-12-22 15:32:02 -0800 | [diff] [blame] | 257 | tenant_config_file = 'config/in-repo/main.yaml' |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 258 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 259 | def test_in_repo_config(self): |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 260 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 261 | A.addApproval('Code-Review', 2) |
| 262 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 263 | self.waitUntilSettled() |
| 264 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 265 | 'SUCCESS') |
| 266 | self.assertEqual(A.data['status'], 'MERGED') |
| 267 | self.assertEqual(A.reported, 2, |
| 268 | "A should report start and success") |
| 269 | self.assertIn('tenant-one-gate', A.messages[1], |
| 270 | "A should transit tenant-one gate") |
James E. Blair | b97ed80 | 2015-12-21 15:55:35 -0800 | [diff] [blame] | 271 | |
James E. Blair | 3a098dd | 2017-10-04 14:37:29 -0700 | [diff] [blame] | 272 | @skip("This test is useful, but not reliable") |
| 273 | def test_full_and_dynamic_reconfig(self): |
| 274 | self.executor_server.hold_jobs_in_build = True |
| 275 | in_repo_conf = textwrap.dedent( |
| 276 | """ |
| 277 | - job: |
| 278 | name: project-test1 |
| 279 | |
| 280 | - project: |
| 281 | name: org/project |
| 282 | tenant-one-gate: |
| 283 | jobs: |
| 284 | - project-test1 |
| 285 | """) |
| 286 | |
| 287 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 288 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 289 | files=file_dict) |
| 290 | A.addApproval('Code-Review', 2) |
| 291 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
| 292 | self.waitUntilSettled() |
| 293 | self.sched.reconfigure(self.config) |
| 294 | self.waitUntilSettled() |
| 295 | |
| 296 | gc.collect() |
| 297 | pipelines = [obj for obj in gc.get_objects() |
| 298 | if isinstance(obj, zuul.model.Pipeline)] |
| 299 | self.assertEqual(len(pipelines), 4) |
| 300 | |
| 301 | self.executor_server.hold_jobs_in_build = False |
| 302 | self.executor_server.release() |
| 303 | self.waitUntilSettled() |
| 304 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 305 | def test_dynamic_config(self): |
| 306 | in_repo_conf = textwrap.dedent( |
| 307 | """ |
| 308 | - job: |
Tobias Henkel | f02cf51 | 2017-07-21 22:55:34 +0200 | [diff] [blame] | 309 | name: project-test1 |
| 310 | |
| 311 | - job: |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 312 | name: project-test2 |
| 313 | |
| 314 | - project: |
| 315 | name: org/project |
| 316 | tenant-one-gate: |
| 317 | jobs: |
| 318 | - project-test2 |
| 319 | """) |
| 320 | |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 321 | in_repo_playbook = textwrap.dedent( |
| 322 | """ |
| 323 | - hosts: all |
| 324 | tasks: [] |
| 325 | """) |
| 326 | |
| 327 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 328 | 'playbooks/project-test2.yaml': in_repo_playbook} |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 329 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 330 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 331 | A.addApproval('Code-Review', 2) |
| 332 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 333 | self.waitUntilSettled() |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 334 | self.assertEqual(A.data['status'], 'MERGED') |
| 335 | self.assertEqual(A.reported, 2, |
| 336 | "A should report start and success") |
| 337 | self.assertIn('tenant-one-gate', A.messages[1], |
| 338 | "A should transit tenant-one gate") |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 339 | self.assertHistory([ |
| 340 | dict(name='project-test2', result='SUCCESS', changes='1,1')]) |
| 341 | |
James E. Blair | c2a5ed7 | 2017-02-20 14:12:01 -0500 | [diff] [blame] | 342 | self.fake_gerrit.addEvent(A.getChangeMergedEvent()) |
James E. Blair | 7bbd7a3 | 2017-03-06 11:36:13 -0800 | [diff] [blame] | 343 | self.waitUntilSettled() |
James E. Blair | c2a5ed7 | 2017-02-20 14:12:01 -0500 | [diff] [blame] | 344 | |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 345 | # Now that the config change is landed, it should be live for |
| 346 | # subsequent changes. |
| 347 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 348 | B.addApproval('Code-Review', 2) |
| 349 | self.fake_gerrit.addEvent(B.addApproval('Approved', 1)) |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 350 | self.waitUntilSettled() |
| 351 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 352 | 'SUCCESS') |
| 353 | self.assertHistory([ |
| 354 | dict(name='project-test2', result='SUCCESS', changes='1,1'), |
| 355 | dict(name='project-test2', result='SUCCESS', changes='2,1')]) |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 356 | |
James E. Blair | 6bc1048 | 2017-10-20 11:28:53 -0700 | [diff] [blame] | 357 | def test_dynamic_template(self): |
| 358 | in_repo_conf = textwrap.dedent( |
| 359 | """ |
| 360 | - job: |
| 361 | name: project-test1 |
| 362 | |
| 363 | - project-template: |
| 364 | name: common-config-template |
| 365 | check: |
| 366 | jobs: |
| 367 | - project-test1 |
| 368 | |
| 369 | - project: |
| 370 | name: org/project |
| 371 | templates: [common-config-template] |
| 372 | """) |
| 373 | |
| 374 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 375 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 376 | files=file_dict) |
| 377 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 378 | self.waitUntilSettled() |
| 379 | self.assertHistory([ |
| 380 | dict(name='template-job', result='SUCCESS', changes='1,1')]) |
| 381 | |
Tobias Henkel | f02cf51 | 2017-07-21 22:55:34 +0200 | [diff] [blame] | 382 | def test_dynamic_config_non_existing_job(self): |
| 383 | """Test that requesting a non existent job fails""" |
| 384 | in_repo_conf = textwrap.dedent( |
| 385 | """ |
| 386 | - job: |
| 387 | name: project-test1 |
| 388 | |
| 389 | - project: |
| 390 | name: org/project |
| 391 | check: |
| 392 | jobs: |
| 393 | - non-existent-job |
| 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) |
| 406 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 407 | self.waitUntilSettled() |
| 408 | self.assertEqual(A.reported, 1, |
| 409 | "A should report failure") |
| 410 | self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1") |
| 411 | self.assertIn('Job non-existent-job not defined', A.messages[0], |
| 412 | "A should have failed the check pipeline") |
| 413 | self.assertHistory([]) |
| 414 | |
| 415 | def test_dynamic_config_non_existing_job_in_template(self): |
| 416 | """Test that requesting a non existent job fails""" |
| 417 | in_repo_conf = textwrap.dedent( |
| 418 | """ |
| 419 | - job: |
| 420 | name: project-test1 |
| 421 | |
| 422 | - project-template: |
| 423 | name: test-template |
| 424 | check: |
| 425 | jobs: |
| 426 | - non-existent-job |
| 427 | |
| 428 | - project: |
| 429 | name: org/project |
| 430 | templates: |
| 431 | - test-template |
| 432 | """) |
| 433 | |
| 434 | in_repo_playbook = textwrap.dedent( |
| 435 | """ |
| 436 | - hosts: all |
| 437 | tasks: [] |
| 438 | """) |
| 439 | |
| 440 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 441 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 442 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 443 | files=file_dict) |
| 444 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 445 | self.waitUntilSettled() |
| 446 | self.assertEqual(A.reported, 1, |
| 447 | "A should report failure") |
| 448 | self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1") |
| 449 | self.assertIn('Job non-existent-job not defined', A.messages[0], |
| 450 | "A should have failed the check pipeline") |
| 451 | self.assertHistory([]) |
| 452 | |
Tobias Henkel | 0f71400 | 2017-06-30 23:30:52 +0200 | [diff] [blame] | 453 | def test_dynamic_config_new_patchset(self): |
| 454 | self.executor_server.hold_jobs_in_build = True |
| 455 | |
| 456 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 457 | check_pipeline = tenant.layout.pipelines['check'] |
| 458 | |
| 459 | in_repo_conf = textwrap.dedent( |
| 460 | """ |
| 461 | - job: |
Tobias Henkel | f02cf51 | 2017-07-21 22:55:34 +0200 | [diff] [blame] | 462 | name: project-test1 |
| 463 | |
| 464 | - job: |
Tobias Henkel | 0f71400 | 2017-06-30 23:30:52 +0200 | [diff] [blame] | 465 | name: project-test2 |
| 466 | |
| 467 | - project: |
| 468 | name: org/project |
| 469 | check: |
| 470 | jobs: |
| 471 | - project-test2 |
| 472 | """) |
| 473 | |
| 474 | in_repo_playbook = textwrap.dedent( |
| 475 | """ |
| 476 | - hosts: all |
| 477 | tasks: [] |
| 478 | """) |
| 479 | |
| 480 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 481 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 482 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 483 | files=file_dict) |
| 484 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 485 | self.waitUntilSettled() |
| 486 | |
| 487 | items = check_pipeline.getAllItems() |
| 488 | self.assertEqual(items[0].change.number, '1') |
| 489 | self.assertEqual(items[0].change.patchset, '1') |
| 490 | self.assertTrue(items[0].live) |
| 491 | |
| 492 | in_repo_conf = textwrap.dedent( |
| 493 | """ |
| 494 | - job: |
Tobias Henkel | 0ce7ec6 | 2017-07-21 22:50:17 +0200 | [diff] [blame] | 495 | name: project-test1 |
| 496 | |
| 497 | - job: |
Tobias Henkel | 0f71400 | 2017-06-30 23:30:52 +0200 | [diff] [blame] | 498 | name: project-test2 |
| 499 | |
| 500 | - project: |
| 501 | name: org/project |
| 502 | check: |
| 503 | jobs: |
| 504 | - project-test1 |
| 505 | - project-test2 |
| 506 | """) |
| 507 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 508 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 509 | |
| 510 | A.addPatchset(files=file_dict) |
| 511 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2)) |
| 512 | |
| 513 | self.waitUntilSettled() |
| 514 | |
| 515 | items = check_pipeline.getAllItems() |
| 516 | self.assertEqual(items[0].change.number, '1') |
| 517 | self.assertEqual(items[0].change.patchset, '2') |
| 518 | self.assertTrue(items[0].live) |
| 519 | |
| 520 | self.executor_server.hold_jobs_in_build = False |
Tobias Henkel | 0ce7ec6 | 2017-07-21 22:50:17 +0200 | [diff] [blame] | 521 | self.executor_server.release('project-test1') |
| 522 | self.waitUntilSettled() |
Tobias Henkel | 0f71400 | 2017-06-30 23:30:52 +0200 | [diff] [blame] | 523 | self.executor_server.release() |
| 524 | self.waitUntilSettled() |
| 525 | |
Tobias Henkel | 0ce7ec6 | 2017-07-21 22:50:17 +0200 | [diff] [blame] | 526 | self.assertHistory([ |
| 527 | dict(name='project-test2', result='ABORTED', changes='1,1'), |
| 528 | dict(name='project-test1', result='SUCCESS', changes='1,2'), |
| 529 | dict(name='project-test2', result='SUCCESS', changes='1,2')]) |
| 530 | |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 531 | def test_in_repo_branch(self): |
| 532 | in_repo_conf = textwrap.dedent( |
| 533 | """ |
| 534 | - job: |
Tobias Henkel | f02cf51 | 2017-07-21 22:55:34 +0200 | [diff] [blame] | 535 | name: project-test1 |
| 536 | |
| 537 | - job: |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 538 | name: project-test2 |
| 539 | |
| 540 | - project: |
| 541 | name: org/project |
| 542 | tenant-one-gate: |
| 543 | jobs: |
| 544 | - project-test2 |
| 545 | """) |
| 546 | |
| 547 | in_repo_playbook = textwrap.dedent( |
| 548 | """ |
| 549 | - hosts: all |
| 550 | tasks: [] |
| 551 | """) |
| 552 | |
| 553 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 554 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 555 | self.create_branch('org/project', 'stable') |
James E. Blair | 72facdc | 2017-08-17 10:29:12 -0700 | [diff] [blame] | 556 | self.fake_gerrit.addEvent( |
| 557 | self.fake_gerrit.getFakeBranchCreatedEvent( |
| 558 | 'org/project', 'stable')) |
James E. Blair | 6069f2b | 2017-09-26 16:34:11 -0700 | [diff] [blame] | 559 | self.waitUntilSettled() |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 560 | A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A', |
| 561 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 562 | A.addApproval('Code-Review', 2) |
| 563 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 564 | self.waitUntilSettled() |
| 565 | self.assertEqual(A.data['status'], 'MERGED') |
| 566 | self.assertEqual(A.reported, 2, |
| 567 | "A should report start and success") |
| 568 | self.assertIn('tenant-one-gate', A.messages[1], |
| 569 | "A should transit tenant-one gate") |
| 570 | self.assertHistory([ |
| 571 | dict(name='project-test2', result='SUCCESS', changes='1,1')]) |
| 572 | self.fake_gerrit.addEvent(A.getChangeMergedEvent()) |
James E. Blair | 7bbd7a3 | 2017-03-06 11:36:13 -0800 | [diff] [blame] | 573 | self.waitUntilSettled() |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 574 | |
| 575 | # The config change should not affect master. |
| 576 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 577 | B.addApproval('Code-Review', 2) |
| 578 | self.fake_gerrit.addEvent(B.addApproval('Approved', 1)) |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 579 | self.waitUntilSettled() |
| 580 | self.assertHistory([ |
| 581 | dict(name='project-test2', result='SUCCESS', changes='1,1'), |
| 582 | dict(name='project-test1', result='SUCCESS', changes='2,1')]) |
| 583 | |
| 584 | # The config change should be live for further changes on |
| 585 | # stable. |
| 586 | C = self.fake_gerrit.addFakeChange('org/project', 'stable', 'C') |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 587 | C.addApproval('Code-Review', 2) |
| 588 | self.fake_gerrit.addEvent(C.addApproval('Approved', 1)) |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 589 | self.waitUntilSettled() |
| 590 | self.assertHistory([ |
| 591 | dict(name='project-test2', result='SUCCESS', changes='1,1'), |
| 592 | dict(name='project-test1', result='SUCCESS', changes='2,1'), |
| 593 | dict(name='project-test2', result='SUCCESS', changes='3,1')]) |
| 594 | |
James E. Blair | a5a1249 | 2017-05-03 11:40:48 -0700 | [diff] [blame] | 595 | def test_crd_dynamic_config_branch(self): |
| 596 | # Test that we can create a job in one repo and be able to use |
| 597 | # it from a different branch on a different repo. |
| 598 | |
| 599 | self.create_branch('org/project1', 'stable') |
James E. Blair | 72facdc | 2017-08-17 10:29:12 -0700 | [diff] [blame] | 600 | self.fake_gerrit.addEvent( |
| 601 | self.fake_gerrit.getFakeBranchCreatedEvent( |
| 602 | 'org/project1', 'stable')) |
James E. Blair | a5a1249 | 2017-05-03 11:40:48 -0700 | [diff] [blame] | 603 | |
| 604 | in_repo_conf = textwrap.dedent( |
| 605 | """ |
| 606 | - job: |
Tobias Henkel | f02cf51 | 2017-07-21 22:55:34 +0200 | [diff] [blame] | 607 | name: project-test1 |
| 608 | |
| 609 | - job: |
James E. Blair | a5a1249 | 2017-05-03 11:40:48 -0700 | [diff] [blame] | 610 | name: project-test2 |
| 611 | |
| 612 | - project: |
| 613 | name: org/project |
| 614 | check: |
| 615 | jobs: |
| 616 | - project-test2 |
| 617 | """) |
| 618 | |
| 619 | in_repo_playbook = textwrap.dedent( |
| 620 | """ |
| 621 | - hosts: all |
| 622 | tasks: [] |
| 623 | """) |
| 624 | |
| 625 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 626 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 627 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 628 | files=file_dict) |
| 629 | |
| 630 | second_repo_conf = textwrap.dedent( |
| 631 | """ |
| 632 | - project: |
| 633 | name: org/project1 |
| 634 | check: |
| 635 | jobs: |
| 636 | - project-test2 |
| 637 | """) |
| 638 | |
| 639 | second_file_dict = {'.zuul.yaml': second_repo_conf} |
| 640 | B = self.fake_gerrit.addFakeChange('org/project1', 'stable', 'B', |
| 641 | files=second_file_dict) |
| 642 | B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % ( |
| 643 | B.subject, A.data['id']) |
| 644 | |
| 645 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 646 | self.waitUntilSettled() |
| 647 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
| 648 | self.waitUntilSettled() |
| 649 | |
| 650 | self.assertEqual(A.reported, 1, "A should report") |
| 651 | self.assertHistory([ |
| 652 | dict(name='project-test2', result='SUCCESS', changes='1,1'), |
| 653 | dict(name='project-test2', result='SUCCESS', changes='1,1 2,1'), |
| 654 | ]) |
| 655 | |
James E. Blair | 9704388 | 2017-09-06 15:51:17 -0700 | [diff] [blame] | 656 | def test_yaml_list_error(self): |
| 657 | in_repo_conf = textwrap.dedent( |
| 658 | """ |
| 659 | job: foo |
| 660 | """) |
| 661 | |
| 662 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 663 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 664 | files=file_dict) |
| 665 | A.addApproval('Code-Review', 2) |
| 666 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
| 667 | self.waitUntilSettled() |
| 668 | |
| 669 | self.assertEqual(A.data['status'], 'NEW') |
| 670 | self.assertEqual(A.reported, 1, |
| 671 | "A should report failure") |
| 672 | self.assertIn('not a list', A.messages[0], |
| 673 | "A should have a syntax error reported") |
| 674 | |
| 675 | def test_yaml_dict_error(self): |
| 676 | in_repo_conf = textwrap.dedent( |
| 677 | """ |
| 678 | - job |
| 679 | """) |
| 680 | |
| 681 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 682 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 683 | files=file_dict) |
| 684 | A.addApproval('Code-Review', 2) |
| 685 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
| 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('not a dictionary', A.messages[0], |
| 692 | "A should have a syntax error reported") |
| 693 | |
| 694 | def test_yaml_key_error(self): |
| 695 | in_repo_conf = textwrap.dedent( |
| 696 | """ |
| 697 | - job: |
| 698 | name: project-test2 |
| 699 | """) |
| 700 | |
| 701 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 702 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 703 | files=file_dict) |
| 704 | A.addApproval('Code-Review', 2) |
| 705 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
| 706 | self.waitUntilSettled() |
| 707 | |
| 708 | self.assertEqual(A.data['status'], 'NEW') |
| 709 | self.assertEqual(A.reported, 1, |
| 710 | "A should report failure") |
| 711 | self.assertIn('has more than one key', A.messages[0], |
| 712 | "A should have a syntax error reported") |
| 713 | |
| 714 | def test_yaml_unknown_error(self): |
| 715 | in_repo_conf = textwrap.dedent( |
| 716 | """ |
| 717 | - foobar: |
| 718 | foo: bar |
| 719 | """) |
| 720 | |
| 721 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 722 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 723 | files=file_dict) |
| 724 | A.addApproval('Code-Review', 2) |
| 725 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
| 726 | self.waitUntilSettled() |
| 727 | |
| 728 | self.assertEqual(A.data['status'], 'NEW') |
| 729 | self.assertEqual(A.reported, 1, |
| 730 | "A should report failure") |
| 731 | self.assertIn('not recognized', A.messages[0], |
| 732 | "A should have a syntax error reported") |
| 733 | |
James E. Blair | 149b69c | 2017-03-02 10:48:16 -0800 | [diff] [blame] | 734 | def test_untrusted_syntax_error(self): |
James E. Blair | e53250c | 2017-03-01 14:34:36 -0800 | [diff] [blame] | 735 | in_repo_conf = textwrap.dedent( |
| 736 | """ |
| 737 | - job: |
| 738 | name: project-test2 |
| 739 | foo: error |
| 740 | """) |
| 741 | |
| 742 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 743 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 744 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 745 | A.addApproval('Code-Review', 2) |
| 746 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | e53250c | 2017-03-01 14:34:36 -0800 | [diff] [blame] | 747 | self.waitUntilSettled() |
| 748 | |
| 749 | self.assertEqual(A.data['status'], 'NEW') |
Tobias Henkel | 9842bd7 | 2017-05-16 13:40:03 +0200 | [diff] [blame] | 750 | self.assertEqual(A.reported, 1, |
| 751 | "A should report failure") |
| 752 | self.assertIn('syntax error', A.messages[0], |
James E. Blair | e53250c | 2017-03-01 14:34:36 -0800 | [diff] [blame] | 753 | "A should have a syntax error reported") |
| 754 | |
James E. Blair | 149b69c | 2017-03-02 10:48:16 -0800 | [diff] [blame] | 755 | def test_trusted_syntax_error(self): |
| 756 | in_repo_conf = textwrap.dedent( |
| 757 | """ |
| 758 | - job: |
| 759 | name: project-test2 |
| 760 | foo: error |
| 761 | """) |
| 762 | |
| 763 | file_dict = {'zuul.yaml': in_repo_conf} |
| 764 | A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A', |
| 765 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 766 | A.addApproval('Code-Review', 2) |
| 767 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | 149b69c | 2017-03-02 10:48:16 -0800 | [diff] [blame] | 768 | self.waitUntilSettled() |
| 769 | |
| 770 | self.assertEqual(A.data['status'], 'NEW') |
Tobias Henkel | 9842bd7 | 2017-05-16 13:40:03 +0200 | [diff] [blame] | 771 | self.assertEqual(A.reported, 1, |
| 772 | "A should report failure") |
| 773 | self.assertIn('syntax error', A.messages[0], |
James E. Blair | 149b69c | 2017-03-02 10:48:16 -0800 | [diff] [blame] | 774 | "A should have a syntax error reported") |
| 775 | |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 776 | def test_untrusted_yaml_error(self): |
| 777 | in_repo_conf = textwrap.dedent( |
| 778 | """ |
| 779 | - job: |
| 780 | foo: error |
| 781 | """) |
| 782 | |
| 783 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 784 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 785 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 786 | A.addApproval('Code-Review', 2) |
| 787 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 788 | self.waitUntilSettled() |
| 789 | |
| 790 | self.assertEqual(A.data['status'], 'NEW') |
Tobias Henkel | 9842bd7 | 2017-05-16 13:40:03 +0200 | [diff] [blame] | 791 | self.assertEqual(A.reported, 1, |
| 792 | "A should report failure") |
| 793 | self.assertIn('syntax error', A.messages[0], |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 794 | "A should have a syntax error reported") |
| 795 | |
James E. Blair | db04e6a | 2017-05-03 14:49:36 -0700 | [diff] [blame] | 796 | def test_untrusted_shadow_error(self): |
| 797 | in_repo_conf = textwrap.dedent( |
| 798 | """ |
| 799 | - job: |
| 800 | name: common-config-test |
| 801 | """) |
| 802 | |
| 803 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 804 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 805 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 806 | A.addApproval('Code-Review', 2) |
| 807 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | db04e6a | 2017-05-03 14:49:36 -0700 | [diff] [blame] | 808 | self.waitUntilSettled() |
| 809 | |
| 810 | self.assertEqual(A.data['status'], 'NEW') |
Tobias Henkel | 9842bd7 | 2017-05-16 13:40:03 +0200 | [diff] [blame] | 811 | self.assertEqual(A.reported, 1, |
| 812 | "A should report failure") |
| 813 | self.assertIn('not permitted to shadow', A.messages[0], |
James E. Blair | db04e6a | 2017-05-03 14:49:36 -0700 | [diff] [blame] | 814 | "A should have a syntax error reported") |
| 815 | |
James E. Blair | d5656ad | 2017-06-02 14:29:41 -0700 | [diff] [blame] | 816 | def test_untrusted_pipeline_error(self): |
| 817 | in_repo_conf = textwrap.dedent( |
| 818 | """ |
| 819 | - pipeline: |
| 820 | name: test |
| 821 | """) |
| 822 | |
| 823 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 824 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 825 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 826 | A.addApproval('Code-Review', 2) |
| 827 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | d5656ad | 2017-06-02 14:29:41 -0700 | [diff] [blame] | 828 | self.waitUntilSettled() |
| 829 | |
| 830 | self.assertEqual(A.data['status'], 'NEW') |
| 831 | self.assertEqual(A.reported, 1, |
| 832 | "A should report failure") |
| 833 | self.assertIn('Pipelines may not be defined', A.messages[0], |
| 834 | "A should have a syntax error reported") |
| 835 | |
| 836 | def test_untrusted_project_error(self): |
| 837 | in_repo_conf = textwrap.dedent( |
| 838 | """ |
| 839 | - project: |
| 840 | name: org/project1 |
| 841 | """) |
| 842 | |
| 843 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 844 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 845 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 846 | A.addApproval('Code-Review', 2) |
| 847 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | d5656ad | 2017-06-02 14:29:41 -0700 | [diff] [blame] | 848 | self.waitUntilSettled() |
| 849 | |
| 850 | self.assertEqual(A.data['status'], 'NEW') |
| 851 | self.assertEqual(A.reported, 1, |
| 852 | "A should report failure") |
| 853 | self.assertIn('the only project definition permitted', A.messages[0], |
| 854 | "A should have a syntax error reported") |
| 855 | |
James E. Blair | f03173b | 2017-10-10 10:46:43 -0700 | [diff] [blame] | 856 | def test_untrusted_depends_on_trusted(self): |
| 857 | with open(os.path.join(FIXTURE_DIR, |
| 858 | 'config/in-repo/git/', |
| 859 | 'common-config/zuul.yaml')) as f: |
| 860 | common_config = f.read() |
| 861 | |
| 862 | common_config += textwrap.dedent( |
| 863 | """ |
| 864 | - job: |
| 865 | name: project-test9 |
| 866 | """) |
| 867 | |
| 868 | file_dict = {'zuul.yaml': common_config} |
| 869 | A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A', |
| 870 | files=file_dict) |
| 871 | in_repo_conf = textwrap.dedent( |
| 872 | """ |
| 873 | - job: |
| 874 | name: project-test1 |
| 875 | - project: |
| 876 | name: org/project |
| 877 | check: |
| 878 | jobs: |
| 879 | - project-test9 |
| 880 | """) |
| 881 | |
| 882 | file_dict = {'zuul.yaml': in_repo_conf} |
| 883 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B', |
| 884 | files=file_dict) |
| 885 | B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % ( |
| 886 | B.subject, A.data['id']) |
| 887 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
| 888 | self.waitUntilSettled() |
| 889 | |
| 890 | self.assertEqual(B.data['status'], 'NEW') |
| 891 | self.assertEqual(B.reported, 1, |
| 892 | "B should report failure") |
| 893 | self.assertIn('depends on a change to a config project', |
| 894 | B.messages[0], |
| 895 | "A should have a syntax error reported") |
| 896 | |
James E. Blair | e64b0e4 | 2017-06-08 11:23:34 -0700 | [diff] [blame] | 897 | def test_duplicate_node_error(self): |
| 898 | in_repo_conf = textwrap.dedent( |
| 899 | """ |
| 900 | - nodeset: |
| 901 | name: duplicate |
| 902 | nodes: |
| 903 | - name: compute |
James E. Blair | 16d96a0 | 2017-06-08 11:32:56 -0700 | [diff] [blame] | 904 | label: foo |
James E. Blair | e64b0e4 | 2017-06-08 11:23:34 -0700 | [diff] [blame] | 905 | - name: compute |
James E. Blair | 16d96a0 | 2017-06-08 11:32:56 -0700 | [diff] [blame] | 906 | label: foo |
James E. Blair | e64b0e4 | 2017-06-08 11:23:34 -0700 | [diff] [blame] | 907 | """) |
| 908 | |
| 909 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 910 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 911 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 912 | A.addApproval('Code-Review', 2) |
| 913 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | e64b0e4 | 2017-06-08 11:23:34 -0700 | [diff] [blame] | 914 | self.waitUntilSettled() |
| 915 | |
| 916 | self.assertEqual(A.data['status'], 'NEW') |
| 917 | self.assertEqual(A.reported, 1, |
| 918 | "A should report failure") |
| 919 | self.assertIn('appears multiple times', A.messages[0], |
| 920 | "A should have a syntax error reported") |
| 921 | |
| 922 | def test_duplicate_group_error(self): |
| 923 | in_repo_conf = textwrap.dedent( |
| 924 | """ |
| 925 | - nodeset: |
| 926 | name: duplicate |
| 927 | nodes: |
| 928 | - name: compute |
James E. Blair | 16d96a0 | 2017-06-08 11:32:56 -0700 | [diff] [blame] | 929 | label: foo |
James E. Blair | e64b0e4 | 2017-06-08 11:23:34 -0700 | [diff] [blame] | 930 | groups: |
| 931 | - name: group |
| 932 | nodes: compute |
| 933 | - name: group |
| 934 | nodes: compute |
| 935 | """) |
| 936 | |
| 937 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 938 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 939 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 940 | A.addApproval('Code-Review', 2) |
| 941 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | e64b0e4 | 2017-06-08 11:23:34 -0700 | [diff] [blame] | 942 | self.waitUntilSettled() |
| 943 | |
| 944 | self.assertEqual(A.data['status'], 'NEW') |
| 945 | self.assertEqual(A.reported, 1, |
| 946 | "A should report failure") |
| 947 | self.assertIn('appears multiple times', A.messages[0], |
| 948 | "A should have a syntax error reported") |
| 949 | |
James E. Blair | 4ae399f | 2017-09-20 17:15:09 -0700 | [diff] [blame] | 950 | def test_secret_not_found_error(self): |
| 951 | in_repo_conf = textwrap.dedent( |
| 952 | """ |
| 953 | - job: |
| 954 | name: test |
| 955 | secrets: does-not-exist |
| 956 | """) |
| 957 | |
| 958 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 959 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 960 | files=file_dict) |
| 961 | A.addApproval('Code-Review', 2) |
| 962 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
| 963 | self.waitUntilSettled() |
| 964 | |
| 965 | self.assertEqual(A.data['status'], 'NEW') |
| 966 | self.assertEqual(A.reported, 1, |
| 967 | "A should report failure") |
| 968 | self.assertIn('secret "does-not-exist" was not found', A.messages[0], |
| 969 | "A should have a syntax error reported") |
| 970 | |
| 971 | def test_nodeset_not_found_error(self): |
| 972 | in_repo_conf = textwrap.dedent( |
| 973 | """ |
| 974 | - job: |
| 975 | name: test |
| 976 | nodeset: does-not-exist |
| 977 | """) |
| 978 | |
| 979 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 980 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 981 | files=file_dict) |
| 982 | A.addApproval('Code-Review', 2) |
| 983 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
| 984 | self.waitUntilSettled() |
| 985 | |
| 986 | self.assertEqual(A.data['status'], 'NEW') |
| 987 | self.assertEqual(A.reported, 1, |
| 988 | "A should report failure") |
| 989 | self.assertIn('nodeset "does-not-exist" was not found', A.messages[0], |
| 990 | "A should have a syntax error reported") |
| 991 | |
James E. Blair | 89e25eb | 2017-09-26 09:11:31 -0700 | [diff] [blame] | 992 | def test_template_not_found_error(self): |
| 993 | in_repo_conf = textwrap.dedent( |
| 994 | """ |
| 995 | - job: |
| 996 | name: project-test1 |
| 997 | - project: |
| 998 | name: org/project |
| 999 | templates: |
| 1000 | - does-not-exist |
| 1001 | """) |
| 1002 | |
| 1003 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 1004 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 1005 | files=file_dict) |
| 1006 | A.addApproval('Code-Review', 2) |
| 1007 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
| 1008 | self.waitUntilSettled() |
| 1009 | |
| 1010 | self.assertEqual(A.data['status'], 'NEW') |
| 1011 | self.assertEqual(A.reported, 1, |
| 1012 | "A should report failure") |
| 1013 | self.assertIn('project template "does-not-exist" was not found', |
| 1014 | A.messages[0], |
| 1015 | "A should have a syntax error reported") |
| 1016 | |
Monty Taylor | 8be3c0c | 2017-10-06 10:37:37 -0500 | [diff] [blame] | 1017 | def test_job_list_in_project_template_not_dict_error(self): |
| 1018 | in_repo_conf = textwrap.dedent( |
| 1019 | """ |
| 1020 | - job: |
| 1021 | name: project-test1 |
| 1022 | - project-template: |
| 1023 | name: some-jobs |
| 1024 | check: |
| 1025 | jobs: |
| 1026 | - project-test1: |
| 1027 | - required-projects: |
| 1028 | org/project2 |
| 1029 | """) |
| 1030 | |
| 1031 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 1032 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 1033 | files=file_dict) |
| 1034 | A.addApproval('Code-Review', 2) |
| 1035 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
| 1036 | self.waitUntilSettled() |
| 1037 | |
| 1038 | self.assertEqual(A.data['status'], 'NEW') |
| 1039 | self.assertEqual(A.reported, 1, |
| 1040 | "A should report failure") |
| 1041 | self.assertIn('expected str for dictionary value', |
| 1042 | A.messages[0], "A should have a syntax error reported") |
| 1043 | |
| 1044 | def test_job_list_in_project_not_dict_error(self): |
| 1045 | in_repo_conf = textwrap.dedent( |
| 1046 | """ |
| 1047 | - job: |
| 1048 | name: project-test1 |
| 1049 | - project: |
| 1050 | name: org/project1 |
| 1051 | check: |
| 1052 | jobs: |
| 1053 | - project-test1: |
| 1054 | - required-projects: |
| 1055 | org/project2 |
| 1056 | """) |
| 1057 | |
| 1058 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 1059 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 1060 | files=file_dict) |
| 1061 | A.addApproval('Code-Review', 2) |
| 1062 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
| 1063 | self.waitUntilSettled() |
| 1064 | |
| 1065 | self.assertEqual(A.data['status'], 'NEW') |
| 1066 | self.assertEqual(A.reported, 1, |
| 1067 | "A should report failure") |
| 1068 | self.assertIn('expected str for dictionary value', |
| 1069 | A.messages[0], "A should have a syntax error reported") |
| 1070 | |
James E. Blair | 1235f14 | 2017-10-07 09:11:43 -0700 | [diff] [blame] | 1071 | def test_project_template(self): |
| 1072 | # Tests that a project template is not modified when used, and |
| 1073 | # can therefore be used in subsequent reconfigurations. |
| 1074 | in_repo_conf = textwrap.dedent( |
| 1075 | """ |
| 1076 | - job: |
| 1077 | name: project-test1 |
| 1078 | - project-template: |
| 1079 | name: some-jobs |
| 1080 | tenant-one-gate: |
| 1081 | jobs: |
| 1082 | - project-test1: |
| 1083 | required-projects: |
| 1084 | - org/project1 |
| 1085 | - project: |
| 1086 | name: org/project |
| 1087 | templates: |
| 1088 | - some-jobs |
| 1089 | """) |
| 1090 | |
| 1091 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 1092 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 1093 | files=file_dict) |
| 1094 | A.addApproval('Code-Review', 2) |
| 1095 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
| 1096 | self.waitUntilSettled() |
| 1097 | self.assertEqual(A.data['status'], 'MERGED') |
| 1098 | self.fake_gerrit.addEvent(A.getChangeMergedEvent()) |
| 1099 | self.waitUntilSettled() |
| 1100 | in_repo_conf = textwrap.dedent( |
| 1101 | """ |
| 1102 | - project: |
| 1103 | name: org/project1 |
| 1104 | templates: |
| 1105 | - some-jobs |
| 1106 | """) |
| 1107 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 1108 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B', |
| 1109 | files=file_dict) |
| 1110 | B.addApproval('Code-Review', 2) |
| 1111 | self.fake_gerrit.addEvent(B.addApproval('Approved', 1)) |
| 1112 | self.waitUntilSettled() |
| 1113 | self.assertEqual(B.data['status'], 'MERGED') |
| 1114 | |
James E. Blair | bccdfcf | 2017-10-07 13:37:26 -0700 | [diff] [blame] | 1115 | def test_job_remove_add(self): |
| 1116 | # Tests that a job can be removed from one repo and added in another. |
| 1117 | # First, remove the current config for project1 since it |
| 1118 | # references the job we want to remove. |
| 1119 | file_dict = {'.zuul.yaml': None} |
| 1120 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A', |
| 1121 | files=file_dict) |
| 1122 | A.setMerged() |
| 1123 | self.fake_gerrit.addEvent(A.getChangeMergedEvent()) |
| 1124 | self.waitUntilSettled() |
| 1125 | # Then propose a change to delete the job from one repo... |
| 1126 | file_dict = {'.zuul.yaml': None} |
| 1127 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B', |
| 1128 | files=file_dict) |
| 1129 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
| 1130 | self.waitUntilSettled() |
| 1131 | # ...and a second that depends on it that adds it to another repo. |
| 1132 | in_repo_conf = textwrap.dedent( |
| 1133 | """ |
| 1134 | - job: |
| 1135 | name: project-test1 |
| 1136 | |
| 1137 | - project: |
| 1138 | name: org/project1 |
| 1139 | check: |
| 1140 | jobs: |
| 1141 | - project-test1 |
| 1142 | """) |
| 1143 | in_repo_playbook = textwrap.dedent( |
| 1144 | """ |
| 1145 | - hosts: all |
| 1146 | tasks: [] |
| 1147 | """) |
| 1148 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 1149 | 'playbooks/project-test1.yaml': in_repo_playbook} |
| 1150 | C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C', |
| 1151 | files=file_dict, |
| 1152 | parent='refs/changes/1/1/1') |
| 1153 | C.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % ( |
| 1154 | C.subject, B.data['id']) |
| 1155 | self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1)) |
| 1156 | self.waitUntilSettled() |
| 1157 | self.assertHistory([ |
| 1158 | dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'), |
| 1159 | ], ordered=False) |
| 1160 | |
James E. Blair | 09f9ffe | 2017-07-11 15:30:25 -0700 | [diff] [blame] | 1161 | def test_multi_repo(self): |
| 1162 | downstream_repo_conf = textwrap.dedent( |
| 1163 | """ |
| 1164 | - project: |
| 1165 | name: org/project1 |
| 1166 | tenant-one-gate: |
| 1167 | jobs: |
| 1168 | - project-test1 |
| 1169 | |
| 1170 | - job: |
| 1171 | name: project1-test1 |
| 1172 | parent: project-test1 |
| 1173 | """) |
| 1174 | |
| 1175 | file_dict = {'.zuul.yaml': downstream_repo_conf} |
| 1176 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A', |
| 1177 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 1178 | A.addApproval('Code-Review', 2) |
| 1179 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
James E. Blair | 09f9ffe | 2017-07-11 15:30:25 -0700 | [diff] [blame] | 1180 | self.waitUntilSettled() |
| 1181 | |
| 1182 | self.assertEqual(A.data['status'], 'MERGED') |
| 1183 | self.fake_gerrit.addEvent(A.getChangeMergedEvent()) |
| 1184 | self.waitUntilSettled() |
| 1185 | |
| 1186 | upstream_repo_conf = textwrap.dedent( |
| 1187 | """ |
| 1188 | - job: |
| 1189 | name: project-test1 |
| 1190 | |
| 1191 | - job: |
| 1192 | name: project-test2 |
| 1193 | |
| 1194 | - project: |
| 1195 | name: org/project |
| 1196 | tenant-one-gate: |
| 1197 | jobs: |
| 1198 | - project-test1 |
| 1199 | """) |
| 1200 | |
| 1201 | file_dict = {'.zuul.yaml': upstream_repo_conf} |
| 1202 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B', |
| 1203 | files=file_dict) |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 1204 | B.addApproval('Code-Review', 2) |
| 1205 | self.fake_gerrit.addEvent(B.addApproval('Approved', 1)) |
James E. Blair | 09f9ffe | 2017-07-11 15:30:25 -0700 | [diff] [blame] | 1206 | self.waitUntilSettled() |
| 1207 | |
| 1208 | self.assertEqual(B.data['status'], 'MERGED') |
| 1209 | self.fake_gerrit.addEvent(B.getChangeMergedEvent()) |
| 1210 | self.waitUntilSettled() |
| 1211 | |
| 1212 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 1213 | # Ensure the latest change is reflected in the config; if it |
| 1214 | # isn't this will raise an exception. |
| 1215 | tenant.layout.getJob('project-test2') |
| 1216 | |
James E. Blair | 332636e | 2017-09-05 10:14:35 -0700 | [diff] [blame] | 1217 | def test_pipeline_error(self): |
| 1218 | with open(os.path.join(FIXTURE_DIR, |
| 1219 | 'config/in-repo/git/', |
| 1220 | 'common-config/zuul.yaml')) as f: |
| 1221 | base_common_config = f.read() |
| 1222 | |
| 1223 | in_repo_conf_A = textwrap.dedent( |
| 1224 | """ |
| 1225 | - pipeline: |
| 1226 | name: periodic |
| 1227 | foo: error |
| 1228 | """) |
| 1229 | |
| 1230 | file_dict = {'zuul.yaml': None, |
| 1231 | 'zuul.d/main.yaml': base_common_config, |
| 1232 | 'zuul.d/test1.yaml': in_repo_conf_A} |
| 1233 | A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A', |
| 1234 | files=file_dict) |
| 1235 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1236 | self.waitUntilSettled() |
| 1237 | self.assertEqual(A.reported, 1, |
| 1238 | "A should report failure") |
| 1239 | self.assertIn('syntax error', |
| 1240 | A.messages[0], |
| 1241 | "A should have an error reported") |
| 1242 | |
| 1243 | def test_change_series_error(self): |
| 1244 | with open(os.path.join(FIXTURE_DIR, |
| 1245 | 'config/in-repo/git/', |
| 1246 | 'common-config/zuul.yaml')) as f: |
| 1247 | base_common_config = f.read() |
| 1248 | |
| 1249 | in_repo_conf_A = textwrap.dedent( |
| 1250 | """ |
| 1251 | - pipeline: |
| 1252 | name: periodic |
| 1253 | foo: error |
| 1254 | """) |
| 1255 | |
| 1256 | file_dict = {'zuul.yaml': None, |
| 1257 | 'zuul.d/main.yaml': base_common_config, |
| 1258 | 'zuul.d/test1.yaml': in_repo_conf_A} |
| 1259 | A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A', |
| 1260 | files=file_dict) |
| 1261 | |
| 1262 | in_repo_conf_B = textwrap.dedent( |
| 1263 | """ |
| 1264 | - job: |
| 1265 | name: project-test2 |
| 1266 | foo: error |
| 1267 | """) |
| 1268 | |
| 1269 | file_dict = {'zuul.yaml': None, |
| 1270 | 'zuul.d/main.yaml': base_common_config, |
| 1271 | 'zuul.d/test1.yaml': in_repo_conf_A, |
| 1272 | 'zuul.d/test2.yaml': in_repo_conf_B} |
| 1273 | B = self.fake_gerrit.addFakeChange('common-config', 'master', 'B', |
| 1274 | files=file_dict) |
| 1275 | B.setDependsOn(A, 1) |
| 1276 | C = self.fake_gerrit.addFakeChange('common-config', 'master', 'C') |
| 1277 | C.setDependsOn(B, 1) |
| 1278 | self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1)) |
| 1279 | self.waitUntilSettled() |
| 1280 | |
| 1281 | self.assertEqual(C.reported, 1, |
| 1282 | "C should report failure") |
| 1283 | self.assertIn('depends on a change that failed to merge', |
| 1284 | C.messages[0], |
| 1285 | "C should have an error reported") |
| 1286 | |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 1287 | |
James E. Blair | c945500 | 2017-09-06 09:22:19 -0700 | [diff] [blame] | 1288 | class TestInRepoJoin(ZuulTestCase): |
| 1289 | # In this config, org/project is not a member of any pipelines, so |
| 1290 | # that we may test the changes that cause it to join them. |
| 1291 | |
| 1292 | tenant_config_file = 'config/in-repo-join/main.yaml' |
| 1293 | |
| 1294 | def test_dynamic_dependent_pipeline(self): |
| 1295 | # Test dynamically adding a project to a |
| 1296 | # dependent pipeline for the first time |
| 1297 | self.executor_server.hold_jobs_in_build = True |
| 1298 | |
| 1299 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 1300 | gate_pipeline = tenant.layout.pipelines['gate'] |
| 1301 | |
| 1302 | in_repo_conf = textwrap.dedent( |
| 1303 | """ |
| 1304 | - job: |
| 1305 | name: project-test1 |
| 1306 | |
| 1307 | - job: |
| 1308 | name: project-test2 |
| 1309 | |
| 1310 | - project: |
| 1311 | name: org/project |
| 1312 | gate: |
| 1313 | jobs: |
| 1314 | - project-test2 |
| 1315 | """) |
| 1316 | |
| 1317 | in_repo_playbook = textwrap.dedent( |
| 1318 | """ |
| 1319 | - hosts: all |
| 1320 | tasks: [] |
| 1321 | """) |
| 1322 | |
| 1323 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 1324 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 1325 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 1326 | files=file_dict) |
| 1327 | A.addApproval('Code-Review', 2) |
| 1328 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
| 1329 | self.waitUntilSettled() |
| 1330 | |
| 1331 | items = gate_pipeline.getAllItems() |
| 1332 | self.assertEqual(items[0].change.number, '1') |
| 1333 | self.assertEqual(items[0].change.patchset, '1') |
| 1334 | self.assertTrue(items[0].live) |
| 1335 | |
| 1336 | self.executor_server.hold_jobs_in_build = False |
| 1337 | self.executor_server.release() |
| 1338 | self.waitUntilSettled() |
| 1339 | |
| 1340 | # Make sure the dynamic queue got cleaned up |
| 1341 | self.assertEqual(gate_pipeline.queues, []) |
| 1342 | |
| 1343 | def test_dynamic_dependent_pipeline_failure(self): |
| 1344 | # Test that a change behind a failing change adding a project |
| 1345 | # to a dependent pipeline is dequeued. |
| 1346 | self.executor_server.hold_jobs_in_build = True |
| 1347 | |
| 1348 | in_repo_conf = textwrap.dedent( |
| 1349 | """ |
| 1350 | - job: |
| 1351 | name: project-test1 |
| 1352 | |
| 1353 | - project: |
| 1354 | name: org/project |
| 1355 | gate: |
| 1356 | jobs: |
| 1357 | - project-test1 |
| 1358 | """) |
| 1359 | |
| 1360 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 1361 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 1362 | files=file_dict) |
| 1363 | self.executor_server.failJob('project-test1', A) |
| 1364 | A.addApproval('Code-Review', 2) |
| 1365 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
| 1366 | self.waitUntilSettled() |
| 1367 | |
| 1368 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1369 | B.addApproval('Code-Review', 2) |
| 1370 | self.fake_gerrit.addEvent(B.addApproval('Approved', 1)) |
| 1371 | self.waitUntilSettled() |
| 1372 | |
James E. Blair | 3490c5d | 2017-09-07 08:33:23 -0700 | [diff] [blame] | 1373 | self.orderedRelease() |
James E. Blair | c945500 | 2017-09-06 09:22:19 -0700 | [diff] [blame] | 1374 | self.waitUntilSettled() |
| 1375 | self.assertEqual(A.reported, 2, |
| 1376 | "A should report start and failure") |
| 1377 | self.assertEqual(A.data['status'], 'NEW') |
| 1378 | self.assertEqual(B.reported, 1, |
| 1379 | "B should report start") |
| 1380 | self.assertHistory([ |
| 1381 | dict(name='project-test1', result='FAILURE', changes='1,1'), |
James E. Blair | 3490c5d | 2017-09-07 08:33:23 -0700 | [diff] [blame] | 1382 | dict(name='project-test1', result='ABORTED', changes='1,1 2,1'), |
James E. Blair | c945500 | 2017-09-06 09:22:19 -0700 | [diff] [blame] | 1383 | ], ordered=False) |
| 1384 | |
James E. Blair | 0af198f | 2017-09-06 09:52:35 -0700 | [diff] [blame] | 1385 | def test_dynamic_dependent_pipeline_absent(self): |
| 1386 | # Test that a series of dependent changes don't report merge |
| 1387 | # failures to a pipeline they aren't in. |
| 1388 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1389 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1390 | B.setDependsOn(A, 1) |
| 1391 | |
| 1392 | A.addApproval('Code-Review', 2) |
| 1393 | A.addApproval('Approved', 1) |
| 1394 | B.addApproval('Code-Review', 2) |
| 1395 | self.fake_gerrit.addEvent(B.addApproval('Approved', 1)) |
| 1396 | self.waitUntilSettled() |
| 1397 | self.assertEqual(A.reported, 0, |
| 1398 | "A should not report") |
| 1399 | self.assertEqual(A.data['status'], 'NEW') |
| 1400 | self.assertEqual(B.reported, 0, |
| 1401 | "B should not report") |
| 1402 | self.assertEqual(B.data['status'], 'NEW') |
| 1403 | self.assertHistory([]) |
| 1404 | |
James E. Blair | c945500 | 2017-09-06 09:22:19 -0700 | [diff] [blame] | 1405 | |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 1406 | class TestAnsible(AnsibleZuulTestCase): |
| 1407 | # A temporary class to hold new tests while others are disabled |
| 1408 | |
| 1409 | tenant_config_file = 'config/ansible/main.yaml' |
| 1410 | |
| 1411 | def test_playbook(self): |
Jamie Lennox | 7655b55 | 2017-03-17 12:33:38 +1100 | [diff] [blame] | 1412 | # Keep the jobdir around so we can inspect contents if an |
| 1413 | # assert fails. |
| 1414 | self.executor_server.keep_jobdir = True |
| 1415 | # Output extra ansible info so we might see errors. |
| 1416 | self.executor_server.verbose = True |
| 1417 | # Add a site variables file, used by check-vars |
| 1418 | path = os.path.join(FIXTURE_DIR, 'config', 'ansible', |
| 1419 | 'variables.yaml') |
| 1420 | self.config.set('executor', 'variables', path) |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 1421 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1422 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1423 | self.waitUntilSettled() |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 1424 | build_timeout = self.getJobFromHistory('timeout') |
Jamie Lennox | 7655b55 | 2017-03-17 12:33:38 +1100 | [diff] [blame] | 1425 | with self.jobLog(build_timeout): |
| 1426 | self.assertEqual(build_timeout.result, 'TIMED_OUT') |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 1427 | build_faillocal = self.getJobFromHistory('faillocal') |
Jamie Lennox | 7655b55 | 2017-03-17 12:33:38 +1100 | [diff] [blame] | 1428 | with self.jobLog(build_faillocal): |
| 1429 | self.assertEqual(build_faillocal.result, 'FAILURE') |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 1430 | build_failpost = self.getJobFromHistory('failpost') |
Jamie Lennox | 7655b55 | 2017-03-17 12:33:38 +1100 | [diff] [blame] | 1431 | with self.jobLog(build_failpost): |
| 1432 | self.assertEqual(build_failpost.result, 'POST_FAILURE') |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 1433 | build_check_vars = self.getJobFromHistory('check-vars') |
Jamie Lennox | 7655b55 | 2017-03-17 12:33:38 +1100 | [diff] [blame] | 1434 | with self.jobLog(build_check_vars): |
| 1435 | self.assertEqual(build_check_vars.result, 'SUCCESS') |
Monty Taylor | aff8b40 | 2017-08-16 18:40:41 -0500 | [diff] [blame] | 1436 | build_check_secret_names = self.getJobFromHistory('check-secret-names') |
| 1437 | with self.jobLog(build_check_secret_names): |
| 1438 | self.assertEqual(build_check_secret_names.result, 'SUCCESS') |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 1439 | build_hello = self.getJobFromHistory('hello-world') |
Jamie Lennox | 7655b55 | 2017-03-17 12:33:38 +1100 | [diff] [blame] | 1440 | with self.jobLog(build_hello): |
| 1441 | self.assertEqual(build_hello.result, 'SUCCESS') |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 1442 | build_python27 = self.getJobFromHistory('python27') |
Jamie Lennox | 7655b55 | 2017-03-17 12:33:38 +1100 | [diff] [blame] | 1443 | with self.jobLog(build_python27): |
| 1444 | self.assertEqual(build_python27.result, 'SUCCESS') |
| 1445 | flag_path = os.path.join(self.test_root, |
| 1446 | build_python27.uuid + '.flag') |
| 1447 | self.assertTrue(os.path.exists(flag_path)) |
| 1448 | copied_path = os.path.join(self.test_root, build_python27.uuid + |
| 1449 | '.copied') |
| 1450 | self.assertTrue(os.path.exists(copied_path)) |
| 1451 | failed_path = os.path.join(self.test_root, build_python27.uuid + |
| 1452 | '.failed') |
| 1453 | self.assertFalse(os.path.exists(failed_path)) |
| 1454 | pre_flag_path = os.path.join(self.test_root, build_python27.uuid + |
| 1455 | '.pre.flag') |
| 1456 | self.assertTrue(os.path.exists(pre_flag_path)) |
| 1457 | post_flag_path = os.path.join(self.test_root, build_python27.uuid + |
| 1458 | '.post.flag') |
| 1459 | self.assertTrue(os.path.exists(post_flag_path)) |
| 1460 | bare_role_flag_path = os.path.join(self.test_root, |
| 1461 | build_python27.uuid + |
| 1462 | '.bare-role.flag') |
| 1463 | self.assertTrue(os.path.exists(bare_role_flag_path)) |
| 1464 | secrets_path = os.path.join(self.test_root, |
| 1465 | build_python27.uuid + '.secrets') |
| 1466 | with open(secrets_path) as f: |
| 1467 | self.assertEqual(f.read(), "test-username test-password") |
James E. Blair | b9c0d77 | 2017-03-03 14:34:49 -0800 | [diff] [blame] | 1468 | |
Jamie Lennox | 7655b55 | 2017-03-17 12:33:38 +1100 | [diff] [blame] | 1469 | msg = A.messages[0] |
| 1470 | success = "{} https://success.example.com/zuul-logs/{}" |
| 1471 | fail = "{} https://failure.example.com/zuul-logs/{}" |
| 1472 | self.assertIn(success.format("python27", build_python27.uuid), msg) |
| 1473 | self.assertIn(fail.format("faillocal", build_faillocal.uuid), msg) |
| 1474 | self.assertIn(success.format("check-vars", |
| 1475 | build_check_vars.uuid), msg) |
| 1476 | self.assertIn(success.format("hello-world", build_hello.uuid), msg) |
| 1477 | self.assertIn(fail.format("timeout", build_timeout.uuid), msg) |
| 1478 | self.assertIn(fail.format("failpost", build_failpost.uuid), msg) |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 1479 | |
James E. Blair | abbaa6f | 2017-04-06 16:11:44 -0700 | [diff] [blame] | 1480 | def _add_job(self, job_name): |
| 1481 | conf = textwrap.dedent( |
| 1482 | """ |
| 1483 | - job: |
| 1484 | name: %s |
| 1485 | |
| 1486 | - project: |
| 1487 | name: org/plugin-project |
| 1488 | check: |
| 1489 | jobs: |
| 1490 | - %s |
| 1491 | """ % (job_name, job_name)) |
| 1492 | |
| 1493 | file_dict = {'.zuul.yaml': conf} |
| 1494 | A = self.fake_gerrit.addFakeChange('org/plugin-project', 'master', 'A', |
| 1495 | files=file_dict) |
| 1496 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1497 | self.waitUntilSettled() |
| 1498 | |
| 1499 | def test_plugins(self): |
| 1500 | # Keep the jobdir around so we can inspect contents if an |
| 1501 | # assert fails. |
| 1502 | self.executor_server.keep_jobdir = True |
| 1503 | # Output extra ansible info so we might see errors. |
| 1504 | self.executor_server.verbose = True |
| 1505 | |
| 1506 | count = 0 |
| 1507 | plugin_tests = [ |
| 1508 | ('passwd', 'FAILURE'), |
| 1509 | ('cartesian', 'SUCCESS'), |
| 1510 | ('consul_kv', 'FAILURE'), |
| 1511 | ('credstash', 'FAILURE'), |
| 1512 | ('csvfile_good', 'SUCCESS'), |
| 1513 | ('csvfile_bad', 'FAILURE'), |
Monty Taylor | 93ad221 | 2017-08-02 14:59:50 -0500 | [diff] [blame] | 1514 | ('uri_bad_path', 'FAILURE'), |
| 1515 | ('uri_bad_scheme', 'FAILURE'), |
Monty Taylor | 788a40e | 2017-08-02 16:14:05 -0500 | [diff] [blame] | 1516 | ('block_local_override', 'FAILURE'), |
Monty Taylor | 8da768f | 2017-08-31 14:15:35 -0500 | [diff] [blame] | 1517 | ('file_local_good', 'SUCCESS'), |
| 1518 | ('file_local_bad', 'FAILURE'), |
James E. Blair | abbaa6f | 2017-04-06 16:11:44 -0700 | [diff] [blame] | 1519 | ] |
| 1520 | for job_name, result in plugin_tests: |
| 1521 | count += 1 |
| 1522 | self._add_job(job_name) |
| 1523 | |
| 1524 | job = self.getJobFromHistory(job_name) |
| 1525 | with self.jobLog(job): |
| 1526 | self.assertEqual(count, len(self.history)) |
| 1527 | build = self.history[-1] |
| 1528 | self.assertEqual(build.result, result) |
| 1529 | |
| 1530 | # TODOv3(jeblair): parse the ansible output and verify we're |
| 1531 | # getting the exception we expect. |
| 1532 | |
James E. Blair | b9c0d77 | 2017-03-03 14:34:49 -0800 | [diff] [blame] | 1533 | |
James E. Blair | a4d4eef | 2017-06-30 14:49:17 -0700 | [diff] [blame] | 1534 | class TestPrePlaybooks(AnsibleZuulTestCase): |
| 1535 | # A temporary class to hold new tests while others are disabled |
| 1536 | |
| 1537 | tenant_config_file = 'config/pre-playbook/main.yaml' |
| 1538 | |
| 1539 | def test_pre_playbook_fail(self): |
| 1540 | # Test that we run the post playbooks (but not the actual |
| 1541 | # playbook) when a pre-playbook fails. |
| 1542 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1543 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1544 | self.waitUntilSettled() |
| 1545 | build = self.getJobFromHistory('python27') |
| 1546 | self.assertIsNone(build.result) |
| 1547 | self.assertIn('RETRY_LIMIT', A.messages[0]) |
| 1548 | flag_path = os.path.join(self.test_root, build.uuid + |
| 1549 | '.main.flag') |
| 1550 | self.assertFalse(os.path.exists(flag_path)) |
| 1551 | pre_flag_path = os.path.join(self.test_root, build.uuid + |
| 1552 | '.pre.flag') |
| 1553 | self.assertFalse(os.path.exists(pre_flag_path)) |
| 1554 | post_flag_path = os.path.join(self.test_root, build.uuid + |
| 1555 | '.post.flag') |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 1556 | self.assertTrue(os.path.exists(post_flag_path), |
| 1557 | "The file %s should exist" % post_flag_path) |
James E. Blair | a4d4eef | 2017-06-30 14:49:17 -0700 | [diff] [blame] | 1558 | |
| 1559 | |
James E. Blair | bacbb88 | 2017-10-17 09:48:23 -0700 | [diff] [blame] | 1560 | class TestPostPlaybooks(AnsibleZuulTestCase): |
| 1561 | tenant_config_file = 'config/post-playbook/main.yaml' |
| 1562 | |
| 1563 | def test_post_playbook_abort(self): |
| 1564 | # Test that when we abort a job in the post playbook, that we |
| 1565 | # don't send back POST_FAILURE. |
| 1566 | self.executor_server.verbose = True |
| 1567 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1568 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1569 | |
| 1570 | while not len(self.builds): |
| 1571 | time.sleep(0.1) |
| 1572 | build = self.builds[0] |
| 1573 | |
| 1574 | post_start = os.path.join(self.test_root, build.uuid + |
| 1575 | '.post_start.flag') |
| 1576 | start = time.time() |
| 1577 | while time.time() < start + 90: |
| 1578 | if os.path.exists(post_start): |
| 1579 | break |
| 1580 | time.sleep(0.1) |
| 1581 | # The post playbook has started, abort the job |
| 1582 | self.fake_gerrit.addEvent(A.getChangeAbandonedEvent()) |
| 1583 | self.waitUntilSettled() |
| 1584 | |
| 1585 | build = self.getJobFromHistory('python27') |
| 1586 | self.assertEqual('ABORTED', build.result) |
| 1587 | |
| 1588 | post_end = os.path.join(self.test_root, build.uuid + |
| 1589 | '.post_end.flag') |
| 1590 | self.assertTrue(os.path.exists(post_start)) |
| 1591 | self.assertFalse(os.path.exists(post_end)) |
| 1592 | |
| 1593 | |
James E. Blair | b9c0d77 | 2017-03-03 14:34:49 -0800 | [diff] [blame] | 1594 | class TestBrokenConfig(ZuulTestCase): |
| 1595 | # Test that we get an appropriate syntax error if we start with a |
| 1596 | # broken config. |
| 1597 | |
| 1598 | tenant_config_file = 'config/broken/main.yaml' |
| 1599 | |
| 1600 | def setUp(self): |
| 1601 | with testtools.ExpectedException( |
| 1602 | zuul.configloader.ConfigurationSyntaxError, |
| 1603 | "\nZuul encountered a syntax error"): |
| 1604 | super(TestBrokenConfig, self).setUp() |
| 1605 | |
| 1606 | def test_broken_config_on_startup(self): |
| 1607 | pass |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 1608 | |
| 1609 | |
| 1610 | class TestProjectKeys(ZuulTestCase): |
| 1611 | # Test that we can generate project keys |
| 1612 | |
| 1613 | # Normally the test infrastructure copies a static key in place |
| 1614 | # for each project before starting tests. This saves time because |
| 1615 | # Zuul's automatic key-generation on startup can be slow. To make |
| 1616 | # sure we exercise that code, in this test we allow Zuul to create |
| 1617 | # keys for the project on startup. |
| 1618 | create_project_keys = True |
Tobias Henkel | abf973e | 2017-07-28 10:07:34 +0200 | [diff] [blame] | 1619 | config_file = 'zuul-connections-gerrit-and-github.conf' |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 1620 | tenant_config_file = 'config/in-repo/main.yaml' |
| 1621 | |
| 1622 | def test_key_generation(self): |
| 1623 | key_root = os.path.join(self.state_root, 'keys') |
| 1624 | private_key_file = os.path.join(key_root, 'gerrit/org/project.pem') |
| 1625 | # Make sure that a proper key was created on startup |
| 1626 | with open(private_key_file, "rb") as f: |
James E. Blair | bf1a4f2 | 2017-03-17 10:59:37 -0700 | [diff] [blame] | 1627 | private_key, public_key = \ |
| 1628 | encryption.deserialize_rsa_keypair(f.read()) |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 1629 | |
| 1630 | with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i: |
| 1631 | fixture_private_key = i.read() |
| 1632 | |
| 1633 | # Make sure that we didn't just end up with the static fixture |
| 1634 | # key |
| 1635 | self.assertNotEqual(fixture_private_key, private_key) |
| 1636 | |
| 1637 | # Make sure it's the right length |
| 1638 | self.assertEqual(4096, private_key.key_size) |
James E. Blair | bce7693 | 2017-05-04 10:03:15 -0700 | [diff] [blame] | 1639 | |
| 1640 | |
James E. Blair | bb94dfa | 2017-07-11 07:45:19 -0700 | [diff] [blame] | 1641 | class RoleTestCase(ZuulTestCase): |
James E. Blair | 1b27f6a | 2017-07-14 14:09:07 -0700 | [diff] [blame] | 1642 | def _assertRolePath(self, build, playbook, content): |
| 1643 | path = os.path.join(self.test_root, build.uuid, |
| 1644 | 'ansible', playbook, 'ansible.cfg') |
| 1645 | roles_paths = [] |
| 1646 | with open(path) as f: |
| 1647 | for line in f: |
| 1648 | if line.startswith('roles_path'): |
| 1649 | roles_paths.append(line) |
| 1650 | print(roles_paths) |
| 1651 | if content: |
| 1652 | self.assertEqual(len(roles_paths), 1, |
| 1653 | "Should have one roles_path line in %s" % |
| 1654 | (playbook,)) |
| 1655 | self.assertIn(content, roles_paths[0]) |
| 1656 | else: |
| 1657 | self.assertEqual(len(roles_paths), 0, |
| 1658 | "Should have no roles_path line in %s" % |
| 1659 | (playbook,)) |
| 1660 | |
James E. Blair | bb94dfa | 2017-07-11 07:45:19 -0700 | [diff] [blame] | 1661 | |
| 1662 | class TestRoles(RoleTestCase): |
| 1663 | tenant_config_file = 'config/roles/main.yaml' |
| 1664 | |
James E. Blair | bce7693 | 2017-05-04 10:03:15 -0700 | [diff] [blame] | 1665 | def test_role(self): |
| 1666 | # This exercises a proposed change to a role being checked out |
| 1667 | # and used. |
| 1668 | A = self.fake_gerrit.addFakeChange('bare-role', 'master', 'A') |
| 1669 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1670 | B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % ( |
| 1671 | B.subject, A.data['id']) |
| 1672 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1673 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
| 1674 | self.waitUntilSettled() |
| 1675 | self.assertHistory([ |
| 1676 | dict(name='project-test', result='SUCCESS', changes='1,1 2,1'), |
| 1677 | ]) |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 1678 | |
James E. Blair | 1b27f6a | 2017-07-14 14:09:07 -0700 | [diff] [blame] | 1679 | def test_role_inheritance(self): |
| 1680 | self.executor_server.hold_jobs_in_build = True |
| 1681 | conf = textwrap.dedent( |
| 1682 | """ |
| 1683 | - job: |
| 1684 | name: parent |
| 1685 | roles: |
| 1686 | - zuul: bare-role |
| 1687 | pre-run: playbooks/parent-pre |
| 1688 | post-run: playbooks/parent-post |
| 1689 | |
| 1690 | - job: |
| 1691 | name: project-test |
| 1692 | parent: parent |
| 1693 | roles: |
| 1694 | - zuul: org/project |
| 1695 | |
| 1696 | - project: |
| 1697 | name: org/project |
| 1698 | check: |
| 1699 | jobs: |
| 1700 | - project-test |
| 1701 | """) |
| 1702 | |
| 1703 | file_dict = {'.zuul.yaml': conf} |
| 1704 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 1705 | files=file_dict) |
| 1706 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1707 | self.waitUntilSettled() |
| 1708 | |
| 1709 | self.assertEqual(len(self.builds), 1) |
| 1710 | build = self.getBuildByName('project-test') |
| 1711 | self._assertRolePath(build, 'pre_playbook_0', 'role_0') |
| 1712 | self._assertRolePath(build, 'playbook_0', 'role_0') |
| 1713 | self._assertRolePath(build, 'playbook_0', 'role_1') |
| 1714 | self._assertRolePath(build, 'post_playbook_0', 'role_0') |
| 1715 | |
| 1716 | self.executor_server.hold_jobs_in_build = False |
| 1717 | self.executor_server.release() |
| 1718 | self.waitUntilSettled() |
| 1719 | |
| 1720 | self.assertHistory([ |
| 1721 | dict(name='project-test', result='SUCCESS', changes='1,1'), |
| 1722 | ]) |
| 1723 | |
James E. Blair | 6f69973 | 2017-07-18 14:19:11 -0700 | [diff] [blame] | 1724 | def test_role_error(self): |
| 1725 | conf = textwrap.dedent( |
| 1726 | """ |
| 1727 | - job: |
| 1728 | name: project-test |
| 1729 | roles: |
| 1730 | - zuul: common-config |
| 1731 | |
| 1732 | - project: |
| 1733 | name: org/project |
| 1734 | check: |
| 1735 | jobs: |
| 1736 | - project-test |
| 1737 | """) |
| 1738 | |
| 1739 | file_dict = {'.zuul.yaml': conf} |
| 1740 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 1741 | files=file_dict) |
| 1742 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1743 | self.waitUntilSettled() |
| 1744 | self.assertIn( |
| 1745 | '- project-test project-test : ERROR Unable to find role', |
| 1746 | A.messages[-1]) |
| 1747 | |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 1748 | |
James E. Blair | bb94dfa | 2017-07-11 07:45:19 -0700 | [diff] [blame] | 1749 | class TestImplicitRoles(RoleTestCase): |
| 1750 | tenant_config_file = 'config/implicit-roles/main.yaml' |
| 1751 | |
| 1752 | def test_missing_roles(self): |
| 1753 | # Test implicit and explicit roles for a project which does |
| 1754 | # not have roles. The implicit role should be silently |
| 1755 | # ignored since the project doesn't supply roles, but if a |
| 1756 | # user declares an explicit role, it should error. |
| 1757 | self.executor_server.hold_jobs_in_build = True |
| 1758 | A = self.fake_gerrit.addFakeChange('org/norole-project', 'master', 'A') |
| 1759 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1760 | self.waitUntilSettled() |
| 1761 | |
| 1762 | self.assertEqual(len(self.builds), 2) |
| 1763 | build = self.getBuildByName('implicit-role-fail') |
| 1764 | self._assertRolePath(build, 'playbook_0', None) |
| 1765 | |
| 1766 | self.executor_server.hold_jobs_in_build = False |
| 1767 | self.executor_server.release() |
| 1768 | self.waitUntilSettled() |
| 1769 | # The retry_limit doesn't get recorded |
| 1770 | self.assertHistory([ |
| 1771 | dict(name='implicit-role-fail', result='SUCCESS', changes='1,1'), |
| 1772 | ]) |
| 1773 | |
| 1774 | def test_roles(self): |
| 1775 | # Test implicit and explicit roles for a project which does |
| 1776 | # have roles. In both cases, we should end up with the role |
| 1777 | # in the path. In the explicit case, ensure we end up with |
| 1778 | # the name we specified. |
| 1779 | self.executor_server.hold_jobs_in_build = True |
| 1780 | A = self.fake_gerrit.addFakeChange('org/role-project', 'master', 'A') |
| 1781 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1782 | self.waitUntilSettled() |
| 1783 | |
| 1784 | self.assertEqual(len(self.builds), 2) |
| 1785 | build = self.getBuildByName('implicit-role-ok') |
| 1786 | self._assertRolePath(build, 'playbook_0', 'role_0') |
| 1787 | |
| 1788 | build = self.getBuildByName('explicit-role-ok') |
| 1789 | self._assertRolePath(build, 'playbook_0', 'role_0') |
| 1790 | |
| 1791 | self.executor_server.hold_jobs_in_build = False |
| 1792 | self.executor_server.release() |
| 1793 | self.waitUntilSettled() |
| 1794 | self.assertHistory([ |
| 1795 | dict(name='implicit-role-ok', result='SUCCESS', changes='1,1'), |
| 1796 | dict(name='explicit-role-ok', result='SUCCESS', changes='1,1'), |
| 1797 | ], ordered=False) |
| 1798 | |
| 1799 | |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 1800 | class TestShadow(ZuulTestCase): |
| 1801 | tenant_config_file = 'config/shadow/main.yaml' |
| 1802 | |
| 1803 | def test_shadow(self): |
| 1804 | # Test that a repo is allowed to shadow another's job definitions. |
| 1805 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1806 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1807 | self.waitUntilSettled() |
| 1808 | self.assertHistory([ |
| 1809 | dict(name='test1', result='SUCCESS', changes='1,1'), |
| 1810 | dict(name='test2', result='SUCCESS', changes='1,1'), |
James E. Blair | adafa6c | 2017-07-12 08:50:56 -0700 | [diff] [blame] | 1811 | ], ordered=False) |
James E. Blair | 196f61a | 2017-06-30 15:42:29 -0700 | [diff] [blame] | 1812 | |
| 1813 | |
| 1814 | class TestDataReturn(AnsibleZuulTestCase): |
| 1815 | tenant_config_file = 'config/data-return/main.yaml' |
| 1816 | |
| 1817 | def test_data_return(self): |
James E. Blair | 196f61a | 2017-06-30 15:42:29 -0700 | [diff] [blame] | 1818 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1819 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1820 | self.waitUntilSettled() |
| 1821 | self.assertHistory([ |
| 1822 | dict(name='data-return', result='SUCCESS', changes='1,1'), |
James E. Blair | 88e79c0 | 2017-07-07 13:36:54 -0700 | [diff] [blame] | 1823 | dict(name='data-return-relative', result='SUCCESS', changes='1,1'), |
James E. Blair | 698703c | 2017-09-15 20:58:30 -0600 | [diff] [blame] | 1824 | dict(name='child', result='SUCCESS', changes='1,1'), |
James E. Blair | 88e79c0 | 2017-07-07 13:36:54 -0700 | [diff] [blame] | 1825 | ], ordered=False) |
| 1826 | self.assertIn('- data-return http://example.com/test/log/url/', |
| 1827 | A.messages[-1]) |
| 1828 | self.assertIn('- data-return-relative ' |
| 1829 | 'http://example.com/test/log/url/docs/index.html', |
James E. Blair | 196f61a | 2017-06-30 15:42:29 -0700 | [diff] [blame] | 1830 | A.messages[-1]) |
Clint Byrum | dc8a090 | 2017-07-20 16:36:27 -0700 | [diff] [blame] | 1831 | |
| 1832 | |
| 1833 | class TestDiskAccounting(AnsibleZuulTestCase): |
| 1834 | config_file = 'zuul-disk-accounting.conf' |
| 1835 | tenant_config_file = 'config/disk-accountant/main.yaml' |
| 1836 | |
| 1837 | def test_disk_accountant_kills_job(self): |
| 1838 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1839 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1840 | self.waitUntilSettled() |
| 1841 | self.assertHistory([ |
| 1842 | dict(name='dd-big-empty-file', result='ABORTED', changes='1,1')]) |
Tristan Cacqueray | 82f864b | 2017-08-01 05:54:42 +0000 | [diff] [blame] | 1843 | |
| 1844 | |
| 1845 | class TestMaxNodesPerJob(AnsibleZuulTestCase): |
| 1846 | tenant_config_file = 'config/multi-tenant/main.yaml' |
| 1847 | |
Tristan Cacqueray | c98bff7 | 2017-09-10 15:25:26 +0000 | [diff] [blame] | 1848 | def test_max_timeout_exceeded(self): |
Tristan Cacqueray | 82f864b | 2017-08-01 05:54:42 +0000 | [diff] [blame] | 1849 | in_repo_conf = textwrap.dedent( |
| 1850 | """ |
| 1851 | - job: |
| 1852 | name: test-job |
James E. Blair | 7e3e688 | 2017-09-20 15:47:13 -0700 | [diff] [blame] | 1853 | nodeset: |
| 1854 | nodes: |
| 1855 | - name: node01 |
| 1856 | label: fake |
| 1857 | - name: node02 |
| 1858 | label: fake |
| 1859 | - name: node03 |
| 1860 | label: fake |
| 1861 | - name: node04 |
| 1862 | label: fake |
| 1863 | - name: node05 |
| 1864 | label: fake |
| 1865 | - name: node06 |
| 1866 | label: fake |
Tristan Cacqueray | 82f864b | 2017-08-01 05:54:42 +0000 | [diff] [blame] | 1867 | """) |
| 1868 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 1869 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A', |
| 1870 | files=file_dict) |
| 1871 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1872 | self.waitUntilSettled() |
| 1873 | self.assertIn('The job "test-job" exceeds tenant max-nodes-per-job 5.', |
| 1874 | A.messages[0], "A should fail because of nodes limit") |
| 1875 | |
| 1876 | B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A', |
| 1877 | files=file_dict) |
| 1878 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
| 1879 | self.waitUntilSettled() |
| 1880 | self.assertNotIn("exceeds tenant max-nodes", B.messages[0], |
| 1881 | "B should not fail because of nodes limit") |
James E. Blair | 2bab6e7 | 2017-08-07 09:52:45 -0700 | [diff] [blame] | 1882 | |
| 1883 | |
Tristan Cacqueray | c98bff7 | 2017-09-10 15:25:26 +0000 | [diff] [blame] | 1884 | class TestMaxTimeout(AnsibleZuulTestCase): |
| 1885 | tenant_config_file = 'config/multi-tenant/main.yaml' |
| 1886 | |
| 1887 | def test_max_nodes_reached(self): |
| 1888 | in_repo_conf = textwrap.dedent( |
| 1889 | """ |
| 1890 | - job: |
| 1891 | name: test-job |
| 1892 | timeout: 3600 |
| 1893 | """) |
| 1894 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 1895 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A', |
| 1896 | files=file_dict) |
| 1897 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1898 | self.waitUntilSettled() |
| 1899 | self.assertIn('The job "test-job" exceeds tenant max-job-timeout', |
| 1900 | A.messages[0], "A should fail because of timeout limit") |
| 1901 | |
| 1902 | B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A', |
| 1903 | files=file_dict) |
| 1904 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
| 1905 | self.waitUntilSettled() |
| 1906 | self.assertNotIn("exceeds tenant max-job-timeout", B.messages[0], |
| 1907 | "B should not fail because of timeout limit") |
| 1908 | |
| 1909 | |
James E. Blair | 2bab6e7 | 2017-08-07 09:52:45 -0700 | [diff] [blame] | 1910 | class TestBaseJobs(ZuulTestCase): |
| 1911 | tenant_config_file = 'config/base-jobs/main.yaml' |
| 1912 | |
| 1913 | def test_multiple_base_jobs(self): |
| 1914 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1915 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1916 | self.waitUntilSettled() |
| 1917 | self.assertHistory([ |
| 1918 | dict(name='my-job', result='SUCCESS', changes='1,1'), |
| 1919 | dict(name='other-job', result='SUCCESS', changes='1,1'), |
| 1920 | ], ordered=False) |
| 1921 | self.assertEqual(self.getJobFromHistory('my-job'). |
| 1922 | parameters['zuul']['jobtags'], |
| 1923 | ['mybase']) |
| 1924 | self.assertEqual(self.getJobFromHistory('other-job'). |
| 1925 | parameters['zuul']['jobtags'], |
| 1926 | ['otherbase']) |
| 1927 | |
| 1928 | def test_untrusted_base_job(self): |
| 1929 | """Test that a base job may not be defined in an untrusted repo""" |
| 1930 | in_repo_conf = textwrap.dedent( |
| 1931 | """ |
| 1932 | - job: |
| 1933 | name: fail-base |
| 1934 | parent: null |
| 1935 | """) |
| 1936 | |
| 1937 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 1938 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 1939 | files=file_dict) |
| 1940 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1941 | self.waitUntilSettled() |
| 1942 | self.assertEqual(A.reported, 1, |
| 1943 | "A should report failure") |
| 1944 | self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1") |
| 1945 | self.assertIn('Base jobs must be defined in config projects', |
| 1946 | A.messages[0]) |
| 1947 | self.assertHistory([]) |
James E. Blair | db08903 | 2017-08-15 13:42:12 -0700 | [diff] [blame] | 1948 | |
| 1949 | |
James E. Blair | df91ab3 | 2017-10-25 17:57:13 -0700 | [diff] [blame^] | 1950 | class TestSecretInheritance(ZuulTestCase): |
| 1951 | tenant_config_file = 'config/secret-inheritance/main.yaml' |
| 1952 | |
| 1953 | def _getSecrets(self, job, pbtype): |
| 1954 | secrets = [] |
| 1955 | build = self.getJobFromHistory(job) |
| 1956 | for pb in build.parameters[pbtype]: |
| 1957 | secrets.append(pb['secrets']) |
| 1958 | return secrets |
| 1959 | |
| 1960 | def _checkTrustedSecrets(self): |
| 1961 | secret = {'longpassword': 'test-passwordtest-password', |
| 1962 | 'password': 'test-password', |
| 1963 | 'username': 'test-username'} |
| 1964 | self.assertEqual( |
| 1965 | self._getSecrets('trusted-secrets', 'playbooks'), |
| 1966 | [{'trusted-secret': secret}, {}]) |
| 1967 | self.assertEqual( |
| 1968 | self._getSecrets('trusted-secrets', 'pre_playbooks'), []) |
| 1969 | self.assertEqual( |
| 1970 | self._getSecrets('trusted-secrets', 'post_playbooks'), []) |
| 1971 | |
| 1972 | self.assertEqual( |
| 1973 | self._getSecrets('trusted-secrets-trusted-child', |
| 1974 | 'playbooks'), |
| 1975 | [{}, {'trusted-secret': secret}, {}]) |
| 1976 | self.assertEqual( |
| 1977 | self._getSecrets('trusted-secrets-trusted-child', |
| 1978 | 'pre_playbooks'), []) |
| 1979 | self.assertEqual( |
| 1980 | self._getSecrets('trusted-secrets-trusted-child', |
| 1981 | 'post_playbooks'), []) |
| 1982 | |
| 1983 | self.assertEqual( |
| 1984 | self._getSecrets('trusted-secrets-untrusted-child', |
| 1985 | 'playbooks'), |
| 1986 | [{}, {'trusted-secret': secret}, {}]) |
| 1987 | self.assertEqual( |
| 1988 | self._getSecrets('trusted-secrets-untrusted-child', |
| 1989 | 'pre_playbooks'), []) |
| 1990 | self.assertEqual( |
| 1991 | self._getSecrets('trusted-secrets-untrusted-child', |
| 1992 | 'post_playbooks'), []) |
| 1993 | |
| 1994 | def _checkUntrustedSecrets(self): |
| 1995 | secret = {'longpassword': 'test-passwordtest-password', |
| 1996 | 'password': 'test-password', |
| 1997 | 'username': 'test-username'} |
| 1998 | self.assertEqual( |
| 1999 | self._getSecrets('untrusted-secrets', 'playbooks'), |
| 2000 | [{'untrusted-secret': secret}, {}]) |
| 2001 | self.assertEqual( |
| 2002 | self._getSecrets('untrusted-secrets', 'pre_playbooks'), []) |
| 2003 | self.assertEqual( |
| 2004 | self._getSecrets('untrusted-secrets', 'post_playbooks'), []) |
| 2005 | |
| 2006 | self.assertEqual( |
| 2007 | self._getSecrets('untrusted-secrets-trusted-child', |
| 2008 | 'playbooks'), |
| 2009 | [{}, {'untrusted-secret': secret}, {}]) |
| 2010 | self.assertEqual( |
| 2011 | self._getSecrets('untrusted-secrets-trusted-child', |
| 2012 | 'pre_playbooks'), []) |
| 2013 | self.assertEqual( |
| 2014 | self._getSecrets('untrusted-secrets-trusted-child', |
| 2015 | 'post_playbooks'), []) |
| 2016 | |
| 2017 | self.assertEqual( |
| 2018 | self._getSecrets('untrusted-secrets-untrusted-child', |
| 2019 | 'playbooks'), |
| 2020 | [{}, {'untrusted-secret': secret}, {}]) |
| 2021 | self.assertEqual( |
| 2022 | self._getSecrets('untrusted-secrets-untrusted-child', |
| 2023 | 'pre_playbooks'), []) |
| 2024 | self.assertEqual( |
| 2025 | self._getSecrets('untrusted-secrets-untrusted-child', |
| 2026 | 'post_playbooks'), []) |
| 2027 | |
| 2028 | def test_trusted_secret_inheritance_check(self): |
| 2029 | A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A') |
| 2030 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 2031 | self.waitUntilSettled() |
| 2032 | self.assertHistory([ |
| 2033 | dict(name='trusted-secrets', result='SUCCESS', changes='1,1'), |
| 2034 | dict(name='trusted-secrets-trusted-child', |
| 2035 | result='SUCCESS', changes='1,1'), |
| 2036 | dict(name='trusted-secrets-untrusted-child', |
| 2037 | result='SUCCESS', changes='1,1'), |
| 2038 | ], ordered=False) |
| 2039 | |
| 2040 | self._checkTrustedSecrets() |
| 2041 | |
| 2042 | def test_untrusted_secret_inheritance_gate(self): |
| 2043 | A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A') |
| 2044 | A.addApproval('Code-Review', 2) |
| 2045 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
| 2046 | self.waitUntilSettled() |
| 2047 | self.assertHistory([ |
| 2048 | dict(name='untrusted-secrets', result='SUCCESS', changes='1,1'), |
| 2049 | dict(name='untrusted-secrets-trusted-child', |
| 2050 | result='SUCCESS', changes='1,1'), |
| 2051 | dict(name='untrusted-secrets-untrusted-child', |
| 2052 | result='SUCCESS', changes='1,1'), |
| 2053 | ], ordered=False) |
| 2054 | |
| 2055 | self._checkUntrustedSecrets() |
| 2056 | |
| 2057 | def test_untrusted_secret_inheritance_check(self): |
| 2058 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2059 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 2060 | self.waitUntilSettled() |
| 2061 | # This configuration tries to run untrusted secrets in an |
| 2062 | # non-post-review pipeline and should therefore run no jobs. |
| 2063 | self.assertHistory([]) |
| 2064 | |
| 2065 | |
James E. Blair | db08903 | 2017-08-15 13:42:12 -0700 | [diff] [blame] | 2066 | class TestSecretLeaks(AnsibleZuulTestCase): |
| 2067 | tenant_config_file = 'config/secret-leaks/main.yaml' |
| 2068 | |
| 2069 | def searchForContent(self, path, content): |
| 2070 | matches = [] |
| 2071 | for (dirpath, dirnames, filenames) in os.walk(path): |
| 2072 | for filename in filenames: |
| 2073 | filepath = os.path.join(dirpath, filename) |
| 2074 | with open(filepath, 'rb') as f: |
| 2075 | if content in f.read(): |
| 2076 | matches.append(filepath[len(path):]) |
| 2077 | return matches |
| 2078 | |
| 2079 | def _test_secret_file(self): |
| 2080 | # Or rather -- test that they *don't* leak. |
| 2081 | # Keep the jobdir around so we can inspect contents. |
| 2082 | self.executor_server.keep_jobdir = True |
| 2083 | conf = textwrap.dedent( |
| 2084 | """ |
| 2085 | - project: |
| 2086 | name: org/project |
| 2087 | check: |
| 2088 | jobs: |
| 2089 | - secret-file |
| 2090 | """) |
| 2091 | |
| 2092 | file_dict = {'.zuul.yaml': conf} |
| 2093 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 2094 | files=file_dict) |
| 2095 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 2096 | self.waitUntilSettled() |
| 2097 | self.assertHistory([ |
| 2098 | dict(name='secret-file', result='SUCCESS', changes='1,1'), |
| 2099 | ], ordered=False) |
| 2100 | matches = self.searchForContent(self.history[0].jobdir.root, |
| 2101 | b'test-password') |
James E. Blair | d6a71ca | 2017-08-18 14:15:05 -0700 | [diff] [blame] | 2102 | self.assertEqual(set(['/work/secret-file.txt']), |
James E. Blair | db08903 | 2017-08-15 13:42:12 -0700 | [diff] [blame] | 2103 | set(matches)) |
| 2104 | |
| 2105 | def test_secret_file(self): |
| 2106 | self._test_secret_file() |
| 2107 | |
| 2108 | def test_secret_file_verbose(self): |
| 2109 | # Output extra ansible info to exercise alternate logging code |
| 2110 | # paths. |
| 2111 | self.executor_server.verbose = True |
| 2112 | self._test_secret_file() |
| 2113 | |
| 2114 | def _test_secret_file_fail(self): |
| 2115 | # Or rather -- test that they *don't* leak. |
| 2116 | # Keep the jobdir around so we can inspect contents. |
| 2117 | self.executor_server.keep_jobdir = True |
| 2118 | conf = textwrap.dedent( |
| 2119 | """ |
| 2120 | - project: |
| 2121 | name: org/project |
| 2122 | check: |
| 2123 | jobs: |
| 2124 | - secret-file-fail |
| 2125 | """) |
| 2126 | |
| 2127 | file_dict = {'.zuul.yaml': conf} |
| 2128 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 2129 | files=file_dict) |
| 2130 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 2131 | self.waitUntilSettled() |
| 2132 | self.assertHistory([ |
| 2133 | dict(name='secret-file-fail', result='FAILURE', changes='1,1'), |
| 2134 | ], ordered=False) |
| 2135 | matches = self.searchForContent(self.history[0].jobdir.root, |
| 2136 | b'test-password') |
James E. Blair | d6a71ca | 2017-08-18 14:15:05 -0700 | [diff] [blame] | 2137 | self.assertEqual(set(['/work/failure-file.txt']), |
James E. Blair | db08903 | 2017-08-15 13:42:12 -0700 | [diff] [blame] | 2138 | set(matches)) |
| 2139 | |
| 2140 | def test_secret_file_fail(self): |
| 2141 | self._test_secret_file_fail() |
| 2142 | |
| 2143 | def test_secret_file_fail_verbose(self): |
| 2144 | # Output extra ansible info to exercise alternate logging code |
| 2145 | # paths. |
| 2146 | self.executor_server.verbose = True |
| 2147 | self._test_secret_file_fail() |
James E. Blair | a00910c | 2017-08-23 09:15:04 -0700 | [diff] [blame] | 2148 | |
| 2149 | |
| 2150 | class TestJobOutput(AnsibleZuulTestCase): |
| 2151 | tenant_config_file = 'config/job-output/main.yaml' |
| 2152 | |
| 2153 | def _get_file(self, build, path): |
| 2154 | p = os.path.join(build.jobdir.root, path) |
| 2155 | with open(p) as f: |
| 2156 | return f.read() |
| 2157 | |
| 2158 | def test_job_output(self): |
Monty Taylor | 0e2489a | 2017-10-10 11:57:29 -0500 | [diff] [blame] | 2159 | # Verify that command standard output appears in the job output, |
| 2160 | # and that failures in the final playbook get logged. |
James E. Blair | a00910c | 2017-08-23 09:15:04 -0700 | [diff] [blame] | 2161 | |
| 2162 | # This currently only verifies we receive output from |
| 2163 | # localhost. Notably, it does not verify we receive output |
| 2164 | # via zuul_console streaming. |
| 2165 | self.executor_server.keep_jobdir = True |
| 2166 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2167 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 2168 | self.waitUntilSettled() |
| 2169 | self.assertHistory([ |
| 2170 | dict(name='job-output', result='SUCCESS', changes='1,1'), |
| 2171 | ], ordered=False) |
| 2172 | |
| 2173 | token = 'Standard output test %s' % (self.history[0].jobdir.src_root) |
| 2174 | j = json.loads(self._get_file(self.history[0], |
| 2175 | 'work/logs/job-output.json')) |
| 2176 | self.assertEqual(token, |
| 2177 | j[0]['plays'][0]['tasks'][0] |
| 2178 | ['hosts']['localhost']['stdout']) |
| 2179 | |
| 2180 | print(self._get_file(self.history[0], |
| 2181 | 'work/logs/job-output.txt')) |
| 2182 | self.assertIn(token, |
| 2183 | self._get_file(self.history[0], |
| 2184 | 'work/logs/job-output.txt')) |
Monty Taylor | 0e2489a | 2017-10-10 11:57:29 -0500 | [diff] [blame] | 2185 | |
| 2186 | def test_job_output_failure_log(self): |
| 2187 | logger = logging.getLogger('zuul.AnsibleJob') |
| 2188 | output = io.StringIO() |
| 2189 | logger.addHandler(logging.StreamHandler(output)) |
| 2190 | |
| 2191 | # Verify that a failure in the last post playbook emits the contents |
| 2192 | # of the json output to the log |
| 2193 | self.executor_server.keep_jobdir = True |
| 2194 | A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A') |
| 2195 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 2196 | self.waitUntilSettled() |
| 2197 | self.assertHistory([ |
| 2198 | dict(name='job-output-failure', |
| 2199 | result='POST_FAILURE', changes='1,1'), |
| 2200 | ], ordered=False) |
| 2201 | |
| 2202 | token = 'Standard output test %s' % (self.history[0].jobdir.src_root) |
| 2203 | j = json.loads(self._get_file(self.history[0], |
| 2204 | 'work/logs/job-output.json')) |
| 2205 | self.assertEqual(token, |
| 2206 | j[0]['plays'][0]['tasks'][0] |
| 2207 | ['hosts']['localhost']['stdout']) |
| 2208 | |
| 2209 | print(self._get_file(self.history[0], |
| 2210 | 'work/logs/job-output.json')) |
| 2211 | self.assertIn(token, |
| 2212 | self._get_file(self.history[0], |
| 2213 | 'work/logs/job-output.txt')) |
| 2214 | |
| 2215 | log_output = output.getvalue() |
| 2216 | self.assertIn('Final playbook failed', log_output) |
| 2217 | self.assertIn('Failure test', log_output) |