Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2017 IBM Corp. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
| 16 | from tests.base import ZuulTestCase, simple_layout |
| 17 | |
| 18 | |
| 19 | class TestGithubRequirements(ZuulTestCase): |
| 20 | """Test pipeline and trigger requirements""" |
| 21 | config_file = 'zuul-github-driver.conf' |
| 22 | |
| 23 | @simple_layout('layouts/requirements-github.yaml', driver='github') |
| 24 | def test_pipeline_require_status(self): |
| 25 | "Test pipeline requirement: status" |
| 26 | A = self.fake_github.openFakePullRequest('org/project1', 'master', 'A') |
| 27 | # A comment event that we will keep submitting to trigger |
| 28 | comment = A.getCommentAddedEvent('test me') |
| 29 | self.fake_github.emitEvent(comment) |
| 30 | self.waitUntilSettled() |
| 31 | # No status from zuul so should not be enqueued |
| 32 | self.assertEqual(len(self.history), 0) |
| 33 | |
| 34 | # An error status should not cause it to be enqueued |
| 35 | A.setStatus(A.head_sha, 'error', 'null', 'null', 'check') |
| 36 | self.fake_github.emitEvent(comment) |
| 37 | self.waitUntilSettled() |
| 38 | self.assertEqual(len(self.history), 0) |
| 39 | |
| 40 | # A success status goes in |
| 41 | A.setStatus(A.head_sha, 'success', 'null', 'null', 'check') |
| 42 | self.fake_github.emitEvent(comment) |
| 43 | self.waitUntilSettled() |
| 44 | self.assertEqual(len(self.history), 1) |
| 45 | self.assertEqual(self.history[0].name, 'project1-pipeline') |
Adam Gandelman | 8c6eeb5 | 2017-01-23 16:31:06 -0800 | [diff] [blame] | 46 | |
| 47 | @simple_layout('layouts/requirements-github.yaml', driver='github') |
| 48 | def test_trigger_require_status(self): |
| 49 | "Test trigger requirement: status" |
| 50 | A = self.fake_github.openFakePullRequest('org/project2', 'master', 'A') |
| 51 | |
| 52 | # An error status should not cause it to be enqueued |
| 53 | A.setStatus(A.head_sha, 'error', 'null', 'null', 'check') |
| 54 | self.fake_github.emitEvent(A.getCommitStatusEvent('check', |
| 55 | state='error')) |
| 56 | self.waitUntilSettled() |
| 57 | self.assertEqual(len(self.history), 0) |
| 58 | |
Jesse Keating | ae4cd27 | 2017-01-30 17:10:44 -0800 | [diff] [blame] | 59 | # A success status from unknown user should not cause it to be |
Adam Gandelman | 8c6eeb5 | 2017-01-23 16:31:06 -0800 | [diff] [blame] | 60 | # enqueued |
| 61 | A.setStatus(A.head_sha, 'success', 'null', 'null', 'check', user='foo') |
| 62 | self.fake_github.emitEvent(A.getCommitStatusEvent('check', |
| 63 | state='success', |
| 64 | user='foo')) |
| 65 | self.waitUntilSettled() |
| 66 | self.assertEqual(len(self.history), 0) |
| 67 | |
Jesse Keating | ae4cd27 | 2017-01-30 17:10:44 -0800 | [diff] [blame] | 68 | # A success status from zuul goes in |
Adam Gandelman | 8c6eeb5 | 2017-01-23 16:31:06 -0800 | [diff] [blame] | 69 | A.setStatus(A.head_sha, 'success', 'null', 'null', 'check') |
| 70 | self.fake_github.emitEvent(A.getCommitStatusEvent('check')) |
| 71 | self.waitUntilSettled() |
| 72 | self.assertEqual(len(self.history), 1) |
| 73 | self.assertEqual(self.history[0].name, 'project2-trigger') |
| 74 | |
| 75 | # An error status for a different context should not cause it to be |
| 76 | # enqueued |
| 77 | A.setStatus(A.head_sha, 'error', 'null', 'null', 'gate') |
| 78 | self.fake_github.emitEvent(A.getCommitStatusEvent('gate', |
| 79 | state='error')) |
| 80 | self.waitUntilSettled() |
| 81 | self.assertEqual(len(self.history), 1) |
Jesse Keating | ae4cd27 | 2017-01-30 17:10:44 -0800 | [diff] [blame] | 82 | |
| 83 | @simple_layout('layouts/requirements-github.yaml', driver='github') |
| 84 | def test_pipeline_require_review_username(self): |
| 85 | "Test pipeline requirement: review username" |
| 86 | |
| 87 | A = self.fake_github.openFakePullRequest('org/project3', 'master', 'A') |
| 88 | # A comment event that we will keep submitting to trigger |
| 89 | comment = A.getCommentAddedEvent('test me') |
| 90 | self.fake_github.emitEvent(comment) |
| 91 | self.waitUntilSettled() |
| 92 | # No approval from derp so should not be enqueued |
| 93 | self.assertEqual(len(self.history), 0) |
| 94 | |
| 95 | # Add an approved review from derp |
| 96 | A.addReview('derp', 'APPROVED') |
| 97 | self.fake_github.emitEvent(comment) |
| 98 | self.waitUntilSettled() |
| 99 | self.assertEqual(len(self.history), 1) |
| 100 | self.assertEqual(self.history[0].name, 'project3-reviewusername') |
| 101 | |
| 102 | @simple_layout('layouts/requirements-github.yaml', driver='github') |
| 103 | def test_pipeline_require_review_state(self): |
| 104 | "Test pipeline requirement: review state" |
| 105 | |
| 106 | A = self.fake_github.openFakePullRequest('org/project4', 'master', 'A') |
| 107 | # Add derp to writers |
| 108 | A.writers.append('derp') |
| 109 | # A comment event that we will keep submitting to trigger |
| 110 | comment = A.getCommentAddedEvent('test me') |
| 111 | self.fake_github.emitEvent(comment) |
| 112 | self.waitUntilSettled() |
| 113 | # No positive review from derp so should not be enqueued |
| 114 | self.assertEqual(len(self.history), 0) |
| 115 | |
| 116 | # A negative review from derp should not cause it to be enqueued |
| 117 | A.addReview('derp', 'CHANGES_REQUESTED') |
| 118 | self.fake_github.emitEvent(comment) |
| 119 | self.waitUntilSettled() |
| 120 | self.assertEqual(len(self.history), 0) |
| 121 | |
| 122 | # A positive from nobody should not cause it to be enqueued |
| 123 | A.addReview('nobody', 'APPROVED') |
| 124 | self.fake_github.emitEvent(comment) |
| 125 | self.waitUntilSettled() |
| 126 | self.assertEqual(len(self.history), 0) |
| 127 | |
| 128 | # A positive review from derp should cause it to be enqueued |
| 129 | A.addReview('derp', 'APPROVED') |
| 130 | self.fake_github.emitEvent(comment) |
| 131 | self.waitUntilSettled() |
| 132 | self.assertEqual(len(self.history), 1) |
| 133 | self.assertEqual(self.history[0].name, 'project4-reviewreq') |
| 134 | |
| 135 | @simple_layout('layouts/requirements-github.yaml', driver='github') |
| 136 | def test_pipeline_require_review_user_state(self): |
| 137 | "Test pipeline requirement: review state from user" |
| 138 | |
| 139 | A = self.fake_github.openFakePullRequest('org/project5', 'master', 'A') |
| 140 | # Add derp and herp to writers |
| 141 | A.writers.extend(('derp', 'herp')) |
| 142 | # A comment event that we will keep submitting to trigger |
| 143 | comment = A.getCommentAddedEvent('test me') |
| 144 | self.fake_github.emitEvent(comment) |
| 145 | self.waitUntilSettled() |
| 146 | # No positive review from derp so should not be enqueued |
| 147 | self.assertEqual(len(self.history), 0) |
| 148 | |
| 149 | # A negative review from derp should not cause it to be enqueued |
| 150 | A.addReview('derp', 'CHANGES_REQUESTED') |
| 151 | self.fake_github.emitEvent(comment) |
| 152 | self.waitUntilSettled() |
| 153 | self.assertEqual(len(self.history), 0) |
| 154 | |
| 155 | # A positive from nobody should not cause it to be enqueued |
| 156 | A.addReview('nobody', 'APPROVED') |
| 157 | self.fake_github.emitEvent(comment) |
| 158 | self.waitUntilSettled() |
| 159 | self.assertEqual(len(self.history), 0) |
| 160 | |
| 161 | # A positive review from herp (a writer) should not cause it to be |
| 162 | # enqueued |
| 163 | A.addReview('herp', 'APPROVED') |
| 164 | self.fake_github.emitEvent(comment) |
| 165 | self.waitUntilSettled() |
| 166 | self.assertEqual(len(self.history), 0) |
| 167 | |
| 168 | # A positive review from derp should cause it to be enqueued |
| 169 | A.addReview('derp', 'APPROVED') |
| 170 | self.fake_github.emitEvent(comment) |
| 171 | self.waitUntilSettled() |
| 172 | self.assertEqual(len(self.history), 1) |
| 173 | self.assertEqual(self.history[0].name, 'project5-reviewuserstate') |