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