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 | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 20 | from tests.base import AnsibleZuulTestCase, ZuulTestCase |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 21 | |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 22 | |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 23 | class TestMultipleTenants(AnsibleZuulTestCase): |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 24 | # A temporary class to hold new tests while others are disabled |
| 25 | |
James E. Blair | 2a629ec | 2015-12-22 15:32:02 -0800 | [diff] [blame] | 26 | tenant_config_file = 'config/multi-tenant/main.yaml' |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 27 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 28 | def test_multiple_tenants(self): |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 29 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [diff] [blame] | 30 | A.addApproval('code-review', 2) |
| 31 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 32 | self.waitUntilSettled() |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 33 | self.assertEqual(self.getJobFromHistory('project1-test1').result, |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 34 | 'SUCCESS') |
James E. Blair | 96c6bf8 | 2016-01-15 16:20:40 -0800 | [diff] [blame] | 35 | self.assertEqual(self.getJobFromHistory('python27').result, |
| 36 | 'SUCCESS') |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 37 | self.assertEqual(A.data['status'], 'MERGED') |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 38 | self.assertEqual(A.reported, 2, |
| 39 | "A should report start and success") |
| 40 | self.assertIn('tenant-one-gate', A.messages[1], |
| 41 | "A should transit tenant-one gate") |
| 42 | self.assertNotIn('tenant-two-gate', A.messages[1], |
| 43 | "A should *not* transit tenant-two gate") |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 44 | |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 45 | B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B') |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [diff] [blame] | 46 | B.addApproval('code-review', 2) |
| 47 | self.fake_gerrit.addEvent(B.addApproval('approved', 1)) |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 48 | self.waitUntilSettled() |
James E. Blair | 96c6bf8 | 2016-01-15 16:20:40 -0800 | [diff] [blame] | 49 | self.assertEqual(self.getJobFromHistory('python27', |
| 50 | 'org/project2').result, |
| 51 | 'SUCCESS') |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 52 | self.assertEqual(self.getJobFromHistory('project2-test1').result, |
| 53 | 'SUCCESS') |
| 54 | self.assertEqual(B.data['status'], 'MERGED') |
| 55 | self.assertEqual(B.reported, 2, |
| 56 | "B should report start and success") |
| 57 | self.assertIn('tenant-two-gate', B.messages[1], |
| 58 | "B should transit tenant-two gate") |
| 59 | self.assertNotIn('tenant-one-gate', B.messages[1], |
| 60 | "B should *not* transit tenant-one gate") |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 61 | |
James E. Blair | 96f2694 | 2015-12-09 10:15:59 -0800 | [diff] [blame] | 62 | self.assertEqual(A.reported, 2, "Activity in tenant two should" |
| 63 | "not affect tenant one") |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 64 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 65 | |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 66 | class TestInRepoConfig(ZuulTestCase): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 67 | # A temporary class to hold new tests while others are disabled |
| 68 | |
James E. Blair | 2a629ec | 2015-12-22 15:32:02 -0800 | [diff] [blame] | 69 | tenant_config_file = 'config/in-repo/main.yaml' |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 70 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 71 | def test_in_repo_config(self): |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 72 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [diff] [blame] | 73 | A.addApproval('code-review', 2) |
| 74 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 75 | self.waitUntilSettled() |
| 76 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 77 | 'SUCCESS') |
| 78 | self.assertEqual(A.data['status'], 'MERGED') |
| 79 | self.assertEqual(A.reported, 2, |
| 80 | "A should report start and success") |
| 81 | self.assertIn('tenant-one-gate', A.messages[1], |
| 82 | "A should transit tenant-one gate") |
James E. Blair | b97ed80 | 2015-12-21 15:55:35 -0800 | [diff] [blame] | 83 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 84 | def test_dynamic_config(self): |
| 85 | in_repo_conf = textwrap.dedent( |
| 86 | """ |
| 87 | - job: |
| 88 | name: project-test2 |
| 89 | |
| 90 | - project: |
| 91 | name: org/project |
| 92 | tenant-one-gate: |
| 93 | jobs: |
| 94 | - project-test2 |
| 95 | """) |
| 96 | |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 97 | in_repo_playbook = textwrap.dedent( |
| 98 | """ |
| 99 | - hosts: all |
| 100 | tasks: [] |
| 101 | """) |
| 102 | |
| 103 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 104 | 'playbooks/project-test2.yaml': in_repo_playbook} |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 105 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 106 | files=file_dict) |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [diff] [blame] | 107 | A.addApproval('code-review', 2) |
| 108 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 109 | self.waitUntilSettled() |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 110 | self.assertEqual(A.data['status'], 'MERGED') |
| 111 | self.assertEqual(A.reported, 2, |
| 112 | "A should report start and success") |
| 113 | self.assertIn('tenant-one-gate', A.messages[1], |
| 114 | "A should transit tenant-one gate") |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 115 | self.assertHistory([ |
| 116 | dict(name='project-test2', result='SUCCESS', changes='1,1')]) |
| 117 | |
James E. Blair | c2a5ed7 | 2017-02-20 14:12:01 -0500 | [diff] [blame] | 118 | self.fake_gerrit.addEvent(A.getChangeMergedEvent()) |
James E. Blair | 7bbd7a3 | 2017-03-06 11:36:13 -0800 | [diff] [blame] | 119 | self.waitUntilSettled() |
James E. Blair | c2a5ed7 | 2017-02-20 14:12:01 -0500 | [diff] [blame] | 120 | |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 121 | # Now that the config change is landed, it should be live for |
| 122 | # subsequent changes. |
| 123 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 124 | B.addApproval('code-review', 2) |
| 125 | self.fake_gerrit.addEvent(B.addApproval('approved', 1)) |
| 126 | self.waitUntilSettled() |
| 127 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 128 | 'SUCCESS') |
| 129 | self.assertHistory([ |
| 130 | dict(name='project-test2', result='SUCCESS', changes='1,1'), |
| 131 | dict(name='project-test2', result='SUCCESS', changes='2,1')]) |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 132 | |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 133 | def test_in_repo_branch(self): |
| 134 | in_repo_conf = textwrap.dedent( |
| 135 | """ |
| 136 | - job: |
| 137 | name: project-test2 |
| 138 | |
| 139 | - project: |
| 140 | name: org/project |
| 141 | tenant-one-gate: |
| 142 | jobs: |
| 143 | - project-test2 |
| 144 | """) |
| 145 | |
| 146 | in_repo_playbook = textwrap.dedent( |
| 147 | """ |
| 148 | - hosts: all |
| 149 | tasks: [] |
| 150 | """) |
| 151 | |
| 152 | file_dict = {'.zuul.yaml': in_repo_conf, |
| 153 | 'playbooks/project-test2.yaml': in_repo_playbook} |
| 154 | self.create_branch('org/project', 'stable') |
| 155 | A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A', |
| 156 | files=file_dict) |
| 157 | A.addApproval('code-review', 2) |
| 158 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
| 159 | self.waitUntilSettled() |
| 160 | self.assertEqual(A.data['status'], 'MERGED') |
| 161 | self.assertEqual(A.reported, 2, |
| 162 | "A should report start and success") |
| 163 | self.assertIn('tenant-one-gate', A.messages[1], |
| 164 | "A should transit tenant-one gate") |
| 165 | self.assertHistory([ |
| 166 | dict(name='project-test2', result='SUCCESS', changes='1,1')]) |
| 167 | self.fake_gerrit.addEvent(A.getChangeMergedEvent()) |
James E. Blair | 7bbd7a3 | 2017-03-06 11:36:13 -0800 | [diff] [blame] | 168 | self.waitUntilSettled() |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 169 | |
| 170 | # The config change should not affect master. |
| 171 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 172 | B.addApproval('code-review', 2) |
| 173 | self.fake_gerrit.addEvent(B.addApproval('approved', 1)) |
| 174 | self.waitUntilSettled() |
| 175 | self.assertHistory([ |
| 176 | dict(name='project-test2', result='SUCCESS', changes='1,1'), |
| 177 | dict(name='project-test1', result='SUCCESS', changes='2,1')]) |
| 178 | |
| 179 | # The config change should be live for further changes on |
| 180 | # stable. |
| 181 | C = self.fake_gerrit.addFakeChange('org/project', 'stable', 'C') |
| 182 | C.addApproval('code-review', 2) |
| 183 | self.fake_gerrit.addEvent(C.addApproval('approved', 1)) |
| 184 | self.waitUntilSettled() |
| 185 | self.assertHistory([ |
| 186 | dict(name='project-test2', result='SUCCESS', changes='1,1'), |
| 187 | dict(name='project-test1', result='SUCCESS', changes='2,1'), |
| 188 | dict(name='project-test2', result='SUCCESS', changes='3,1')]) |
| 189 | |
James E. Blair | 149b69c | 2017-03-02 10:48:16 -0800 | [diff] [blame] | 190 | def test_untrusted_syntax_error(self): |
James E. Blair | e53250c | 2017-03-01 14:34:36 -0800 | [diff] [blame] | 191 | in_repo_conf = textwrap.dedent( |
| 192 | """ |
| 193 | - job: |
| 194 | name: project-test2 |
| 195 | foo: error |
| 196 | """) |
| 197 | |
| 198 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 199 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 200 | files=file_dict) |
| 201 | A.addApproval('code-review', 2) |
| 202 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
| 203 | self.waitUntilSettled() |
| 204 | |
| 205 | self.assertEqual(A.data['status'], 'NEW') |
| 206 | self.assertEqual(A.reported, 2, |
| 207 | "A should report start and failure") |
| 208 | self.assertIn('syntax error', A.messages[1], |
| 209 | "A should have a syntax error reported") |
| 210 | |
James E. Blair | 149b69c | 2017-03-02 10:48:16 -0800 | [diff] [blame] | 211 | def test_trusted_syntax_error(self): |
| 212 | in_repo_conf = textwrap.dedent( |
| 213 | """ |
| 214 | - job: |
| 215 | name: project-test2 |
| 216 | foo: error |
| 217 | """) |
| 218 | |
| 219 | file_dict = {'zuul.yaml': in_repo_conf} |
| 220 | A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A', |
| 221 | files=file_dict) |
| 222 | A.addApproval('code-review', 2) |
| 223 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
| 224 | self.waitUntilSettled() |
| 225 | |
| 226 | self.assertEqual(A.data['status'], 'NEW') |
| 227 | self.assertEqual(A.reported, 2, |
| 228 | "A should report start and failure") |
| 229 | self.assertIn('syntax error', A.messages[1], |
| 230 | "A should have a syntax error reported") |
| 231 | |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 232 | def test_untrusted_yaml_error(self): |
| 233 | in_repo_conf = textwrap.dedent( |
| 234 | """ |
| 235 | - job: |
| 236 | foo: error |
| 237 | """) |
| 238 | |
| 239 | file_dict = {'.zuul.yaml': in_repo_conf} |
| 240 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', |
| 241 | files=file_dict) |
| 242 | A.addApproval('code-review', 2) |
| 243 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
| 244 | self.waitUntilSettled() |
| 245 | |
| 246 | self.assertEqual(A.data['status'], 'NEW') |
| 247 | self.assertEqual(A.reported, 2, |
| 248 | "A should report start and failure") |
| 249 | self.assertIn('syntax error', A.messages[1], |
| 250 | "A should have a syntax error reported") |
| 251 | |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 252 | |
| 253 | class TestAnsible(AnsibleZuulTestCase): |
| 254 | # A temporary class to hold new tests while others are disabled |
| 255 | |
| 256 | tenant_config_file = 'config/ansible/main.yaml' |
| 257 | |
| 258 | def test_playbook(self): |
| 259 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 260 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 261 | self.waitUntilSettled() |
Paul Belanger | 96618ed | 2017-03-01 09:42:33 -0500 | [diff] [blame] | 262 | build = self.getJobFromHistory('timeout') |
| 263 | self.assertEqual(build.result, 'ABORTED') |
Monty Taylor | c231d93 | 2017-02-03 09:57:15 -0600 | [diff] [blame] | 264 | build = self.getJobFromHistory('faillocal') |
| 265 | self.assertEqual(build.result, 'FAILURE') |
James E. Blair | a92cbc8 | 2017-01-23 14:56:49 -0800 | [diff] [blame] | 266 | build = self.getJobFromHistory('python27') |
| 267 | self.assertEqual(build.result, 'SUCCESS') |
| 268 | flag_path = os.path.join(self.test_root, build.uuid + '.flag') |
| 269 | self.assertTrue(os.path.exists(flag_path)) |
Monty Taylor | c231d93 | 2017-02-03 09:57:15 -0600 | [diff] [blame] | 270 | copied_path = os.path.join(self.test_root, build.uuid + |
| 271 | '.copied') |
| 272 | self.assertTrue(os.path.exists(copied_path)) |
| 273 | failed_path = os.path.join(self.test_root, build.uuid + |
| 274 | '.failed') |
| 275 | self.assertFalse(os.path.exists(failed_path)) |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 276 | pre_flag_path = os.path.join(self.test_root, build.uuid + |
| 277 | '.pre.flag') |
| 278 | self.assertTrue(os.path.exists(pre_flag_path)) |
| 279 | post_flag_path = os.path.join(self.test_root, build.uuid + |
| 280 | '.post.flag') |
| 281 | self.assertTrue(os.path.exists(post_flag_path)) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 282 | bare_role_flag_path = os.path.join(self.test_root, |
| 283 | build.uuid + '.bare-role.flag') |
| 284 | self.assertTrue(os.path.exists(bare_role_flag_path)) |