github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/plugins/lifecycle/close_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain 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, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package lifecycle 18 19 import ( 20 "errors" 21 "testing" 22 23 "github.com/sirupsen/logrus" 24 25 "k8s.io/test-infra/prow/github" 26 ) 27 28 type fakeClientClose struct { 29 commented bool 30 closed bool 31 AssigneesAdded []string 32 labels []string 33 } 34 35 func (c *fakeClientClose) CreateComment(owner, repo string, number int, comment string) error { 36 c.commented = true 37 return nil 38 } 39 40 func (c *fakeClientClose) CloseIssue(owner, repo string, number int) error { 41 c.closed = true 42 return nil 43 } 44 45 func (c *fakeClientClose) ClosePR(owner, repo string, number int) error { 46 c.closed = true 47 return nil 48 } 49 50 func (c *fakeClientClose) IsCollaborator(owner, repo, login string) (bool, error) { 51 if login == "collaborator" { 52 return true, nil 53 } 54 return false, nil 55 } 56 57 func (c *fakeClientClose) GetIssueLabels(owner, repo string, number int) ([]github.Label, error) { 58 var labels []github.Label 59 for _, l := range c.labels { 60 if l == "error" { 61 return nil, errors.New("issue label 500") 62 } 63 labels = append(labels, github.Label{Name: l}) 64 } 65 return labels, nil 66 } 67 68 func TestCloseComment(t *testing.T) { 69 var testcases = []struct { 70 name string 71 action github.GenericCommentEventAction 72 state string 73 body string 74 commenter string 75 labels []string 76 shouldClose bool 77 shouldComment bool 78 }{ 79 { 80 name: "non-close comment", 81 action: github.GenericCommentActionCreated, 82 state: "open", 83 body: "uh oh", 84 commenter: "random-person", 85 shouldClose: false, 86 shouldComment: false, 87 }, 88 { 89 name: "close by author", 90 action: github.GenericCommentActionCreated, 91 state: "open", 92 body: "/close", 93 commenter: "author", 94 shouldClose: true, 95 shouldComment: true, 96 }, 97 { 98 name: "close by author, trailing space.", 99 action: github.GenericCommentActionCreated, 100 state: "open", 101 body: "/close \r", 102 commenter: "author", 103 shouldClose: true, 104 shouldComment: true, 105 }, 106 { 107 name: "close by collaborator", 108 action: github.GenericCommentActionCreated, 109 state: "open", 110 body: "/close", 111 commenter: "collaborator", 112 shouldClose: true, 113 shouldComment: true, 114 }, 115 { 116 name: "close edited by author", 117 action: github.GenericCommentActionEdited, 118 state: "open", 119 body: "/close", 120 commenter: "author", 121 shouldClose: false, 122 shouldComment: false, 123 }, 124 { 125 name: "close by author on closed issue", 126 action: github.GenericCommentActionCreated, 127 state: "closed", 128 body: "/close", 129 commenter: "author", 130 shouldClose: false, 131 shouldComment: false, 132 }, 133 { 134 name: "close by non-collaborator on active issue, cannot close", 135 action: github.GenericCommentActionCreated, 136 state: "open", 137 body: "/close", 138 commenter: "non-collaborator", 139 shouldClose: false, 140 shouldComment: true, 141 }, 142 { 143 name: "close by non-collaborator on stale issue", 144 action: github.GenericCommentActionCreated, 145 state: "open", 146 body: "/close", 147 commenter: "non-collaborator", 148 labels: []string{"lifecycle/stale"}, 149 shouldClose: true, 150 shouldComment: true, 151 }, 152 { 153 name: "close by non-collaborator on rotten issue", 154 action: github.GenericCommentActionCreated, 155 state: "open", 156 body: "/close", 157 commenter: "non-collaborator", 158 labels: []string{"lifecycle/rotten"}, 159 shouldClose: true, 160 shouldComment: true, 161 }, 162 { 163 name: "cannot close stale issue by non-collaborator when list issue fails", 164 action: github.GenericCommentActionCreated, 165 state: "open", 166 body: "/close", 167 commenter: "non-collaborator", 168 labels: []string{"error"}, 169 shouldClose: false, 170 shouldComment: true, 171 }, 172 } 173 for _, tc := range testcases { 174 fc := &fakeClientClose{labels: tc.labels} 175 e := &github.GenericCommentEvent{ 176 Action: tc.action, 177 IssueState: tc.state, 178 Body: tc.body, 179 User: github.User{Login: tc.commenter}, 180 Number: 5, 181 IssueAuthor: github.User{Login: "author"}, 182 } 183 if err := handleClose(fc, logrus.WithField("plugin", "fake-close"), e); err != nil { 184 t.Errorf("For case %s, didn't expect error from handle: %v", tc.name, err) 185 continue 186 } 187 if tc.shouldClose && !fc.closed { 188 t.Errorf("For case %s, should have closed but didn't.", tc.name) 189 } else if !tc.shouldClose && fc.closed { 190 t.Errorf("For case %s, should not have closed but did.", tc.name) 191 } 192 if tc.shouldComment && !fc.commented { 193 t.Errorf("For case %s, should have commented but didn't.", tc.name) 194 } else if !tc.shouldComment && fc.commented { 195 t.Errorf("For case %s, should not have commented but did.", tc.name) 196 } 197 } 198 }