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