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 | |
Jesse Keating | 2dfc651 | 2017-01-19 14:31:44 -0800 | [diff] [blame] | 251 | # A +2 from nobody should not cause it to be enqueued |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 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 | |
Jesse Keating | 2dfc651 | 2017-01-19 14:31:44 -0800 | [diff] [blame] | 259 | # A +2 from jenkins should allow it to be enqueued |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 260 | B.addApproval('verified', 2, username='jenkins') |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 261 | self.fake_gerrit.addEvent(comment) |
| 262 | self.waitUntilSettled() |
| 263 | self.assertEqual(len(self.history), 2) |
| 264 | self.assertEqual(self.history[1].name, job) |
| 265 | |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 266 | |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 267 | class TestRequirementsState(ZuulTestCase): |
| 268 | """Requirements with simple state requirement""" |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 269 | |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 270 | tenant_config_file = 'config/requirements/state/main.yaml' |
Jamie Lennox | 8ce6558 | 2016-11-23 15:20:39 +1100 | [diff] [blame] | 271 | |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 272 | def test_pipeline_require_current_patchset(self): |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 273 | # Create two patchsets and let their tests settle out. Then |
| 274 | # comment on first patchset and check that no additional |
| 275 | # jobs are run. |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 276 | A = self.fake_gerrit.addFakeChange('current-project', 'master', 'A') |
| 277 | self.fake_gerrit.addEvent(A.addApproval('code-review', 1)) |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 278 | self.waitUntilSettled() |
| 279 | A.addPatchset() |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 280 | self.fake_gerrit.addEvent(A.addApproval('code-review', 1)) |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 281 | self.waitUntilSettled() |
| 282 | |
| 283 | self.assertEqual(len(self.history), 2) # one job for each ps |
| 284 | self.fake_gerrit.addEvent(A.getChangeCommentEvent(1)) |
| 285 | self.waitUntilSettled() |
| 286 | |
| 287 | # Assert no new jobs ran after event for old patchset. |
| 288 | self.assertEqual(len(self.history), 2) |
| 289 | |
| 290 | # Make sure the same event on a new PS will trigger |
| 291 | self.fake_gerrit.addEvent(A.getChangeCommentEvent(2)) |
| 292 | self.waitUntilSettled() |
| 293 | self.assertEqual(len(self.history), 3) |
| 294 | |
| 295 | def test_pipeline_require_open(self): |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 296 | A = self.fake_gerrit.addFakeChange('open-project', 'master', 'A', |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 297 | status='MERGED') |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 298 | self.fake_gerrit.addEvent(A.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), 0) |
| 301 | |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 302 | B = self.fake_gerrit.addFakeChange('open-project', 'master', 'B') |
| 303 | self.fake_gerrit.addEvent(B.addApproval('code-review', 2)) |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 304 | self.waitUntilSettled() |
| 305 | self.assertEqual(len(self.history), 1) |
| 306 | |
| 307 | def test_pipeline_require_status(self): |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 308 | A = self.fake_gerrit.addFakeChange('status-project', 'master', 'A', |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 309 | status='MERGED') |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 310 | self.fake_gerrit.addEvent(A.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), 0) |
| 313 | |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 314 | B = self.fake_gerrit.addFakeChange('status-project', 'master', 'B') |
| 315 | self.fake_gerrit.addEvent(B.addApproval('code-review', 2)) |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 316 | self.waitUntilSettled() |
| 317 | self.assertEqual(len(self.history), 1) |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 318 | |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 319 | |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 320 | class TestRequirementsRejectUsername(ZuulTestCase): |
| 321 | """Requirements with reject username requirement""" |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 322 | |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 323 | tenant_config_file = 'config/requirements/reject-username/main.yaml' |
Jamie Lennox | d62939f | 2016-11-23 15:53:49 +1100 | [diff] [blame] | 324 | |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 325 | def _test_require_reject_username(self, project, job): |
| 326 | "Test negative username's match" |
| 327 | # Should only trigger if Jenkins hasn't voted. |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 328 | # add in a change with no comments |
| 329 | A = self.fake_gerrit.addFakeChange(project, 'master', 'A') |
| 330 | self.waitUntilSettled() |
| 331 | self.assertEqual(len(self.history), 0) |
| 332 | |
| 333 | # add in a comment that will trigger |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 334 | self.fake_gerrit.addEvent(A.addApproval('code-review', 1, |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 335 | username='reviewer')) |
| 336 | self.waitUntilSettled() |
| 337 | self.assertEqual(len(self.history), 1) |
| 338 | self.assertEqual(self.history[0].name, job) |
| 339 | |
| 340 | # add in a comment from jenkins user which shouldn't trigger |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 341 | self.fake_gerrit.addEvent(A.addApproval('verified', 1, |
| 342 | username='jenkins')) |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 343 | self.waitUntilSettled() |
| 344 | self.assertEqual(len(self.history), 1) |
| 345 | |
| 346 | # Check future reviews also won't trigger as a 'jenkins' user has |
| 347 | # commented previously |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 348 | self.fake_gerrit.addEvent(A.addApproval('code-review', 1, |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 349 | username='reviewer')) |
| 350 | self.waitUntilSettled() |
| 351 | self.assertEqual(len(self.history), 1) |
| 352 | |
| 353 | def test_pipeline_reject_username(self): |
| 354 | "Test negative pipeline requirement: no comment from jenkins" |
| 355 | return self._test_require_reject_username('org/project1', |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 356 | 'project1-job') |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 357 | |
| 358 | def test_trigger_reject_username(self): |
| 359 | "Test negative trigger requirement: no comment from jenkins" |
| 360 | return self._test_require_reject_username('org/project2', |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 361 | 'project2-job') |
| 362 | |
| 363 | |
| 364 | class TestRequirementsReject(ZuulTestCase): |
| 365 | """Requirements with reject requirement""" |
| 366 | |
| 367 | tenant_config_file = 'config/requirements/reject/main.yaml' |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 368 | |
| 369 | def _test_require_reject(self, project, job): |
| 370 | "Test no approval matches a reject param" |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 371 | A = self.fake_gerrit.addFakeChange(project, 'master', 'A') |
| 372 | self.waitUntilSettled() |
| 373 | self.assertEqual(len(self.history), 0) |
| 374 | |
| 375 | # First positive vote should not queue until jenkins has +1'd |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 376 | comment = A.addApproval('verified', 1, username='reviewer_a') |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 377 | self.fake_gerrit.addEvent(comment) |
| 378 | self.waitUntilSettled() |
| 379 | self.assertEqual(len(self.history), 0) |
| 380 | |
| 381 | # Jenkins should put in a +1 which will also queue |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 382 | comment = A.addApproval('verified', 1, username='jenkins') |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 383 | self.fake_gerrit.addEvent(comment) |
| 384 | self.waitUntilSettled() |
| 385 | self.assertEqual(len(self.history), 1) |
| 386 | self.assertEqual(self.history[0].name, job) |
| 387 | |
| 388 | # Negative vote should not queue |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 389 | comment = A.addApproval('verified', -1, username='reviewer_b') |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 390 | self.fake_gerrit.addEvent(comment) |
| 391 | self.waitUntilSettled() |
| 392 | self.assertEqual(len(self.history), 1) |
| 393 | |
| 394 | # Future approvals should do nothing |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 395 | comment = A.addApproval('verified', 1, username='reviewer_c') |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 396 | self.fake_gerrit.addEvent(comment) |
| 397 | self.waitUntilSettled() |
| 398 | self.assertEqual(len(self.history), 1) |
| 399 | |
| 400 | # Change/update negative vote should queue |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 401 | comment = A.addApproval('verified', 1, username='reviewer_b') |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 402 | self.fake_gerrit.addEvent(comment) |
| 403 | self.waitUntilSettled() |
| 404 | self.assertEqual(len(self.history), 2) |
| 405 | self.assertEqual(self.history[1].name, job) |
| 406 | |
| 407 | # Future approvals should also queue |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 408 | comment = A.addApproval('verified', 1, username='reviewer_d') |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 409 | self.fake_gerrit.addEvent(comment) |
| 410 | self.waitUntilSettled() |
| 411 | self.assertEqual(len(self.history), 3) |
| 412 | self.assertEqual(self.history[2].name, job) |
| 413 | |
| 414 | def test_pipeline_require_reject(self): |
| 415 | "Test pipeline requirement: rejections absent" |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 416 | return self._test_require_reject('org/project1', 'project1-job') |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 417 | |
| 418 | def test_trigger_require_reject(self): |
| 419 | "Test trigger requirement: rejections absent" |
Jamie Lennox | d981b7d | 2016-11-23 16:05:12 +1100 | [diff] [blame] | 420 | return self._test_require_reject('org/project2', 'project2-job') |