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 | |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 20 | from cryptography.hazmat.primitives import serialization |
| 21 | from cryptography.hazmat.backends import default_backend |
James E. Blair | b9c0d77 | 2017-03-03 14:34:49 -0800 | [diff] [blame] | 22 | import testtools |
| 23 | |
| 24 | import zuul.configloader |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 25 | from tests.base import AnsibleZuulTestCase, ZuulTestCase, FIXTURE_DIR |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 26 | |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 27 | |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 28 | class TestMultipleTenants(AnsibleZuulTestCase): |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 29 | # A temporary class to hold new tests while others are disabled |
| 30 | |
James E. Blair | 2a629ec | 2015-12-22 15:32:02 -0800 | [diff] [blame] | 31 | tenant_config_file = 'config/multi-tenant/main.yaml' |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 32 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 33 | def test_multiple_tenants(self): |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 34 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [diff] [blame] | 35 | A.addApproval('code-review', 2) |
| 36 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 37 | self.waitUntilSettled() |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 38 | self.assertEqual(self.getJobFromHistory('project1-test1').result, |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 39 | 'SUCCESS') |
James E. Blair | 96c6bf8 | 2016-01-15 16:20:40 -0800 | [diff] [blame] | 40 | self.assertEqual(self.getJobFromHistory('python27').result, |
| 41 | 'SUCCESS') |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 42 | self.assertEqual(A.data['status'], 'MERGED') |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 43 | self.assertEqual(A.reported, 2, |
| 44 | "A should report start and success") |
| 45 | self.assertIn('tenant-one-gate', A.messages[1], |
| 46 | "A should transit tenant-one gate") |
| 47 | self.assertNotIn('tenant-two-gate', A.messages[1], |
| 48 | "A should *not* transit tenant-two gate") |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 49 | |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 50 | B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B') |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [diff] [blame] | 51 | B.addApproval('code-review', 2) |
| 52 | self.fake_gerrit.addEvent(B.addApproval('approved', 1)) |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 53 | self.waitUntilSettled() |
James E. Blair | 96c6bf8 | 2016-01-15 16:20:40 -0800 | [diff] [blame] | 54 | self.assertEqual(self.getJobFromHistory('python27', |
| 55 | 'org/project2').result, |
| 56 | 'SUCCESS') |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 57 | self.assertEqual(self.getJobFromHistory('project2-test1').result, |
| 58 | 'SUCCESS') |
| 59 | self.assertEqual(B.data['status'], 'MERGED') |
| 60 | self.assertEqual(B.reported, 2, |
| 61 | "B should report start and success") |
| 62 | self.assertIn('tenant-two-gate', B.messages[1], |
| 63 | "B should transit tenant-two gate") |
| 64 | self.assertNotIn('tenant-one-gate', B.messages[1], |
| 65 | "B should *not* transit tenant-one gate") |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 66 | |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 67 | self.assertEqual(A.reported, 2, "Activity in tenant two should" |
| 68 | "not affect tenant one") |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 69 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 70 | |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 71 | class TestInRepoConfig(ZuulTestCase): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 72 | # A temporary class to hold new tests while others are disabled |
| 73 | |
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') |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [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: |
| 93 | name: project-test2 |
| 94 | |
| 95 | - project: |
| 96 | name: org/project |
| 97 | tenant-one-gate: |
| 98 | jobs: |
| 99 | - project-test2 |
| 100 | """) |
| 101 | |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 102 | in_repo_playbook = textwrap.dedent( |
| 103 | """ |
| 104 | - hosts: all |
| 105 | tasks: [] |
| 106 | """) |
| 107 | |
| 108 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 109 | 'playbooks/project-test2.yaml': in_repo_playbook} |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 110 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 111 | files=file_dict) |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [diff] [blame] | 112 | A.addApproval('code-review', 2) |
| 113 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 114 | self.waitUntilSettled() |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 115 | self.assertEqual(A.data['status'], 'MERGED') |
| 116 | self.assertEqual(A.reported, 2, |
| 117 | "A should report start and success") |
| 118 | self.assertIn('tenant-one-gate', A.messages[1], |
| 119 | "A should transit tenant-one gate") |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 120 | self.assertHistory([ |
| 121 | dict(name='project-test2', result='SUCCESS', changes='1,1')]) |
| 122 | |
James E. Blair | c2a5ed7 | 2017-02-20 14:12:01 -0500 | [diff] [blame] | 123 | self.fake_gerrit.addEvent(A.getChangeMergedEvent()) |
James E. Blair | 7bbd7a3 | 2017-03-06 11:36:13 -0800 | [diff] [blame] | 124 | self.waitUntilSettled() |
James E. Blair | c2a5ed7 | 2017-02-20 14:12:01 -0500 | [diff] [blame] | 125 | |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 126 | # Now that the config change is landed, it should be live for |
| 127 | # subsequent changes. |
| 128 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 129 | B.addApproval('code-review', 2) |
| 130 | self.fake_gerrit.addEvent(B.addApproval('approved', 1)) |
| 131 | self.waitUntilSettled() |
| 132 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 133 | 'SUCCESS') |
| 134 | self.assertHistory([ |
| 135 | dict(name='project-test2', result='SUCCESS', changes='1,1'), |
| 136 | dict(name='project-test2', result='SUCCESS', changes='2,1')]) |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 137 | |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 138 | def test_in_repo_branch(self): |
| 139 | in_repo_conf = textwrap.dedent( |
| 140 | """ |
| 141 | - job: |
| 142 | name: project-test2 |
| 143 | |
| 144 | - project: |
| 145 | name: org/project |
| 146 | tenant-one-gate: |
| 147 | jobs: |
| 148 | - project-test2 |
| 149 | """) |
| 150 | |
| 151 | in_repo_playbook = textwrap.dedent( |
| 152 | """ |
| 153 | - hosts: all |
| 154 | tasks: [] |
| 155 | """) |
| 156 | |
| 157 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 158 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 159 | self.create_branch('org/project', 'stable') |
| 160 | A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A', |
| 161 | files=file_dict) |
| 162 | A.addApproval('code-review', 2) |
| 163 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
| 164 | self.waitUntilSettled() |
| 165 | self.assertEqual(A.data['status'], 'MERGED') |
| 166 | self.assertEqual(A.reported, 2, |
| 167 | "A should report start and success") |
| 168 | self.assertIn('tenant-one-gate', A.messages[1], |
| 169 | "A should transit tenant-one gate") |
| 170 | self.assertHistory([ |
| 171 | dict(name='project-test2', result='SUCCESS', changes='1,1')]) |
| 172 | self.fake_gerrit.addEvent(A.getChangeMergedEvent()) |
James E. Blair | 7bbd7a3 | 2017-03-06 11:36:13 -0800 | [diff] [blame] | 173 | self.waitUntilSettled() |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 174 | |
| 175 | # The config change should not affect master. |
| 176 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 177 | B.addApproval('code-review', 2) |
| 178 | self.fake_gerrit.addEvent(B.addApproval('approved', 1)) |
| 179 | self.waitUntilSettled() |
| 180 | self.assertHistory([ |
| 181 | dict(name='project-test2', result='SUCCESS', changes='1,1'), |
| 182 | dict(name='project-test1', result='SUCCESS', changes='2,1')]) |
| 183 | |
| 184 | # The config change should be live for further changes on |
| 185 | # stable. |
| 186 | C = self.fake_gerrit.addFakeChange('org/project', 'stable', 'C') |
| 187 | C.addApproval('code-review', 2) |
| 188 | self.fake_gerrit.addEvent(C.addApproval('approved', 1)) |
| 189 | self.waitUntilSettled() |
| 190 | self.assertHistory([ |
| 191 | dict(name='project-test2', result='SUCCESS', changes='1,1'), |
| 192 | dict(name='project-test1', result='SUCCESS', changes='2,1'), |
| 193 | dict(name='project-test2', result='SUCCESS', changes='3,1')]) |
| 194 | |
James E. Blair | 149b69c | 2017-03-02 10:48:16 -0800 | [diff] [blame] | 195 | def test_untrusted_syntax_error(self): |
James E. Blair | e53250c | 2017-03-01 14:34:36 -0800 | [diff] [blame] | 196 | in_repo_conf = textwrap.dedent( |
| 197 | """ |
| 198 | - job: |
| 199 | name: project-test2 |
| 200 | foo: error |
| 201 | """) |
| 202 | |
| 203 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 204 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 205 | files=file_dict) |
| 206 | A.addApproval('code-review', 2) |
| 207 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
| 208 | self.waitUntilSettled() |
| 209 | |
| 210 | self.assertEqual(A.data['status'], 'NEW') |
| 211 | self.assertEqual(A.reported, 2, |
| 212 | "A should report start and failure") |
| 213 | self.assertIn('syntax error', A.messages[1], |
| 214 | "A should have a syntax error reported") |
| 215 | |
James E. Blair | 149b69c | 2017-03-02 10:48:16 -0800 | [diff] [blame] | 216 | def test_trusted_syntax_error(self): |
| 217 | in_repo_conf = textwrap.dedent( |
| 218 | """ |
| 219 | - job: |
| 220 | name: project-test2 |
| 221 | foo: error |
| 222 | """) |
| 223 | |
| 224 | file_dict = {'zuul.yaml': in_repo_conf} |
| 225 | A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A', |
| 226 | files=file_dict) |
| 227 | A.addApproval('code-review', 2) |
| 228 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
| 229 | self.waitUntilSettled() |
| 230 | |
| 231 | self.assertEqual(A.data['status'], 'NEW') |
| 232 | self.assertEqual(A.reported, 2, |
| 233 | "A should report start and failure") |
| 234 | self.assertIn('syntax error', A.messages[1], |
| 235 | "A should have a syntax error reported") |
| 236 | |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 237 | def test_untrusted_yaml_error(self): |
| 238 | in_repo_conf = textwrap.dedent( |
| 239 | """ |
| 240 | - job: |
| 241 | foo: error |
| 242 | """) |
| 243 | |
| 244 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 245 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 246 | files=file_dict) |
| 247 | A.addApproval('code-review', 2) |
| 248 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
| 249 | self.waitUntilSettled() |
| 250 | |
| 251 | self.assertEqual(A.data['status'], 'NEW') |
| 252 | self.assertEqual(A.reported, 2, |
| 253 | "A should report start and failure") |
| 254 | self.assertIn('syntax error', A.messages[1], |
| 255 | "A should have a syntax error reported") |
| 256 | |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 257 | |
| 258 | class TestAnsible(AnsibleZuulTestCase): |
| 259 | # A temporary class to hold new tests while others are disabled |
| 260 | |
| 261 | tenant_config_file = 'config/ansible/main.yaml' |
| 262 | |
| 263 | def test_playbook(self): |
| 264 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 265 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 266 | self.waitUntilSettled() |
Paul Belanger | 96618ed | 2017-03-01 09:42:33 -0500 | [diff] [blame] | 267 | build = self.getJobFromHistory('timeout') |
| 268 | self.assertEqual(build.result, 'ABORTED') |
Monty Taylor | c231d93 | 2017-02-03 09:57:15 -0600 | [diff] [blame] | 269 | build = self.getJobFromHistory('faillocal') |
| 270 | self.assertEqual(build.result, 'FAILURE') |
Paul Belanger | e2b8d49 | 2017-03-22 18:57:40 -0400 | [diff] [blame] | 271 | build = self.getJobFromHistory('check-vars') |
Paul Belanger | 30ba93a | 2017-03-16 16:28:10 -0400 | [diff] [blame] | 272 | self.assertEqual(build.result, 'SUCCESS') |
James E. Blair | a92cbc8 | 2017-01-23 14:56:49 -0800 | [diff] [blame] | 273 | build = self.getJobFromHistory('python27') |
| 274 | self.assertEqual(build.result, 'SUCCESS') |
| 275 | flag_path = os.path.join(self.test_root, build.uuid + '.flag') |
| 276 | self.assertTrue(os.path.exists(flag_path)) |
Monty Taylor | c231d93 | 2017-02-03 09:57:15 -0600 | [diff] [blame] | 277 | copied_path = os.path.join(self.test_root, build.uuid + |
| 278 | '.copied') |
| 279 | self.assertTrue(os.path.exists(copied_path)) |
| 280 | failed_path = os.path.join(self.test_root, build.uuid + |
| 281 | '.failed') |
| 282 | self.assertFalse(os.path.exists(failed_path)) |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 283 | pre_flag_path = os.path.join(self.test_root, build.uuid + |
| 284 | '.pre.flag') |
| 285 | self.assertTrue(os.path.exists(pre_flag_path)) |
| 286 | post_flag_path = os.path.join(self.test_root, build.uuid + |
| 287 | '.post.flag') |
| 288 | self.assertTrue(os.path.exists(post_flag_path)) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 289 | bare_role_flag_path = os.path.join(self.test_root, |
| 290 | build.uuid + '.bare-role.flag') |
| 291 | self.assertTrue(os.path.exists(bare_role_flag_path)) |
James E. Blair | b9c0d77 | 2017-03-03 14:34:49 -0800 | [diff] [blame] | 292 | |
| 293 | |
| 294 | class TestBrokenConfig(ZuulTestCase): |
| 295 | # Test that we get an appropriate syntax error if we start with a |
| 296 | # broken config. |
| 297 | |
| 298 | tenant_config_file = 'config/broken/main.yaml' |
| 299 | |
| 300 | def setUp(self): |
| 301 | with testtools.ExpectedException( |
| 302 | zuul.configloader.ConfigurationSyntaxError, |
| 303 | "\nZuul encountered a syntax error"): |
| 304 | super(TestBrokenConfig, self).setUp() |
| 305 | |
| 306 | def test_broken_config_on_startup(self): |
| 307 | pass |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 308 | |
| 309 | |
| 310 | class TestProjectKeys(ZuulTestCase): |
| 311 | # Test that we can generate project keys |
| 312 | |
| 313 | # Normally the test infrastructure copies a static key in place |
| 314 | # for each project before starting tests. This saves time because |
| 315 | # Zuul's automatic key-generation on startup can be slow. To make |
| 316 | # sure we exercise that code, in this test we allow Zuul to create |
| 317 | # keys for the project on startup. |
| 318 | create_project_keys = True |
| 319 | tenant_config_file = 'config/in-repo/main.yaml' |
| 320 | |
| 321 | def test_key_generation(self): |
| 322 | key_root = os.path.join(self.state_root, 'keys') |
| 323 | private_key_file = os.path.join(key_root, 'gerrit/org/project.pem') |
| 324 | # Make sure that a proper key was created on startup |
| 325 | with open(private_key_file, "rb") as f: |
| 326 | private_key = serialization.load_pem_private_key( |
| 327 | f.read(), |
| 328 | password=None, |
| 329 | backend=default_backend() |
| 330 | ) |
| 331 | |
| 332 | with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i: |
| 333 | fixture_private_key = i.read() |
| 334 | |
| 335 | # Make sure that we didn't just end up with the static fixture |
| 336 | # key |
| 337 | self.assertNotEqual(fixture_private_key, private_key) |
| 338 | |
| 339 | # Make sure it's the right length |
| 340 | self.assertEqual(4096, private_key.key_size) |