James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright 2012-2014 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 | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 17 | import time |
| 18 | |
| 19 | from tests.base import ZuulTestCase |
| 20 | |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 21 | |
Jamie Lennox | b59a73f | 2016-11-23 14:27:18 +1100 | [diff] [blame] | 22 | class TestRequirementsApprovalNewerThan(ZuulTestCase): |
| 23 | """Requirements with a newer-than comment requirement""" |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 24 | |
Jamie Lennox | b59a73f | 2016-11-23 14:27:18 +1100 | [diff] [blame] | 25 | tenant_config_file = 'config/requirements/newer-than/main.yaml' |
| 26 | |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 27 | def test_pipeline_require_approval_newer_than(self): |
| 28 | "Test pipeline requirement: approval newer than" |
| 29 | return self._test_require_approval_newer_than('org/project1', |
Jamie Lennox | b59a73f | 2016-11-23 14:27:18 +1100 | [diff] [blame] | 30 | 'project1-job') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 31 | |
| 32 | def test_trigger_require_approval_newer_than(self): |
| 33 | "Test trigger requirement: approval newer than" |
| 34 | return self._test_require_approval_newer_than('org/project2', |
Jamie Lennox | b59a73f | 2016-11-23 14:27:18 +1100 | [diff] [blame] | 35 | 'project2-job') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 36 | |
| 37 | def _test_require_approval_newer_than(self, project, job): |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 38 | A = self.fake_gerrit.addFakeChange(project, 'master', 'A') |
| 39 | # A comment event that we will keep submitting to trigger |
Jamie Lennox | b59a73f | 2016-11-23 14:27:18 +1100 | [diff] [blame] | 40 | comment = A.addApproval('code-review', 2, username='nobody') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 41 | self.fake_gerrit.addEvent(comment) |
| 42 | self.waitUntilSettled() |
| 43 | # No +1 from Jenkins so should not be enqueued |
| 44 | self.assertEqual(len(self.history), 0) |
| 45 | |
| 46 | # Add a too-old +1, should not be enqueued |
Jamie Lennox | b59a73f | 2016-11-23 14:27:18 +1100 | [diff] [blame] | 47 | A.addApproval('verified', 1, username='jenkins', |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 48 | granted_on=time.time() - 72 * 60 * 60) |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 49 | self.fake_gerrit.addEvent(comment) |
| 50 | self.waitUntilSettled() |
| 51 | self.assertEqual(len(self.history), 0) |
| 52 | |
| 53 | # Add a recent +1 |
Jamie Lennox | b59a73f | 2016-11-23 14:27:18 +1100 | [diff] [blame] | 54 | self.fake_gerrit.addEvent(A.addApproval('verified', 1, |
| 55 | username='jenkins')) |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 56 | self.fake_gerrit.addEvent(comment) |
| 57 | self.waitUntilSettled() |
| 58 | self.assertEqual(len(self.history), 1) |
| 59 | self.assertEqual(self.history[0].name, job) |
| 60 | |
Jamie Lennox | b59a73f | 2016-11-23 14:27:18 +1100 | [diff] [blame] | 61 | |
Jamie Lennox | 8faa71e | 2016-11-23 14:33:52 +1100 | [diff] [blame] | 62 | class TestRequirementsApprovalOlderThan(ZuulTestCase): |
| 63 | """Requirements with a older-than comment requirement""" |
Jamie Lennox | b59a73f | 2016-11-23 14:27:18 +1100 | [diff] [blame] | 64 | |
Jamie Lennox | 8faa71e | 2016-11-23 14:33:52 +1100 | [diff] [blame] | 65 | tenant_config_file = 'config/requirements/older-than/main.yaml' |
Jamie Lennox | b59a73f | 2016-11-23 14:27:18 +1100 | [diff] [blame] | 66 | |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 67 | def test_pipeline_require_approval_older_than(self): |
| 68 | "Test pipeline requirement: approval older than" |
| 69 | return self._test_require_approval_older_than('org/project1', |
Jamie Lennox | 8faa71e | 2016-11-23 14:33:52 +1100 | [diff] [blame] | 70 | 'project1-job') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 71 | |
| 72 | def test_trigger_require_approval_older_than(self): |
| 73 | "Test trigger requirement: approval older than" |
| 74 | return self._test_require_approval_older_than('org/project2', |
Jamie Lennox | 8faa71e | 2016-11-23 14:33:52 +1100 | [diff] [blame] | 75 | 'project2-job') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 76 | |
| 77 | def _test_require_approval_older_than(self, project, job): |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 78 | A = self.fake_gerrit.addFakeChange(project, 'master', 'A') |
| 79 | # A comment event that we will keep submitting to trigger |
Jamie Lennox | 8faa71e | 2016-11-23 14:33:52 +1100 | [diff] [blame] | 80 | comment = A.addApproval('code-review', 2, username='nobody') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 81 | self.fake_gerrit.addEvent(comment) |
| 82 | self.waitUntilSettled() |
| 83 | # No +1 from Jenkins so should not be enqueued |
| 84 | self.assertEqual(len(self.history), 0) |
| 85 | |
| 86 | # Add a recent +1 which should not be enqueued |
Jamie Lennox | 8faa71e | 2016-11-23 14:33:52 +1100 | [diff] [blame] | 87 | A.addApproval('verified', 1) |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 88 | self.fake_gerrit.addEvent(comment) |
| 89 | self.waitUntilSettled() |
| 90 | self.assertEqual(len(self.history), 0) |
| 91 | |
| 92 | # Add an old +1 which should be enqueued |
Jamie Lennox | 8faa71e | 2016-11-23 14:33:52 +1100 | [diff] [blame] | 93 | A.addApproval('verified', 1, username='jenkins', |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 94 | granted_on=time.time() - 72 * 60 * 60) |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 95 | self.fake_gerrit.addEvent(comment) |
| 96 | self.waitUntilSettled() |
| 97 | self.assertEqual(len(self.history), 1) |
| 98 | self.assertEqual(self.history[0].name, job) |
| 99 | |
Jamie Lennox | 8faa71e | 2016-11-23 14:33:52 +1100 | [diff] [blame] | 100 | |
Jamie Lennox | db3b4e0 | 2016-11-23 14:36:12 +1100 | [diff] [blame] | 101 | class TestRequirementsUserName(ZuulTestCase): |
| 102 | """Requirements with a username requirement""" |
Jamie Lennox | 8faa71e | 2016-11-23 14:33:52 +1100 | [diff] [blame] | 103 | |
Jamie Lennox | db3b4e0 | 2016-11-23 14:36:12 +1100 | [diff] [blame] | 104 | tenant_config_file = 'config/requirements/username/main.yaml' |
Jamie Lennox | 8faa71e | 2016-11-23 14:33:52 +1100 | [diff] [blame] | 105 | |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 106 | def test_pipeline_require_approval_username(self): |
| 107 | "Test pipeline requirement: approval username" |
| 108 | return self._test_require_approval_username('org/project1', |
Jamie Lennox | db3b4e0 | 2016-11-23 14:36:12 +1100 | [diff] [blame] | 109 | 'project1-job') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 110 | |
| 111 | def test_trigger_require_approval_username(self): |
| 112 | "Test trigger requirement: approval username" |
| 113 | return self._test_require_approval_username('org/project2', |
Jamie Lennox | db3b4e0 | 2016-11-23 14:36:12 +1100 | [diff] [blame] | 114 | 'project2-job') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 115 | |
| 116 | def _test_require_approval_username(self, project, job): |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 117 | A = self.fake_gerrit.addFakeChange(project, 'master', 'A') |
| 118 | # A comment event that we will keep submitting to trigger |
Jamie Lennox | db3b4e0 | 2016-11-23 14:36:12 +1100 | [diff] [blame] | 119 | comment = A.addApproval('code-review', 2, username='nobody') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 120 | self.fake_gerrit.addEvent(comment) |
| 121 | self.waitUntilSettled() |
| 122 | # No approval from Jenkins so should not be enqueued |
| 123 | self.assertEqual(len(self.history), 0) |
| 124 | |
| 125 | # Add an approval from Jenkins |
Jamie Lennox | db3b4e0 | 2016-11-23 14:36:12 +1100 | [diff] [blame] | 126 | A.addApproval('verified', 1, username='jenkins') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 127 | self.fake_gerrit.addEvent(comment) |
| 128 | self.waitUntilSettled() |
| 129 | self.assertEqual(len(self.history), 1) |
| 130 | self.assertEqual(self.history[0].name, job) |
| 131 | |
Jamie Lennox | db3b4e0 | 2016-11-23 14:36:12 +1100 | [diff] [blame] | 132 | |
Jamie Lennox | 90cb6b9 | 2016-11-23 14:37:19 +1100 | [diff] [blame] | 133 | class TestRequirementsEmail(ZuulTestCase): |
| 134 | """Requirements with a email requirement""" |
Jamie Lennox | db3b4e0 | 2016-11-23 14:36:12 +1100 | [diff] [blame] | 135 | |
Jamie Lennox | 90cb6b9 | 2016-11-23 14:37:19 +1100 | [diff] [blame] | 136 | tenant_config_file = 'config/requirements/email/main.yaml' |
Jamie Lennox | db3b4e0 | 2016-11-23 14:36:12 +1100 | [diff] [blame] | 137 | |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 138 | def test_pipeline_require_approval_email(self): |
| 139 | "Test pipeline requirement: approval email" |
| 140 | return self._test_require_approval_email('org/project1', |
Jamie Lennox | 90cb6b9 | 2016-11-23 14:37:19 +1100 | [diff] [blame] | 141 | 'project1-job') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 142 | |
| 143 | def test_trigger_require_approval_email(self): |
| 144 | "Test trigger requirement: approval email" |
| 145 | return self._test_require_approval_email('org/project2', |
Jamie Lennox | 90cb6b9 | 2016-11-23 14:37:19 +1100 | [diff] [blame] | 146 | 'project2-job') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 147 | |
| 148 | def _test_require_approval_email(self, project, job): |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 149 | A = self.fake_gerrit.addFakeChange(project, 'master', 'A') |
| 150 | # A comment event that we will keep submitting to trigger |
Jamie Lennox | 90cb6b9 | 2016-11-23 14:37:19 +1100 | [diff] [blame] | 151 | comment = A.addApproval('code-review', 2, username='nobody') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 152 | self.fake_gerrit.addEvent(comment) |
| 153 | self.waitUntilSettled() |
| 154 | # No approval from Jenkins so should not be enqueued |
| 155 | self.assertEqual(len(self.history), 0) |
| 156 | |
| 157 | # Add an approval from Jenkins |
Jamie Lennox | 90cb6b9 | 2016-11-23 14:37:19 +1100 | [diff] [blame] | 158 | A.addApproval('verified', 1, username='jenkins') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 159 | self.fake_gerrit.addEvent(comment) |
| 160 | self.waitUntilSettled() |
| 161 | self.assertEqual(len(self.history), 1) |
| 162 | self.assertEqual(self.history[0].name, job) |
| 163 | |
Jamie Lennox | 90cb6b9 | 2016-11-23 14:37:19 +1100 | [diff] [blame] | 164 | |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 165 | class TestRequirementsVote1(ZuulTestCase): |
| 166 | """Requirements with a voting requirement""" |
Jamie Lennox | 90cb6b9 | 2016-11-23 14:37:19 +1100 | [diff] [blame] | 167 | |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 168 | tenant_config_file = 'config/requirements/vote1/main.yaml' |
Jamie Lennox | 90cb6b9 | 2016-11-23 14:37:19 +1100 | [diff] [blame] | 169 | |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 170 | def test_pipeline_require_approval_vote1(self): |
| 171 | "Test pipeline requirement: approval vote with one value" |
| 172 | return self._test_require_approval_vote1('org/project1', |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 173 | 'project1-job') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 174 | |
| 175 | def test_trigger_require_approval_vote1(self): |
| 176 | "Test trigger requirement: approval vote with one value" |
| 177 | return self._test_require_approval_vote1('org/project2', |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 178 | 'project2-job') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 179 | |
| 180 | def _test_require_approval_vote1(self, project, job): |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 181 | A = self.fake_gerrit.addFakeChange(project, 'master', 'A') |
| 182 | # A comment event that we will keep submitting to trigger |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 183 | comment = A.addApproval('code-review', 2, username='nobody') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 184 | self.fake_gerrit.addEvent(comment) |
| 185 | self.waitUntilSettled() |
| 186 | # No approval from Jenkins so should not be enqueued |
| 187 | self.assertEqual(len(self.history), 0) |
| 188 | |
| 189 | # A -1 from jenkins should not cause it to be enqueued |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 190 | A.addApproval('verified', -1, username='jenkins') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 191 | self.fake_gerrit.addEvent(comment) |
| 192 | self.waitUntilSettled() |
| 193 | self.assertEqual(len(self.history), 0) |
| 194 | |
| 195 | # A +1 should allow it to be enqueued |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 196 | A.addApproval('verified', 1, username='jenkins') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 197 | self.fake_gerrit.addEvent(comment) |
| 198 | self.waitUntilSettled() |
| 199 | self.assertEqual(len(self.history), 1) |
| 200 | self.assertEqual(self.history[0].name, job) |
| 201 | |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 202 | |
| 203 | class TestRequirementsVote2(ZuulTestCase): |
| 204 | """Requirements with a voting requirement""" |
| 205 | |
| 206 | tenant_config_file = 'config/requirements/vote2/main.yaml' |
| 207 | |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 208 | def test_pipeline_require_approval_vote2(self): |
| 209 | "Test pipeline requirement: approval vote with two values" |
| 210 | return self._test_require_approval_vote2('org/project1', |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 211 | 'project1-job') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 212 | |
| 213 | def test_trigger_require_approval_vote2(self): |
| 214 | "Test trigger requirement: approval vote with two values" |
| 215 | return self._test_require_approval_vote2('org/project2', |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 216 | 'project2-job') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 217 | |
| 218 | def _test_require_approval_vote2(self, project, job): |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 219 | A = self.fake_gerrit.addFakeChange(project, 'master', 'A') |
| 220 | # A comment event that we will keep submitting to trigger |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 221 | comment = A.addApproval('code-review', 2, username='nobody') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 222 | self.fake_gerrit.addEvent(comment) |
| 223 | self.waitUntilSettled() |
| 224 | # No approval from Jenkins so should not be enqueued |
| 225 | self.assertEqual(len(self.history), 0) |
| 226 | |
| 227 | # A -1 from jenkins should not cause it to be enqueued |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 228 | A.addApproval('verified', -1, username='jenkins') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 229 | self.fake_gerrit.addEvent(comment) |
| 230 | self.waitUntilSettled() |
| 231 | self.assertEqual(len(self.history), 0) |
| 232 | |
| 233 | # A -2 from jenkins should not cause it to be enqueued |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 234 | A.addApproval('verified', -2, username='jenkins') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 235 | self.fake_gerrit.addEvent(comment) |
| 236 | self.waitUntilSettled() |
| 237 | self.assertEqual(len(self.history), 0) |
| 238 | |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 239 | # A +1 from jenkins should allow it to be enqueued |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 240 | A.addApproval('verified', 1, username='jenkins') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 241 | self.fake_gerrit.addEvent(comment) |
| 242 | self.waitUntilSettled() |
| 243 | self.assertEqual(len(self.history), 1) |
| 244 | self.assertEqual(self.history[0].name, job) |
| 245 | |
Jesse Keating | 2dfc651 | 2017-01-19 14:31:44 -0800 | [diff] [blame] | 246 | # A +2 from nobody should not cause it to be enqueued |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 247 | B = self.fake_gerrit.addFakeChange(project, 'master', 'B') |
| 248 | # A comment event that we will keep submitting to trigger |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 249 | comment = B.addApproval('code-review', 2, username='nobody') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 250 | self.fake_gerrit.addEvent(comment) |
| 251 | self.waitUntilSettled() |
| 252 | self.assertEqual(len(self.history), 1) |
| 253 | |
Jesse Keating | 2dfc651 | 2017-01-19 14:31:44 -0800 | [diff] [blame] | 254 | # A +2 from jenkins should allow it to be enqueued |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 255 | B.addApproval('verified', 2, username='jenkins') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 256 | self.fake_gerrit.addEvent(comment) |
| 257 | self.waitUntilSettled() |
| 258 | self.assertEqual(len(self.history), 2) |
| 259 | self.assertEqual(self.history[1].name, job) |
| 260 | |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 261 | |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 262 | class TestRequirementsState(ZuulTestCase): |
| 263 | """Requirements with simple state requirement""" |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 264 | |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 265 | tenant_config_file = 'config/requirements/state/main.yaml' |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 266 | |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 267 | def test_pipeline_require_current_patchset(self): |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 268 | # Create two patchsets and let their tests settle out. Then |
| 269 | # comment on first patchset and check that no additional |
| 270 | # jobs are run. |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 271 | A = self.fake_gerrit.addFakeChange('current-project', 'master', 'A') |
| 272 | self.fake_gerrit.addEvent(A.addApproval('code-review', 1)) |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 273 | self.waitUntilSettled() |
| 274 | A.addPatchset() |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 275 | self.fake_gerrit.addEvent(A.addApproval('code-review', 1)) |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 276 | self.waitUntilSettled() |
| 277 | |
| 278 | self.assertEqual(len(self.history), 2) # one job for each ps |
| 279 | self.fake_gerrit.addEvent(A.getChangeCommentEvent(1)) |
| 280 | self.waitUntilSettled() |
| 281 | |
| 282 | # Assert no new jobs ran after event for old patchset. |
| 283 | self.assertEqual(len(self.history), 2) |
| 284 | |
| 285 | # Make sure the same event on a new PS will trigger |
| 286 | self.fake_gerrit.addEvent(A.getChangeCommentEvent(2)) |
| 287 | self.waitUntilSettled() |
| 288 | self.assertEqual(len(self.history), 3) |
| 289 | |
| 290 | def test_pipeline_require_open(self): |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 291 | A = self.fake_gerrit.addFakeChange('open-project', 'master', 'A', |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 292 | status='MERGED') |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 293 | self.fake_gerrit.addEvent(A.addApproval('code-review', 2)) |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 294 | self.waitUntilSettled() |
| 295 | self.assertEqual(len(self.history), 0) |
| 296 | |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 297 | B = self.fake_gerrit.addFakeChange('open-project', 'master', 'B') |
| 298 | self.fake_gerrit.addEvent(B.addApproval('code-review', 2)) |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 299 | self.waitUntilSettled() |
| 300 | self.assertEqual(len(self.history), 1) |
| 301 | |
| 302 | def test_pipeline_require_status(self): |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 303 | A = self.fake_gerrit.addFakeChange('status-project', 'master', 'A', |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 304 | status='MERGED') |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 305 | self.fake_gerrit.addEvent(A.addApproval('code-review', 2)) |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 306 | self.waitUntilSettled() |
| 307 | self.assertEqual(len(self.history), 0) |
| 308 | |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 309 | B = self.fake_gerrit.addFakeChange('status-project', 'master', 'B') |
| 310 | self.fake_gerrit.addEvent(B.addApproval('code-review', 2)) |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 311 | self.waitUntilSettled() |
| 312 | self.assertEqual(len(self.history), 1) |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 313 | |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 314 | |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 315 | class TestRequirementsRejectUsername(ZuulTestCase): |
| 316 | """Requirements with reject username requirement""" |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 317 | |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 318 | tenant_config_file = 'config/requirements/reject-username/main.yaml' |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 319 | |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 320 | def _test_require_reject_username(self, project, job): |
| 321 | "Test negative username's match" |
| 322 | # Should only trigger if Jenkins hasn't voted. |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 323 | # add in a change with no comments |
| 324 | A = self.fake_gerrit.addFakeChange(project, 'master', 'A') |
| 325 | self.waitUntilSettled() |
| 326 | self.assertEqual(len(self.history), 0) |
| 327 | |
| 328 | # add in a comment that will trigger |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 329 | self.fake_gerrit.addEvent(A.addApproval('code-review', 1, |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 330 | username='reviewer')) |
| 331 | self.waitUntilSettled() |
| 332 | self.assertEqual(len(self.history), 1) |
| 333 | self.assertEqual(self.history[0].name, job) |
| 334 | |
| 335 | # add in a comment from jenkins user which shouldn't trigger |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 336 | self.fake_gerrit.addEvent(A.addApproval('verified', 1, |
| 337 | username='jenkins')) |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 338 | self.waitUntilSettled() |
| 339 | self.assertEqual(len(self.history), 1) |
| 340 | |
| 341 | # Check future reviews also won't trigger as a 'jenkins' user has |
| 342 | # commented previously |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 343 | self.fake_gerrit.addEvent(A.addApproval('code-review', 1, |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 344 | username='reviewer')) |
| 345 | self.waitUntilSettled() |
| 346 | self.assertEqual(len(self.history), 1) |
| 347 | |
| 348 | def test_pipeline_reject_username(self): |
| 349 | "Test negative pipeline requirement: no comment from jenkins" |
| 350 | return self._test_require_reject_username('org/project1', |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 351 | 'project1-job') |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 352 | |
| 353 | def test_trigger_reject_username(self): |
| 354 | "Test negative trigger requirement: no comment from jenkins" |
| 355 | return self._test_require_reject_username('org/project2', |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 356 | 'project2-job') |
| 357 | |
| 358 | |
| 359 | class TestRequirementsReject(ZuulTestCase): |
| 360 | """Requirements with reject requirement""" |
| 361 | |
| 362 | tenant_config_file = 'config/requirements/reject/main.yaml' |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 363 | |
| 364 | def _test_require_reject(self, project, job): |
| 365 | "Test no approval matches a reject param" |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 366 | A = self.fake_gerrit.addFakeChange(project, 'master', 'A') |
| 367 | self.waitUntilSettled() |
| 368 | self.assertEqual(len(self.history), 0) |
| 369 | |
| 370 | # First positive vote should not queue until jenkins has +1'd |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 371 | comment = A.addApproval('verified', 1, username='reviewer_a') |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 372 | self.fake_gerrit.addEvent(comment) |
| 373 | self.waitUntilSettled() |
| 374 | self.assertEqual(len(self.history), 0) |
| 375 | |
| 376 | # Jenkins should put in a +1 which will also queue |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 377 | comment = A.addApproval('verified', 1, username='jenkins') |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 378 | self.fake_gerrit.addEvent(comment) |
| 379 | self.waitUntilSettled() |
| 380 | self.assertEqual(len(self.history), 1) |
| 381 | self.assertEqual(self.history[0].name, job) |
| 382 | |
| 383 | # Negative vote should not queue |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 384 | comment = A.addApproval('verified', -1, username='reviewer_b') |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 385 | self.fake_gerrit.addEvent(comment) |
| 386 | self.waitUntilSettled() |
| 387 | self.assertEqual(len(self.history), 1) |
| 388 | |
| 389 | # Future approvals should do nothing |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 390 | comment = A.addApproval('verified', 1, username='reviewer_c') |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 391 | self.fake_gerrit.addEvent(comment) |
| 392 | self.waitUntilSettled() |
| 393 | self.assertEqual(len(self.history), 1) |
| 394 | |
| 395 | # Change/update negative vote should queue |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 396 | comment = A.addApproval('verified', 1, username='reviewer_b') |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 397 | self.fake_gerrit.addEvent(comment) |
| 398 | self.waitUntilSettled() |
| 399 | self.assertEqual(len(self.history), 2) |
| 400 | self.assertEqual(self.history[1].name, job) |
| 401 | |
| 402 | # Future approvals should also queue |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 403 | comment = A.addApproval('verified', 1, username='reviewer_d') |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 404 | self.fake_gerrit.addEvent(comment) |
| 405 | self.waitUntilSettled() |
| 406 | self.assertEqual(len(self.history), 3) |
| 407 | self.assertEqual(self.history[2].name, job) |
| 408 | |
| 409 | def test_pipeline_require_reject(self): |
| 410 | "Test pipeline requirement: rejections absent" |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 411 | return self._test_require_reject('org/project1', 'project1-job') |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 412 | |
| 413 | def test_trigger_require_reject(self): |
| 414 | "Test trigger requirement: rejections absent" |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 415 | return self._test_require_reject('org/project2', 'project2-job') |