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