github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/plugins/close/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 close 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 fakeClient struct { 29 commented bool 30 closed bool 31 AssigneesAdded []string 32 } 33 34 func (c *fakeClient) CreateComment(owner, repo string, number int, comment string) error { 35 c.commented = true 36 return nil 37 } 38 39 func (c *fakeClient) CloseIssue(owner, repo string, number int) error { 40 c.closed = true 41 return nil 42 } 43 44 func (c *fakeClient) ClosePR(owner, repo string, number int) error { 45 c.closed = true 46 return nil 47 } 48 49 func (c *fakeClient) IsMember(owner, login string) (bool, error) { 50 if login == "non-member" { 51 return false, nil 52 } 53 return true, nil 54 } 55 56 func (c *fakeClient) AssignIssue(owner, repo string, number int, assignees []string) error { 57 if assignees[0] == "non-member" || assignees[0] == "non-owner-assign-error" { 58 return errors.New("Failed to assign") 59 } 60 c.AssigneesAdded = append(c.AssigneesAdded, assignees...) 61 return nil 62 } 63 64 func TestCloseComment(t *testing.T) { 65 // "a" is the author, "r1", and "r2" are reviewers. 66 var testcases = []struct { 67 name string 68 action github.GenericCommentEventAction 69 state string 70 body string 71 commenter string 72 shouldClose bool 73 shouldComment bool 74 shouldAssign bool 75 }{ 76 { 77 name: "non-close comment", 78 action: github.GenericCommentActionCreated, 79 state: "open", 80 body: "uh oh", 81 commenter: "o", 82 shouldClose: false, 83 shouldComment: false, 84 }, 85 { 86 name: "close by author", 87 action: github.GenericCommentActionCreated, 88 state: "open", 89 body: "/close", 90 commenter: "a", 91 shouldClose: true, 92 shouldComment: false, 93 }, 94 { 95 name: "close by author, trailing space.", 96 action: github.GenericCommentActionCreated, 97 state: "open", 98 body: "/close \r", 99 commenter: "a", 100 shouldClose: true, 101 shouldComment: false, 102 }, 103 { 104 name: "close by reviewer", 105 action: github.GenericCommentActionCreated, 106 state: "open", 107 body: "/close", 108 commenter: "r1", 109 shouldClose: true, 110 shouldComment: false, 111 }, 112 { 113 name: "close edited by author", 114 action: github.GenericCommentActionEdited, 115 state: "open", 116 body: "/close", 117 commenter: "a", 118 shouldClose: false, 119 shouldComment: false, 120 }, 121 { 122 name: "close by author on closed issue", 123 action: github.GenericCommentActionCreated, 124 state: "closed", 125 body: "/close", 126 commenter: "a", 127 shouldClose: false, 128 shouldComment: false, 129 }, 130 { 131 name: "close by other person, non-member cannot close", 132 action: github.GenericCommentActionCreated, 133 state: "open", 134 body: "/close", 135 commenter: "non-member", 136 shouldClose: false, 137 shouldComment: true, 138 shouldAssign: false, 139 }, 140 { 141 name: "close by other person, failed to assign", 142 action: github.GenericCommentActionCreated, 143 state: "open", 144 body: "/close", 145 commenter: "non-owner-assign-error", 146 shouldClose: false, 147 shouldComment: true, 148 shouldAssign: false, 149 }, 150 { 151 name: "close by other person, assign and close", 152 action: github.GenericCommentActionCreated, 153 state: "open", 154 body: "/close", 155 commenter: "non-owner", 156 shouldClose: true, 157 shouldComment: false, 158 shouldAssign: true, 159 }, 160 } 161 for _, tc := range testcases { 162 fc := &fakeClient{} 163 e := &github.GenericCommentEvent{ 164 Action: tc.action, 165 IssueState: tc.state, 166 Body: tc.body, 167 User: github.User{Login: tc.commenter}, 168 Number: 5, 169 Assignees: []github.User{{Login: "a"}, {Login: "r1"}, {Login: "r2"}}, 170 IssueAuthor: github.User{Login: "a"}, 171 } 172 if err := handle(fc, logrus.WithField("plugin", pluginName), e); err != nil { 173 t.Errorf("For case %s, didn't expect error from handle: %v", tc.name, err) 174 continue 175 } 176 if tc.shouldClose && !fc.closed { 177 t.Errorf("For case %s, should have closed but didn't.", tc.name) 178 } else if !tc.shouldClose && fc.closed { 179 t.Errorf("For case %s, should not have closed but did.", tc.name) 180 } 181 if tc.shouldComment && !fc.commented { 182 t.Errorf("For case %s, should have commented but didn't.", tc.name) 183 } else if !tc.shouldComment && fc.commented { 184 t.Errorf("For case %s, should not have commented but did.", tc.name) 185 } 186 if tc.shouldAssign && len(fc.AssigneesAdded) != 1 { 187 t.Errorf("For case %s, should have assigned but didn't.", tc.name) 188 } else if !tc.shouldAssign && len(fc.AssigneesAdded) == 1 { 189 t.Errorf("For case %s, should not have assigned but did.", tc.name) 190 } 191 } 192 }